osem-fcy/lib/tasks/db.rake

37 lines
1.3 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
namespace :db do
desc 'Bootstrap the database for the current RAILS_ENV (create, setup & seed if the database does not exist)'
task bootstrap: :environment do
Rake::Task['db:create'].invoke
if ActiveRecord::Base.connection.tables.empty?
puts 'Bootstrapping the database...'
Rake::Task['db:setup'].invoke
Rake::Task['db:seed'].invoke
else
puts 'Database exists, skipping bootstrap...'
end
end
2020-07-10 18:29:57 -07:00
2021-02-18 21:03:28 -08:00
desc 'Import a given file into the database'
2020-07-10 18:29:57 -07:00
task :import, [:path] => :environment do |_t, args|
dump_path = args.path
connection_config = ActiveRecord::Base.connection_config
case connection_config[:adapter]
2021-02-18 21:03:28 -08:00
when 'postgresql'
2020-07-10 18:29:57 -07:00
system("PGPASSWORD=#{connection_config[:password]} pg_restore " \
2021-02-18 21:03:28 -08:00
'--verbose --clean --no-acl --no-owner ' \
2020-07-10 18:29:57 -07:00
"--username=#{connection_config[:username]} " \
"-d #{connection_config[:database]} #{dump_path}")
2021-02-18 21:03:28 -08:00
when 'mysql', 'mysql2'
2020-07-10 18:29:57 -07:00
system("mysql -u #{connection_config[:username]} " \
"-p#{connection_config[:password]} " \
"#{connection_config[:database]} < #{dump_path}")
else
2021-02-18 21:03:28 -08:00
raise NotImplementedError.new("An importer hasn't been implemented for: " \
"#{connection_config[:adapter]}")
2020-07-10 18:29:57 -07:00
end
end
end