mirror of
https://gh.wpcy.net/https://github.com/discourse/discourse.git
synced 2026-05-27 15:41:10 +08:00
Previously, `20131022045114_add_uncategorized_category.rb` always created an Uncategorized category and wrote the matching `uncategorized_category_id` site setting, duplicating work that `db/fixtures/500_categories.rb` (via `SeedData::Categories`) already does on every fresh install. This change gates the insert/update block on whether any category-less regular topics exist — the only case where the migration genuinely needs to backfill before the CHECK constraint can be added. The `ALTER TABLE … ADD CONSTRAINT has_category_id` still runs unconditionally so the schema is identical on both paths. Two later migrations (`20140715190552_remove_uncategorized_parents.rb` and `20150818190757_create_embeddable_hosts.rb`) read `uncategorized_category_id` via `PG::Result#[](0)`, which raises `IndexError` rather than returning `nil` when the row is missing — previously dormant because the original migration always wrote the setting. They are updated here to guard with `cmd_tuples`/`ntuples` before indexing. Extracted from https://github.com/discourse/discourse/pull/39788.
42 lines
1.4 KiB
Ruby
Vendored
42 lines
1.4 KiB
Ruby
Vendored
# frozen_string_literal: true
|
|
|
|
class CreateEmbeddableHosts < ActiveRecord::Migration[4.2]
|
|
def change
|
|
create_table :embeddable_hosts, force: true do |t|
|
|
t.string :host, null: false
|
|
t.integer :category_id, null: false
|
|
t.timestamps null: false
|
|
end
|
|
|
|
category_id = 0
|
|
category_row =
|
|
execute(
|
|
"SELECT c.id FROM categories AS c
|
|
INNER JOIN site_settings AS s ON s.value = c.name
|
|
WHERE s.name = 'embed_category'",
|
|
)
|
|
|
|
category_id = category_row[0]["id"].to_i if category_row.cmd_tuples > 0
|
|
|
|
if category_id == 0
|
|
uncategorized =
|
|
execute("SELECT value FROM site_settings WHERE name = 'uncategorized_category_id'")
|
|
category_id = uncategorized[0]["value"].to_i if uncategorized.cmd_tuples > 0
|
|
end
|
|
|
|
embeddable_hosts = execute("SELECT value FROM site_settings WHERE name = 'embeddable_hosts'")
|
|
if embeddable_hosts && embeddable_hosts.cmd_tuples > 0
|
|
val = embeddable_hosts[0]["value"]
|
|
if val.present?
|
|
records = val.split("\n")
|
|
if records.present?
|
|
records.each do |h|
|
|
execute "INSERT INTO embeddable_hosts (host, category_id, created_at, updated_at) VALUES ('#{h}', #{category_id}, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)"
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
execute "DELETE FROM site_settings WHERE name IN ('embeddable_hosts', 'embed_category')"
|
|
end
|
|
end
|