Sha256: 4121df362b49374e221c51f74963b342380cb04baf088f824c04cae3cf66d0ea

Contents?: true

Size: 1.36 KB

Versions: 11

Compression:

Stored size: 1.36 KB

Contents

require "rails"
require "active_record"
require "recursive-open-struct"

class Jets::Commands::Db::Tasks
  # Ugly but it loads ActiveRecord database tasks
  def self.load!
    # Jets.boot # Jets.boot here screws up jets -h, the db_config doesnt seem to match exactly
    # but seems to be working anyway.
    db_configs = Jets.application.config.database
    ActiveRecord::Tasks::DatabaseTasks.database_configuration = db_configs
    ActiveRecord::Tasks::DatabaseTasks.migrations_paths = ["db/migrate"]
    ActiveRecord::Tasks::DatabaseTasks.seed_loader = Seeder.new("#{Jets.root}db/seeds.rb")

    # Need to mock out the usage of Rails.application in:
    # activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb
    Rails.application = RecursiveOpenStruct.new(
      config: {
        paths: {
          db: ["db"],
        }
      },
      paths: {
        "db/migrate": ["db/migrate"]
      }
    )
    load "active_record/railties/databases.rake"

    load File.expand_path("../environment-task.rake", __FILE__)
  end

  # Thanks: https://stackoverflow.com/questions/19206764/how-can-i-load-activerecord-database-tasks-on-a-ruby-project-outside-rails/24840749
  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
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
jets-1.4.11 lib/jets/commands/db/tasks.rb
jets-1.4.10 lib/jets/commands/db/tasks.rb
jets-1.4.9 lib/jets/commands/db/tasks.rb
jets-1.4.8 lib/jets/commands/db/tasks.rb
jets-1.4.7 lib/jets/commands/db/tasks.rb
jets-1.4.6 lib/jets/commands/db/tasks.rb
jets-1.4.5 lib/jets/commands/db/tasks.rb
jets-1.4.4 lib/jets/commands/db/tasks.rb
jets-1.4.3 lib/jets/commands/db/tasks.rb
jets-1.4.2 lib/jets/commands/db/tasks.rb
jets-1.4.1 lib/jets/commands/db/tasks.rb