2020-08-26 20:29:57 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class CreateDataExplorerQueries < ActiveRecord::Migration[6.0]
|
|
|
|
def up
|
|
|
|
create_table :data_explorer_queries do |t|
|
|
|
|
t.string :name
|
|
|
|
t.text :description
|
|
|
|
t.text :sql, default: "SELECT 1", null: false
|
|
|
|
t.integer :user_id
|
|
|
|
t.datetime :last_run_at
|
|
|
|
t.boolean :hidden, default: false, null: false
|
|
|
|
t.timestamps
|
|
|
|
end
|
|
|
|
|
|
|
|
create_table :data_explorer_query_groups do |t|
|
|
|
|
t.integer :query_id
|
|
|
|
t.integer :group_id
|
|
|
|
t.index :query_id
|
|
|
|
t.index :group_id
|
|
|
|
end
|
|
|
|
add_index(:data_explorer_query_groups, [:query_id, :group_id], unique: true)
|
|
|
|
|
|
|
|
DB.exec <<~SQL, now: Time.zone.now
|
|
|
|
INSERT INTO data_explorer_queries(id, name, description, sql, user_id, last_run_at, hidden, created_at, updated_at)
|
|
|
|
SELECT
|
|
|
|
(replace(key, 'q:',''))::integer,
|
|
|
|
value::json->>'name',
|
|
|
|
value::json->>'description',
|
|
|
|
value::json->>'sql',
|
2020-08-27 00:47:43 -04:00
|
|
|
CASE WHEN (value::json->'created_by')::text = 'null' THEN
|
|
|
|
null
|
|
|
|
WHEN (value::json->'created_by')::text = '""' THEN
|
|
|
|
null
|
|
|
|
WHEN (value::jsonb ? 'created_by') THEN
|
2020-08-27 00:16:39 -04:00
|
|
|
(value::json->>'created_by')::integer
|
|
|
|
ELSE
|
|
|
|
null
|
|
|
|
END,
|
2020-08-26 20:29:57 -04:00
|
|
|
CASE WHEN (value::json->'last_run_at')::text = 'null' THEN
|
|
|
|
null
|
|
|
|
WHEN (value::json->'last_run_at')::text = '""' THEN
|
|
|
|
null
|
|
|
|
ELSE
|
|
|
|
(value::json->'last_run_at')::text::timestamptz
|
|
|
|
END,
|
2020-08-26 22:29:27 -04:00
|
|
|
CASE WHEN (value::json->'hidden')::text = 'null' THEN
|
2020-08-26 20:29:57 -04:00
|
|
|
false
|
2020-08-26 22:29:27 -04:00
|
|
|
WHEN (value::jsonb ? 'hidden') THEN
|
2020-08-26 20:29:57 -04:00
|
|
|
(value::json->'hidden')::text::boolean
|
2020-08-26 22:29:27 -04:00
|
|
|
ELSE
|
|
|
|
false
|
2020-08-26 20:29:57 -04:00
|
|
|
END,
|
|
|
|
:now,
|
|
|
|
:now
|
|
|
|
FROM plugin_store_rows
|
2020-09-03 02:34:29 -04:00
|
|
|
WHERE plugin_name = 'discourse-data-explorer' AND type_name = 'JSON'
|
2020-08-26 20:29:57 -04:00
|
|
|
SQL
|
|
|
|
|
|
|
|
DB.query("SELECT * FROM plugin_store_rows WHERE plugin_name = 'discourse-data-explorer' AND type_name = 'JSON'").each do |row|
|
|
|
|
json = JSON.parse(row.value)
|
|
|
|
next if json['group_ids'].blank?
|
|
|
|
query_id = DB.query("SELECT id FROM data_explorer_queries WHERE
|
|
|
|
name = ? AND sql = ?", json['name'], json['sql']).first.id
|
|
|
|
|
|
|
|
json['group_ids'].each do |group_id|
|
2020-08-27 00:16:39 -04:00
|
|
|
next if group_id.blank? || query_id.blank?
|
2020-08-26 20:29:57 -04:00
|
|
|
DB.exec <<~SQL
|
2020-09-03 02:34:29 -04:00
|
|
|
INSERT INTO data_explorer_query_groups(query_id, group_id)
|
2020-08-26 20:29:57 -04:00
|
|
|
VALUES(#{query_id}, #{group_id})
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
end
|
2020-09-03 02:34:29 -04:00
|
|
|
|
|
|
|
DB.exec <<~SQL
|
|
|
|
SELECT
|
|
|
|
setval(
|
|
|
|
pg_get_serial_sequence('data_explorer_queries', 'id'),
|
2020-09-07 14:32:23 -04:00
|
|
|
(select greatest(max(id), 1) from data_explorer_queries)
|
2020-09-03 02:34:29 -04:00
|
|
|
);
|
|
|
|
SQL
|
2020-08-26 20:29:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def down
|
|
|
|
raise ActiveRecord::IrreversibleMigration
|
|
|
|
end
|
|
|
|
end
|