Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Aug 18, 2025

This PR adds support for creating MySQL backend instances with an existing *sql.DB connection, addressing the need for better database connection management and integration with existing applications.

Changes

Added a new constructor NewMysqlBackendWithDB(db *sql.DB, opts ...option) that allows users to provide their own configured database connection instead of having the backend create one internally.

Key Benefits

  • Better connection pool control: Users can configure MaxOpenConns, MaxIdleConns, ConnMaxLifetime, etc.
  • Shared connections: Database connections can be shared across multiple components
  • Advanced configurations: Support for connection parameters not easily expressed through the original DSN-based constructor
  • Integration flexibility: Easier integration with existing database management strategies

Usage Examples

Original constructor (unchanged):

backend := mysql.NewMysqlBackend("localhost", 3306, "user", "password", "dbname")

New constructor:

dsn := "user:password@tcp(localhost:3306)/dbname?parseTime=true&multiStatements=true"
db, err := sql.Open("mysql", dsn)
db.SetMaxOpenConns(25)
db.SetMaxIdleConns(10)
db.SetConnMaxLifetime(time.Hour)

backend := mysql.NewMysqlBackendWithDB(db)

Implementation Details

  • Refactored common logic into shared newMysqlBackend internal function
  • Updated migration logic to handle both DSN-based and existing DB scenarios
  • Maintained full backward compatibility with existing NewMysqlBackend
  • Added comprehensive test coverage including integration tests
  • Updated documentation with usage examples and important notes

Note: When using NewMysqlBackendWithDB, ensure your connection string includes multiStatements=true if you plan to use automatic migrations (enabled by default).

Fixes #412.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Accept existing db instance for mysql backend Add NewMysqlBackendWithDB constructor to accept existing database connections Aug 18, 2025
Copilot AI requested a review from cschleiden August 18, 2025 18:32
@cschleiden cschleiden marked this pull request as ready for review August 19, 2025 03:03
Copilot AI and others added 2 commits August 20, 2025 21:00
Co-authored-by: cschleiden <2201819+cschleiden@users.noreply.github.com>
Co-authored-by: cschleiden <2201819+cschleiden@users.noreply.github.com>
@cschleiden
Copy link
Owner

I want to support this, but need to think a bit more about the migrations logic. Not run those automatically when passing in a DB connection? Probably better to have more control here

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