Skip to content

Instantly share code, notes, and snippets.

@bvajda
Created August 21, 2011 11:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bvajda/1160489 to your computer and use it in GitHub Desktop.
Save bvajda/1160489 to your computer and use it in GitHub Desktop.
Switching between ActiveRecord and DataMapper - the gory details (https://github.com/datamapper/datamapper.github.com.git)

DM's Integrity Map causes issues when setting up tables in legacy repositories. The rails console can access the models, but the controller cannot. The (temporary?) solution is to add the following lines to the models. http://groups.google.com/group/datamapper/browse_thread/thread/16dc095f33097050#

def self.default_repository_name
  :your_repository
end
def self.repository(name = nil, &block) 
  super(:your_repository, &block) 
end

In order to avoid migration in a legacy database (repository :legacy):

# ./spec/spec_helper.rb
config.before(:suite) do
  DataMapper::Model.descendants.entries.each do |model|
    model.auto_migrate!(:default) unless model.default_repository_name == :legacy
  end
end

# call the bellow rake task to upgrade the table that are not in the :legacy repository
# ./lib/tasks/limited_auto_upgrade.rake

task :environment do
  require File.expand_path('../../config/boot', File.dirname(__FILE__))
end

desc "Perform auto_upgrade on the default repository only"
task :limited_auto_upgrade => :environment do
  DataMapper::Model.descendants.entries.reject { |m| m.default_repository_name == :excluded_repository }.each do |model|
    puts "Upgrading #{model.name}"
    model.auto_migrate!(:default)
  end
end

The first comment in the blocks shows the file. The subsequent comments in the same block mean that the line needs to be commented out in the file.

# app/config/application.rb
require "active_record/railtie"
# require "dm-rails/railtie"

# spec/spec_helper.rb
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true

# config/environments/development.rb
config.action_mailer.raise_delivery_errors = false

# config/environments/production.rb
config.action_mailer.raise_delivery_errors = false

# config/environments/test.rb
config.action_mailer.delivery_method = :test

# controllers/application_controller.rb
# require 'dm-rails/middleware/identity_map'
class ApplicationController < ActionController::Base
  protect_from_forgery
  # use Rails::DataMapper::Middleware::IdentityMap
end

The first comment in the blocks shows the file. The subsequent comments in the same block mean that the line needs to be commented out in the file.

# app/config/application.rb
# require "active_record/railtie"
require "dm-rails/railtie"

# spec/spec_helper.rb
# config.fixture_path = "#{::Rails.root}/spec/fixtures"
# config.use_transactional_fixtures = true

# config/environments/development.rb
# config.action_mailer.raise_delivery_errors = false

# config/environments/production.rb
# config.action_mailer.raise_delivery_errors = false

# config/environment/test.rb
# config.action_mailer.delivery_method = :test

# controllers/application_controller.rb
require 'dm-rails/middleware/identity_map'
class ApplicationController < ActionController::Base
  protect_from_forgery
  use Rails::DataMapper::Middleware::IdentityMap
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment