From f914c1f16e63aa4e667db1bca45071a1db43fc5d Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Fri, 7 Feb 2025 01:30:08 -0800 Subject: [PATCH] Add migrations * Update preview state default to pending * Adds indexes * Adds PgHero and PgStatements --- ...15_set_preview_state_default_to_pending.rb | 7 +++++ ...20224953_add_missing_indexes_to_parties.rb | 12 +++++++ ...154_add_missing_indexes_to_grid_objects.rb | 8 +++++ ...0250201041406_create_pghero_query_stats.rb | 15 +++++++++ .../20250201120655_enable_pg_statements.rb | 9 ++++++ .../20250201120842_remove_unused_index.rb | 5 +++ ...170037_add_optimized_indexes_to_parties.rb | 8 +++++ db/schema.rb | 31 ++++++++++++++++--- 8 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 db/migrate/20250120214715_set_preview_state_default_to_pending.rb create mode 100644 db/migrate/20250120224953_add_missing_indexes_to_parties.rb create mode 100644 db/migrate/20250201030154_add_missing_indexes_to_grid_objects.rb create mode 100644 db/migrate/20250201041406_create_pghero_query_stats.rb create mode 100644 db/migrate/20250201120655_enable_pg_statements.rb create mode 100644 db/migrate/20250201120842_remove_unused_index.rb create mode 100644 db/migrate/20250201170037_add_optimized_indexes_to_parties.rb diff --git a/db/migrate/20250120214715_set_preview_state_default_to_pending.rb b/db/migrate/20250120214715_set_preview_state_default_to_pending.rb new file mode 100644 index 0000000..5a83222 --- /dev/null +++ b/db/migrate/20250120214715_set_preview_state_default_to_pending.rb @@ -0,0 +1,7 @@ +class SetPreviewStateDefaultToPending < ActiveRecord::Migration[8.0] + def up + Party.where(preview_state: nil).find_each do |party| + party.update_column(:preview_state, :pending) + end + end +end diff --git a/db/migrate/20250120224953_add_missing_indexes_to_parties.rb b/db/migrate/20250120224953_add_missing_indexes_to_parties.rb new file mode 100644 index 0000000..25b2c4a --- /dev/null +++ b/db/migrate/20250120224953_add_missing_indexes_to_parties.rb @@ -0,0 +1,12 @@ +class AddMissingIndexesToParties < ActiveRecord::Migration[8.0] + def change + add_index :parties, :visibility + add_index :parties, :element + add_index :parties, :created_at + add_index :parties, [:weapons_count, :characters_count, :summons_count], + name: 'index_parties_on_counters' + add_index :parties, [:visibility, :created_at], + name: 'index_parties_on_visibility_created_at' + add_index :parties, :shortcode + end +end diff --git a/db/migrate/20250201030154_add_missing_indexes_to_grid_objects.rb b/db/migrate/20250201030154_add_missing_indexes_to_grid_objects.rb new file mode 100644 index 0000000..a79b854 --- /dev/null +++ b/db/migrate/20250201030154_add_missing_indexes_to_grid_objects.rb @@ -0,0 +1,8 @@ +class AddMissingIndexesToGridObjects < ActiveRecord::Migration[8.0] + def change + add_index :parties, :raid_id unless index_exists?(:parties, :raid_id) + add_index :characters, :granblue_id unless index_exists?(:characters, :granblue_id) + add_index :summons, :granblue_id unless index_exists?(:summons, :granblue_id) + add_index :weapons, :granblue_id unless index_exists?(:weapons, :granblue_id) + end +end diff --git a/db/migrate/20250201041406_create_pghero_query_stats.rb b/db/migrate/20250201041406_create_pghero_query_stats.rb new file mode 100644 index 0000000..74aaaa9 --- /dev/null +++ b/db/migrate/20250201041406_create_pghero_query_stats.rb @@ -0,0 +1,15 @@ +class CreatePgheroQueryStats < ActiveRecord::Migration[8.0] + def change + create_table :pghero_query_stats do |t| + t.text :database + t.text :user + t.text :query + t.integer :query_hash, limit: 8 + t.float :total_time + t.integer :calls, limit: 8 + t.timestamp :captured_at + end + + add_index :pghero_query_stats, [:database, :captured_at] + end +end diff --git a/db/migrate/20250201120655_enable_pg_statements.rb b/db/migrate/20250201120655_enable_pg_statements.rb new file mode 100644 index 0000000..d8e5894 --- /dev/null +++ b/db/migrate/20250201120655_enable_pg_statements.rb @@ -0,0 +1,9 @@ +class EnablePgStatements < ActiveRecord::Migration[8.0] + def up + execute 'CREATE EXTENSION IF NOT EXISTS pg_stat_statements;' + end + + def down + execute 'DROP EXTENSION IF EXISTS pg_stat_statements;' + end +end diff --git a/db/migrate/20250201120842_remove_unused_index.rb b/db/migrate/20250201120842_remove_unused_index.rb new file mode 100644 index 0000000..54889a3 --- /dev/null +++ b/db/migrate/20250201120842_remove_unused_index.rb @@ -0,0 +1,5 @@ +class RemoveUnusedIndex < ActiveRecord::Migration[8.0] + def change + remove_index :parties, :visibility + end +end diff --git a/db/migrate/20250201170037_add_optimized_indexes_to_parties.rb b/db/migrate/20250201170037_add_optimized_indexes_to_parties.rb new file mode 100644 index 0000000..e03d989 --- /dev/null +++ b/db/migrate/20250201170037_add_optimized_indexes_to_parties.rb @@ -0,0 +1,8 @@ +class AddOptimizedIndexesToParties < ActiveRecord::Migration[8.0] + def change + # Add composite index for grid positions since we order by these + add_index :grid_weapons, [:party_id, :position], name: 'index_grid_weapons_on_party_id_and_position' + add_index :grid_characters, [:party_id, :position], name: 'index_grid_characters_on_party_id_and_position' + add_index :grid_summons, [:party_id, :position], name: 'index_grid_summons_on_party_id_and_position' + end +end diff --git a/db/schema.rb b/db/schema.rb index b07f427..e5dedd9 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,13 +10,13 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.0].define(version: 2025_01_19_062554) do +ActiveRecord::Schema[8.0].define(version: 2025_02_01_170037) do # These are extensions that must be enabled in order to support this database enable_extension "btree_gin" enable_extension "pg_catalog.plpgsql" + enable_extension "pg_stat_statements" enable_extension "pg_trgm" enable_extension "pgcrypto" - enable_extension "uuid-ossp" create_table "app_updates", primary_key: "updated_at", id: :datetime, force: :cascade do |t| t.string "update_type", null: false @@ -67,6 +67,7 @@ ActiveRecord::Schema[8.0].define(version: 2025_01_19_062554) do t.string "kamigame", default: "" t.string "nicknames_en", default: [], null: false, array: true t.string "nicknames_jp", default: [], null: false, array: true + t.index ["granblue_id"], name: "index_characters_on_granblue_id" t.index ["name_en"], name: "index_characters_on_name_en", opclass: :gin_trgm_ops, using: :gin end @@ -129,6 +130,7 @@ ActiveRecord::Schema[8.0].define(version: 2025_01_19_062554) do t.integer "awakening_level", default: 1 t.index ["awakening_id"], name: "index_grid_characters_on_awakening_id" t.index ["character_id"], name: "index_grid_characters_on_character_id" + t.index ["party_id", "position"], name: "index_grid_characters_on_party_id_and_position" t.index ["party_id"], name: "index_grid_characters_on_party_id" end @@ -143,6 +145,7 @@ ActiveRecord::Schema[8.0].define(version: 2025_01_19_062554) do t.datetime "updated_at", null: false t.integer "transcendence_step", default: 0, null: false t.boolean "quick_summon", default: false + t.index ["party_id", "position"], name: "index_grid_summons_on_party_id_and_position" t.index ["party_id"], name: "index_grid_summons_on_party_id" t.index ["summon_id"], name: "index_grid_summons_on_summon_id" end @@ -168,6 +171,7 @@ ActiveRecord::Schema[8.0].define(version: 2025_01_19_062554) do t.integer "transcendence_step", default: 0 t.string "weapon_key4_id" t.index ["awakening_id"], name: "index_grid_weapons_on_awakening_id" + t.index ["party_id", "position"], name: "index_grid_weapons_on_party_id_and_position" t.index ["party_id"], name: "index_grid_weapons_on_party_id" t.index ["weapon_id"], name: "index_grid_weapons_on_weapon_id" t.index ["weapon_key1_id"], name: "index_grid_weapons_on_weapon_key1_id" @@ -304,18 +308,24 @@ ActiveRecord::Schema[8.0].define(version: 2025_01_19_062554) do t.datetime "preview_generated_at" t.string "preview_s3_key" t.index ["accessory_id"], name: "index_parties_on_accessory_id" + t.index ["created_at"], name: "index_parties_on_created_at" + t.index ["element"], name: "index_parties_on_element" t.index ["guidebook1_id"], name: "index_parties_on_guidebook1_id" t.index ["guidebook2_id"], name: "index_parties_on_guidebook2_id" t.index ["guidebook3_id"], name: "index_parties_on_guidebook3_id" t.index ["job_id"], name: "index_parties_on_job_id" t.index ["preview_generated_at"], name: "index_parties_on_preview_generated_at" t.index ["preview_state"], name: "index_parties_on_preview_state" + t.index ["raid_id"], name: "index_parties_on_raid_id" + t.index ["shortcode"], name: "index_parties_on_shortcode" t.index ["skill0_id"], name: "index_parties_on_skill0_id" t.index ["skill1_id"], name: "index_parties_on_skill1_id" t.index ["skill2_id"], name: "index_parties_on_skill2_id" t.index ["skill3_id"], name: "index_parties_on_skill3_id" t.index ["source_party_id"], name: "index_parties_on_source_party_id" t.index ["user_id"], name: "index_parties_on_user_id" + t.index ["visibility", "created_at"], name: "index_parties_on_visibility_created_at" + t.index ["weapons_count", "characters_count", "summons_count"], name: "index_parties_on_counters" end create_table "pg_search_documents", force: :cascade do |t| @@ -331,6 +341,17 @@ ActiveRecord::Schema[8.0].define(version: 2025_01_19_062554) do t.index ["searchable_type", "searchable_id"], name: "index_pg_search_documents_on_searchable" end + create_table "pghero_query_stats", force: :cascade do |t| + t.text "database" + t.text "user" + t.text "query" + t.bigint "query_hash" + t.float "total_time" + t.bigint "calls" + t.datetime "captured_at", precision: nil + t.index ["database", "captured_at"], name: "index_pghero_query_stats_on_database_and_captured_at" + end + create_table "raid_groups", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| t.string "name_en", null: false t.string "name_jp", null: false @@ -349,7 +370,6 @@ ActiveRecord::Schema[8.0].define(version: 2025_01_19_062554) do t.integer "element" t.string "slug" t.uuid "group_id" - t.index ["group_id"], name: "index_raids_on_group_id" end create_table "sparks", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| @@ -400,6 +420,7 @@ ActiveRecord::Schema[8.0].define(version: 2025_01_19_062554) do t.date "transcendence_date" t.string "nicknames_en", default: [], null: false, array: true t.string "nicknames_jp", default: [], null: false, array: true + t.index ["granblue_id"], name: "index_summons_on_granblue_id" t.index ["name_en"], name: "index_summons_on_name_en", opclass: :gin_trgm_ops, using: :gin end @@ -474,6 +495,7 @@ ActiveRecord::Schema[8.0].define(version: 2025_01_19_062554) do t.boolean "transcendence", default: false t.date "transcendence_date" t.string "recruits" + t.index ["granblue_id"], name: "index_weapons_on_granblue_id" t.index ["name_en"], name: "index_weapons_on_name_en", opclass: :gin_trgm_ops, using: :gin t.index ["recruits"], name: "index_weapons_on_recruits" end @@ -501,9 +523,10 @@ ActiveRecord::Schema[8.0].define(version: 2025_01_19_062554) do add_foreign_key "parties", "job_skills", column: "skill2_id" add_foreign_key "parties", "job_skills", column: "skill3_id" add_foreign_key "parties", "jobs" + add_foreign_key "parties", "parties", column: "source_party_id" add_foreign_key "parties", "raids" add_foreign_key "parties", "users" - add_foreign_key "raids", "raid_groups", column: "group_id" + add_foreign_key "raids", "raid_groups", column: "group_id", name: "raids_group_id_fkey" add_foreign_key "weapon_awakenings", "awakenings" add_foreign_key "weapon_awakenings", "weapons" end