From 9489dff71d76772e16f7ea0e2f4ccc9045249f37 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Fri, 25 Nov 2016 15:54:13 +0100 Subject: [PATCH] =?UTF-8?q?Reset=20pk=E2=80=99s=20for=20postgresql=20after?= =?UTF-8?q?=20any=20importing=20of=20data=20with=20id=E2=80=99s.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/seeds.rb | 6 +----- lib/auto_wizard.rb | 3 +++ lib/db_helper.rb | 31 +++++++++++++++++++++++++++++++ lib/import/otrs.rb | 3 +-- lib/import/zendesk.rb | 3 +-- 5 files changed, 37 insertions(+), 9 deletions(-) create mode 100644 lib/db_helper.rb diff --git a/db/seeds.rb b/db/seeds.rb index 5762b971d..849fabaae 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -5447,11 +5447,7 @@ Karma::Activity.create_or_update( ) # reset primary key sequences -if ActiveRecord::Base.connection_config[:adapter] == 'postgresql' - ActiveRecord::Base.connection.tables.each do |t| - ActiveRecord::Base.connection.reset_pk_sequence!(t) - end -end +DbHelper.import_post # install locales and translations Locale.create_if_not_exists( diff --git a/lib/auto_wizard.rb b/lib/auto_wizard.rb index 509409a04..9e71fbeed 100644 --- a/lib/auto_wizard.rb +++ b/lib/auto_wizard.rb @@ -146,6 +146,9 @@ returns } } + # reset primary key sequences + DbHelper.import_post + # remove auto wizard file FileUtils.rm auto_wizard_file_location diff --git a/lib/db_helper.rb b/lib/db_helper.rb new file mode 100644 index 000000000..27b61f699 --- /dev/null +++ b/lib/db_helper.rb @@ -0,0 +1,31 @@ +# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/ + +class DbHelper + +=begin + +execute post database statements after import (e. g. reset primary key sequences for postgresql) + + DbHelper.import_post + +or only for certan tables + + DbHelper.import_post(table_name) + +=end + + def self.import_post(table = nil) + return if ActiveRecord::Base.connection_config[:adapter] != 'postgresql' + + tables = if table + [table] + else + ActiveRecord::Base.connection.tables + end + + tables.each do |t| + ActiveRecord::Base.connection.reset_pk_sequence!(t) + end + end + +end diff --git a/lib/import/otrs.rb b/lib/import/otrs.rb index eb5a6971e..0fb26b19e 100644 --- a/lib/import/otrs.rb +++ b/lib/import/otrs.rb @@ -1526,8 +1526,7 @@ module Import::OTRS # reset primary key sequences def self._reset_pk(table) - return if ActiveRecord::Base.connection_config[:adapter] != 'postgresql' - ActiveRecord::Base.connection.reset_pk_sequence!(table) + DbHelper.import_post(table) end # create customers for article diff --git a/lib/import/zendesk.rb b/lib/import/zendesk.rb index 4e9bc9ab6..838525e20 100644 --- a/lib/import/zendesk.rb +++ b/lib/import/zendesk.rb @@ -998,8 +998,7 @@ module Import::Zendesk # reset primary key sequences def self._reset_pk(table) - return if ActiveRecord::Base.connection_config[:adapter] != 'postgresql' - ActiveRecord::Base.connection.reset_pk_sequence!(table) + DbHelper.import_post(table) end def get_custom_fields(custom_fields)