【问题标题】:How can I load ActiveRecord database tasks on a Ruby project outside Rails?如何在 Rails 之外的 Ruby 项目上加载 ActiveRecord 数据库任务?
【发布时间】:2013-10-12 23:01:44
【问题描述】:

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 中写什么?

提前致谢

【问题讨论】:

    标签: ruby activerecord rake


    【解决方案1】:

    最简单的做法是加载已在 databases.rake 中定义的任务。这是它是如何完成的 GIST。

    灵感来自this GIST by Drogus

    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'
    

    【讨论】:

    • DatabaseTasks.database_configuration = YAML::load(ERB.new(File.read(db_config_path)).result) 使其与 ERB 标签一起使用。见:edgeguides.rubyonrails.org/…
    【解决方案2】:

    您可以尝试独立迁移 gem: https://github.com/thuss/standalone-migrations

    【讨论】:

    【解决方案3】:

    对于 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 的示例。

    【讨论】:

    • 感谢您的回答。这适用于 ActiveRecord 3.2.X 吗?或仅适用于 4.1。我认为没有。
    • 这个只能和4.0以上版本一起使用,老版本需要自己写任务..
    【解决方案4】:

    创建你自己的! 参考 Rails 之一:

    https://github.com/rails/rails/blob/master/activerecord/lib/active_record/railties/databases.rake

    1. 创建一个 Rake 任务文件。要使用 Rake,通常需要一个包含 Rake 任务文件的任务文件夹。这些文件具有“.task”扩展名。
    2. 研究给定链接的文件。
    3. 获取该文件的部分内容,甚至文件的全部内容,并将其添加到您的新 Rake 任务文件中。
    4. 确保您的 Rakefile 加载了这些任务文件。你的 Rakefile 应该是这样的

    -

    Dir[File.join(PROJECT_ROOT,  'tasks', '**', '*.rake')].each do |file|
      load file
    end
    

    【讨论】:

    • 感谢您的回答。如何创建自己的和参考的 Rails 之一?
    【解决方案5】:

    我相信即使您不使用 Sinatra,您也可以使用 sinatra-activerecord gem。我刚刚通过要求该 gem 然后添加

    解决了这个问题
    require 'sinatra/activerecord/rake'
    

    致我的rakefile

    一旦我添加了require 行,db 任务就会出现在我的rake -T 中!

    【讨论】:

      【解决方案6】:

      如果你使用的是 Sinatra,你可以使用这个 gem:

      https://github.com/janko-m/sinatra-activerecord

      但是,如果你也不使用它,里面的源代码提供了一个很好的例子来说明如何实现 AR rake 任务。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-12-11
        • 2016-08-23
        • 1970-01-01
        • 2015-01-31
        • 1970-01-01
        • 2011-02-13
        • 2012-10-11
        • 2018-11-11
        相关资源
        最近更新 更多