From 2e25d4f292f0b6da0c63daf82fa947201cd07639 Mon Sep 17 00:00:00 2001 From: Shana Moore Date: Fri, 20 Sep 2024 08:13:01 -0700 Subject: [PATCH 1/2] Update hyrax-webapp --- hyrax-webapp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hyrax-webapp b/hyrax-webapp index adedae5..b193a39 160000 --- a/hyrax-webapp +++ b/hyrax-webapp @@ -1 +1 @@ -Subproject commit adedae582f3123cf1044b9b5272fd1ffdb49c69e +Subproject commit b193a393d7ec971bf55dff73910f805912024667 From a26da4e59823d1c15342e278d614d419894b88d7 Mon Sep 17 00:00:00 2001 From: Rob Kaufman Date: Fri, 20 Sep 2024 11:17:33 -0700 Subject: [PATCH 2/2] move seed script to ruby, make it more responsive to db changes --- bin/db-migrate-seed.sh | 84 +++++++++++++++++++++++++++++++++--------- 1 file changed, 67 insertions(+), 17 deletions(-) diff --git a/bin/db-migrate-seed.sh b/bin/db-migrate-seed.sh index 32684ff..e0ce684 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'