diff --git a/db/migrate/20250112032852_add_external_api_images_generated_md5_column_and_site_index.rb b/db/migrate/20250112032852_add_external_api_images_generated_md5_column_and_site_index.rb new file mode 100644 index 0000000..118392b --- /dev/null +++ b/db/migrate/20250112032852_add_external_api_images_generated_md5_column_and_site_index.rb @@ -0,0 +1,15 @@ +class AddExternalAPIImagesGeneratedMd5ColumnAndSiteIndex < ActiveRecord::Migration[7.1] + def change + add_index(:external_api_images, :site) + reversible do |r| + r.up do + exec_query("ALTER TABLE external_api_images ADD COLUMN md5 TEXT GENERATED ALWAYS AS (cached_data->'file'->>'md5') STORED") + add_index(:external_api_images, :md5) + end + r.down do + exec_query("ALTER TABLE external_api_images DROP COLUMN md5") + remove_index(:external_api_images, :md5) + end + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 6433cd0..ae9ab32 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_10_21_212746) do +ActiveRecord::Schema[7.1].define(version: 2025_01_12_032852) do create_schema "e621" # These are extensions that must be enabled in order to support this database @@ -157,7 +157,10 @@ ActiveRecord::Schema[7.1].define(version: 2024_10_21_212746) do t.datetime "cache_last_updated_at", default: -> { "CURRENT_TIMESTAMP(3)" }, null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false + r t.virtual "md5", type: :text, as: "((cached_data -> 'file'::text) ->> 'md5'::text)", stored: true t.index ["api_image_id"], name: "index_external_api_images_on_api_image_id" + t.index ["md5"], name: "index_external_api_images_on_md5" + t.index ["site"], name: "index_external_api_images_on_site" end create_table "good_job_batches", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|