Skip to content

Patch ActiveRecord::ConnectionNotEstablished snag#3

Merged
jbaxendale-ut merged 5 commits into
mainfrom
fix/db-connection
Apr 17, 2026
Merged

Patch ActiveRecord::ConnectionNotEstablished snag#3
jbaxendale-ut merged 5 commits into
mainfrom
fix/db-connection

Conversation

@jbaxendale-ut
Copy link
Copy Markdown

Updated data_migrate’s db:prepare:with_data initialization check so it inspects the schema_migrations table through the temporary pool connection for the database currently being prepared.

This avoids the nested connection checkout path that can raise ActiveRecord::ConnectionNotEstablished on newer Rails immediately after a database is created.

We needed this because db:prepare was working, but db:prepare:with_data was failing during the “is this database already initialized?” check.

The fix keeps the existing Rails-compatible behavior intact: missing databases are still created, and databases that already exist but have not been initialized still have schema restored before migrations run.

Updated data_migrate’s db:prepare:with_data initialization check so it inspects the schema_migrations table through the temporary pool connection for the database currently being prepared.

This avoids the nested connection checkout path that can raise ActiveRecord::ConnectionNotEstablished on newer Rails immediately after a database is created.

We needed this because db:prepare was working, but db:prepare:with_data was failing during the “is this database already initialized?” check.

The fix keeps the existing Rails-compatible behavior intact: missing databases are still created, and databases that already exist but have not been initialized still have schema restored before migrations run.
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates DataMigrate::DatabaseTasks.database_initialized? to check for the schema migrations table using the temporary pool’s checked-out connection (instead of implicitly using the global ActiveRecord connection), avoiding ActiveRecord::ConnectionNotEstablished during db:prepare:with_data right after database creation on newer Rails.

Changes:

  • Switch initialization detection to pool.with_connection { |conn| conn.data_source_exists?(schema_migration.table_name) }.
  • Add specs asserting initialization checks are performed via the temporary pool connection and correctly handle missing schema_migrations.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
lib/data_migrate/database_tasks.rb Fixes initialization check to query schema_migrations via the temporary pool’s connection to avoid nested checkout/connection establishment issues.
spec/data_migrate/database_tasks_spec.rb Adds focused tests for the new database_initialized? behavior using a temporary pool + connection double.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@jbaxendale-ut
Copy link
Copy Markdown
Author

What changed:

  • 8b88eb9 tightened the earlier patch shape.
  • 2b68131 changed database_initialized? to use Rails’ own
    ActiveRecord::Tasks::DatabaseTasks.migration_connection_pool.schema_migration.table_exists? on newer Rails, instead
    of checking through the temporary pool.
  • 928b15f added a version-safe fallback for older Rails that do not implement migration_connection_pool, using the
    pre-existing DataMigrate::RailsHelper.schema_migration.table_exists?.
  • 8c0a2f8 removed the now-unused pool argument from database_initialized? and cleaned up the specs accordingly.

Why we needed it:

  • The original problem was specific to db:prepare:with_data on newer Rails: after creating a database, the temporary-
    pool-based initialization check could hit a stale connection path and raise ActiveRecord::ConnectionNotEstablished.
  • Mirroring Rails’ own db:prepare initialization check fixed that on newer Rails.
  • That Rails API does not exist on 6.1, 7.0, or 7.1, so the fallback was necessary to keep older versions working.
  • The “created but not initialized database” behavior was intentionally preserved the whole way: if the database
    exists but lacks schema_migrations, schema still gets loaded before migrations run.

What did not change:

  • We did not change the overall prepare_all_with_data control flow.
  • We did not change create/retry semantics for missing databases.
  • We did not broaden the patch into schema loading, migration ordering, or seed behavior.

@jbaxendale-ut jbaxendale-ut merged commit 21dd485 into main Apr 17, 2026
42 checks passed
@jbaxendale-ut jbaxendale-ut deleted the fix/db-connection branch April 17, 2026 16:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants