mirror of
https://github.com/discourse/discourse.git
synced 2025-09-06 10:50:21 +08:00
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
33 lines
1.2 KiB
Ruby
33 lines
1.2 KiB
Ruby
class AddStaffCategory < ActiveRecord::Migration[4.2]
|
|
def up
|
|
return if Rails.env.test?
|
|
|
|
I18n.overrides_disabled do
|
|
result = DB.exec "SELECT 1 FROM site_settings where name = 'staff_category_id'"
|
|
if result == 0
|
|
description = I18n.t('staff_category_description')
|
|
name = I18n.t('staff_category_name')
|
|
|
|
if DB.exec("SELECT 1 FROM categories where name ilike :name", name: name) == 0
|
|
|
|
result = DB.query_single "INSERT INTO categories
|
|
(name, color, text_color, created_at, updated_at, user_id, slug, description, read_restricted, position)
|
|
VALUES (:name, '283890', 'FFFFFF', now(), now(), -1, '', :description, true, 2)
|
|
RETURNING id", name: name, description: description
|
|
|
|
category_id = result.first.to_i
|
|
|
|
DB.exec "UPDATE categories SET slug=:slug WHERE id=:category_id",
|
|
slug: Slug.for(name, "#{category_id}-category"), category_id: category_id
|
|
|
|
DB.exec "INSERT INTO site_settings(name, data_type, value, created_at, updated_at)
|
|
VALUES ('staff_category_id', 3, #{category_id.to_i}, now(), now())"
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
def down
|
|
# Do nothing
|
|
end
|
|
end
|