0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-05 21:57:36 +08:00
discourse/db/migrate/20140715190552_remove_uncategorized_parents.rb
David Taylor b37f303c2a
DEV: Skip Uncategorized insert in add_uncategorized_category on fresh installs (#39971)
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.
2026-05-13 12:13:05 +01:00

14 lines
411 B
Ruby
Vendored

# frozen_string_literal: true
class RemoveUncategorizedParents < ActiveRecord::Migration[4.2]
def up
uncat = execute("SELECT value FROM site_settings WHERE name = 'uncategorized_category_id'")
row = uncat.first if uncat && uncat.ntuples > 0
if row && row["value"]
execute "UPDATE categories SET parent_category_id = NULL where id = #{row["value"].to_i}"
end
end
def down
end
end