From 1f90f3044fafd43d7507dd23b94a3deb8ba8bd99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Thu, 20 Feb 2014 18:42:17 +0100 Subject: [PATCH] BUGFIX: restore wasn't working when not using multisite in production --- lib/backup_restore.rb | 5 ++-- lib/export/exporter.rb | 23 ++++++++++--------- lib/import/importer.rb | 9 ++++---- .../rails_multisite/connection_management.rb | 14 +---------- 4 files changed, 21 insertions(+), 30 deletions(-) diff --git a/lib/backup_restore.rb b/lib/backup_restore.rb index cdad087176c..60aae544c7f 100644 --- a/lib/backup_restore.rb +++ b/lib/backup_restore.rb @@ -94,6 +94,7 @@ module BackupRestore -- move all "source" tables to "destination" schema FOR row IN SELECT tablename FROM pg_tables WHERE schemaname = '#{source}' LOOP + EXECUTE 'DROP TABLE IF EXISTS #{destination}.' || quote_ident(row.tablename) || ' CASCADE;'; EXECUTE 'ALTER TABLE #{source}.' || quote_ident(row.tablename) || ' SET SCHEMA #{destination};'; END LOOP; END$$; @@ -104,8 +105,8 @@ module BackupRestore def self.database_configuration if Rails.env.production? - conn = RailsMultisite::ConnectionManagement - db_conf = DatabaseConfiguration.new(conn.current_host, conn.current_username, conn.current_password, conn.current_db) + db = ActiveRecord::Base.connection_pool.spec.config + db_conf = DatabaseConfiguration.new(db["host"], db["username"], db["password"], db["database"]) else db = Rails.configuration.database_configuration[Rails.env] db_conf = DatabaseConfiguration.new(db["host"], db["username"], db["password"], db["database"]) diff --git a/lib/export/exporter.rb b/lib/export/exporter.rb index b444951ae72..dd8b495f710 100644 --- a/lib/export/exporter.rb +++ b/lib/export/exporter.rb @@ -155,18 +155,19 @@ module Export db_conf = BackupRestore.database_configuration password_argument = "PGPASSWORD=#{db_conf.password}" if db_conf.password.present? - host_argument = "--host=#{db_conf.host}" if db_conf.host.present? + host_argument = "--host=#{db_conf.host}" if db_conf.host.present? + username_argument = "--username=#{db_conf.username}" if db_conf.username.present? - [ password_argument, # pass the password to pg_dump - "pg_dump", # the pg_dump command - "--schema=public", # only public schema - "--file='#{@dump_filename}'", # output to the dump.sql file - "--no-owner", # do not output commands to set ownership of objects - "--no-privileges", # prevent dumping of access privileges - "--verbose", # specifies verbose mode - host_argument, # the hostname to connect to - "--username=#{db_conf.username}", # the username to connect as - db_conf.database # the name of the database to dump + [ password_argument, # pass the password to pg_dump (if any) + "pg_dump", # the pg_dump command + "--schema=public", # only public schema + "--file='#{@dump_filename}'", # output to the dump.sql file + "--no-owner", # do not output commands to set ownership of objects + "--no-privileges", # prevent dumping of access privileges + "--verbose", # specifies verbose mode + host_argument, # the hostname to connect to (if any) + username_argument, # the username to connect as (if any) + db_conf.database # the name of the database to dump ].join(" ") end diff --git a/lib/import/importer.rb b/lib/import/importer.rb index ccc3d147a57..7bf4bfe0f5b 100644 --- a/lib/import/importer.rb +++ b/lib/import/importer.rb @@ -203,15 +203,16 @@ module Import db_conf = BackupRestore.database_configuration password_argument = "PGPASSWORD=#{db_conf.password}" if db_conf.password.present? - host_argument = "--host=#{db_conf.host}" if db_conf.host.present? + host_argument = "--host=#{db_conf.host}" if db_conf.host.present? + username_argument = "--username=#{db_conf.username}" if db_conf.username.present? - [ password_argument, # pass the password to psql + [ password_argument, # pass the password to psql (if any) "psql", # the psql command "--dbname='#{db_conf.database}'", # connect to database *dbname* "--file='#{@dump_filename}'", # read the dump "--single-transaction", # all or nothing (also runs COPY commands faster) - host_argument, # the hostname to connect to - "--username=#{db_conf.username}" # the username to connect as + host_argument, # the hostname to connect to (if any) + username_argument # the username to connect as (if any) ].join(" ") end diff --git a/vendor/gems/rails_multisite/lib/rails_multisite/connection_management.rb b/vendor/gems/rails_multisite/lib/rails_multisite/connection_management.rb index 4b9452d4ac7..cb78c6cc0cb 100644 --- a/vendor/gems/rails_multisite/lib/rails_multisite/connection_management.rb +++ b/vendor/gems/rails_multisite/lib/rails_multisite/connection_management.rb @@ -80,19 +80,7 @@ module RailsMultisite def self.current_hostname config = ActiveRecord::Base.connection_pool.spec.config - config[:host_names].nil? ? current_host : config[:host_names].first - end - - def self.current_host - ActiveRecord::Base.connection_pool.spec.config[:host] - end - - def self.current_username - ActiveRecord::Base.connection_pool.spec.config[:username] - end - - def self.current_password - ActiveRecord::Base.connection_pool.spec.config[:password] + config[:host_names].nil? ? config[:host] : config[:host_names].first end def self.clear_settings!