A platform for community discussion. Free, open, simple.
FIX: Add `table_schema` filter to `mark_readonly` default check (#37037)
Régis Hanol2623d7a6078a
diff --git a/lib/migration/column_dropper.rb b/lib/migration/column_dropper.rbindex 5f72eb4b38a..649af059a6d 100644--- a/lib/migration/column_dropper.rb+++ b/lib/migration/column_dropper.rb@@ -8,7 +8,8 @@ module Migrationhas_default = DB.query_single(<<~SQL, table_name: table_name, column_name: column_name).firstSELECT column_default IS NOT NULLFROM information_schema.columns- WHERE table_name = :table_name+ WHERE table_schema = 'public'+ AND table_name = :table_nameAND column_name = :column_nameSQLdiff --git a/spec/lib/migration/column_dropper_spec.rb b/spec/lib/migration/column_dropper_spec.rbindex d5a42177a39..eef390e073b 100644--- a/spec/lib/migration/column_dropper_spec.rb+++ b/spec/lib/migration/column_dropper_spec.rb@@ -28,6 +28,33 @@ RSpec.describe Migration::ColumnDropper doendend+ describe ".mark_readonly" do+ it "only checks the public schema for default values" do+ DB.exec <<~SQL+ CREATE TABLE test_public_schema (id INTEGER, col TEXT);+ CREATE SCHEMA other_schema;+ CREATE TABLE other_schema.test_public_schema (id INTEGER, col TEXT DEFAULT 'has_default');+ SQL++ # Stub to reverse query results, simulating a database where non-public+ # schemas come first (e.g., due to backup/restore OID ordering).+ # Without the table_schema='public' fix, this causes mark_readonly to+ # incorrectly see a default value from the other schema.+ original_method = DB.method(:query_single)+ allow(DB).to receive(:query_single) do |*args, **kwargs|+ results = original_method.call(*args, **kwargs)+ results.reverse+ end++ expect { described_class.mark_readonly("test_public_schema", "col") }.not_to raise_error++ described_class.drop_readonly("test_public_schema", "col")+ ensure+ DB.exec "DROP SCHEMA IF EXISTS other_schema CASCADE"+ DB.exec "DROP TABLE IF EXISTS test_public_schema CASCADE"+ end+ end+describe ".mark_readonly" dolet(:table_name) { "table_with_readonly_column" }