Skip to content

Commit

Permalink
improve core migration compatibility and fix pad issue
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Jan 18, 2024
1 parent c3278ef commit c32788b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
4 changes: 2 additions & 2 deletions cmd/rockhopper/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func status(cmd *cobra.Command, args []string) error {

t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"Package", "Migration", "Applied At", "Current"})
t.AppendHeader(table.Row{"Package", "Version ID", "Source File", "Applied At", "Current"})

for pkgName, migrations := range migrationMap {
currentVersion, err := db.CurrentVersion(ctx, pkgName)
Expand All @@ -97,7 +97,7 @@ func status(cmd *cobra.Command, args []string) error {
}

t.AppendRow(table.Row{
migration.Package, migration.Source, formatAppliedAt(migration.Record), currentVersionMark(migration.Version, currentVersion),
migration.Package, migration.Version, migration.Source, formatAppliedAt(migration.Record), currentVersionMark(migration.Version, currentVersion),
})
}

Expand Down
41 changes: 23 additions & 18 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,34 +307,39 @@ func (db *DB) migrateLegacyGooseTable(ctx context.Context) error {
return rollbackAndLogErr(err, tx, "unable to alter table")
}

if err := execAndCheckErr(tx, ctx,
fmt.Sprintf(`RENAME TABLE %s TO %s`, legacyGooseTableName, TableName)); err != nil {
return rollbackAndLogErr(err, tx, "unable to rename table")
}
/*
if err := execAndCheckErr(tx, ctx,
fmt.Sprintf(`RENAME TABLE %s TO %s`, legacyGooseTableName, TableName)); err != nil {
return rollbackAndLogErr(err, tx, "unable to rename table")
}
*/

case *PostgresDialect, *RedshiftDialect:
if err := execAndCheckErr(tx, ctx,
`ALTER TABLE goose_db_version ADD COLUMN package VARCHAR(125) NOT NULL DEFAULT 'main'`); err != nil {
return rollbackAndLogErr(err, tx, "unable to alter table")
}

if err := execAndCheckErr(tx, ctx,
fmt.Sprintf(`ALTER TABLE %s RENAME TO %s`, legacyGooseTableName, TableName)); err != nil {
return rollbackAndLogErr(err, tx, "unable to rename table")
}
/*
if err := execAndCheckErr(tx, ctx,
fmt.Sprintf(`ALTER TABLE %s RENAME TO %s`, legacyGooseTableName, TableName)); err != nil {
return rollbackAndLogErr(err, tx, "unable to rename table")
}
*/

case *Sqlite3Dialect, *SqlServerDialect:
if err := execAndCheckErr(tx, ctx,
fmt.Sprintf(`INSERT INTO %s(id, package, version_id, is_applied, tstamp) SELECT id, 'main', version_id, is_applied, tstamp FROM %s`,
TableName,
legacyGooseTableName),
); err != nil {
return rollbackAndLogErr(err, tx, "unable to execute insert from select")
}
}

if err := execAndCheckErr(tx, ctx, fmt.Sprintf(`DROP TABLE %s`, legacyGooseTableName)); err != nil {
return rollbackAndLogErr(err, tx, "unable to drop legacy table")
}
if err := execAndCheckErr(tx, ctx,
fmt.Sprintf(`INSERT INTO %s(package, version_id, is_applied, tstamp) SELECT 'main', version_id, is_applied, tstamp FROM %s`,
TableName,
legacyGooseTableName),
); err != nil {
return rollbackAndLogErr(err, tx, "unable to execute insert from select")
}

if err := execAndCheckErr(tx, ctx, fmt.Sprintf(`DROP TABLE %s`, legacyGooseTableName)); err != nil {
return rollbackAndLogErr(err, tx, "unable to drop legacy table")
}

return tx.Commit()
Expand Down
7 changes: 6 additions & 1 deletion utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ func previewSQL(s string) string {
idx := strings.LastIndex(s[:width], " ")
if idx > 0 && idx > int(float64(width)*(2.0/3.0)) && idx < len(s)-3 {
s = s[:idx] + "..."
return s + strings.Repeat(" ", width-len(s))
padSize := width - len(s)
if padSize > 0 {
s += strings.Repeat(" ", padSize)
}

return s
}

return s[:width]
Expand Down

0 comments on commit c32788b

Please sign in to comment.