From 636ad2cebd86fa5e669018b45f757d7aaaf22f53 Mon Sep 17 00:00:00 2001 From: Michael Ball Date: Fri, 10 Jul 2020 18:29:57 -0700 Subject: [PATCH] Add import rake task --- lib/tasks/db.rake | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lib/tasks/db.rake b/lib/tasks/db.rake index 39db898f..3c9cded7 100644 --- a/lib/tasks/db.rake +++ b/lib/tasks/db.rake @@ -12,4 +12,25 @@ namespace :db do puts 'Database exists, skipping bootstrap...' end end + + desc "Import a given file into the database" + task :import, [:path] => :environment do |_t, args| + dump_path = args.path + connection_config = ActiveRecord::Base.connection_config + + case connection_config[:adapter] + when "postgresql" + system("PGPASSWORD=#{connection_config[:password]} pg_restore " \ + "--verbose --clean --no-acl --no-owner " \ + "--username=#{connection_config[:username]} " \ + "-d #{connection_config[:database]} #{dump_path}") + when "mysql", "mysql2" + system("mysql -u #{connection_config[:username]} " \ + "-p#{connection_config[:password]} " \ + "#{connection_config[:database]} < #{dump_path}") + else + raise NotImplementedError, "An importer hasn't been implemented for: " \ + "#{connection_config[:adapter]}" + end + end end