From 3a43fb196dc1bd4fb3a8fedbdf415c09af6ea3d4 Mon Sep 17 00:00:00 2001 From: David Muir Sharnoff Date: Sat, 24 Dec 2022 14:11:54 -0800 Subject: [PATCH] rename Database.Name -> Database.DBName --- api.go | 12 ++++++------ apply.go | 18 +++++++++--------- lsmysql/check.go | 2 +- lsmysql/mysql.go | 9 ++++++--- lspostgres/postgres.go | 8 +++++--- lssinglestore/singlestore.go | 7 ++++--- 6 files changed, 31 insertions(+), 25 deletions(-) diff --git a/api.go b/api.go index a339b64..b2ee5f3 100644 --- a/api.go +++ b/api.go @@ -85,7 +85,7 @@ type Database struct { migrationIndex map[MigrationName]Migration errors []error db *sql.DB - Name string + DBName string driver Driver sequence []Migration // in order of execution status map[MigrationName]*MigrationStatus @@ -162,12 +162,12 @@ func New(ctx context.Context, options Options) *Schema { // NewDatabase creates a Database object. For Postgres and Mysql this is bundled into // lspostgres.New() and lsmysql.New(). -func (s *Schema) NewDatabase(log *internal.Log, name string, db *sql.DB, driver Driver) (*Database, error) { - if _, ok := s.databases[name]; ok { - return nil, errors.Errorf("Duplicate database '%s'", name) +func (s *Schema) NewDatabase(log *internal.Log, dbName string, db *sql.DB, driver Driver) (*Database, error) { + if _, ok := s.databases[dbName]; ok { + return nil, errors.Errorf("Duplicate database '%s'", dbName) } database := &Database{ - Name: name, + DBName: dbName, db: db, byLibrary: make(map[string][]Migration), migrationIndex: make(map[MigrationName]Migration), @@ -176,7 +176,7 @@ func (s *Schema) NewDatabase(log *internal.Log, name string, db *sql.DB, driver driver: driver, log: log, } - s.databases[name] = database + s.databases[dbName] = database s.databaseOrder = append(s.databaseOrder, database) return database, nil } diff --git a/apply.go b/apply.go index 1f8c8e9..1153d91 100644 --- a/apply.go +++ b/apply.go @@ -67,7 +67,7 @@ func (s *Schema) Migrate(ctx context.Context) (err error) { } }() if s.options.Overrides.ErrorIfMigrateNeeded && !d.done(s) { - return errors.Errorf("Migrations required for %s", d.Name) + return errors.Errorf("Migrations required for %s", d.DBName) } return d.migrate(ctx, s) }(d) @@ -108,7 +108,7 @@ func (d *Database) prepare(ctx context.Context) error { d.sequence[i] = m if d.Options.DebugLogging { d.log.Debug("Migration sequence", map[string]interface{}{ - "database": d.Name, + "database": d.DBName, "library": m.Base().Name.Library, "name": m.Base().Name.Name, }) @@ -163,7 +163,7 @@ func (d *Database) migrate(ctx context.Context, s *Schema) (err error) { if d.done(s) { d.log.Info("No migrations needed", map[string]interface{}{ - "database": d.Name, + "database": d.DBName, }) return nil } @@ -173,7 +173,7 @@ func (d *Database) migrate(ctx context.Context, s *Schema) (err error) { } d.log.Info("Starting migrations", map[string]interface{}{ - "database": d.Name, + "database": d.DBName, }) lastUnfishedSyncronous := d.lastUnfinishedSynchrnous() @@ -182,7 +182,7 @@ func (d *Database) migrate(ctx context.Context, s *Schema) (err error) { if m.Base().Status().Done { if d.Options.DebugLogging { d.log.Trace("Migration already done", map[string]interface{}{ - "database": d.Name, + "database": d.DBName, "library": m.Base().Name.Library, "name": m.Base().Name.Name, }) @@ -193,7 +193,7 @@ func (d *Database) migrate(ctx context.Context, s *Schema) (err error) { if m.Base().async && i > lastUnfishedSyncronous && !s.options.Overrides.EverythingSynchronous { // This and all following migrations are async d.log.Info("The remaining migrations are async starting from", map[string]interface{}{ - "database": d.Name, + "database": d.DBName, "library": m.Base().Name.Library, "name": m.Base().Name.Name, }) @@ -215,7 +215,7 @@ func (d *Database) migrate(ctx context.Context, s *Schema) (err error) { func (d *Database) doOneMigration(ctx context.Context, m Migration) (bool, error) { if d.Options.DebugLogging { d.log.Debug("Starting migration", map[string]interface{}{ - "database": d.Name, + "database": d.DBName, "library": m.Base().Name.Library, "name": m.Base().Name.Name, }) @@ -301,11 +301,11 @@ func (d *Database) allDone(m Migration, err error) { } if err == nil { d.log.Info("Migrations complete", map[string]interface{}{ - "database": d.Name, + "database": d.DBName, }) } else { d.log.Info("Migrations failed", map[string]interface{}{ - "database": d.Name, + "database": d.DBName, "error": err, }) } diff --git a/lsmysql/check.go b/lsmysql/check.go index e54970b..e43325b 100644 --- a/lsmysql/check.go +++ b/lsmysql/check.go @@ -44,7 +44,7 @@ func CheckScript(s string) error { return fmt.Errorf("data command '%s' combined with DDL command '%s': %w", seenData, seenDDL, ErrDataAndDDL) } if nonIdempotent != "" { - return fmt.Errorf("non-idempotent DSL '%s': %w", nonIdempotent, ErrNonIdempotentDDL) + return fmt.Errorf("non-idempotent DDL '%s': %w", nonIdempotent, ErrNonIdempotentDDL) } return nil } diff --git a/lsmysql/mysql.go b/lsmysql/mysql.go index c54e911..2ea5ce5 100644 --- a/lsmysql/mysql.go +++ b/lsmysql/mysql.go @@ -57,8 +57,11 @@ func WithoutDatabase(p *MySQL) { p.skipDatabase = true } -// New creates a libschema.Database with a mysql driver built in. -func New(log *internal.Log, name string, schema *libschema.Schema, db *sql.DB, options ...MySQLOpt) (*libschema.Database, *MySQL, error) { +// New creates a libschema.Database with a mysql driver built in. The dbName +// parameter specifies the name of the database, but that name is not actaully +// used anywhere except for logging. To override the database used for the +// migrations set the SchemaOverride option. +func New(log *internal.Log, dbName string, schema *libschema.Schema, db *sql.DB, options ...MySQLOpt) (*libschema.Database, *MySQL, error) { m := &MySQL{ db: db, trackingSchemaTable: trackingSchemaTable, @@ -69,7 +72,7 @@ func New(log *internal.Log, name string, schema *libschema.Schema, db *sql.DB, o var d *libschema.Database if !m.skipDatabase { var err error - d, err = schema.NewDatabase(log, name, db, m) + d, err = schema.NewDatabase(log, dbName, db, m) if err != nil { return nil, nil, err } diff --git a/lspostgres/postgres.go b/lspostgres/postgres.go index d31dc82..ed5ca06 100644 --- a/lspostgres/postgres.go +++ b/lspostgres/postgres.go @@ -22,9 +22,11 @@ type Postgres struct { lockTx *sql.Tx } -// New creates a libschema.Database with a postgres driver built in. -func New(log *internal.Log, name string, schema *libschema.Schema, db *sql.DB) (*libschema.Database, error) { - return schema.NewDatabase(log, name, db, &Postgres{}) +// New creates a libschema.Database with a postgres driver built in. The dbName +// parameter is used internally by libschema, but does not affect where migrations +// are actually applied. +func New(log *internal.Log, dbName string, schema *libschema.Schema, db *sql.DB) (*libschema.Database, error) { + return schema.NewDatabase(log, dbName, db, &Postgres{}) } type pmigration struct { diff --git a/lssinglestore/singlestore.go b/lssinglestore/singlestore.go index 8ac1121..7262bf3 100644 --- a/lssinglestore/singlestore.go +++ b/lssinglestore/singlestore.go @@ -1,3 +1,4 @@ +// Package lssinglestore is a libschema.Driver for connecting to SingleStore databases. package lssinglestore import ( @@ -41,8 +42,8 @@ type SingleStore struct { } // New creates a libschema.Database with a Singlestore driver built in. -func New(log *internal.Log, name string, schema *libschema.Schema, db *sql.DB) (*libschema.Database, *SingleStore, error) { - _, mysql, err := lsmysql.New(log, name, schema, db, +func New(log *internal.Log, dbName string, schema *libschema.Schema, db *sql.DB) (*libschema.Database, *SingleStore, error) { + _, mysql, err := lsmysql.New(log, dbName, schema, db, lsmysql.WithoutDatabase, lsmysql.WithTrackingTableQuoter(trackingSchemaTable), ) @@ -53,7 +54,7 @@ func New(log *internal.Log, name string, schema *libschema.Schema, db *sql.DB) ( MySQL: mysql, db: db, } - database, err := schema.NewDatabase(log, name, db, s2) + database, err := schema.NewDatabase(log, dbName, db, s2) if err != nil { return nil, nil, err }