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

@@ -19,10 +19,10 @@ class Notification < ActiveRecord::Base
after_commit :refresh_notification_count, on: [:create, :update, :destroy]
def self.ensure_consistency!
Notification.exec_sql <<-SQL
DB.exec(<<~SQL, Notification.types[:private_message])
DELETE
FROM notifications n
WHERE notification_type = #{Notification.types[:private_message]}
WHERE notification_type = ?
AND NOT EXISTS (
SELECT 1
FROM posts p
@@ -152,17 +152,15 @@ class Notification < ActiveRecord::Base
if notifications.present?
ids = Notification.exec_sql("
ids = DB.query_single(<<~SQL, count.to_i)
SELECT n.id FROM notifications n
WHERE
n.notification_type = 6 AND
n.user_id = #{user.id.to_i} AND
NOT read
ORDER BY n.id ASC
LIMIT #{count.to_i}
").values.map do |x, _|
x.to_i
end
LIMIT ?
SQL
if ids.length > 0
notifications += user