discourse/lib/comment_migration.rb
Sam 5f64fd0a21 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
2018-06-19 16:13:36 +10:00

59 lines
1.4 KiB
Ruby

class CommentMigration < ActiveRecord::Migration[4.2]
def comments_up
raise "Not implemented"
end
def up
comments_up.each do |table|
table[1].each do |column|
table_name = table[0]
column_name = column[0]
comment = column[1]
if column_name == :_table
DB.exec "COMMENT ON TABLE #{table_name} IS ?", comment
puts " COMMENT ON TABLE #{table_name}"
else
DB.exec "COMMENT ON COLUMN #{table_name}.#{column_name} IS ?", comment
puts " COMMENT ON COLUMN #{table_name}.#{column_name}"
end
end
end
end
def comments_down
{}
end
def down
replace_nils(comments_up).deep_merge(comments_down).each do |table|
table[1].each do |column|
table_name = table[0]
column_name = column[0]
comment = column[1]
if column_name == :_table
DB.exec "COMMENT ON TABLE #{table_name} IS ?", comment
puts " COMMENT ON TABLE #{table_name}"
else
DB.exec "COMMENT ON COLUMN #{table_name}.#{column_name} IS ?", comment
puts " COMMENT ON COLUMN #{table_name}.#{column_name}"
end
end
end
end
private
def replace_nils(hash)
hash.each do |key, value|
if Hash === value
hash[key] = replace_nils value
else
hash[key] = nil
end
end
end
end