From d1ee2e53a3a9a751ea6f93efcc64862650cfaa46 Mon Sep 17 00:00:00 2001 From: bradrobertson Date: Mon, 6 Jun 2011 16:19:20 -0400 Subject: [PATCH] clean up gemspec new version cleanup unused files --- .bundle/config | 2 - .rvmrc | 1 + HISTORY.md | 13 ++++ README.markdown => README.md | 0 apartment.gemspec | 7 +- lib/apartment.rb | 3 - .../associations/multi_tenant_association.rb | 0 lib/apartment/config.rb | 40 ++++++++--- lib/apartment/config/default_config.yml | 17 ----- lib/apartment/database.rb | 70 ++++++++++++------- lib/apartment/version.rb | 2 +- 11 files changed, 92 insertions(+), 63 deletions(-) delete mode 100644 .bundle/config create mode 100644 .rvmrc create mode 100644 HISTORY.md rename README.markdown => README.md (100%) delete mode 100644 lib/apartment/associations/multi_tenant_association.rb delete mode 100644 lib/apartment/config/default_config.yml diff --git a/.bundle/config b/.bundle/config deleted file mode 100644 index 8ebbe30..0000000 --- a/.bundle/config +++ /dev/null @@ -1,2 +0,0 @@ ---- -BUNDLE_DISABLE_SHARED_GEMS: "1" diff --git a/.rvmrc b/.rvmrc new file mode 100644 index 0000000..63dd168 --- /dev/null +++ b/.rvmrc @@ -0,0 +1 @@ +rvm 1.9.2@apartment diff --git a/HISTORY.md b/HISTORY.md new file mode 100644 index 0000000..769e23b --- /dev/null +++ b/HISTORY.md @@ -0,0 +1,13 @@ +# 0.2.0 + * June 6, 2011 * + + - Refactor to use more rails/active_support functionality + - Refactor config to lazily load apartment.yml if exists + - Remove OStruct and just use hashes for fetching methods + - Added schema load on create instead of migrating from scratch + +# 0.1.3 + * March 30, 2011 * + + - Original pass from Ryan + diff --git a/README.markdown b/README.md similarity index 100% rename from README.markdown rename to README.md diff --git a/apartment.gemspec b/apartment.gemspec index 22ff080..5528289 100644 --- a/apartment.gemspec +++ b/apartment.gemspec @@ -1,6 +1,3 @@ -# Generated by jeweler -# DO NOT EDIT THIS FILE DIRECTLY -# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec' # -*- encoding: utf-8 -*- $:.push File.expand_path("../lib", __FILE__) require "apartment/version" @@ -13,7 +10,7 @@ Gem::Specification.new do |s| s.date = %q{2011-04-18} s.summary = %q{A Ruby gem for managing database multitenancy in Rails applications} s.description = %q{Apartment allows Rails applications to deal with database multitenancy} - s.email = %q{ryan@ryanbrunner.com} + s.email = %w{ryan@ryanbrunner.com bradleyrobertson@gmail.com} s.files = `git ls-files`.split("\n") s.test_files = `git ls-files -- {spec}/*`.split("\n") @@ -22,7 +19,7 @@ Gem::Specification.new do |s| s.require_paths = ["lib"] s.rubygems_version = %q{1.3.7} - s.add_dependency 'rails', '~> 3.0.7' + s.add_dependency 'rails', '~> 3.0.5' s.add_development_dependency 'rspec', '~> 2.6.0' end diff --git a/lib/apartment.rb b/lib/apartment.rb index 5666af4..abf49e8 100644 --- a/lib/apartment.rb +++ b/lib/apartment.rb @@ -4,8 +4,5 @@ module Apartment - def self.included(base) - base.extend Apartment::ClassMethods - end end diff --git a/lib/apartment/associations/multi_tenant_association.rb b/lib/apartment/associations/multi_tenant_association.rb deleted file mode 100644 index e69de29..0000000 diff --git a/lib/apartment/config.rb b/lib/apartment/config.rb index 199aff3..14594a9 100644 --- a/lib/apartment/config.rb +++ b/lib/apartment/config.rb @@ -1,11 +1,35 @@ +require 'active_support/hash_with_indifferent_access' + module Apartment - require 'ostruct' + + module Config + + extend self + + @default_config = { + :excluded_models => ["User"], + :use_postgres_schemas => true + } + + # Always query from config object, fallback to super method_missing + def method_missing(method) + config[method] || super + end + + protected + + def config + @config ||= begin + @default_config.merge!(YAML.load_file(config_file).symbolize_keys) if File.exists?(config_file) + + @default_config + end + end + + def config_file + File.join(Rails.root, "config/apartment.yml") + end + + end - config_file = File.join(Rails.root, "config/apartment.yml") - config = { - :excluded_models => ["User"], - :use_postgres_schemas => true - } - - Config = OpenStruct.new config.merge(YAML.load_file(config_file)) if File.exists? config_file end diff --git a/lib/apartment/config/default_config.yml b/lib/apartment/config/default_config.yml deleted file mode 100644 index 4116d26..0000000 --- a/lib/apartment/config/default_config.yml +++ /dev/null @@ -1,17 +0,0 @@ -# Apartment configuration - -# Excluded models -# Indicate which models should be excluded from multi-tenancy. If you have information -# that does not vary between databases, or information that should be considered global, -# specify it here. Any classes involved in determining which database to access MUST be -# specified here - -excluded_models: [User] - -# Postgres integration mode -# By default, apartment switches databases in Postgres by changing the schema search path -# of the current environment database. If preferred, this can be turned off and Postgres will -# look in different databases for multi-tenanted models. - -use_postgres_schemas: true - diff --git a/lib/apartment/database.rb b/lib/apartment/database.rb index ef7dd67..c0ecdb4 100644 --- a/lib/apartment/database.rb +++ b/lib/apartment/database.rb @@ -1,4 +1,5 @@ -require 'active_support' +require 'active_support/core_ext/string/inflections' +require 'active_record' module Apartment module Database @@ -6,18 +7,12 @@ module Database def switch(database) - if database.nil? - ActiveRecord::Base.establish_connection(config) - return - end + # Just connect to default db and return + return ActiveRecord::Base.establish_connection(config) if database.nil? - switched_config = multi_tenantify(database) - - puts switched_config.to_yaml - - ActiveRecord::Base.establish_connection(switched_config) + connect_to_new(database) - puts Apartment::Config.excluded_models + puts "Apartment::Config.excluded_models: #{Apartment::Config.excluded_models}" Apartment::Config.excluded_models.each do |excluded_model| klass = excluded_model.constantize @@ -27,43 +22,60 @@ def switch(database) puts "Excluding class #{excluded_model}" klass.establish_connection(config) - end + end end def create(database) - switched_config = multi_tenantify(database) - - ActiveRecord::Base.establish_connection(switched_config) + # Postgres will (optionally) use 'schemas' instead of actual dbs, create a new schema while connected to main (global) db + ActiveRecord::Base.connection.execute("create schema #{database}") if use_schemas? + + connect_to_new(database) - if config["adapter"] == "postgresql" - ActiveRecord::Base.connection.execute('create table schema_migrations(version varchar(255))') - end + load_database_schema - migrate(database) + # Manually init schema migrations table (apparently there were issues with Postgres) + ActiveRecord::ConnectionAdapters::SchemaStatements.initialize_schema_migrations_table end def migrate(database) - switched_config = multi_tenantify(database) - - ActiveRecord::Base.establish_connection(switched_config) + connect_to_new(database) - ActiveRecord::Migrator.migrate(File.join(Rails.root, 'db', 'migrate')) + ActiveRecord::Migrator.migrate(File.join(Rails.root, ActiveRecord::Migrator.migrations_path)) ActiveRecord::Base.establish_connection(config) end protected - def get_default_database - Rails.configuration.database_configuration[Rails.env] + def load_database_schema + file = "#{Rails.root}/db/schema.rb" + if File.exists?(file) + load(file) + else + abort %{#{file} doesn't exist yet. Run "rake db:migrate" to create it then try again} + end + end + + # Are we using postgres schemas + def use_schemas?(conf) + (conf || config)['adapter'] == "postgresql" && Config.use_postgres_schemas + end + + # Generate new connection config and connect + def connect_to_new(database) + switched_config = multi_tenantify(database) + + puts "connecting to db with config: #{switched_config.to_yaml}" + + ActiveRecord::Base.establish_connection(switched_config) end - + def multi_tenantify(database) new_config = config.clone - if new_config['adapter'] == "postgresql" + if use_schemas?(new_config) new_config['schema_search_path'] = database else new_config['database'] = new_config['database'].gsub(Rails.env.to_s, "#{database}_#{Rails.env}") @@ -72,6 +84,10 @@ def multi_tenantify(database) new_config end + def get_default_database + Rails.configuration.database_configuration[Rails.env] + end + private def config diff --git a/lib/apartment/version.rb b/lib/apartment/version.rb index 744e63a..6300f9a 100644 --- a/lib/apartment/version.rb +++ b/lib/apartment/version.rb @@ -1,3 +1,3 @@ module Apartment - VERSION = "0.1.3" + VERSION = "0.2.0" end \ No newline at end of file