Skip to content

Instantly share code, notes, and snippets.

@jcoleman
Created March 30, 2023 15:32
Show Gist options
  • Save jcoleman/58f537ada03b5a51e49d2e66d83f15b9 to your computer and use it in GitHub Desktop.
Save jcoleman/58f537ada03b5a51e49d2e66d83f15b9 to your computer and use it in GitHub Desktop.
Using ActiveRecord Migrations outside of a Rails app
require "bundler/gem_tasks"
require "rspec/core/rake_task"
RSpec::Core::RakeTask.new(:spec)
task :default => :spec
require "erb"
require "yaml"
require "active_record"
include ActiveRecord::Tasks
root = File.expand_path("..", __FILE__)
DatabaseTasks.env = ENV["APP_ENV"] || "test"
DatabaseTasks.database_configuration = YAML.load(
ERB.new(
File.read(File.join(root, "config/database.yml"))
).result
)
DatabaseTasks.db_dir = File.join(root, "db")
DatabaseTasks.fixtures_path = File.join root, "test/fixtures"
DatabaseTasks.migrations_paths = [File.join(root, "db/migrate")]
DatabaseTasks.root = root
ActiveRecord::Base.schema_format = :sql
task :environment do
ActiveRecord::Base.configurations = DatabaseTasks.database_configuration
ActiveRecord::Base.establish_connection(DatabaseTasks.env.to_sym)
end
load 'active_record/railties/databases.rake'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment