From c3b4c1a56019511eab8dcfd9b789fc7106dc2683 Mon Sep 17 00:00:00 2001 From: VirtualStaticVoid Date: Wed, 21 Nov 2012 16:03:21 +0200 Subject: [PATCH 1/5] added support for ActiveRecord schema format `:sql` in migrations --- lib/apartment/adapters/abstract_adapter.rb | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/apartment/adapters/abstract_adapter.rb b/lib/apartment/adapters/abstract_adapter.rb index a99e0331..85b77277 100644 --- a/lib/apartment/adapters/abstract_adapter.rb +++ b/lib/apartment/adapters/abstract_adapter.rb @@ -148,8 +148,12 @@ def environmentify(database) # def import_database_schema ActiveRecord::Schema.verbose = false # do not log schema load output. - - load_or_abort(Apartment.database_schema_file) if Apartment.database_schema_file + if Rails.application.config.active_record.schema_format == :sql + raise ApartmentError, "Using the :sql schema_format for ActiveRecord is not supported when using Postgres schemas." if Apartment.use_postgres_schemas + execute_or_abort("#{Rails.root}/db/structure.sql") + else + load_or_abort("#{Rails.root}/db/schema.rb") + end end # Return a new config that is multi-tenanted @@ -170,6 +174,17 @@ def load_or_abort(file) end end + # Load a SQL file and execute it or abort if it doesn't exists + # + def execute_or_abort(file) + if File.exists?(file) + structure_sql = open(file, 'r').read + ActiveRecord::Base.connection.execute(structure_sql) + else + abort %{#{file} doesn't exist yet} + end + end + # Exceptions to rescue from on db operations # def rescuable_exceptions @@ -181,6 +196,7 @@ def rescuable_exceptions def rescue_from [] end + end end -end \ No newline at end of file +end From ae93128645b2692e8003e3d2c566a170f1a5fff0 Mon Sep 17 00:00:00 2001 From: VirtualStaticVoid Date: Sun, 13 Oct 2013 22:15:19 +0200 Subject: [PATCH 2/5] introduced `schema_format` configuration --- README.md | 5 +++++ lib/apartment.rb | 8 +++++++- lib/apartment/railtie.rb | 1 + 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1634182e..722a9982 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,9 @@ other databases, the database will be created as a new DB within the system. When you create a new database, all migrations will be run against that database, so it will be up to date when create returns. +You can configure the `schema_format` used to create a new database. The options are `:ruby` (the default) and `:sql`. For `:ruby` the 'db/schema.rb' file is used, and for `:sql` the 'db/structure.sql' is used. +When used within Rails the setting is assigned from the configured schema format set in 'application.rb' so there is no need to duplicate the configuration setting. + #### Notes on PostgreSQL PostgreSQL works slightly differently than other databases when creating a new DB. If you @@ -53,6 +56,8 @@ would not allow a full new database to be created. One can optionally use the full database creation instead if they want, though this is not recommended +The `:sql` schema format cannot be used when PostgreSQL schemas are used, since the `structure.sql` file sets the schema search path. + ### Switching Databases To switch databases using Apartment, use the following command: diff --git a/lib/apartment.rb b/lib/apartment.rb index 0e2ab7ba..1159bcda 100644 --- a/lib/apartment.rb +++ b/lib/apartment.rb @@ -11,7 +11,7 @@ class << self extend Forwardable ACCESSOR_METHODS = [:use_schemas, :seed_after_create, :prepend_environment, :append_environment] - WRITER_METHODS = [:database_names, :database_schema_file, :excluded_models, :default_schema, :persistent_schemas, :connection_class] + WRITER_METHODS = [:database_names, :database_schema_file, :excluded_models, :default_schema, :persistent_schemas, :connection_class, :schema_format] attr_accessor(*ACCESSOR_METHODS) attr_writer(*WRITER_METHODS) @@ -45,6 +45,12 @@ def connection_class @connection_class || ActiveRecord::Base end + # Schema format :ruby or :sql + # as per http://guides.rubyonrails.org/configuring.html#configuring-rails-components + def schema_format + @schema_format || :ruby + end + def database_schema_file return @database_schema_file if defined?(@database_schema_file) diff --git a/lib/apartment/railtie.rb b/lib/apartment/railtie.rb index 12f5de33..a64d68e8 100644 --- a/lib/apartment/railtie.rb +++ b/lib/apartment/railtie.rb @@ -17,6 +17,7 @@ class Railtie < Rails::Railtie config.seed_after_create = false config.prepend_environment = false config.append_environment = false + config.schema_format = Rails.application.config.active_record.schema_format end ActiveRecord::Migrator.migrations_paths = Rails.application.paths['db/migrate'].to_a From dd2822fcdc3f42e1c890ff212a1a1288db0cb718 Mon Sep 17 00:00:00 2001 From: VirtualStaticVoid Date: Sun, 13 Oct 2013 22:18:35 +0200 Subject: [PATCH 3/5] removed Rails dependencies --- lib/apartment.rb | 8 +++++++- lib/apartment/adapters/abstract_adapter.rb | 8 ++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/apartment.rb b/lib/apartment.rb index 1159bcda..9a6fcf6c 100644 --- a/lib/apartment.rb +++ b/lib/apartment.rb @@ -54,7 +54,13 @@ def schema_format def database_schema_file return @database_schema_file if defined?(@database_schema_file) - @database_schema_file = Rails.root.join('db', 'schema.rb') + if defined?(Rails) + @database_schema_file = if schema_format == :sql + Rails.root.join('db', 'structure.sql') + else + Rails.root.join('db', 'schema.rb') + end + end end # Reset all the config for Apartment diff --git a/lib/apartment/adapters/abstract_adapter.rb b/lib/apartment/adapters/abstract_adapter.rb index 85b77277..c137d3ef 100644 --- a/lib/apartment/adapters/abstract_adapter.rb +++ b/lib/apartment/adapters/abstract_adapter.rb @@ -148,11 +148,11 @@ def environmentify(database) # def import_database_schema ActiveRecord::Schema.verbose = false # do not log schema load output. - if Rails.application.config.active_record.schema_format == :sql - raise ApartmentError, "Using the :sql schema_format for ActiveRecord is not supported when using Postgres schemas." if Apartment.use_postgres_schemas - execute_or_abort("#{Rails.root}/db/structure.sql") + if Apartment.schema_format == :sql + raise ApartmentError, "Using the :sql schema format is not supported when using Postgres schemas." if Apartment.use_postgres_schemas + execute_or_abort(Apartment.database_schema_file) else - load_or_abort("#{Rails.root}/db/schema.rb") + load_or_abort(Apartment.database_schema_file) end end From 2d52fe5df2112c6f2271e5d7eb4010114de254b6 Mon Sep 17 00:00:00 2001 From: VirtualStaticVoid Date: Mon, 14 Oct 2013 08:56:06 +0200 Subject: [PATCH 4/5] added specs for schema_format and database_schema_file configuration options --- spec/unit/config_spec.rb | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/spec/unit/config_spec.rb b/spec/unit/config_spec.rb index 1270836e..a6cf18f5 100644 --- a/spec/unit/config_spec.rb +++ b/spec/unit/config_spec.rb @@ -28,6 +28,34 @@ Apartment.use_schemas.should be_false end + it "should set schema_format" do + Apartment.configure do |config| + config.schema_format = :sql + end + Apartment.schema_format.should eq(:sql) + end + + it "should set database_schema_file" do + Apartment.configure do |config| + config.database_schema_file = 'a_file' + end + Apartment.database_schema_file.should eq('a_file') + end + + it "should set database_schema_file for :ruby schema format" do + Apartment.configure do |config| + config.schema_format = :ruby + end + Apartment.database_schema_file.to_s.should match(/schema.rb/) + end + + it "should set database_schema_file for :sql schema format" do + Apartment.configure do |config| + config.schema_format = :sql + end + Apartment.database_schema_file.to_s.should match(/structure.sql/) + end + it "should set seed_after_create" do Apartment.configure do |config| config.excluded_models = [] From 5173059af0e3abd75abaed9b355e63d9216e909f Mon Sep 17 00:00:00 2001 From: VirtualStaticVoid Date: Mon, 14 Oct 2013 09:31:05 +0200 Subject: [PATCH 5/5] correction to logic to handle nil database_schema_file configuration --- lib/apartment/adapters/abstract_adapter.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/apartment/adapters/abstract_adapter.rb b/lib/apartment/adapters/abstract_adapter.rb index c137d3ef..6e5f0f8f 100644 --- a/lib/apartment/adapters/abstract_adapter.rb +++ b/lib/apartment/adapters/abstract_adapter.rb @@ -147,6 +147,8 @@ def environmentify(database) # Import the database schema # def import_database_schema + return if Apartment.database_schema_file.nil? + ActiveRecord::Schema.verbose = false # do not log schema load output. if Apartment.schema_format == :sql raise ApartmentError, "Using the :sql schema format is not supported when using Postgres schemas." if Apartment.use_postgres_schemas