|
1 | | -require "bundler/gem_tasks" |
2 | | -require "yaml" |
3 | | -require "active_record" |
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'bundler/gem_tasks' |
| 4 | +require 'yaml' |
| 5 | +require 'active_record' |
4 | 6 |
|
5 | 7 | namespace :test do |
6 | 8 | task :all do |
7 | | - Dir.glob("./gemfiles/Gemfile*").each do |gemfile| |
8 | | - next if gemfile.end_with?(".lock") |
| 9 | + Dir.glob('./gemfiles/Gemfile*').each do |gemfile| |
| 10 | + next if gemfile.end_with?('.lock') |
| 11 | + |
9 | 12 | puts "Running specs for #{Pathname.new(gemfile).basename}" |
10 | 13 | system("BUNDLE_GEMFILE=#{gemfile} bundle install > /dev/null && BUNDLE_GEMFILE=#{gemfile} bundle exec rspec") |
11 | | - puts "" |
| 14 | + puts '' |
12 | 15 | end |
13 | 16 | end |
14 | 17 | end |
15 | 18 |
|
16 | 19 | namespace :db do |
17 | | - database_config = YAML.load(File.open("./spec/support/database.yml")) |
18 | | - admin_database_config = database_config.merge(database: "mysql") |
19 | | - migration_path = File.expand_path("./spec/support/migrations") |
| 20 | + database_config = YAML.load(File.open('./spec/support/database.yml')) |
| 21 | + migration_path = File.expand_path('./spec/support/migrations') |
20 | 22 |
|
21 | | - desc "Create the database" |
| 23 | + desc 'Create the database' |
22 | 24 | task :create do |
23 | | - ActiveRecord::Base.establish_connection(admin_database_config) |
24 | | - ActiveRecord::Base.connection.create_database(database_config.fetch(:database)) |
25 | | - puts "Database created." |
| 25 | + # SQLite3 creates the database file automatically, just ensure directory exists |
| 26 | + db_file = database_config.fetch(:database) |
| 27 | + FileUtils.mkdir_p(File.dirname(db_file)) unless File.dirname(db_file) == '.' |
| 28 | + puts 'Database ready (SQLite3).' |
26 | 29 | end |
27 | 30 |
|
28 | | - desc "Migrate the database" |
| 31 | + desc 'Migrate the database' |
29 | 32 | task :migrate do |
30 | 33 | ActiveRecord::Base.establish_connection(database_config) |
31 | | - ActiveRecord::Migrator.migrate(migration_path) |
32 | | - Rake::Task["db:schema"].invoke |
33 | | - puts "Database migrated." |
| 34 | + ActiveRecord::MigrationContext.new(migration_path).migrate |
| 35 | + Rake::Task['db:schema'].invoke |
| 36 | + puts 'Database migrated.' |
34 | 37 | end |
35 | 38 |
|
36 | | - desc "Drop the database" |
| 39 | + desc 'Drop the database' |
37 | 40 | task :drop do |
38 | | - ActiveRecord::Base.establish_connection(admin_database_config) |
39 | | - ActiveRecord::Base.connection.drop_database(database_config.fetch(:database)) |
40 | | - puts "Database deleted." |
| 41 | + # For SQLite3, just delete the file |
| 42 | + db_file = database_config.fetch(:database) |
| 43 | + File.delete(db_file) if File.exist?(db_file) |
| 44 | + puts 'Database deleted.' |
41 | 45 | end |
42 | 46 |
|
43 | | - desc "Reset the database" |
| 47 | + desc 'Reset the database' |
44 | 48 | task reset: [:drop, :create, :migrate] |
45 | | - desc 'Create a db/schema.rb file that is portable against any DB supported by AR' |
| 49 | + desc 'Create a db/schema.rb file that is portable against any DB supported by AR' |
46 | 50 |
|
47 | 51 | task :schema do |
48 | 52 | # Noop to make ActiveRecord happy |
|
0 commit comments