Class: Translatomatic::Database
- Inherits:
-
Object
- Object
- Translatomatic::Database
- Includes:
- Util
- Defined in:
- lib/translatomatic/database.rb
Overview
Database functions
Class Method Summary collapse
-
.enabled?(options = {}) ⇒ boolean
True if we can connect to the database.
Instance Method Summary collapse
-
#connect ⇒ boolean
Connect to the database.
-
#create ⇒ boolean
Create the database.
-
#disconnect ⇒ void
Disconnect from the database.
-
#drop ⇒ void
Drop the database.
-
#exists? ⇒ Boolean
Test if the database exists.
-
#initialize(options = {}) ⇒ Database
constructor
A new instance of Database.
-
#migrate ⇒ void
Run outstanding migrations against the database.
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( = {}) @env = [:database_env] || DEFAULT_ENV @db_config = database_config(@env, ) @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.
9 10 11 |
# File 'lib/translatomatic/database.rb', line 9 def enabled?( = {}) new().connect end |
Instance Method Details
#connect ⇒ boolean
Connect to the database
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 |
#create ⇒ boolean
Create the database
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. false end end |
#disconnect ⇒ void
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 |
#drop ⇒ void
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
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 |
#migrate ⇒ void
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 |