Class: Ardb::Adapter::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/ardb/adapter/base.rb

Direct Known Subclasses

Mysql, Postgresql, Sqlite

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



9
10
11
12
13
14
15
16
# File 'lib/ardb/adapter/base.rb', line 9

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"
end

Instance Attribute Details

#config_settingsObject (readonly)

Returns the value of attribute config_settings.



6
7
8
# File 'lib/ardb/adapter/base.rb', line 6

def config_settings
  @config_settings
end

#databaseObject (readonly)

Returns the value of attribute database.



6
7
8
# File 'lib/ardb/adapter/base.rb', line 6

def database
  @database
end

#ruby_schema_pathObject (readonly)

Returns the value of attribute ruby_schema_path.



7
8
9
# File 'lib/ardb/adapter/base.rb', line 7

def ruby_schema_path
  @ruby_schema_path
end

#schema_formatObject (readonly)

Returns the value of attribute schema_format.



7
8
9
# File 'lib/ardb/adapter/base.rb', line 7

def schema_format
  @schema_format
end

#sql_schema_pathObject (readonly)

Returns the value of attribute sql_schema_path.



7
8
9
# File 'lib/ardb/adapter/base.rb', line 7

def sql_schema_path
  @sql_schema_path
end

Instance Method Details

#==(other_adapter) ⇒ Object



87
88
89
# File 'lib/ardb/adapter/base.rb', line 87

def ==(other_adapter)
  self.class == other_adapter.class
end

#connect_dbObject



24
25
26
# File 'lib/ardb/adapter/base.rb', line 24

def connect_db
  ActiveRecord::Base.connection
end

#create_db(*args) ⇒ Object

Raises:

  • (NotImplementedError)


21
# File 'lib/ardb/adapter/base.rb', line 21

def create_db(*args); raise NotImplementedError; end

#drop_db(*args) ⇒ Object

Raises:

  • (NotImplementedError)


22
# File 'lib/ardb/adapter/base.rb', line 22

def drop_db(*args);   raise NotImplementedError; end

#drop_tables(*args) ⇒ Object

Raises:

  • (NotImplementedError)


47
# File 'lib/ardb/adapter/base.rb', line 47

def drop_tables(*args); raise NotImplementedError; end

#dump_ruby_schemaObject



75
76
77
78
79
80
81
# File 'lib/ardb/adapter/base.rb', line 75

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|
    ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
  end
end

#dump_schemaObject



66
67
68
69
70
71
72
73
# File 'lib/ardb/adapter/base.rb', line 66

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
  $stdout = current_stdout
end

#dump_sql_schemaObject

Raises:

  • (NotImplementedError)


83
84
85
# File 'lib/ardb/adapter/base.rb', line 83

def dump_sql_schema
  raise NotImplementedError
end

#foreign_key_add_sql(*args) ⇒ Object

Raises:

  • (NotImplementedError)


18
# File 'lib/ardb/adapter/base.rb', line 18

def foreign_key_add_sql(*args);  raise NotImplementedError; end

#foreign_key_drop_sql(*args) ⇒ Object

Raises:

  • (NotImplementedError)


19
# File 'lib/ardb/adapter/base.rb', line 19

def foreign_key_drop_sql(*args); raise NotImplementedError; end

#load_ruby_schemaObject



58
59
60
# File 'lib/ardb/adapter/base.rb', line 58

def load_ruby_schema
  load @ruby_schema_path
end

#load_schemaObject



49
50
51
52
53
54
55
56
# File 'lib/ardb/adapter/base.rb', line 49

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
  $stdout = current_stdout
end

#load_sql_schemaObject

Raises:

  • (NotImplementedError)


62
63
64
# File 'lib/ardb/adapter/base.rb', line 62

def load_sql_schema
  raise NotImplementedError
end

#migrate_dbObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ardb/adapter/base.rb', line 28

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::Migration.verbose = verbose
  ActiveRecord::Migrator.migrate(migrations_path, version) do |migration|
    ENV["MIGRATE_SCOPE"].blank? || (ENV["MIGRATE_SCOPE"] == migration.scope)
  end
end