lib/schema_dev/tasks/dbms.rb in schema_dev-4.0.0 vs lib/schema_dev/tasks/dbms.rb in schema_dev-4.1.0

- old
+ new

@@ -1,49 +1,57 @@ +# frozen_string_literal: true + require 'schema_dev/config' +require 'schema_dev/rspec' -dbms = SchemaDev::Config.load.dbms +dbms = SchemaDev::Config.load.db -DATABASES = %w[schema_plus_test] - if dbms.any? - { - postgresql: { uservar: 'POSTGRESQL_DB_USER', defaultuser: 'schema_plus', create: "createdb -U '%{user}' %{dbname}", drop: "dropdb -U '%{user}' %{dbname}" }, - mysql: { uservar: 'MYSQL_DB_USER', defaultuser: 'schema_plus', create: "mysqladmin -u '%{user}' create %{dbname}", drop: "mysqladmin -u '%{user}' -f drop %{dbname}" } - }.slice(*dbms).each do |dbm, info| + (%w[postgresql mysql2] & dbms).each do |dbm| namespace dbm do - user = ENV.fetch info[:uservar], info[:defaultuser] - task :create_databases do - DATABASES.each do |dbname| - system(info[:create] % {user: user, dbname: dbname}) - end + task :create_database do + require 'active_record' + + config = SchemaDev::Rspec.db_configuration(dbm) + + ActiveRecord::Tasks::DatabaseTasks.create(config) end task :drop_databases do - DATABASES.each do |dbname| - system(info[:drop] % {user: user, dbname: dbname}) - end + require 'active_record' + + config = SchemaDev::Rspec.db_configuration(dbm) + + ActiveRecord::Tasks::DatabaseTasks.drop(config) end end end + desc 'Create database for CI run' + task :create_ci_database do + require 'active_record' + + config = SchemaDev::Rspec.db_configuration + + ActiveRecord::Tasks::DatabaseTasks.create(config) unless config['adapter'] == 'sqlite3' + end + desc 'Create test databases' task :create_databases do - invoke_multiple(dbms, "create_databases") + invoke_multiple(dbms, 'create_database') end desc 'Drop test databases' task :drop_databases do - invoke_multiple(dbms, "drop_databases") + invoke_multiple(dbms, 'drop_database') end def invoke_multiple(namespaces, task) - failed = namespaces.reject { |adapter| - begin - Rake::Task["#{adapter}:#{task}"].invoke - true - rescue => e - warn "\n#{e}\n" - false - end - } + failed = namespaces.reject do |adapter| + Rake::Task["#{adapter}:#{task}"].invoke + true + rescue => e + warn "\n#{e}\n" + false + end fail "Failure in: #{failed.join(', ')}" if failed.any? end end