lib/backhoe.rb in backhoe-0.2.0 vs lib/backhoe.rb in backhoe-0.3.0
- old
+ new
@@ -1,8 +1,10 @@
require "backhoe/version"
-require "rake"
-require "fileutils"
+require "backhoe/mysql"
+require "backhoe/postgresql"
+require "backhoe/sqlite"
+require "active_record"
module Backhoe
mattr_accessor(:file_path) { "db/data.sql" }
class << self
@@ -20,75 +22,9 @@
const_get(database_config["adapter"].camelize)
end
def database_config
ActiveRecord::Base.configurations[Rails.env || "development"]
- end
- end
-
- class Base < Struct.new(:config, :file_path)
- include Rake::DSL
-
- private
-
- def database
- config["database"]
- end
- end
-
- class Mysql < Base
- def dump skip_tables: []
- mysqldump = `which mysqldump`.strip
- raise RuntimeError, "Cannot find mysqldump." if mysqldump.blank?
- sh "#{mysqldump} --no-create-db --single-transaction --quick -e #{skip_table_options(skip_tables)} #{mysql_options} > #{file_path}"
- end
-
- def load
- mysql = `which mysql`.strip
- raise RuntimeError, "Cannot find mysql." if mysql.blank?
- sh "#{mysql} #{mysql_options} < #{file_path}"
- end
-
- private
-
- def skip_table_options skip_tables
- skip_tables.map do |table|
- "--ignore-table=#{config["database"]}.#{table}"
- end.join(" ")
- end
-
- def mysql_options
- options = " -u #{config["username"]}"
- options += " -p'#{config["password"]}'" if config["password"]
- options += " -h #{config["host"]}" if config["host"]
- options += " -S #{config["socket"]}" if config["socket"]
- options += " '#{config["database"]}'"
- end
- end
-
- Mysql2 = Mysql
-
- class Sqlite3 < Base
- def dump **_
- FileUtils.cp database, file_path
- end
-
- def load
- FileUtils.cp file_path, database
- end
- end
-
- class Postgresql < Base
- def dump **_
- pg_dump = `which pg_dump`.strip
- raise RuntimeError, "Cannot find pg_dump." if pg_dump.blank?
- sh "#{pg_dump} -c -f#{file_path} #{database}"
- end
-
- def load
- psql = `which psql`.strip
- raise RuntimeError, "Cannot find psql." if psql.blank?
- sh "#{psql} -q -d#{database} -f#{file_path}"
end
end
end