diff --git a/app/controllers/catalog_controller_decorator.rb b/app/controllers/catalog_controller_decorator.rb index 3ef21828..3bf180a4 100644 --- a/app/controllers/catalog_controller_decorator.rb +++ b/app/controllers/catalog_controller_decorator.rb @@ -119,8 +119,8 @@ def self.uploaded_field # TODO: replace CatalogController.creator_field to return 'creator_ssi' config.add_sort_field "creator_ssi asc", label: "Author" # TODO: replace CatalogController.created_field to return 'created_ssi' - config.add_sort_field "created_ssi asc", label: "Published Date (Ascending)" - config.add_sort_field "created_ssi desc", label: "Published Date (Descending)" + config.add_sort_field "#{CatalogController.created_field} asc", label: "Published Date (Ascending)" + config.add_sort_field "#{CatalogController.created_field} desc", label: "Published Date (Descending)" config.add_sort_field "#{CatalogController.modified_field} asc", label: "Upload Date (Ascending)" config.add_sort_field "#{CatalogController.modified_field} desc", label: "Upload Date (Descending)" end diff --git a/bin/db-migrate-seed.sh b/bin/db-migrate-seed.sh index 32684ff8..e0ce6843 100755 --- a/bin/db-migrate-seed.sh +++ b/bin/db-migrate-seed.sh @@ -1,17 +1,67 @@ -#!/bin/sh -set -e - -db-wait.sh "$DB_HOST:$DB_PORT" -if [ "$FCREPO_HOST" ]; then - db-wait.sh "$FCREPO_HOST:$FCREPO_PORT" -fi -db-wait.sh "$SOLR_HOST:$SOLR_PORT" - -migrations_run=`PGPASSWORD=$DATABASE_PASSWORD psql -h $DATABASE_HOST -U $DATABASE_USER $DATABASE_NAME -t -c "SELECT version FROM schema_migrations ORDER BY schema_migrations" | wc -c` -migrations_fs=`ls -l db/migrate/ | awk '{print $9}' | grep -o '[0-9]\+' | wc -c` -echo "relation 'schema migrations' does not exist will be printed when the db hasn't been created yet." -if [[ "$migrations_run" -lt "$migrations_fs" ]]; then - bundle exec rails db:create - bundle exec rails db:migrate - bundle exec rails db:seed -fi +#!/usr/bin/env ruby + +require 'open3' +require 'bundler' + +def db_wait(address) + system("db-wait.sh #{address}") +end + +def run_command(command) + stdout, stderr, status = Open3.capture3(command) + raise stderr unless status.success? + stdout +end + +def migrations_list(query) + result = run_command(query) + result.split("\n").map(&:strip).reject(&:empty?) +end + +def bundled_migrations + migration_list = Bundler.load.specs.inject([]) do |arr, spec| + if File.exist?("#{spec.full_gem_path}/lib/*/engine.rb") + migrations = Dir.glob("#{spec.full_gem_path}/db/migrate/*") + migrations.each do |migration_path| + arr.push(File.basename(migration_path).split('_').first) + end + end + arr + end + Dir.glob('db/migrate/*.rb').each do |migration_path| + migration_list.push(File.basename(migration_path).split('_').first) + end + migration_list +end + +begin + db_host = ENV['DB_HOST'] + db_port = ENV['DB_PORT'] + fcrepo_host = ENV['FCREPO_HOST'] + fcrepo_port = ENV['FCREPO_PORT'] + solr_host = ENV['SOLR_HOST'] + solr_port = ENV['SOLR_PORT'] + db_user = ENV['DB_USER'] + db_name = ENV['DB_NAME'] + db_password = ENV['DB_PASSWORD'] + + db_wait("#{db_host}:#{db_port}") + db_wait("#{fcrepo_host}:#{fcrepo_port}") if fcrepo_host + db_wait("#{solr_host}:#{solr_port}") + + migrations_run_query = "PGPASSWORD=#{db_password} psql -h #{db_host} -U #{db_user} #{db_name} -t -c \"SELECT version FROM schema_migrations ORDER BY schema_migrations\"" + migrations_run = migrations_list(migrations_run_query) + + migrations_fs = bundled_migrations + + if (migrations_fs - migrations_run).size > 0 + run_command('bundle exec rails db:create') + run_command('bundle exec rails db:migrate') + run_command('bundle exec rails db:seed') + end +rescue => e + puts "An error occurred: #{e.message}" + exit 1 +end + +puts 'all migrations have been run'