From a31dc0a84ae74f4181e445dd92a957e1cb0bff35 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Mon, 2 Sep 2024 10:09:40 +0100 Subject: [PATCH] DEV: Raise an exception if trying to set a readonly column with default (#27416) --- lib/migration/column_dropper.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/migration/column_dropper.rb b/lib/migration/column_dropper.rb index 83d2feec640..5f72eb4b38a 100644 --- a/lib/migration/column_dropper.rb +++ b/lib/migration/column_dropper.rb @@ -5,6 +5,15 @@ require "migration/base_dropper" module Migration class ColumnDropper def self.mark_readonly(table_name, column_name) + has_default = DB.query_single(<<~SQL, table_name: table_name, column_name: column_name).first + SELECT column_default IS NOT NULL + FROM information_schema.columns + WHERE table_name = :table_name + AND column_name = :column_name + SQL + + raise "You must drop a column's default value before marking it as readonly" if has_default + BaseDropper.create_readonly_function(table_name, column_name) DB.exec <<~SQL