2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-20 02:20:50 -05:00
|
|
|
require_dependency 'migration/base_dropper'
|
|
|
|
|
|
|
|
module Migration
|
2018-10-08 02:47:38 -05:00
|
|
|
class ColumnDropper
|
2018-03-20 02:20:50 -05:00
|
|
|
def self.mark_readonly(table_name, column_name)
|
2018-10-08 02:47:38 -05:00
|
|
|
BaseDropper.create_readonly_function(table_name, column_name)
|
2018-03-20 02:20:50 -05:00
|
|
|
|
2018-06-19 01:13:14 -05:00
|
|
|
DB.exec <<~SQL
|
2018-10-08 02:47:38 -05:00
|
|
|
CREATE TRIGGER #{BaseDropper.readonly_trigger_name(table_name, column_name)}
|
2018-03-20 02:20:50 -05:00
|
|
|
BEFORE INSERT OR UPDATE OF #{column_name}
|
|
|
|
ON #{table_name}
|
|
|
|
FOR EACH ROW
|
|
|
|
WHEN (NEW.#{column_name} IS NOT NULL)
|
2018-10-08 02:47:38 -05:00
|
|
|
EXECUTE PROCEDURE #{BaseDropper.readonly_function_name(table_name, column_name)};
|
2018-03-20 02:20:50 -05:00
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
2018-10-08 02:47:38 -05:00
|
|
|
def self.execute_drop(table, columns)
|
|
|
|
table = table.to_s
|
2018-03-20 02:20:50 -05:00
|
|
|
|
2018-10-08 02:47:38 -05:00
|
|
|
columns.each do |column|
|
|
|
|
column = column.to_s
|
2019-04-28 22:58:52 -05:00
|
|
|
self.drop_readonly(table, column)
|
2018-03-20 02:20:50 -05:00
|
|
|
# safe cause it is protected on method entry, can not be passed in params
|
2018-10-08 02:47:38 -05:00
|
|
|
DB.exec("ALTER TABLE #{table} DROP COLUMN IF EXISTS #{column}")
|
2018-03-20 02:20:50 -05:00
|
|
|
end
|
|
|
|
end
|
2019-04-28 22:58:52 -05:00
|
|
|
|
2019-08-08 09:06:27 -05:00
|
|
|
def self.drop_readonly(table_name, column_name)
|
|
|
|
BaseDropper.drop_readonly_function(table_name, column_name)
|
|
|
|
|
|
|
|
# Backward compatibility for old functions created in the public schema
|
|
|
|
DB.exec("DROP FUNCTION IF EXISTS #{BaseDropper.old_readonly_function_name(table_name, column_name)} CASCADE")
|
2019-04-28 22:58:52 -05:00
|
|
|
end
|
2018-03-20 02:20:50 -05:00
|
|
|
end
|
|
|
|
end
|