class Translatomatic::Database
Database
functions
Constants
- CUSTOM_CONF
- DB_PATH
- DEFAULT_CONF
- DEFAULT_ENV
- GEM_ROOT
- INTERNAL_CONF
- MIGRATIONS_PATH
Public Class Methods
enabled?(options = {})
click to toggle source
@param options [Hash<Symbol,Object>] Database
options @return [boolean] True if we can connect to the database
# File lib/translatomatic/database.rb, line 9 def enabled?(options = {}) new(options).connect end
new(options = {})
click to toggle source
# 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 unless env_config[@env] raise t('database.no_environment', env: @env, file: db_config_path) end @env_config = env_config[@env] || {} init_active_record create unless exists? end
Private Class Methods
join_path(*parts)
click to toggle source
# File lib/translatomatic/database.rb, line 105 def join_path(*parts) File.realpath(File.join(*parts)) end
Public Instance Methods
connect()
click to toggle source
Connect to the database @return [boolean] True if the connection was established
# File lib/translatomatic/database.rb, line 28 def connect ActiveRecord::Base.establish_connection(@env_config) migrate true rescue LoadError false end
create()
click to toggle source
Create the database @return [boolean] True if the database was created
# File lib/translatomatic/database.rb, line 67 def create 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
disconnect()
click to toggle source
Disconnect from the database @return [void]
# File lib/translatomatic/database.rb, line 38 def disconnect ActiveRecord::Base.remove_connection end
drop()
click to toggle source
Drop the database @return [void]
# File lib/translatomatic/database.rb, line 79 def drop disconnect ActiveRecord::Tasks::DatabaseTasks.drop(@env_config) log.debug t('database.deleted') end
exists?()
click to toggle source
Test if the database exists @return [Boolean] true if the database exists
# File lib/translatomatic/database.rb, line 44 def exists? begin return true if sqlite_database_exists? return false unless connect ActiveRecord::Base.connection.tables rescue StandardError return false end true end
locale()
click to toggle source
Shortcut to locale model class @return [Class] Translatomatic::Model::Locale
# File lib/translatomatic/database.rb, line 93 def locale Translatomatic::Model::Locale end
migrate()
click to toggle source
Run outstanding migrations against the database @return [void]
# File lib/translatomatic/database.rb, line 57 def migrate return if @migrated ActiveRecord::Migrator.migrate(MIGRATIONS_PATH) ActiveRecord::Base.clear_cache! log.debug t('database.migrated') @migrated = true end
text()
click to toggle source
Shortcut to text model class @return [Class] Translatomatic::Model::Text
# File lib/translatomatic/database.rb, line 87 def text Translatomatic::Model::Text end
Private Instance Methods
database_config(env, options)
click to toggle source
return database config as a hash
# File lib/translatomatic/database.rb, line 148 def database_config(env, options) if options[:database_config].is_a?(Hash) return { env => options[:database_config] } end db_config_path = database_config_path(options) dbconfig = File.read(db_config_path) dbconfig.gsub!(/\$HOME/, Dir.home) dbconfig.gsub!(/\$GEM_ROOT/, GEM_ROOT) YAML.safe_load(dbconfig) || {} end
database_config_path(options)
click to toggle source
return path to database config
# File lib/translatomatic/database.rb, line 137 def database_config_path(options) if options[:database_env] == 'test' INTERNAL_CONF # used for rspec elsif options[:database_config] options[:database_config] else DEFAULT_CONF end end
init_active_record()
click to toggle source
# File lib/translatomatic/database.rb, line 110 def init_active_record 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 end
sqlite_database_exists?()
click to toggle source
# File lib/translatomatic/database.rb, line 118 def sqlite_database_exists? @env_config['adapter'] == 'sqlite3' && File.exist?(@env_config['database']) end