diff --git a/libraries/drivers_db_factory.rb b/libraries/drivers_db_factory.rb index 9cc4a4f2..bf34e6d3 100644 --- a/libraries/drivers_db_factory.rb +++ b/libraries/drivers_db_factory.rb @@ -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 diff --git a/libraries/drivers_db_sqlite.rb b/libraries/drivers_db_sqlite.rb index cf14627b..568d6642 100644 --- a/libraries/drivers_db_sqlite.rb +++ b/libraries/drivers_db_sqlite.rb @@ -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' @@ -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 diff --git a/libraries/drivers_framework_base.rb b/libraries/drivers_framework_base.rb index 6e1e4a25..20615737 100644 --- a/libraries/drivers_framework_base.rb +++ b/libraries/drivers_framework_base.rb @@ -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 @@ -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 diff --git a/libraries/helpers.rb b/libraries/helpers.rb index 743ab5ed..d57f2274 100644 --- a/libraries/helpers.rb +++ b/libraries/helpers.rb @@ -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