Skip to content

Commit

Permalink
Fix schema migration loading from temporary working dir in dev_dump (t…
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorbg authored Sep 11, 2023
1 parent 74543b3 commit 17634a7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
16 changes: 16 additions & 0 deletions WcaOnRails/lib/db_dump_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ module DbDumpHelper
def self.dump_developer_db
Dir.mktmpdir do |dir|
FileUtils.cd dir do
# WARNING: Headache ahead! By using Rails-DSL database schema files, the migrations in the dev export can break.
# Rails uses the timestamp at the top of the schema file to determine which migration is the latest one.
# It then proceeds to glob the migration folder for older migrations and inserts them when loading the schema.
# However, this glob is _relative_ to the Rails root. Due to our chdir into a temporary directory (where we can
# write files to our heart's desire) the glob returns an empty list. So we symlink the migrations into our tmp
# working directory to make sure that Rails can find them when loading/dumping the schema.
primary_connection = ActiveRecord::Base.connection
migration_paths = primary_connection.migration_context.migrations_paths

migration_paths.each do |migration_path|
FileUtils.mkpath(File.dirname(migration_path))

abs_migrations = File.join(Rails.application.root, migration_path)
FileUtils.ln_s abs_migrations, migration_path, verbose: true
end

DatabaseDumper.development_dump(DEVELOPER_EXPORT_SQL)

self.zip_and_permalink(DEVELOPER_EXPORT_FOLDER, DEVELOPER_EXPORT_SQL_PERMALINK, nil, DEVELOPER_EXPORT_SQL)
Expand Down
4 changes: 3 additions & 1 deletion WcaOnRails/lib/tasks/db.rake
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ namespace :db do
LogTask.log_task "Clobbering contents of '#{config.database}' with #{DbDumpHelper::DEVELOPER_EXPORT_SQL}" do
ActiveRecord::Tasks::DatabaseTasks.drop config
ActiveRecord::Tasks::DatabaseTasks.create config
ActiveRecord::Tasks::DatabaseTasks.load_schema config

# Explicitly loading the schema is not necessary because the downloaded SQL dump file contains CREATE TABLE
# definitions, so if we load the schema here the SOURCE command below would overwrite it anyways

DatabaseDumper.mysql("SOURCE #{DbDumpHelper::DEVELOPER_EXPORT_SQL}", config.database)
end
Expand Down

0 comments on commit 17634a7

Please sign in to comment.