Skip to content

Commit

Permalink
upgrading mysql adapter, fixes current_database bug
Browse files Browse the repository at this point in the history
better commenting

working on fixing migration test errors
  • Loading branch information
bradrobertson committed Dec 2, 2011
1 parent 277a761 commit f706089
Show file tree
Hide file tree
Showing 13 changed files with 335 additions and 42 deletions.
20 changes: 4 additions & 16 deletions README.md
@@ -1,16 +1,6 @@
# Apartment
*Multitenancy for Rails 3*

> IMPORTANT! While I will do everything possible to get Apartment working for Rails 3.1, I haven't, as of yet, had this opportunity.
> There have been significant changes in the adapters such as prepared statements that might cause some issues.
> If anyone is successfully using Apartment with 3.1 please let me know, but please ensure that you're testing data integrity properly (ie. that queries are made in the right schema) as prepared_statements
> could really throw a wrench in that one.
> I know for a fact that Rails 3.1 missed a patch to make postgresql more schema aware, but it seems to have made it into Rails 3.1.1. So at the very least, use 3.1.1

Apartment provides tools to help you deal with multiple databases in your Rails
application. If you need to have certain data sequestered based on account or company,
but still allow some data to exist in a common database, Apartment can help.
Expand Down Expand Up @@ -129,11 +119,7 @@ from `Apartment.database_names`

### Delayed::Job

In Apartment's current state, it doesn't seem to queue jobs properly using DJ. For whatever reason, DJ jobs are created in the current schema, even though the DJ
is part of the ignored models. I have to look into this further, but until then use `Apartment::Delayed::Job.enqueue` to ensure that queues are placed in the public schema

In order to make ActiveRecord models play nice with DJ and Apartment, include `Apartment::Delayed::Requirements` in any model that is being serialized by DJ. Also ensure
that a `database` attribute is set on this model *before* it is serialized, to ensure that when it is fetched again, it is done so in the proper Apartment db context. For example:
In order to make ActiveRecord models play nice with DJ and Apartment, include `Apartment::Delayed::Requirements` in any model that is being serialized by DJ. Also ensure that the `database` attribute (provided by Apartment::Delayed::Requirements) is set on this model *before* it is serialized, to ensure that when it is fetched again, it is done so in the proper Apartment db context. For example:

class SomeModel < ActiveRecord::Base
include Apartment::Delayed::Requirements
Expand All @@ -151,8 +137,10 @@ that a `database` attribute is set on this model *before* it is serialized, to e
end
end

## Contributing
### Development

* The Local setup for development assumes that a root user with no password exists for both mysql and postgresl
* Rake tasks (see the Rakefile) will help you setup your dbs necessary to run tests
* Please issue pull requests to the `development` branch. All development happens here, master is used for releases
* Ensure that your code is accompanied with tests. No code will be merged without tests

Expand Down
6 changes: 0 additions & 6 deletions Rakefile
Expand Up @@ -17,12 +17,6 @@ namespace :spec do
end
end

namespace :unit do
RSpec::Core::RakeTask.new(:adapters) do |spec|
spec.pattern = "spec/unit/adapters/**/*_spec.rb"
end
end

end

task :default => :spec
Expand Down
4 changes: 2 additions & 2 deletions apartment.gemspec
Expand Up @@ -18,14 +18,14 @@ Gem::Specification.new do |s|
s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.7}

s.add_dependency 'rails', '~> 3.1.1' # must be >= 3.0.10 due to poor schema support pre 3.0.10, but < 3.1 because it hasn't been fully tested yet
s.add_dependency 'rails', '~> 3.1.2'
s.add_development_dependency 'rake', '~> 0.8.7'
s.add_development_dependency 'sqlite3'
s.add_development_dependency 'rspec', '~> 2.6.0'
s.add_development_dependency 'rspec-rails', '~> 2.6.1'
s.add_development_dependency 'capybara', '1.0.0'
s.add_development_dependency 'pg', '~> 0.11.0'
s.add_development_dependency 'mysql2', '0.2.7'
s.add_development_dependency 'mysql2', '0.3.7'
s.add_development_dependency "silent-postgres", "~> 0.1.1"
s.add_development_dependency 'delayed_job', '~> 2.1.4'
end
6 changes: 1 addition & 5 deletions lib/apartment.rb
Expand Up @@ -13,11 +13,7 @@ def configure

