Open HoneyryderChuck opened 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
@zlobz, is 'My Rakefile' a solution or a problematic one?
This seems to be duplicate of https://github.com/TalentBox/sequel-rails/issues/102
@bunufi, solution.
I was running
rake db:create
, and I got this: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 thepg_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 thecreatedb
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:db:drop
db:create
db:load
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.