[英]How can I load ActiveRecord database tasks on a Ruby project outside Rails?
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中要寫什么?
提前致謝
最簡單的方法是加載已在databases.rake中定義的任務。 這是一個如何完成它的GIST。
靈感來自Drogus的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'
您可以嘗試獨立遷移gem: https : //github.com/thuss/standalone-migrations
對於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
available
對於Rails 4.x:
如果您想在ruby應用程序中使用ActiveRecord rake任務,請查看文檔 。
在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')
你也需要在這里對如何在Ruby aplication使用ActiveRecord的例子。
創建你自己的! 盡管參考了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
即使你沒有使用Sinatra,我相信你也可以使用sinatra-activerecord
gem。 我剛剛通過要求寶石然后添加來解決這個問題
require 'sinatra/activerecord/rake'
到我的rakefile
。
一旦我添加了require
行, db
任務出現在我的rake -T
!
如果您使用的是Sinatra,則可以使用此gem:
https://github.com/janko-m/sinatra-activerecord
但是,如果您不使用它,則內部源代碼提供了有關如何實現AR rake任務的良好示例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.