lib/ardb/adapter/base.rb in ardb-0.27.3 vs lib/ardb/adapter/base.rb in ardb-0.28.0

- old
+ new

@@ -1,22 +1,31 @@ +require 'ardb' + module Ardb; end -class Ardb::Adapter +module Ardb::Adapter class Base - attr_reader :config_settings, :database - attr_reader :schema_format, :ruby_schema_path, :sql_schema_path + attr_reader :config - def initialize - @config_settings = Ardb.config.db_settings - @database = Ardb.config.db.database - @schema_format = Ardb.config.schema_format - schema_path = Ardb.config.schema_path - @ruby_schema_path = "#{schema_path}.rb" - @sql_schema_path = "#{schema_path}.sql" + def initialize(config) + @config = config end + def connect_hash; self.config.activerecord_connect_hash; end + def database; self.config.database; end + def migrations_path; self.config.migrations_path; end + def schema_format; self.config.schema_format; end + + def ruby_schema_path + @ruby_schema_path ||= "#{self.config.schema_path}.rb" + end + + def sql_schema_path + @sql_schema_path ||= "#{self.config.schema_path}.sql" + end + def escape_like_pattern(pattern, escape_char = nil) escape_char ||= "\\" pattern = pattern.to_s.dup pattern.gsub!(escape_char){ escape_char * 2 } # don't allow custom wildcards @@ -27,46 +36,48 @@ 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.connection + ActiveRecord::Base.establish_connection(self.connect_hash) + # 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 - migrations_path = Ardb.config.migrations_path if defined?(ActiveRecord::Migration::CommandRecorder) require 'ardb/migration_helpers' ActiveRecord::Migration::CommandRecorder.class_eval do include Ardb::MigrationHelpers::RecorderMixin end end - ActiveRecord::Migrator.migrations_path = migrations_path + ActiveRecord::Migrator.migrations_path = self.migrations_path ActiveRecord::Migration.verbose = verbose - ActiveRecord::Migrator.migrate(migrations_path, version) do |migration| + ActiveRecord::Migrator.migrate(self.migrations_path, version) do |migration| ENV["MIGRATE_SCOPE"].blank? || (ENV["MIGRATE_SCOPE"] == migration.scope) end end - def drop_tables(*args); raise NotImplementedError; end - def load_schema # silence STDOUT current_stdout = $stdout.dup $stdout = File.new('/dev/null', 'w') - load_ruby_schema if @schema_format == :ruby - load_sql_schema if @schema_format == :sql + 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 def load_ruby_schema - load @ruby_schema_path + load self.ruby_schema_path end def load_sql_schema raise NotImplementedError end @@ -74,27 +85,31 @@ def dump_schema # silence STDOUT current_stdout = $stdout.dup $stdout = File.new('/dev/null', 'w') dump_ruby_schema - dump_sql_schema if @schema_format == :sql + 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' - FileUtils.mkdir_p File.dirname(@ruby_schema_path) - File.open(@ruby_schema_path, 'w:utf-8') do |file| + FileUtils.mkdir_p File.dirname(self.ruby_schema_path) + File.open(self.ruby_schema_path, 'w:utf-8') do |file| ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file) end end def dump_sql_schema raise NotImplementedError end - def ==(other_adapter) - self.class == other_adapter.class + def ==(other) + if other.kind_of?(self.class) + self.config == other.config + else + super + end end end end