Skip to content

Commit

Permalink
clean up gemspec
Browse files Browse the repository at this point in the history
new version

cleanup unused files
  • Loading branch information
bradrobertson committed Jun 6, 2011
1 parent 8950ebd commit d1ee2e5
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 63 deletions.
2 changes: 0 additions & 2 deletions .bundle/config

This file was deleted.

1 change: 1 addition & 0 deletions .rvmrc
@@ -0,0 +1 @@
rvm 1.9.2@apartment
13 changes: 13 additions & 0 deletions 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

File renamed without changes.
7 changes: 2 additions & 5 deletions 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"
Expand All @@ -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")

Expand All @@ -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
3 changes: 0 additions & 3 deletions lib/apartment.rb
Expand Up @@ -4,8 +4,5 @@

module Apartment

def self.included(base)
base.extend Apartment::ClassMethods
end
end

Empty file.
40 changes: 32 additions & 8 deletions 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
17 changes: 0 additions & 17 deletions lib/apartment/config/default_config.yml

This file was deleted.

70 changes: 43 additions & 27 deletions lib/apartment/database.rb
@@ -1,23 +1,18 @@
require 'active_support'
require 'active_support/core_ext/string/inflections'
require 'active_record'

module Apartment
module Database
extend self

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
Expand All @@ -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}")
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/apartment/version.rb
@@ -1,3 +1,3 @@
module Apartment
VERSION = "0.1.3"
VERSION = "0.2.0"
end

0 comments on commit d1ee2e5

Please sign in to comment.