Skip to content

Commit dedd446

Browse files
modify rakefile to support both sqlite3 and mysql
1 parent 25e118a commit dedd446

File tree

1 file changed

+33
-8
lines changed

1 file changed

+33
-8
lines changed

Rakefile

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,59 @@ end
1515

1616
namespace :db do
1717
database_config = YAML.load(File.open("./spec/support/database.yml"))
18-
admin_database_config = database_config.merge(database: "mysql")
1918
migration_path = File.expand_path("./spec/support/migrations")
2019

2120
desc "Create the database"
2221
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
2634
end
2735

2836
desc "Migrate the database"
2937
task :migrate do
3038
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+
3249
Rake::Task["db:schema"].invoke
3350
puts "Database migrated."
3451
end
3552

3653
desc "Drop the database"
3754
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
4065
puts "Database deleted."
4166
end
4267

4368
desc "Reset the database"
4469
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'
4671

4772
task :schema do
4873
# Noop to make ActiveRecord happy

0 commit comments

Comments
 (0)