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.



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

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.



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

def config_settings
  @config_settings
end

#databaseObject (readonly)

Returns the value of attribute database.



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

def database
  @database
end

#ruby_schema_pathObject (readonly)

Returns the value of attribute ruby_schema_path.



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

def ruby_schema_path
  @ruby_schema_path
end

#schema_formatObject (readonly)

Returns the value of attribute schema_format.



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

def schema_format
  @schema_format
end

#sql_schema_pathObject (readonly)

Returns the value of attribute sql_schema_path.



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

def sql_schema_path
  @sql_schema_path
end

Instance Method Details

#==(other_adapter) ⇒ Object



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

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

#create_db(*args) ⇒ Object

Raises:

  • (NotImplementedError)


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

def create_db(*args); raise NotImplementedError; end

#drop_db(*args) ⇒ Object

Raises:

  • (NotImplementedError)


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

def drop_db(*args);   raise NotImplementedError; end

#drop_tables(*args) ⇒ Object

Raises:

  • (NotImplementedError)


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

def drop_tables(*args); raise NotImplementedError; end

#dump_ruby_schemaObject



70
71
72
73
74
75
76
# File 'lib/ardb/adapter/base.rb', line 70

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



61
62
63
64
65
66
67
68
# File 'lib/ardb/adapter/base.rb', line 61

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)


78
79
80
# File 'lib/ardb/adapter/base.rb', line 78

def dump_sql_schema
  raise NotImplementedError
end

#foreign_key_add_sql(*args) ⇒ Object

Raises:

  • (NotImplementedError)


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

def foreign_key_add_sql(*args);  raise NotImplementedError; end

#foreign_key_drop_sql(*args) ⇒ Object

Raises:

  • (NotImplementedError)


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

def foreign_key_drop_sql(*args); raise NotImplementedError; end

#load_ruby_schemaObject



53
54
55
# File 'lib/ardb/adapter/base.rb', line 53

def load_ruby_schema
  load @ruby_schema_path
end

#load_schemaObject



44
45
46
47
48
49
50
51
# File 'lib/ardb/adapter/base.rb', line 44

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)


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

def load_sql_schema
  raise NotImplementedError
end

#migrate_dbObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ardb/adapter/base.rb', line 23

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