# Be careful not to use `return` here so both Proc and lambda can be used without breaking
def database_names
if @database_names.respond_to?(:call)
@database_names.call
else
@database_names
end
@database_names.respond_to?(:call) ? @database_names.call : @database_names
end

# Default to none
Expand Down
1 change: 0 additions & 1 deletion lib/apartment/adapters/postgresql_adapter.rb
Expand Up @@ -93,7 +93,6 @@ def reset
#
def connect_to_new(database = nil)
return reset if database.nil?
ActiveRecord::Base.connection.clear_cache!
ActiveRecord::Base.connection.schema_search_path = database

rescue ActiveRecord::StatementInvalid => e
Expand Down
16 changes: 12 additions & 4 deletions lib/apartment/railtie.rb
Expand Up @@ -3,8 +3,10 @@
module Apartment
class Railtie < Rails::Railtie

# Set up our default config options
# Do this before the app initializers run so we don't override custom settings
#
# Set up our default config options
# Do this before the app initializers run so we don't override custom settings
#
config.before_initialize do
Apartment.configure do |config|
config.excluded_models = []
Expand All @@ -18,16 +20,22 @@ class Railtie < Rails::Railtie
# Hook into ActionDispatch::Reloader to ensure Apartment is properly initialized
# Note that this doens't entirely work as expected in Development, because this is called before classes are reloaded
# See the above middleware/console declarations below to help with this. Hope to fix that soon.
#
config.to_prepare do
Apartment::Database.init
end

#
# Ensure rake tasks are loaded
#
rake_tasks do
load 'tasks/apartment.rake'
end

# The following initializers are a workaround to the fact that I can't properly hook into the rails reloader
# Note this is technically valid for any environment where cache_classes is false, for us, it's just development
#
# The following initializers are a workaround to the fact that I can't properly hook into the rails reloader
# Note this is technically valid for any environment where cache_classes is false, for us, it's just development
#
if Rails.env.development?

# Apartment::Reloader is middleware to initialize things properly on each request to dev
Expand Down
5 changes: 2 additions & 3 deletions lib/apartment/reloader.rb
Expand Up @@ -6,9 +6,8 @@ class Reloader
# Necessary due to code reload (annoying). I couldn't figure out how to properly hook into
# the Rails reload process *after* files are reloaded, so I've used this in the meantime.
#
# Note that this has one MAJOR caveat. Doing `reload!` in the console in development WILL NOT run init again
# Thus, excluded models will not be processed again and will be queried from the current_schema rather than public.
# I hope to fix this soon
# Also see apartment/console for the re-definition of reload! that re-init's Apartment
#
def initialize(app)
@app = app
end
Expand Down
1 change: 0 additions & 1 deletion spec/adapters/postgresql_adapter_spec.rb
Expand Up @@ -48,7 +48,6 @@
end

it "should yield to block if passed" do
Apartment::Test.migrate # ensure we have latest schema in the public
subject.drop(schema2) # so we don't get errors on creation

@count = 0 # set our variable so its visible in and outside of blocks
Expand Down
1 change: 1 addition & 0 deletions spec/dummy/config/database.yml
Expand Up @@ -7,6 +7,7 @@ test:
min_messages: WARNING
pool: 5
timeout: 5000

development:
adapter: postgresql
database: apartment_postgresql_development
Expand Down
13 changes: 13 additions & 0 deletions spec/dummy/db/migrate/20111202022214_create_table_books.rb
@@ -0,0 +1,13 @@
class CreateTableBooks < ActiveRecord::Migration
def up
create_table :books do |t|
t.string :name
t.integer :pages
t.datetime :published
end
end

def down
drop_table :books
end
end

0 comments on commit f706089

Please sign in to comment.