Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refator database configuration for generators
- Extract common behaviour between generators (app, container) - Add unit specs covering database config logic - Add multi-platform support (mri, jruby) - Fix CLI specs setup
- Loading branch information
1 parent
e23cb3c
commit 0dd9559
Showing
7 changed files
with
207 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
module Lotus | ||
module Generators | ||
class DatabaseConfig | ||
SUPPORTED_ENGINES = { | ||
'mysql' => { type: :sql, mri: 'mysql', jruby: 'jdbc-mysql' }, | ||
'mysql2' => { type: :sql, mri: 'mysql2', jruby: 'jdbc-mysql' }, | ||
'postgresql' => { type: :sql, mri: 'pg', jruby: 'jdbc-postgres' }, | ||
'postgres' => { type: :sql, mri: 'pg', jruby: 'jdbc-postgres' }, | ||
'sqlite' => { type: :sql, mri: 'sqlite3', jruby: 'jdbc-sqlite3' }, | ||
'sqlite3' => { type: :sql, mri: 'sqlite3', jruby: 'jdbc-sqlite3' }, | ||
'filesystem' => { type: :file_system, mri: nil, jruby: nil }, | ||
'memory' => { type: :memory, mri: nil, jruby: nil } | ||
}.freeze | ||
|
||
attr_reader :engine, :name | ||
|
||
def initialize(engine, name) | ||
@engine = engine | ||
@name = name | ||
|
||
SUPPORTED_ENGINES.key?(engine) or fail "\"#{ engine }\" is not a valid database type" | ||
end | ||
|
||
def to_hash | ||
{ | ||
gem: gem, | ||
uri: uri, | ||
type: type | ||
} | ||
end | ||
|
||
def type | ||
SUPPORTED_ENGINES[engine][:type] | ||
end | ||
|
||
def sql? | ||
type == :sql | ||
end | ||
|
||
private | ||
|
||
def platform | ||
Lotus::Utils.jruby? ? :jruby : :mri | ||
end | ||
|
||
def platform_prefix | ||
:jdbc if Lotus::Utils.jruby? | ||
end | ||
|
||
def uri | ||
{ | ||
development: environment_uri(:development), | ||
test: environment_uri(:test) | ||
} | ||
end | ||
|
||
def gem | ||
SUPPORTED_ENGINES[engine][platform] | ||
end | ||
|
||
def base_uri | ||
case engine | ||
when 'mysql' | ||
"mysql://localhost/#{ name }" | ||
when 'mysql2' | ||
"mysql2://localhost/#{ name }" | ||
when 'postgresql', 'postgres' | ||
"postgres://localhost/#{ name }" | ||
when 'sqlite', 'sqlite3' | ||
"sqlite://db/#{ Shellwords.escape(name) }" | ||
when 'memory' | ||
"memory://localhost/#{ name }" | ||
when 'filesystem' | ||
"file:///db/#{ Shellwords.escape(name) }" | ||
end | ||
end | ||
|
||
def environment_uri(environment) | ||
case engine | ||
when 'sqlite', 'sqlite3' | ||
"#{ platform_prefix }#{ base_uri }_#{ environment }.sqlite" | ||
else | ||
"#{ platform_prefix }#{ base_uri }_#{ environment }" | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.