diff --git a/bin/setup b/bin/setup index b3e34c143..43e31e3cd 100755 --- a/bin/setup +++ b/bin/setup @@ -1,16 +1,22 @@ #!/usr/bin/env ruby require 'fileutils' -include FileUtils # path to your application root. APP_ROOT = File.expand_path('..', __dir__) +DB = if ENV['DATABASE_URL'].to_s.empty? + 'PracticalDeveloper_development' + else + ENV['DATABASE_URL'] + end + def system!(*args) system(*args) || abort("\n== Command #{args} failed ==") end -chdir APP_ROOT do - # This script is a starting point to setup your application. +FileUtils.chdir APP_ROOT do + # This script is a way to set up or update your development environment automatically. + # This script is idempotent, so that you can run it at anytime and get an expectable outcome. # Add necessary setup steps to this file. puts '== Installing dependencies ==' @@ -24,16 +30,22 @@ chdir APP_ROOT do puts "\n== Copying sample files ==" unless File.exist?('config/database.yml') - cp 'config/database.yml.sample', 'config/database.yml' + FileUtils.cp 'config/database.yml.sample', 'config/database.yml' end puts "\n== Preparing database ==" - system! 'bin/rails db:setup' + command = "psql -o /dev/null -q -n -c 'select 1' #{DB}" + db_exists = system(command) + if db_exists + system! 'bin/rails db:migrate' + else + system! 'bin/rails db:setup' + end puts "\n== Preparing Elasticsearch ==" system! 'bin/rails search:setup' - puts "\n== Update Data ==" + puts "\n== Updating Data ==" system! 'bin/rails data_updates:run' puts "\n== Removing old logs and tempfiles ==" diff --git a/bin/update b/bin/update deleted file mode 100755 index 314b371f1..000000000 --- a/bin/update +++ /dev/null @@ -1,37 +0,0 @@ -#!/usr/bin/env ruby -require 'fileutils' -include FileUtils - -# path to your application root. -APP_ROOT = File.expand_path('..', __dir__) - -def system!(*args) - system(*args) || abort("\n== Command #{args} failed ==") -end - -chdir APP_ROOT do - # This script is a way to update your development environment automatically. - # Add necessary update steps to this file. - - puts '== Installing dependencies ==' - system! 'gem install bundler --conservative' - system('bundle check') || system!('bundle install') - - # Install JavaScript dependencies if using Yarn - system('bin/yarn') - - puts "\n== Updating database ==" - system! 'bin/rails db:migrate' - - puts "\n== Update Elasticsearch ==" - system! 'bin/rails search:setup' - - puts "\n== Update Data ==" - system! 'bin/rails data_updates:run' - - puts "\n== Removing old logs and tempfiles ==" - system! 'bin/rails log:clear tmp:clear' - - puts "\n== Restarting application server ==" - system! 'bin/rails restart' -end diff --git a/docs/backend/data-update-scripts.md b/docs/backend/data-update-scripts.md index b9d54ca23..130ef6b53 100644 --- a/docs/backend/data-update-scripts.md +++ b/docs/backend/data-update-scripts.md @@ -41,12 +41,10 @@ end ``` Once your script is in place then you can either run `rails data_updates:run` -manually or you can let our setup and update scripts handle it. In our local -[bin/setup](https://github.com/thepracticaldev/dev.to/blob/master/bin/setup#L36) -and -[bin/update](https://github.com/thepracticaldev/dev.to/blob/master/bin/update#L26) -scripts you will see we have added an additional task to "Update Data". This -kicks off the rake task `data_updates:run` for you. +manually or you can let our setup script handle it. In our local +[bin/setup](https://github.com/thepracticaldev/dev.to/blob/master/bin/setup) +script you will see we have added an additional task to update data. This kicks +off the rake task `data_updates:run` for you. The rake task itself will check the `lib/data_update_scripts` folder to see if there are any new scripts that need to be run. It does this by reading all of