Skip to content

Remove ContextCreator from Database interface for compatibility with external SQL packages - #7

Merged
ziflex merged 2 commits into
masterfrom
copilot/fix-09cb999e-9ceb-4bcc-910a-bb876fc6ee23
Aug 31, 2025
Merged

Remove ContextCreator from Database interface for compatibility with external SQL packages#7
ziflex merged 2 commits into
masterfrom
copilot/fix-09cb999e-9ceb-4bcc-910a-bb876fc6ee23

Conversation

Copilot AI commented Aug 31, 2025

Copy link
Copy Markdown
Contributor

The Database interface previously embedded ContextCreator, making it incompatible with external SQL packages like sqlx that don't implement a Context(context.Context) Context method. This prevented users from using dbx with existing database abstractions.

Problem

External packages like sqlx provide their own database abstractions that implement the core SQL operations but don't have dbx-specific methods like Context(). This made it impossible to use them with dbx's transaction system:

// This would fail to compile before this change
type SqlxDB struct { *sqlx.DB }
var _ dbx.Database = &SqlxDB{} // Error: missing Context method

Solution

This PR removes ContextCreator from the core Database interface while maintaining full backward compatibility:

Core Changes

  1. Minimal Database Interface: No longer embeds ContextCreator, making it compatible with any SQL package
  2. DatabaseWithContext: New optional interface for databases that support context creation
  3. Enhanced Helper Functions: NewContextFrom and NewDatabaseContext work with any Database implementation
  4. Preserved Functionality: All existing dbx features continue to work unchanged

Usage Examples

External Package Compatibility (now works):

type ExternalDB struct { *sql.DB }
// Implements all Database methods...
var _ dbx.Database = &ExternalDB{} // ✅ Compiles successfully

// Can be used in transactions
err := dbx.Transaction(ctx, externalDB, func(txCtx dbx.Context) error {
    return txCtx.Executor().Exec("INSERT INTO users (name) VALUES (?)", "John")
})

Existing dbx Usage (unchanged):

dbxDB := dbx.New(sqlDB)           // Returns DatabaseWithContext
ctx := dbxDB.Context(background)  // ✅ Still works

Flexible Context Creation:

// Works with any Database
dbCtx := dbx.NewDatabaseContext(ctx, anyDatabase)
dbCtx := dbx.NewContextFrom(ctx, anyDatabase)

Backward Compatibility

All existing code continues to work without modification. The New() function returns DatabaseWithContext (which embeds both Database and ContextCreator), so existing users retain the Context() method while new users can implement just the minimal Database interface.

Testing

  • All existing tests pass without modification
  • Added compatibility verification for external database implementations
  • Transaction system works seamlessly with both dbx and external databases

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: ziflex <1607148+ziflex@users.noreply.github.com>
Copilot AI changed the title [WIP] Remove ContextCreator from Database interface in order to make it compatible with other sql packages like sqlx. Remove ContextCreator from Database interface for compatibility with external SQL packages Aug 31, 2025
Copilot AI requested a review from ziflex August 31, 2025 16:42
@ziflex
ziflex marked this pull request as ready for review August 31, 2025 16:51
@ziflex
ziflex merged commit 1eaade6 into master Aug 31, 2025
8 checks passed
@ziflex
ziflex deleted the copilot/fix-09cb999e-9ceb-4bcc-910a-bb876fc6ee23 branch August 31, 2025 16:59
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.

2 participants