Skip to content

feat(storage/sql): one file per SQL provider instead of switches in sql.go #266

Description

@Lutherwaves

Check Existing Issues

  • I have searched the existing issues and discussions.

Problem Description

Each SQL provider's specifics are spread across switch statements in storage/sql.go. Adding a provider means editing all of them, plus a new Lucene dialect file:

Function Provider-specific part
OpenConnection building the connection (DSN)
CreateSchema skipping schema creation on SQLite
CreateMigrationTable a different table definition for each provider
UpdateMigrationTable SQLite vs. everything else
GetLatestMigration SQLite vs. everything else

The default branches make it easy to cover a provider by accident. For example, CreateMigrationTable creates migrations on MySQL, while UpdateMigrationTable and GetLatestMigration use <schema>.migrations. Not verified against MySQL yet.

Desired Solution you'd like

Use the same pattern as storage/search/lucene/dialect.go: one private interface, one file per provider, one lookup table.

type sqlProvider interface {
	dialector(config map[string]string) (gorm.Dialector, error)
	migrationsTable(schema string) string
	migrationsDDL(schema string) string
	hasSchemas() bool
}

var sqlProviders = map[StorageProviders]sqlProvider{
	POSTGRESQL: postgresProvider{},
	MYSQL:      mysqlProvider{},
	SQLITE:     sqliteProvider{},
}
  • sql_postgres.go, sql_mysql.go, sql_sqlite.go hold the provider code. postgresDSN / mysqlDSN from fix(storage/sql): escape connection string values #265 move there as-is.
  • SQLAdapter looks up its provider once in OpenConnection, and the switches become method calls.
  • The "supported providers" error is built from the map keys.
  • Adding a provider means adding one file and one map entry.

Keep it private: an exported registry would be new API that nobody needs yet. Use an explicit map, not init() registration.

Additional Context

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:storageStorage adapters (SQL/Dynamo/Cosmos/Memory)enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions