Skip to content

Commit

Permalink
tests passing again
Browse files Browse the repository at this point in the history
added init method that must be called before any apartment action happens
  • Loading branch information
bradrobertson committed Jun 9, 2011
1 parent 301ea09 commit a61bacd
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 35 deletions.
3 changes: 2 additions & 1 deletion apartment.gemspec
Expand Up @@ -19,7 +19,8 @@ Gem::Specification.new do |s|
s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.7}

s.add_dependency 'rails', '~> 3.0.5'
s.add_dependency 'rails', '~> 3.0.8'
s.add_development_dependency 'rspec', '~> 2.6.0'
s.add_development_dependency 'pg', '~> 0.11.0'

end
2 changes: 1 addition & 1 deletion lib/apartment/config.rb
Expand Up @@ -7,7 +7,7 @@ module Config
extend self

@default_config = {
:excluded_models => ["User", "Admin::Company"],
:excluded_models => [],
:use_postgres_schemas => true
}

Expand Down
29 changes: 18 additions & 11 deletions lib/apartment/database.rb
Expand Up @@ -5,17 +5,21 @@ module Apartment
module Database
extend self

# Call init to establish a connection to the public schema on all excluded models
# This must be done before creating any new schemas or switching
def init
Apartment::Config.excluded_models.each do |excluded_model|
klass = excluded_model.constantize
klass.establish_connection(config)
end
end

def switch(database = nil)

# Just connect to default db and return
return ActiveRecord::Base.establish_connection(config) if database.nil?

connect_to_new(database)

Apartment::Config.excluded_models.each do |excluded_model|
klass = excluded_model.constantize
klass.establish_connection(config)
end
end

def reset
Expand All @@ -24,16 +28,21 @@ def reset

def create(database)
# 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?
create_schema(database) if use_schemas?
# TODO create database if not using schemas

connect_and_reset(database) do
connect_to_new(database).tap do
import_database_schema

# Manually init schema migrations table (apparently there were issues with Postgres when this isn't done)
ActiveRecord::Base.connection.initialize_schema_migrations_table
end
end

def create_schema(name)
ActiveRecord::Base.connection.execute("CREATE SCHEMA #{name}")
end

# Migrate to latest
def migrate(database)
connect_and_reset(database){ ActiveRecord::Migrator.migrate(ActiveRecord::Migrator.migrations_path) }
Expand All @@ -53,15 +62,13 @@ def rollback(database, step = 1)
connect_and_reset(database){ ActiveRecord::Migrator.rollback(ActiveRecord::Migrator.migrations_path, step) }
end

def run(direction, path, version)
end

protected

def connect_and_reset(database)
connect_to_new(database)
yield if block_given?
reset
ensure
reset
end

def import_database_schema
Expand Down
64 changes: 42 additions & 22 deletions spec/unit/database_spec.rb
Expand Up @@ -20,6 +20,13 @@
Apartment::Database.switch
end

it "should fail with wrong schema" do
pending("requires actual connetion to DB, need a better setup for this")
expect {
Apartment::Database.switch('some_nonexistent_schema')
}.to raise_error
end

context "using postgres schemas" do

before do
Expand All @@ -38,28 +45,32 @@
end
end

context "with exclusions" do
before do
Apartment::Config.stub(:excluded_models).and_return ['Admin::Company', 'User']
end

it "should connect excluded model with original config" do
Admin::Company.should_receive(:establish_connection).with config
User.should_receive(:establish_connection).with config
Apartment::Database.switch(schema_name)
end

it "should raise an error for unkown class names" do
Apartment::Config.stub(:excluded_models).and_return ['Admin::Company', 'User', "Unknown::Class"]
lambda{
Apartment::Database.switch(schema_name)
}.should raise_error
end

end

end

describe "#init" do

context "with model exclusions" do
before do
Apartment::Config.stub(:excluded_models).and_return ['Admin::Company', 'User']
end

it "should connect excluded model with original config" do
Admin::Company.should_receive(:establish_connection).with config
User.should_receive(:establish_connection).with config
Apartment::Database.init
end

it "should raise an error for unkown class names" do
Apartment::Config.stub(:excluded_models).and_return ['Admin::Company', 'User', "Unknown::Class"]

expect{
Apartment::Database.init
}.to raise_error
end

end

end

describe "#create" do
Expand All @@ -75,12 +86,10 @@
end

it "should create the new schema" do
ActiveRecord::Base.connection.should_receive(:execute).with("create schema #{schema_name}")
ActiveRecord::Base.connection.should_receive(:execute).with("CREATE SCHEMA #{schema_name}")
Apartment::Database.create(schema_name)
end

# need more tests

end

context "without postgres schemas" do
Expand All @@ -96,6 +105,17 @@
end
end

describe "#create_schema" do
let(:schema_name){ "some_schema" }

it "should succeed" do
ActiveRecord::Base.connection.should_receive(:execute).with("CREATE SCHEMA #{schema_name}")
Apartment::Database.create_schema(schema_name)
end

it "should sanitize name"
end

context "migrations" do
before do
ActiveRecord::Base.stub(:establish_connection)
Expand Down

0 comments on commit a61bacd

Please sign in to comment.