From 297d9bb044fb927ee9762fb9991d39c252ec17df Mon Sep 17 00:00:00 2001 From: Ryan Lue Date: Fri, 4 May 2018 13:40:42 +0800 Subject: [PATCH] Clean up database.yml files --- config/database.yml.test-mysql | 11 ----- config/database.yml.test-postgresql | 11 ----- contrib/database.yml | 30 +++++++++++++ {config => contrib}/database.yml.pkgr | 0 lib/tasks/bootstrap.rake | 63 ++++++++++++++++++--------- 5 files changed, 73 insertions(+), 42 deletions(-) delete mode 100644 config/database.yml.test-mysql delete mode 100644 config/database.yml.test-postgresql create mode 100644 contrib/database.yml rename {config => contrib}/database.yml.pkgr (100%) diff --git a/config/database.yml.test-mysql b/config/database.yml.test-mysql deleted file mode 100644 index f6b525b4c..000000000 --- a/config/database.yml.test-mysql +++ /dev/null @@ -1,11 +0,0 @@ -# Warning: The database defined as "test" will be erased and -# re-generated from your development database when you run "rake". -# Do not set this db to the same as development or production. -test: - adapter: mysql2 - database: zammad_test - pool: 50 - timeout: 5000 - encoding: utf8 - username: some_user - password: some_pass diff --git a/config/database.yml.test-postgresql b/config/database.yml.test-postgresql deleted file mode 100644 index bda094235..000000000 --- a/config/database.yml.test-postgresql +++ /dev/null @@ -1,11 +0,0 @@ -# Warning: The database defined as "test" will be erased and -# re-generated from your development database when you run "rake". -# Do not set this db to the same as development or production. -test: - adapter: postgresql - database: zammad_test - pool: 50 - timeout: 5000 - encoding: utf8 - username: postgres - password: diff --git a/contrib/database.yml b/contrib/database.yml new file mode 100644 index 000000000..c3c4a6d33 --- /dev/null +++ b/contrib/database.yml @@ -0,0 +1,30 @@ +default: &default + # For details on connection pooling, see Rails configuration guide + # http://guides.rubyonrails.org/configuring.html#database-pooling + pool: 50 + timeout: 5000 + encoding: utf8 + # postgresql ----------------------------------------------------------------- + adapter: postgresql + # username: + # password: + # mysql ---------------------------------------------------------------------- + # adapter: mysql2 + # host: localhost + # username: + # password: + +production: + <<: *default + database: zammad_prod + +development: + <<: *default + database: zammad_development + +# Warning: The database defined as "test" will be erased and +# re-generated from your development database when you run "rake". +# Do not set this db to the same as development or production. +test: + <<: *default + database: zammad_test diff --git a/config/database.yml.pkgr b/contrib/database.yml.pkgr similarity index 100% rename from config/database.yml.pkgr rename to contrib/database.yml.pkgr diff --git a/lib/tasks/bootstrap.rake b/lib/tasks/bootstrap.rake index 7a6487152..41e63d6c9 100644 --- a/lib/tasks/bootstrap.rake +++ b/lib/tasks/bootstrap.rake @@ -1,31 +1,54 @@ +module BootstrapRakeHelper + APP_CACHE = Dir.glob(Rails.root.join('tmp', 'cache*')) + SERVER_LOG = Rails.root.join('log', "#{Rails.env}.log") + AUTO_WIZARD = { source: Rails.root.join('contrib', 'auto_wizard_test.json'), + dest: Rails.root.join('auto_wizard.json') }.freeze + DB_CONFIG = { source: Rails.root.join('contrib', 'database.yml'), + dest: Rails.root.join('config', 'database.yml') }.freeze + + def flush_cache_and_logs + FileUtils.rm_rf(APP_CACHE) + File.write(SERVER_LOG, '') + end + + def run_auto_wizard + FileUtils.ln(AUTO_WIZARD[:source], AUTO_WIZARD[:dest], force: true) + AutoWizard.setup + + # set system init to done + UserInfo.current_user_id = 1 + Setting.set('system_init_done', true) + end + + def add_database_config + raise Errno::ENOENT, 'contrib/database.yml not found' unless File.exist?(DB_CONFIG[:source]) + + if File.exist?(DB_CONFIG[:dest]) + return if FileUtils.identical?(DB_CONFIG[:source], DB_CONFIG[:dest]) + printf 'config/database.yml: File exists. Overwrite? [Y/n] ' + return if STDIN.gets.chomp.downcase == 'n' + end + + FileUtils.cp(DB_CONFIG[:source], DB_CONFIG[:dest]) + end +end + namespace :bs do desc 'Bootstrap the application' - task :init => %i[db:create db:migrate db:seed] do + task :init => %i[db_config db:create db:migrate db:seed] do + include BootstrapRakeHelper run_auto_wizard end desc 'Reset the application to its initial state' task :reset => %i[db:reset] do + include BootstrapRakeHelper run_auto_wizard flush_cache_and_logs end -end - -APP_CACHE = Dir.glob(Rails.root.join('tmp', 'cache*')) -SERVER_LOG = Rails.root.join('log', "#{Rails.env}.log") -AUTO_WIZARD = { source: Rails.root.join('contrib', 'auto_wizard_test.json'), - dest: Rails.root.join('auto_wizard.json') }.freeze - -def flush_cache_and_logs - FileUtils.rm_rf(APP_CACHE) - File.write(SERVER_LOG, '') -end - -def run_auto_wizard - FileUtils.ln(AUTO_WIZARD[:source], AUTO_WIZARD[:dest], force: true) - AutoWizard.setup - - # set system init to done - UserInfo.current_user_id = 1 - Setting.set('system_init_done', true) + + task :db_config do + include BootstrapRakeHelper + add_database_config + end end