mirror of
https://github.com/discourse/discourse.git
synced 2024-11-26 14:03:39 +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
78 lines
2.1 KiB
Ruby
78 lines
2.1 KiB
Ruby
require_dependency 'migration/base_dropper'
|
|
|
|
module Migration
|
|
class Migration::TableDropper < BaseDropper
|
|
def self.delayed_drop(table_name:, after_migration:, delay: nil, on_drop: nil)
|
|
validate_table_name(table_name)
|
|
|
|
TableDropper.new(table_name, nil, after_migration, delay, on_drop).delayed_drop
|
|
end
|
|
|
|
def self.delayed_rename(old_name:, new_name:, after_migration:, delay: nil, on_drop: nil)
|
|
validate_table_name(old_name)
|
|
validate_table_name(new_name)
|
|
|
|
TableDropper.new(old_name, new_name, after_migration, delay, on_drop).delayed_drop
|
|
end
|
|
|
|
def self.read_only_table(table_name)
|
|
create_readonly_function(table_name)
|
|
|
|
DB.exec <<~SQL
|
|
CREATE TRIGGER #{readonly_trigger_name(table_name)}
|
|
BEFORE INSERT OR UPDATE OR DELETE OR TRUNCATE
|
|
ON #{table_name}
|
|
FOR EACH STATEMENT
|
|
EXECUTE PROCEDURE #{readonly_function_name(table_name)};
|
|
SQL
|
|
end
|
|
|
|
private
|
|
|
|
def initialize(old_name, new_name, after_migration, delay, on_drop)
|
|
super(after_migration, delay, on_drop)
|
|
|
|
@old_name = old_name
|
|
@new_name = new_name
|
|
end
|
|
|
|
def droppable?
|
|
builder = DB.build(<<~SQL)
|
|
SELECT 1
|
|
FROM INFORMATION_SCHEMA.TABLES
|
|
/*where*/
|
|
LIMIT 1
|
|
SQL
|
|
|
|
builder.where(table_exists(":new_name")) if @new_name.present?
|
|
|
|
builder.where("table_schema = 'public'")
|
|
.where(table_exists(":old_name"))
|
|
.where(previous_migration_done)
|
|
.exec(old_name: @old_name,
|
|
new_name: @new_name,
|
|
delay: "#{@delay} seconds",
|
|
after_migration: @after_migration) > 0
|
|
end
|
|
|
|
def table_exists(table_name_placeholder)
|
|
<<~SQL
|
|
EXISTS(
|
|
SELECT 1
|
|
FROM INFORMATION_SCHEMA.TABLES
|
|
WHERE table_schema = 'public' AND
|
|
table_name = #{table_name_placeholder}
|
|
)
|
|
SQL
|
|
end
|
|
|
|
def execute_drop!
|
|
DB.exec("DROP TABLE IF EXISTS #{@old_name}")
|
|
|
|
DB.exec <<~SQL
|
|
DROP FUNCTION IF EXISTS #{BaseDropper.readonly_function_name(@old_name)} CASCADE;
|
|
SQL
|
|
end
|
|
end
|
|
end
|