Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable database encoding configuration #230

Merged
merged 1 commit into from
Oct 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions database.default.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Configurable by DSN or YAML Hash
#
# DSN (Data Source Name) examples
#
# sqlite3:
Expand All @@ -8,8 +10,30 @@
# postgres://user:pass@host/database
# heroku:
# <%= ENV['DATABASE_URL'] %>
#
# Hash example
#
# production:
# adapter: mysql
# database: database
# username: user
# password: pass
# host: host
# encoding: UTF-8-MB4
#
# which is equivalent to
#
# production:
# dsn: mysql://user:pass@host/database
#
production:
dsn: <%= ENV['DATABASE_URL'] %>
# adapter: <%= ENV['DATABASE_ADAPTER'] %>
# database: <%= ENV['DATABASE_NAME'] %>
# username: <%= ENV['DATABASE_USER'] %>
# password: <%= ENV['DATABASE_PASSWORD'] %>
# host: <%= ENV['DATABASE_HOST'] %>
# encoding: UTF-8-MB4
development:
dsn: sqlite3://<%= root %>/db/development.sqlite3
test:
Expand Down
14 changes: 13 additions & 1 deletion lib/lokka.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,20 @@ def admin_theme_dir
#
# @return [String] DSN (Data Source Name) is configuration for database.
def dsn
database_config['dsn']

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

end

##
# Data Source Hash
#
# @return [Hash] DSH (Data Source Hash) is configuration for database.
def dsh
database_config.dup.delete_if {|key, _| key == 'dsn' }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space between { and | missing.
Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

end

def database_config
filename = File.exist?("#{Lokka.root}/database.yml") ? 'database.yml' : 'database.default.yml'
YAML.load(ERB.new(File.read("#{Lokka.root}/#{filename}")).result(binding))[self.env]['dsn']
YAML.load(ERB.new(File.read("#{Lokka.root}/#{filename}")).result(binding))[self.env]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer using YAML.safe_load over YAML.load.
Line is too long. [90/80]
Redundant self detected.

end

##
Expand Down
7 changes: 6 additions & 1 deletion lib/lokka/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ class Database

def connect
DataMapper.finalize
DataMapper.setup(:default, Lokka.dsn)
config = if Lokka.dsn.present?
Lokka.dsn
else
Lokka.dsh
end
DataMapper.setup(:default, config)
self
end

Expand Down