Skip to content

Commit

Permalink
move seed script to ruby, make it more responsive to db changes (#821)
Browse files Browse the repository at this point in the history
Previous version of this script did not handle engine migrations and
would get the wrong count pretty easily.
  • Loading branch information
ShanaLMoore authored Sep 23, 2024
2 parents 063cde0 + a26da4e commit 8b9a9e0
Showing 1 changed file with 67 additions and 17 deletions.
84 changes: 67 additions & 17 deletions bin/db-migrate-seed.sh
Original file line number Diff line number Diff line change
@@ -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'

0 comments on commit 8b9a9e0

Please sign in to comment.