0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-06 03:25:17 +08:00
discourse/db/migrate/20201109170951_migrate_github_user_infos.rb
David Taylor 4e7578b76e
DEV: Check & resolve sequence issues during db:check_structure_dump (#40118)
As with rows, database sequence values are not persisted in
`structure.sql`. This commit adds a new check which detects any sequence
values introduced by migrations, and throws an error.

Existing issues are fixes up:
1. `enable_bookmarks_with_reminders` was being inserted and removed from
site settings unnecessarily. Removed.

2. user_associated_accounts and data_explorer_queries were having their
sequences updated unnecessarily on empty databases. Updated them to be
conditional.

3. Groups PK had been modified in a couple of different ways. A new
migration normalizes the start & current values to `40`, matching the
current behaviour

4. Badges PK was intended to START_AT 100. However, seed_fu/activerecord
were resetting the sequence to a lower number than that. A before_save
hook on the model already exists to work around that problem. Added a
migration to reset the sequence STARTS AT to 1, to make it clear that it
has no effect and we're relying on the model layer.

5. Flags PK STARTS_AT was already fixed in
`67305dc7bb`. This migration just
normalizes the current value, to fix up any sites which may have been
deployed with the `structure.sql` over the last few days
2026-05-18 15:23:41 +01:00

43 lines
1.2 KiB
Ruby
Vendored

# frozen_string_literal: true
class MigrateGithubUserInfos < ActiveRecord::Migration[6.0]
def up
# If the user_associated_accounts table is currently empty,
# maintain the primary key from github_user_infos
# This is useful for people that are using data explorer to access the data
maintain_ids = DB.query_single("SELECT count(*) FROM user_associated_accounts")[0] == 0
inserted_count = DB.exec(<<~SQL)
INSERT INTO user_associated_accounts (
provider_name,
provider_uid,
user_id,
info,
last_used,
created_at,
updated_at
#{", id" if maintain_ids}
) SELECT
'github',
github_user_id,
user_id,
json_build_object('nickname', screen_name),
updated_at,
created_at,
updated_at
#{", id" if maintain_ids}
FROM github_user_infos
SQL
execute <<~SQL if maintain_ids && inserted_count > 0
SELECT setval(
pg_get_serial_sequence('user_associated_accounts', 'id'),
(select max(id) from user_associated_accounts)
);
SQL
end
def down
raise ActiveRecord::IrreversibleMigration
end
end