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

@ -626,7 +626,7 @@ class ImportScripts::Base
def update_topic_status
puts "", "Updating topic status"
Topic.exec_sql(<<~SQL)
DB.exec(<<~SQL)
UPDATE topics AS t
SET closed = TRUE
WHERE EXISTS(
@ -636,7 +636,7 @@ class ImportScripts::Base
)
SQL
Topic.exec_sql(<<~SQL)
DB.exec(<<~SQL)
UPDATE topics AS t
SET archived = TRUE
WHERE EXISTS(
@ -646,7 +646,7 @@ class ImportScripts::Base
)
SQL
TopicCustomField.exec_sql(<<~SQL)
DB.exec(<<~SQL)
DELETE FROM topic_custom_fields
WHERE name IN ('import_closed', 'import_archived')
SQL
@ -654,7 +654,7 @@ class ImportScripts::Base
def update_bumped_at
puts "", "Updating bumped_at on topics"
Post.exec_sql("update topics t set bumped_at = COALESCE((select max(created_at) from posts where topic_id = t.id and post_type = #{Post.types[:regular]}), bumped_at)")
DB.exec("update topics t set bumped_at = COALESCE((select max(created_at) from posts where topic_id = t.id and post_type = #{Post.types[:regular]}), bumped_at)")
end
def update_last_posted_at
@ -674,7 +674,7 @@ class ImportScripts::Base
AND users.last_posted_at <> lpa.last_posted_at
SQL
User.exec_sql(sql)
DB.exec(sql)
end
def update_user_stats
@ -707,7 +707,7 @@ class ImportScripts::Base
AND user_stats.first_post_created_at <> sub.first_post_created_at
SQL
User.exec_sql(sql)
DB.exec(sql)
puts "", "Updating user post_count..."
@ -725,7 +725,7 @@ class ImportScripts::Base
AND user_stats.post_count <> sub.post_count
SQL
User.exec_sql(sql)
DB.exec(sql)
puts "", "Updating user topic_count..."
@ -743,15 +743,15 @@ class ImportScripts::Base
AND user_stats.topic_count <> sub.topic_count
SQL
User.exec_sql(sql)
DB.exec(sql)
end
# scripts that are able to import last_seen_at from the source data should override this method
def update_last_seen_at
puts "", "Updating last seen at on users"
User.exec_sql("UPDATE users SET last_seen_at = created_at WHERE last_seen_at IS NULL")
User.exec_sql("UPDATE users SET last_seen_at = last_posted_at WHERE last_posted_at IS NOT NULL")
DB.exec("UPDATE users SET last_seen_at = created_at WHERE last_seen_at IS NULL")
DB.exec("UPDATE users SET last_seen_at = last_posted_at WHERE last_posted_at IS NOT NULL")
end
def update_feature_topic_users