From 772882ee547db1a69af55fcdd81543c05905e559 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Jos=C3=A9=20Mata?= Date: Sat, 2 May 2026 10:02:42 +0000 Subject: [PATCH] Fix Sophtron schema drift --- ...0260502120000_fix_sophtron_schema_drift.rb | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 db/migrate/20260502120000_fix_sophtron_schema_drift.rb diff --git a/db/migrate/20260502120000_fix_sophtron_schema_drift.rb b/db/migrate/20260502120000_fix_sophtron_schema_drift.rb new file mode 100644 index 000000000..af8da7b0c --- /dev/null +++ b/db/migrate/20260502120000_fix_sophtron_schema_drift.rb @@ -0,0 +1,36 @@ +# Aligns sophtron tables with constraints that PR #596 added to db/schema.rb +# without writing a corresponding migration. Idempotent so envs that already +# match (e.g., those bootstrapped via db:schema:load) re-run cleanly. +class FixSophtronSchemaDrift < ActiveRecord::Migration[7.2] + disable_ddl_transaction! + + INDEX_NAME = "idx_unique_sophtron_accounts_per_item".freeze + + def up + unless index_exists?(:sophtron_accounts, [ :sophtron_item_id, :account_id ], unique: true, name: INDEX_NAME) + add_index :sophtron_accounts, + [ :sophtron_item_id, :account_id ], + unique: true, + name: INDEX_NAME, + algorithm: :concurrently + end + + change_column_null :sophtron_items, :user_id, false if column_nullable?(:sophtron_items, :user_id) + change_column_null :sophtron_items, :access_key, false if column_nullable?(:sophtron_items, :access_key) + end + + def down + if index_exists?(:sophtron_accounts, name: INDEX_NAME) + remove_index :sophtron_accounts, name: INDEX_NAME, algorithm: :concurrently + end + + change_column_null :sophtron_items, :user_id, true + change_column_null :sophtron_items, :access_key, true + end + + private + + def column_nullable?(table, column) + connection.columns(table).find { |c| c.name == column.to_s }&.null + end +end