Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rename Database.Name -> Database.DBName #62

Merged
merged 1 commit into from
Dec 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand All @@ -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
}
Expand Down
18 changes: 9 additions & 9 deletions apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
})
Expand Down Expand Up @@ -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
}
Expand All @@ -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()
Expand All @@ -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,
})
Expand All @@ -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,
})
Expand All @@ -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,
})
Expand Down Expand Up @@ -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,
})
}
Expand Down
2 changes: 1 addition & 1 deletion lsmysql/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
9 changes: 6 additions & 3 deletions lsmysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}
Expand Down
8 changes: 5 additions & 3 deletions lspostgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions lssinglestore/singlestore.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package lssinglestore is a libschema.Driver for connecting to SingleStore databases.
package lssinglestore

import (
Expand Down Expand Up @@ -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),
)
Expand All @@ -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
}
Expand Down