Module: Mimi::DB

Extended by:
Helpers
Includes:
Core::Module
Defined in:
lib/mimi/db.rb,
lib/mimi/db/lock.rb,
lib/mimi/db/model.rb,
lib/mimi/db/dictate.rb,
lib/mimi/db/helpers.rb,
lib/mimi/db/version.rb,
lib/mimi/db/extensions.rb,
lib/mimi/db/dictate/dsl.rb,
lib/mimi/db/foreign_key.rb,
lib/mimi/db/lock/mysql_lock.rb,
lib/mimi/db/dictate/explorer.rb,
lib/mimi/db/dictate/migrator.rb,
lib/mimi/db/lock/sqlite_lock.rb,
lib/mimi/db/dictate/schema_diff.rb,
lib/mimi/db/lock/postgresql_lock.rb,
lib/mimi/db/dictate/type_defaults.rb,
lib/mimi/db/dictate/schema_definition.rb

Defined Under Namespace

Modules: Dictate, Extensions, ForeignKey, Helpers, Lock Classes: Model

Constant Summary collapse

VERSION =
'0.3.4'.freeze

Class Method Summary collapse

Methods included from Helpers

all_table_names, clear!, create!, create_if_not_exist!, database_exist?, db_table_names, diff_schema, drop!, execute, model_table_names, models, transaction, update_schema!, with_log_level

Class Method Details

.configureObject



62
63
64
65
66
67
# File 'lib/mimi/db.rb', line 62

def self.configure(*)
  super
  if Mimi.const_defined?(:Application)
    @logger = Mimi::Application.logger
  end
end

.connectionSequel::<...>::Database

Returns active DB connection

Returns:



77
78
79
# File 'lib/mimi/db.rb', line 77

def self.connection
  @connection
end

.loggerObject



69
70
71
# File 'lib/mimi/db.rb', line 69

def self.logger
  @logger ||= Mimi::Logger.new
end

.module_manifestObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/mimi/db.rb', line 25

def self.module_manifest
  {
    db_adapter: {
      desc: 'Database adapter ("sqlite3", "postgresql", "mysql" etc)',
      default: 'sqlite3'
    },
    db_database: {
      desc: 'Database name (e.g. "tmp/mydb")',
      # required
    },
    db_host: {
      desc: 'Database host',
      default: nil
    },
    db_port: {
      desc: 'Database port',
      default: nil
    },
    db_username: {
      desc: 'Database username',
      default: nil
    },
    db_password: {
      desc: 'Database password',
      default: nil
    },
    db_pool: {
      desc: 'Database connection pool size',
      default: 15
    },
    db_log_level: {
      desc: 'Logging level for database layer ("debug", "info" etc)',
      default: 'debug'
    }
  }
end

.module_pathObject



21
22
23
# File 'lib/mimi/db.rb', line 21

def self.module_path
  Pathname.new(__dir__).join('..').join('..').expand_path
end

.sequel_configHash

Returns Sequel connection parameters

Returns:

  • (Hash)


113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/mimi/db.rb', line 113

def self.sequel_config
  {
    adapter: sequel_config_canonical_adapter_name(module_options[:db_adapter]),
    database: module_options[:db_database],
    host: module_options[:db_host],
    port: module_options[:db_port],
    user: module_options[:db_username],
    password: module_options[:db_password],
    encoding: module_options[:db_encoding],
    max_connections: module_options[:db_pool],
    sql_log_level: module_options[:db_log_level],
    logger: logger
  }
end

.sequel_config_canonical_adapter_name(adapter_name) ⇒ String

Returns a standard Sequel adapter name converted from any variation of adapter names.

Examples:

sequel_config_canonical_adapter_name(:sqlite3) # => 'sqlite'

Parameters:

  • adapter_name (String, Symbol)

Returns:

  • (String)


96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/mimi/db.rb', line 96

def self.sequel_config_canonical_adapter_name(adapter_name)
  case adapter_name.to_s.downcase
  when 'sqlite', 'sqlite3'
    'sqlite'
  when 'postgres', 'postgresql'
    'postgres'
  when 'cockroach', 'cockroachdb'
    'cockroachdb'
  else
    adapter_name.to_s.downcase
  end
end

.startObject



81
82
83
84
85
86
# File 'lib/mimi/db.rb', line 81

def self.start
  Mimi::DB::Extensions.start
  @connection = Sequel.connect(sequel_config)
  Mimi.require_files(module_options[:require_files]) if module_options[:require_files]
  super
end