|
15 | 15 |
|
16 | 16 | namespace :db do |
17 | 17 | database_config = YAML.load(File.open("./spec/support/database.yml")) |
18 | | - admin_database_config = database_config.merge(database: "mysql") |
19 | 18 | migration_path = File.expand_path("./spec/support/migrations") |
20 | 19 |
|
21 | 20 | desc "Create the database" |
22 | 21 | 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." |
| 22 | + if database_config[:adapter] == 'sqlite3' |
| 23 | + # For SQLite3, just ensure the directory exists |
| 24 | + db_file = database_config.fetch(:database) |
| 25 | + FileUtils.mkdir_p(File.dirname(db_file)) unless File.dirname(db_file) == '.' |
| 26 | + puts "Database created (SQLite3 will create file on first connection)." |
| 27 | + else |
| 28 | + # For other databases (MySQL, PostgreSQL, etc.) |
| 29 | + admin_config = database_config.merge(database: "mysql") |
| 30 | + ActiveRecord::Base.establish_connection(admin_config) |
| 31 | + ActiveRecord::Base.connection.create_database(database_config.fetch(:database)) |
| 32 | + puts "Database created." |
| 33 | + end |
26 | 34 | end |
27 | 35 |
|
28 | 36 | desc "Migrate the database" |
29 | 37 | task :migrate do |
30 | 38 | ActiveRecord::Base.establish_connection(database_config) |
31 | | - ActiveRecord::Migrator.migrate(migration_path) |
| 39 | + |
| 40 | + # Handle different Rails versions |
| 41 | + active_record_version = Gem::Version.new(ActiveRecord::VERSION::STRING) |
| 42 | + |
| 43 | + if active_record_version >= Gem::Version.new("6.0") |
| 44 | + ActiveRecord::MigrationContext.new(migration_path).migrate |
| 45 | + else |
| 46 | + ActiveRecord::Migrator.migrate(migration_path) |
| 47 | + end |
| 48 | + |
32 | 49 | Rake::Task["db:schema"].invoke |
33 | 50 | puts "Database migrated." |
34 | 51 | end |
35 | 52 |
|
36 | 53 | desc "Drop the database" |
37 | 54 | task :drop do |
38 | | - ActiveRecord::Base.establish_connection(admin_database_config) |
39 | | - ActiveRecord::Base.connection.drop_database(database_config.fetch(:database)) |
| 55 | + if database_config[:adapter] == 'sqlite3' |
| 56 | + # For SQLite3, just delete the file |
| 57 | + db_file = database_config.fetch(:database) |
| 58 | + File.delete(db_file) if File.exist?(db_file) |
| 59 | + else |
| 60 | + # For other databases (MySQL, PostgreSQL, etc.) |
| 61 | + admin_config = database_config.merge(database: "mysql") |
| 62 | + ActiveRecord::Base.establish_connection(admin_config) |
| 63 | + ActiveRecord::Base.connection.drop_database(database_config.fetch(:database)) |
| 64 | + end |
40 | 65 | puts "Database deleted." |
41 | 66 | end |
42 | 67 |
|
43 | 68 | desc "Reset the database" |
44 | 69 | task reset: [:drop, :create, :migrate] |
45 | | - desc 'Create a db/schema.rb file that is portable against any DB supported by AR' |
| 70 | + desc 'Create a db/schema.rb file that is portable against any DB supported by AR' |
46 | 71 |
|
47 | 72 | task :schema do |
48 | 73 | # Noop to make ActiveRecord happy |
|
0 commit comments