mirror of
https://github.com/discourse/discourse.git
synced 2024-11-27 03:43:45 +08:00
5f64fd0a21
Introduce new patterns for direct sql that are safe and fast. MiniSql is not prone to memory bloat that can happen with direct PG usage. It also has an extremely fast materializer and very a convenient API - DB.exec(sql, *params) => runs sql returns row count - DB.query(sql, *params) => runs sql returns usable objects (not a hash) - DB.query_hash(sql, *params) => runs sql returns an array of hashes - DB.query_single(sql, *params) => runs sql and returns a flat one dimensional array - DB.build(sql) => returns a sql builder See more at: https://github.com/discourse/mini_sql
65 lines
1.9 KiB
Ruby
65 lines
1.9 KiB
Ruby
require_dependency 'migration/base_dropper'
|
|
|
|
module Migration
|
|
class ColumnDropper < BaseDropper
|
|
def self.drop(table:, after_migration:, columns:, delay: nil, on_drop: nil)
|
|
validate_table_name(table)
|
|
columns.each { |column| validate_column_name(column) }
|
|
|
|
ColumnDropper.new(table, columns, after_migration, delay, on_drop).delayed_drop
|
|
end
|
|
|
|
def self.mark_readonly(table_name, column_name)
|
|
create_readonly_function(table_name, column_name)
|
|
|
|
DB.exec <<~SQL
|
|
CREATE TRIGGER #{readonly_trigger_name(table_name, column_name)}
|
|
BEFORE INSERT OR UPDATE OF #{column_name}
|
|
ON #{table_name}
|
|
FOR EACH ROW
|
|
WHEN (NEW.#{column_name} IS NOT NULL)
|
|
EXECUTE PROCEDURE #{readonly_function_name(table_name, column_name)};
|
|
SQL
|
|
end
|
|
|
|
private
|
|
|
|
def initialize(table, columns, after_migration, delay, on_drop)
|
|
super(after_migration, delay, on_drop)
|
|
|
|
@table = table
|
|
@columns = columns
|
|
end
|
|
|
|
def droppable?
|
|
builder = SqlBuilder.new(<<~SQL)
|
|
SELECT 1
|
|
FROM INFORMATION_SCHEMA.COLUMNS
|
|
/*where*/
|
|
LIMIT 1
|
|
SQL
|
|
|
|
builder.where("table_schema = 'public'")
|
|
.where("table_name = :table")
|
|
.where("column_name IN (:columns)")
|
|
.where(previous_migration_done)
|
|
.exec(table: @table,
|
|
columns: @columns,
|
|
delay: "#{@delay} seconds",
|
|
after_migration: @after_migration).to_a.length > 0
|
|
end
|
|
|
|
def execute_drop!
|
|
@columns.each do |column|
|
|
DB.exec <<~SQL
|
|
DROP TRIGGER IF EXISTS #{BaseDropper.readonly_trigger_name(@table, column)} ON #{@table};
|
|
DROP FUNCTION IF EXISTS #{BaseDropper.readonly_function_name(@table, column)} CASCADE;
|
|
SQL
|
|
|
|
# safe cause it is protected on method entry, can not be passed in params
|
|
DB.exec("ALTER TABLE #{@table} DROP COLUMN IF EXISTS #{column}")
|
|
end
|
|
end
|
|
end
|
|
end
|