diff options
author | Rémy Coutable <remy@rymai.me> | 2017-09-12 10:39:58 +0000 |
---|---|---|
committer | Jarka Kadlecova <jarka@gitlab.com> | 2017-09-12 17:47:14 +0200 |
commit | 16cf8604bd0e57d26317ea0d938f6693598a9a71 (patch) | |
tree | cf3e602a8cdc11e9e9fa2efbe5d9a34775a47dba | |
parent | 8ea4dc84564f04e385b0b972d234d7c3e502bf23 (diff) | |
download | gitlab-ce-16cf8604bd0e57d26317ea0d938f6693598a9a71.tar.gz |
Merge branch 'reset-events-primary-key-mysql' into 'master'10-0-stable-prepare-rc1
Reset primary keys after swapping events tables
See merge request !14182
-rw-r--r-- | db/migrate/20170830131015_swap_event_migration_tables.rb | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/db/migrate/20170830131015_swap_event_migration_tables.rb b/db/migrate/20170830131015_swap_event_migration_tables.rb index 5128d1b2fe7..a256de4a8af 100644 --- a/db/migrate/20170830131015_swap_event_migration_tables.rb +++ b/db/migrate/20170830131015_swap_event_migration_tables.rb @@ -7,6 +7,10 @@ class SwapEventMigrationTables < ActiveRecord::Migration # Set this constant to true if this migration requires downtime. DOWNTIME = false + class Event < ActiveRecord::Base + self.table_name = 'events' + end + def up rename_tables end @@ -19,5 +23,25 @@ class SwapEventMigrationTables < ActiveRecord::Migration rename_table :events, :events_old rename_table :events_for_migration, :events rename_table :events_old, :events_for_migration + + # Once swapped we need to reset the primary key of the new "events" table to + # make sure that data created starts with the right value. This isn't + # necessary for events_for_migration since we replicate existing primary key + # values to it. + if Gitlab::Database.postgresql? + reset_primary_key_for_postgresql + else + reset_primary_key_for_mysql + end + end + + def reset_primary_key_for_postgresql + reset_pk_sequence!(Event.table_name) + end + + def reset_primary_key_for_mysql + amount = Event.pluck('COALESCE(MAX(id), 1)').first + + execute "ALTER TABLE #{Event.table_name} AUTO_INCREMENT = #{amount}" end end |