Skip to content

Conversation

@cschleiden
Copy link
Owner

Fixes #412

Allows passing an existing *sql.DB connection to MySQL, Postgres, and SQLite backends instead of requiring connection parameters.

Changes

  • Add NewMysqlBackendWithDB, NewPostgresBackendWithDB, NewSqliteBackendWithDB
  • Add ownsConnection field to prevent closing user-provided connections
  • MySQL: Add WithMigrationDSN option for migrations with external connections
  • Postgres/SQLite: Migrations work directly with passed connection

Behavior when using *WithDB constructors

  • Migrations are disabled by default
  • Close() will not close the database connection
  • MySQL requires WithMigrationDSN if ApplyMigrations is enabled (due to multi-statement requirement)

Implements #412 - allows passing an existing *sql.DB connection to
MySQL, Postgres, and SQLite backends instead of requiring connection
parameters.

Changes:
- Add NewMysqlBackendWithDB, NewPostgresBackendWithDB, NewSqliteBackendWithDB
- Add ownsConnection field to prevent closing user-provided connections
- MySQL: Add WithMigrationDSN option for migrations with external connections
- Postgres/SQLite: Migrations work directly with passed connection

When using *WithDB constructors:
- Migrations are disabled by default
- Close() will not close the database connection
- MySQL requires WithMigrationDSN if ApplyMigrations is enabled
Tests cover:
- Using provided connection (ownsConnection=false)
- Close() not closing user-provided connections
- Migrations disabled by default
- Migrations can be enabled
- MySQL: migration fails without MigrationDSN
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.

Accept existing db instance for mysql backend

2 participants