TalentBox / sequel-rails

A gem for using Sequel with Rails 5.x, 6.x, 7.x
http://talentbox.github.io/sequel-rails/
MIT License
324 stars 82 forks source link

rake db:create somehow still dependent from environment, and under certain extensions that breaks #117

Open HoneyryderChuck opened 8 years ago

HoneyryderChuck commented 8 years ago

I was running rake db:create, and I got this:

Sequel::DatabaseConnectionError: PG::ConnectionBad: FATAL:  database "app_cucumber" does not 
exist

I assume that this works in a normal case. Problem is, it runs the rake environment before. This will load the environment, which will run all initializers, including the sequel after_connect bit, which loads the extensions, plugins, etc. I'm using the pg_enum extension, which ships with sequel, and apparen1y this will trigger some sql in the database, which will not exist because I'm creating it.

As the db:create task runs the createdb cli script anyways, why load the environment? Granted, maybe some variables come through there. But this breaks cucumber-rails runs. Why?

sequel-rails redefines the db:test:prepare task. It calls:

And this might be the culprit. rails defines it as an empty task, and I don't know if this should be the concern of sequel-rails, as other gems like database_cleaner can take care of whatever we need to do in the database.

zlobz commented 8 years ago

My Rakefile


module SequelRails::Storage
  Postgresql = Postgres
end
SequelRails.configuration = OpenStruct.new

SequelRails::Migrations.module_eval do
  def self.dump_schema_information(opts = {})
    sql = opts.fetch :sql
    adapter = SequelRails::Storage.adapter_for(opts.fetch(:config_or_env, Rails.env))
    db = ::Sequel::Model.db
    res = ''

    if available_migrations?
      migrator_class = ::Sequel::Migrator.send(:migrator_class, migrations_dir)
      migrator = migrator_class.new db, migrations_dir
      res << adapter.schema_information_dump(migrator, sql)
    end
    res
  end
end

class DatabaseTasks
  def initialize env = nil
    @env = env || Rails.env
  end

  def db_config
    Rails.configuration.database_configuration[@env]
  end

  def drop
    SequelRails::Storage.drop_environment(db_config)
  end

  def create
    SequelRails::Storage.create_environment(db_config)
  end

  def dump
    filename = ENV['DB_STRUCTURE'] || Rails.root.join('db', 'structure.sql')
    SequelRails::Storage.dump_environment(db_config, filename) or return false
    schema_info = SequelRails::Migrations.dump_schema_information(sql: true, config_or_env: db_config)
    ::File.write filename, schema_info, mode: 'a'
    true
  end

  def self.migrate
    SequelRails::Migrations.migrate(ENV['VERSION'])
  end

  def self.seed
    require Rails.root.join('db/seeder')
    Seeder.()
  end
end

namespace :db do
  task recreate: %i[drop create migrate dump seed]

  task :drop do
    DatabaseTasks.new.drop or abort "Could not drop database"
  end

  task :create do
    DatabaseTasks.new.create or abort "Could not create database"
  end

  task :migrate do
    DatabaseTasks.migrate
  end

  task :seed => :environment do
    DatabaseTasks.seed
  end

  task :dump do
    DatabaseTasks.new.dump or abort "Could not dump structure"
    #Rake::Task["#{sequel_rails_namespace}:structure:dump"].reenable
  end
end
bunufi commented 6 years ago

@zlobz, is 'My Rakefile' a solution or a problematic one?

bunufi commented 6 years ago

This seems to be duplicate of https://github.com/TalentBox/sequel-rails/issues/102

zlobz commented 6 years ago

@bunufi, solution.