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

@@ -535,7 +535,7 @@ class ImportScripts::Lithium < ImportScripts::Base
end
puts "loading data into temp table"
PostAction.exec_sql("create temp table like_data(user_id int, post_id int, created_at timestamp without time zone)")
DB.exec("create temp table like_data(user_id int, post_id int, created_at timestamp without time zone)")
PostAction.transaction do
results.each do |result|
@@ -544,17 +544,17 @@ class ImportScripts::Lithium < ImportScripts::Base
next unless result["user_id"] && result["post_id"]
PostAction.exec_sql("INSERT INTO like_data VALUES (:user_id,:post_id,:created_at)",
user_id: result["user_id"],
post_id: result["post_id"],
created_at: result["created_at"]
)
DB.exec("INSERT INTO like_data VALUES (:user_id,:post_id,:created_at)",
user_id: result["user_id"],
post_id: result["post_id"],
created_at: result["created_at"]
)
end
end
puts "creating missing post actions"
PostAction.exec_sql <<-SQL
DB.exec <<~SQL
INSERT INTO post_actions (post_id, user_id, post_action_type_id, created_at, updated_at)
SELECT l.post_id, l.user_id, 2, l.created_at, l.created_at FROM like_data l
@@ -563,7 +563,7 @@ class ImportScripts::Lithium < ImportScripts::Base
SQL
puts "creating missing user actions"
UserAction.exec_sql <<-SQL
DB.exec <<~SQL
INSERT INTO user_actions (user_id, action_type, target_topic_id, target_post_id, acting_user_id, created_at, updated_at)
SELECT pa.user_id, 1, p.topic_id, p.id, pa.user_id, pa.created_at, pa.created_at
FROM post_actions pa
@@ -574,7 +574,7 @@ class ImportScripts::Lithium < ImportScripts::Base
SQL
# reverse action
UserAction.exec_sql <<-SQL
DB.exec <<~SQL
INSERT INTO user_actions (user_id, action_type, target_topic_id, target_post_id, acting_user_id, created_at, updated_at)
SELECT p.user_id, 2, p.topic_id, p.id, pa.user_id, pa.created_at, pa.created_at
FROM post_actions pa
@@ -586,7 +586,7 @@ class ImportScripts::Lithium < ImportScripts::Base
SQL
puts "updating like counts on posts"
Post.exec_sql <<-SQL
DB.exec <<~SQL
UPDATE posts SET like_count = coalesce(cnt,0)
FROM (
SELECT post_id, count(*) cnt
@@ -600,7 +600,7 @@ class ImportScripts::Lithium < ImportScripts::Base
puts "updating like counts on topics"
Post.exec_sql <<-SQL
DB.exec <<-SQL
UPDATE topics SET like_count = coalesce(cnt,0)
FROM (
SELECT topic_id, sum(like_count) cnt
@@ -627,7 +627,7 @@ class ImportScripts::Lithium < ImportScripts::Base
end
puts "loading data into temp table"
PostAction.exec_sql("create temp table accepted_data(post_id int primary key)")
DB.exec("create temp table accepted_data(post_id int primary key)")
PostAction.transaction do
results.each do |result|
@@ -635,7 +635,7 @@ class ImportScripts::Lithium < ImportScripts::Base
next unless result["post_id"]
PostAction.exec_sql("INSERT INTO accepted_data VALUES (:post_id)",
DB.exec("INSERT INTO accepted_data VALUES (:post_id)",
post_id: result["post_id"]
)
@@ -643,7 +643,7 @@ class ImportScripts::Lithium < ImportScripts::Base
end
puts "deleting dupe answers"
PostAction.exec_sql <<-SQL
DB.exec <<~SQL
DELETE FROM accepted_data WHERE post_id NOT IN (
SELECT post_id FROM
(
@@ -656,7 +656,7 @@ class ImportScripts::Lithium < ImportScripts::Base
SQL
puts "importing accepted answers"
PostAction.exec_sql <<-SQL
DB.exec <<~SQL
INSERT into post_custom_fields (name, value, post_id, created_at, updated_at)
SELECT 'is_accepted_answer', 'true', a.post_id, current_timestamp, current_timestamp
FROM accepted_data a
@@ -665,7 +665,7 @@ class ImportScripts::Lithium < ImportScripts::Base
SQL
puts "marking accepted topics"
PostAction.exec_sql <<-SQL
DB.exec <<~SQL
INSERT into topic_custom_fields (name, value, topic_id, created_at, updated_at)
SELECT 'accepted_answer_post_id', a.post_id::varchar, p.topic_id, current_timestamp, current_timestamp
FROM accepted_data a
@@ -797,10 +797,10 @@ class ImportScripts::Lithium < ImportScripts::Base
results.map { |r| r["post_id"] }.each_slice(500) do |ids|
mapped = ids.map { |id| existing_map[id] }.compact
Topic.exec_sql("
UPDATE topics SET closed = true
WHERE id IN (SELECT topic_id FROM posts where id in (:ids))
", ids: mapped) if mapped.present?
DB.exec(<<~SQL, ids: mapped) if mapped.present?
UPDATE topics SET closed = true
WHERE id IN (SELECT topic_id FROM posts where id in (:ids))
SQL
end
end
@@ -819,8 +819,8 @@ class ImportScripts::Lithium < ImportScripts::Base
WHERE pm.id IS NULL AND f.name = 'import_unique_id'
SQL
r = Permalink.exec_sql sql
puts "#{r.cmd_tuples} permalinks to topics added!"
r = DB.exec sql
puts "#{r} permalinks to topics added!"
sql = <<-SQL
INSERT INTO permalinks (url, post_id, created_at, updated_at)
@@ -831,8 +831,8 @@ SQL
WHERE pm.id IS NULL AND f.name = 'import_unique_id'
SQL
r = Permalink.exec_sql sql
puts "#{r.cmd_tuples} permalinks to posts added!"
r = DB.exec sql
puts "#{r} permalinks to posts added!"
end