Class: Translatomatic::Database

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

Overview

Database functions

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Database

Returns a new instance of Database.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/translatomatic/database.rb', line 14

def initialize(options = {})
  @env = options[:database_env] || DEFAULT_ENV
  @db_config = database_config(@env, options)
  @env_config = @db_config
  raise t("database.no_environment",
    env: @env, file: 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 Method Details

.enabled?(options = {}) ⇒ boolean

Returns True if we can connect to the database.

Parameters:

  • options (Hash<Symbol,Object>) (defaults to: {})

    Database options

Returns:

  • (boolean)

    True if we can connect to the database



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

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

Instance Method Details

#connectboolean

Connect to the database

Returns:

  • (boolean)

    True if the connection was established



33
34
35
36
37
38
39
40
# File 'lib/translatomatic/database.rb', line 33

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

#createboolean

Create the database

Returns:

  • (boolean)

    True if the database was created



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/translatomatic/database.rb', line 72

def create
  begin
    ActiveRecord::Tasks::DatabaseTasks.create(@env_config)
    log.debug t("database.created")
    true
  rescue LoadError => e
    log.debug t("database.could_not_create")
    log.error e.message
    false
  end
end

#disconnectvoid

This method returns an undefined value.

Disconnect from the database



44
45
46
# File 'lib/translatomatic/database.rb', line 44

def disconnect
  ActiveRecord::Base.remove_connection
end

#dropvoid

This method returns an undefined value.

Drop the database



86
87
88
89
90
# File 'lib/translatomatic/database.rb', line 86

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

#exists?Boolean

Test if the database exists

Returns:

  • (Boolean)

    true if the database exists



50
51
52
53
54
55
56
57
58
59
# File 'lib/translatomatic/database.rb', line 50

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



63
64
65
66
67
68
# File 'lib/translatomatic/database.rb', line 63

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