lib/ardb/adapter/base.rb in ardb-0.28.3 vs lib/ardb/adapter/base.rb in ardb-0.29.0

- old
+ new

@@ -1,16 +1,15 @@ -require 'ardb' +require "ardb" module Ardb; end module Ardb::Adapter - class Base - attr_reader :config def initialize(config) @config = config + validate! end def connect_hash; self.config.activerecord_connect_hash; end def database; self.config.database; end def migrations_path; self.config.migrations_path; end @@ -31,47 +30,45 @@ # don't allow custom wildcards pattern.gsub!(/%|_/){ |wildcard_char| "#{escape_char}#{wildcard_char}" } pattern end - def foreign_key_add_sql(*args); raise NotImplementedError; end - def foreign_key_drop_sql(*args); raise NotImplementedError; end - def create_db(*args); raise NotImplementedError; end def drop_db(*args); raise NotImplementedError; end def drop_tables(*args); raise NotImplementedError; end def connect_db ActiveRecord::Base.establish_connection(self.connect_hash) - # checkout a connection to ensure we can connect to the DB, we don't do + # checkout a connection to ensure we can connect to the DB, we don"t do # anything with the connection and immediately check it back in ActiveRecord::Base.connection_pool.with_connection{ } end def migrate_db - verbose = ENV["MIGRATE_QUIET"].nil? - version = ENV["MIGRATE_VERSION"] ? ENV["MIGRATE_VERSION"].to_i : nil + migrate_db_up + end - if defined?(ActiveRecord::Migration::CommandRecorder) - require 'ardb/migration_helpers' - ActiveRecord::Migration::CommandRecorder.class_eval do - include Ardb::MigrationHelpers::RecorderMixin - end - end + def migrate_db_up(target_version = nil) + migration_context.up(target_version) + end - ActiveRecord::Migrator.migrations_path = self.migrations_path - ActiveRecord::Migration.verbose = verbose - ActiveRecord::Migrator.migrate(self.migrations_path, version) do |migration| - ENV["MIGRATE_SCOPE"].blank? || (ENV["MIGRATE_SCOPE"] == migration.scope) - end + def migrate_db_down(target_version = nil) + migration_context.down(target_version) end + def migrate_db_forward(steps = 1) + migration_context.forward(steps) + end + + def migrate_db_backward(steps = 1) + migration_context.rollback(steps) + end + def load_schema - # silence STDOUT - current_stdout = $stdout.dup - $stdout = File.new('/dev/null', 'w') + current_stdout = $stdout.dup # silence STDOUT + $stdout = File.new("/dev/null", "w") load_ruby_schema if self.schema_format == Ardb::Config::RUBY_SCHEMA_FORMAT load_sql_schema if self.schema_format == Ardb::Config::SQL_SCHEMA_FORMAT $stdout = current_stdout end @@ -82,22 +79,21 @@ def load_sql_schema raise NotImplementedError end def dump_schema - # silence STDOUT - current_stdout = $stdout.dup - $stdout = File.new('/dev/null', 'w') + current_stdout = $stdout.dup # silence STDOUT + $stdout = File.new("/dev/null", "w") dump_ruby_schema dump_sql_schema if self.schema_format == Ardb::Config::SQL_SCHEMA_FORMAT $stdout = current_stdout end def dump_ruby_schema - require 'active_record/schema_dumper' + require "active_record/schema_dumper" FileUtils.mkdir_p File.dirname(self.ruby_schema_path) - File.open(self.ruby_schema_path, 'w:utf-8') do |file| + File.open(self.ruby_schema_path, "w:utf-8") do |file| ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file) end end def dump_sql_schema @@ -110,8 +106,16 @@ else super end end - end + private + def validate! + # override as needed + end + + def migration_context + ActiveRecord::MigrationContext.new(migrations_path) + end + end end