Skip to content
This repository has been archived by the owner on Dec 31, 2022. It is now read-only.

Commit

Permalink
fix(db) Allow RDS + null databases to coexist (#125)
Browse files Browse the repository at this point in the history
Fixes #122
  • Loading branch information
nickmarden authored and ajgon committed Oct 23, 2017
1 parent 7aeb78e commit 7a9a821
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 39 deletions.
3 changes: 2 additions & 1 deletion libraries/drivers_db_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ def self.detect_engine(app, node, options)
db_driver.allowed_engines.include?(
options.try(:[], :rds).try(:[], 'engine') ||
node.try(:[], 'deploy').try(:[], app['shortname']).try(:[], db_driver.driver_type).try(:[], 'adapter') ||
node.try(:[], 'defaults').try(:[], db_driver.driver_type).try(:[], 'adapter')
node.try(:[], 'defaults').try(:[], db_driver.driver_type).try(:[], 'adapter') ||
'sqlite'
)
end
end
Expand Down
48 changes: 48 additions & 0 deletions libraries/drivers_db_sqlite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ class Sqlite < Base
allowed_engines :sqlite, :sqlite3
packages debian: 'libsqlite3-dev', rhel: 'sqlite-devel'

def deploy_before_symlink
link_sqlite_database
end

def deploy_before_migrate
link_sqlite_database
end

def validate_node_engine
populate_node_engine unless node_engine
:node_engine
end

def out
output = super
output[:database] ||= 'db/data.sqlite3'
Expand All @@ -16,6 +29,41 @@ def out
def url(deploy_dir)
"sqlite://#{deploy_dir}/shared/#{out[:database]}"
end

private

# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
def link_sqlite_database
deploy_to = deploy_dir(app)
relative_db_path = out[:database]
shared_directory_path = File.join(deploy_to, 'shared', relative_db_path.sub(%r{/[^/]+\.sqlite3?$}, ''))
release_path = Dir[File.join(deploy_to, 'releases', '*')].last

context.directory shared_directory_path do
recursive true
not_if { ::File.exist?(shared_directory_path) }
end

db_path = File.join(deploy_to, 'shared', relative_db_path)
context.file db_path do
action :create
not_if { ::File.exist?(File.join(deploy_to, 'shared', relative_db_path)) }
end

context.link File.join(release_path, relative_db_path) do
to db_path
not_if { ::File.exist?(::File.join(release_path, relative_db_path)) }
end
end
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize

def populate_node_engine
framework = Drivers::Framework::Factory.build(context, app)
deploy = context.node['deploy']
deploy[app['shortname']][driver_type] ||= {}
deploy[app['shortname']][driver_type][:database] = "db/#{app['shortname']}_#{framework.deploy_env}.sqlite"
context.node['deploy'] = deploy
end
end
end
end
40 changes: 3 additions & 37 deletions libraries/drivers_framework_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,6 @@ def configure
configure_logrotate
end

def deploy_before_migrate
link_sqlite_database
end

def deploy_before_symlink
link_sqlite_database unless migrate?
end

def deploy_before_restart
assets_precompile if out[:assets_precompile]
end
Expand All @@ -49,40 +41,14 @@ def assets_precompile
end
end

# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
def link_sqlite_database
return unless database_url.start_with?('sqlite')
deploy_to = deploy_dir(app)
db_path = database_url.sub('sqlite://', '')
relative_db_path = db_path.sub(deploy_to, '').sub(%r{^/+shared/+}, '')
release_path = Dir[File.join(deploy_to, 'releases', '*')].last
shared_directory_path = File.join(deploy_to, 'shared', relative_db_path.sub(%r{/[^/]+\.sqlite3?$}, ''))

context.directory shared_directory_path do
recursive true
not_if { ::File.exist?(shared_directory_path) }
end

context.file File.join(deploy_to, 'shared', relative_db_path) do
action :create
not_if { ::File.exist?(File.join(deploy_to, 'shared', relative_db_path)) }
end

context.link File.join(release_path, relative_db_path) do
to db_path
not_if { ::File.exist?(::File.join(release_path, relative_db_path)) }
end
end
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize

def database_url
deploy_to = deploy_dir(app)
applicable_databases.first.try(:url, deploy_to) ||
"sqlite://#{deploy_to}/shared/db/#{app['shortname']}_#{deploy_env}.sqlite"
applicable_databases.first.try(:url, deploy_to)
end

def applicable_databases
Array.wrap(options[:databases]).select(&:applicable_for_configuration?)
dbs = options[:databases] || Drivers::Db::Factory.build(context, app)
Array.wrap(dbs).select(&:applicable_for_configuration?)
end

def environment
Expand Down
2 changes: 1 addition & 1 deletion libraries/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def every_enabled_application
end

def every_enabled_rds(context, application)
data = rdses.presence || [Drivers::Db::Factory.build(context, application)]
data = [rdses.presence, Drivers::Db::Factory.build(context, application)].flatten.compact
data.each do |rds|
yield rds
end
Expand Down

0 comments on commit 7a9a821

Please sign in to comment.