DEV: remove exec_sql and replace with mini_sql

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
This commit is contained in:
Sam
2018-06-19 16:13:14 +10:00
parent cc3fc87dd7
commit 5f64fd0a21
112 changed files with 782 additions and 763 deletions

View File

@ -48,7 +48,7 @@ module Migration
"Discourse: #{column_name} in #{table_name} is readonly" :
"Discourse: #{table_name} is read only"
ActiveRecord::Base.exec_sql <<~SQL
DB.exec <<~SQL
CREATE OR REPLACE FUNCTION #{readonly_function_name(table_name, column_name)} RETURNS trigger AS $rcr$
BEGIN
RAISE EXCEPTION '#{message}';

View File

@ -12,7 +12,7 @@ module Migration
def self.mark_readonly(table_name, column_name)
create_readonly_function(table_name, column_name)
ActiveRecord::Base.exec_sql <<~SQL
DB.exec <<~SQL
CREATE TRIGGER #{readonly_trigger_name(table_name, column_name)}
BEFORE INSERT OR UPDATE OF #{column_name}
ON #{table_name}
@ -51,13 +51,13 @@ module Migration
def execute_drop!
@columns.each do |column|
ActiveRecord::Base.exec_sql <<~SQL
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
ActiveRecord::Base.exec_sql("ALTER TABLE #{@table} DROP COLUMN IF EXISTS #{column}")
DB.exec("ALTER TABLE #{@table} DROP COLUMN IF EXISTS #{column}")
end
end
end

View File

@ -18,7 +18,7 @@ module Migration
def self.read_only_table(table_name)
create_readonly_function(table_name)
ActiveRecord::Base.exec_sql <<~SQL
DB.exec <<~SQL
CREATE TRIGGER #{readonly_trigger_name(table_name)}
BEFORE INSERT OR UPDATE OR DELETE OR TRUNCATE
ON #{table_name}
@ -37,7 +37,7 @@ module Migration
end
def droppable?
builder = SqlBuilder.new(<<~SQL)
builder = DB.build(<<~SQL)
SELECT 1
FROM INFORMATION_SCHEMA.TABLES
/*where*/
@ -52,7 +52,7 @@ module Migration
.exec(old_name: @old_name,
new_name: @new_name,
delay: "#{@delay} seconds",
after_migration: @after_migration).to_a.length > 0
after_migration: @after_migration) > 0
end
def table_exists(table_name_placeholder)
@ -67,9 +67,9 @@ module Migration
end
def execute_drop!
ActiveRecord::Base.exec_sql("DROP TABLE IF EXISTS #{@old_name}")
DB.exec("DROP TABLE IF EXISTS #{@old_name}")
ActiveRecord::Base.exec_sql <<~SQL
DB.exec <<~SQL
DROP FUNCTION IF EXISTS #{BaseDropper.readonly_function_name(@old_name)} CASCADE;
SQL
end