ActiveRecord 3.2.14
我想在非Rails Ruby项目中使用ActiveRecord。我希望可以使用ActiveRecord定义的rake任务。我怎么能这样做?
rake db:create # Create the database from DATABASE_URL or config/database.yml for the current Rails.env (use db:create:all to create all dbs in the config)
rake db:drop # Drops the database using DATABASE_URL or the current Rails.env (use db:drop:all to drop all databases)
rake db:fixtures:load # Load fixtures into the current environment's database
rake db:migrate # Migrate the database (options: VERSION=x, VERBOSE=false)
rake db:migrate:status # Display status of migrations
rake db:rollback # Rolls the schema back to the previous version (specify steps w/ STEP=n)
rake db:schema:dump # Create a db/schema.rb file that can be portably used against any DB supported by AR
rake db:schema:load # Load a schema.rb file into the database
rake db:seed # Load the seed data from db/seeds.rb
rake db:setup # Create the database, load the schema, and initialize with the seed data (use db:reset to also drop the db first)
rake db:structure:dump # Dump the database structure to db/structure.sql
rake db:version # Retrieves the current schema version number
以上列表是我希望能够在使用ActiveRecord的非Rails Ruby项目上使用的任务列表。我在Rakefile中写什么?
提前致谢
答案 0 :(得分:17)
最简单的方法是加载已在databases.rake中定义的任务。这是一个关于如何完成的GIST。
<强> Rakefile.rb 强>
require 'yaml'
require 'logger'
require 'active_record'
include ActiveRecord::Tasks
class Seeder
def initialize(seed_file)
@seed_file = seed_file
end
def load_seed
raise "Seed file '#{@seed_file}' does not exist" unless File.file?(@seed_file)
load @seed_file
end
end
root = File.expand_path '..', __FILE__
DatabaseTasks.env = ENV['ENV'] || 'development'
DatabaseTasks.database_configuration = YAML.load(File.read(File.join(root, 'config/database.yml')))
DatabaseTasks.db_dir = File.join root, 'db'
DatabaseTasks.fixtures_path = File.join root, 'test/fixtures'
DatabaseTasks.migrations_paths = [File.join(root, 'db/migrate')]
DatabaseTasks.seed_loader = Seeder.new File.join root, 'db/seeds.rb'
DatabaseTasks.root = root
task :environment do
ActiveRecord::Base.configurations = DatabaseTasks.database_configuration
ActiveRecord::Base.establish_connection DatabaseTasks.env.to_sym
end
load 'active_record/railties/databases.rake'
答案 1 :(得分:5)
您可以尝试使用standalone-migrations gem: https://github.com/thuss/standalone-migrations
答案 2 :(得分:1)
对于Rails 3.x:
您需要手动创建任务。这里的例子是如何添加它们(这个例子使用像Rails这样的环境变量):
namespace :db do
desc "Drop and create the current database"
task :recreate => :environment do
abcs = ActiveRecord::Base.configurations
ActiveRecord::Base.establish_connection(abcs[RAILS_ENV])
ActiveRecord::Base.connection.recreate_database(ActiveRecord::Base.connection.current_database)
end
end
您可以使用rake db:recreate
任务
对于Rails 4.x:
如果您想在ruby应用中使用ActiveRecord rake任务,请查看documentation。
Rails之外的DatabaseTasks示例用法如下:
include ActiveRecord::Tasks
DatabaseTasks.database_configuration = YAML.load(File.read('my_database_config.yml'))
DatabaseTasks.db_dir = 'db'
# other settings...
DatabaseTasks.create_current('production')
另外,您有here一个关于如何在ruby应用程序中使用ActiveRecord的示例。
答案 3 :(得分:1)
创建自己的! 尽管参考了Rails:
https://github.com/rails/rails/blob/master/activerecord/lib/active_record/railties/databases.rake
-
Dir[File.join(PROJECT_ROOT, 'tasks', '**', '*.rake')].each do |file|
load file
end
答案 4 :(得分:1)
我相信即使您没有使用Sinatra,也可以使用sinatra-activerecord
gem。我只是通过要求该gem然后添加
require 'sinatra/activerecord/rake'
到我的rakefile
。
一旦我在require
中显示的db
行中添加了rake -T
行,
答案 5 :(得分:0)
如果您使用的是Sinatra,则可以使用此gem:
https://github.com/janko-m/sinatra-activerecord
但是,如果你也不使用它,里面的源代码就如何实现AR rake任务提供了一个很好的例子。