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 @@ Migration::ColumnDropper.drop(
},
on_drop: ->() {
STDERR.puts "Removing superflous user stats columns!"
ActiveRecord::Base.exec_sql "DROP FUNCTION IF EXISTS first_unread_topic_for(int)"
DB.exec "DROP FUNCTION IF EXISTS first_unread_topic_for(int)"
}
)

View File

@@ -10,18 +10,18 @@ uncat_id = -1 unless Numeric === uncat_id
if uncat_id == -1 || !Category.exists?(uncat_id)
puts "Seeding uncategorized category!"
result = Category.exec_sql "SELECT 1 FROM categories WHERE lower(name) = 'uncategorized'"
count = DB.exec "SELECT 1 FROM categories WHERE lower(name) = 'uncategorized'"
name = 'Uncategorized'
name << SecureRandom.hex if result.count > 0
name << SecureRandom.hex if count > 0
result = Category.exec_sql "INSERT INTO categories
result = DB.query_single "INSERT INTO categories
(name,color,slug,description,text_color, user_id, created_at, updated_at, position, name_lower)
VALUES ('#{name}', 'AB9364', 'uncategorized', '', 'FFFFFF', -1, now(), now(), 1, '#{name.downcase}' )
RETURNING id
"
category_id = result[0]["id"].to_i
category_id = result.first.to_i
Category.exec_sql "DELETE FROM site_settings where name = 'uncategorized_category_id'"
Category.exec_sql "INSERT INTO site_settings(name, data_type, value, created_at, updated_at)
DB.exec "DELETE FROM site_settings where name = 'uncategorized_category_id'"
DB.exec "INSERT INTO site_settings(name, data_type, value, created_at, updated_at)
VALUES ('uncategorized_category_id', 3, #{category_id}, now(), now())"
end

View File

@@ -31,7 +31,7 @@ BadgeGrouping.seed do |g|
end
# BUGFIX
Badge.exec_sql <<-SQL.squish
DB.exec <<-SQL.squish
UPDATE badges
SET badge_grouping_id = -1
WHERE NOT EXISTS (

View File

@@ -36,7 +36,7 @@ unless Rails.env.test?
end
# Reset topic count because we don't count the description topic
Category.exec_sql "UPDATE categories SET topic_count = 0 WHERE id = #{lounge.id}"
DB.exec "UPDATE categories SET topic_count = 0 WHERE id = #{lounge.id}"
end
end
end

View File

@@ -25,7 +25,7 @@ unless Rails.env.test?
end
# Reset topic count because we don't count the description topic
Category.exec_sql "UPDATE categories SET topic_count = 0 WHERE id = #{meta.id}"
DB.exec "UPDATE categories SET topic_count = 0 WHERE id = #{meta.id}"
end
end
end

View File

@@ -33,7 +33,7 @@ unless Rails.env.test?
end
# Reset topic count because we don't count the description topic
Category.exec_sql "UPDATE categories SET topic_count = 0 WHERE id = #{staff.id}"
DB.exec "UPDATE categories SET topic_count = 0 WHERE id = #{staff.id}"
end
end
end