Class: Translatomatic::Database

Inherits:
Object
  • Object
show all
Extended by:
DefineOptions
Includes:
Util
Defined in:
lib/translatomatic/database.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#locale, #log, #string

Constructor Details

#initialize(options = {}) ⇒ Database



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/translatomatic/database.rb', line 21

def initialize(options = {})
  @env = options[:database_env] || DEFAULT_ENV
  @db_config = database_config(@env, options)
  @env_config = @db_config
  raise "no environment '#{@env}' in #{db_config_path}" unless @env_config[@env]
  @env_config = @env_config[@env] || {}

  ActiveRecord::Base.configurations = @db_config
  ActiveRecord::Tasks::DatabaseTasks.env = @env
  ActiveRecord::Tasks::DatabaseTasks.db_dir = DB_PATH
  ActiveRecord::Tasks::DatabaseTasks.root = DB_PATH
  ActiveRecord::Tasks::DatabaseTasks.database_configuration = @db_config
  create unless exists?
  migrate
end

Class Attribute Details

.optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/translatomatic/database.rb', line 8

def options
  @options
end

Class Method Details

.enabled?(options = {}) ⇒ Boolean



12
13
14
# File 'lib/translatomatic/database.rb', line 12

def enabled?(options = {})
  new(options).connect
end

Instance Method Details

#connectboolean

Connect to the database



39
40
41
42
43
44
45
46
# File 'lib/translatomatic/database.rb', line 39

def connect
  begin
    ActiveRecord::Base.establish_connection(@env_config)
    true
  rescue LoadError
    false
  end
end

#createboolean

Create the database



78
79
80
81
82
83
84
85
86
87
# File 'lib/translatomatic/database.rb', line 78

def create
  begin
    ActiveRecord::Tasks::DatabaseTasks.create(@env_config)
    log.debug "Database created."
    true
  rescue LoadError => e
    log.debug "Database could not be created: " + e.message
    false
  end
end

#disconnectvoid

This method returns an undefined value.

Disconnect from the database



50
51
52
# File 'lib/translatomatic/database.rb', line 50

def disconnect
  ActiveRecord::Base.remove_connection
end

#dropvoid

This method returns an undefined value.

Drop the database



91
92
93
94
95
# File 'lib/translatomatic/database.rb', line 91

def drop
  disconnect
  ActiveRecord::Tasks::DatabaseTasks.drop(@env_config)
  log.debug "Database deleted."
end

#exists?Boolean

Test if the database exists



56
57
58
59
60
61
62
63
64
65
# File 'lib/translatomatic/database.rb', line 56

def exists?
  begin
    return true if sqlite_database_exists?
    return false unless connect
    ActiveRecord::Base.connection.tables
  rescue
    return false
  end
  true
end

#migratevoid

This method returns an undefined value.

Run outstanding migrations against the database



69
70
71
72
73
74
# File 'lib/translatomatic/database.rb', line 69

def migrate
  return false unless connect
  ActiveRecord::Migrator.migrate(MIGRATIONS_PATH)
  ActiveRecord::Base.clear_cache!
  log.debug "Database migrated."
end