-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
436 additions
and
288 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package commands | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/go-nacelle/log/v2" | ||
"github.com/go-nacelle/pgutil" | ||
"github.com/go-nacelle/pgutil/cmd/migrate/internal/database" | ||
"github.com/go-nacelle/pgutil/cmd/migrate/internal/flags" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func DescribeCommand(logger log.Logger) *cobra.Command { | ||
var ( | ||
databaseURL string | ||
) | ||
|
||
describeCmd := &cobra.Command{ | ||
Use: "describe", | ||
Short: "Describe the current database schema", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return describe(databaseURL, logger) | ||
}, | ||
} | ||
|
||
flags.RegisterDatabaseURLFlag(describeCmd, &databaseURL) | ||
return describeCmd | ||
} | ||
|
||
func describe(databaseURL string, logger log.Logger) error { | ||
db, err := database.Dial(databaseURL, logger) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
description, err := pgutil.DescribeSchema(context.Background(), db) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
serialized, err := json.Marshal(description) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("%s\n", serialized) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package commands | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/go-nacelle/log/v2" | ||
"github.com/go-nacelle/pgutil" | ||
"github.com/go-nacelle/pgutil/cmd/migrate/internal/database" | ||
"github.com/go-nacelle/pgutil/cmd/migrate/internal/flags" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func DriftCommand(logger log.Logger) *cobra.Command { | ||
var ( | ||
databaseURL string | ||
) | ||
|
||
driftCmd := &cobra.Command{ | ||
Use: "drift", | ||
Short: "Compare the current database schema against the expected schema", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return drift(databaseURL, logger, args[0]) | ||
}, | ||
} | ||
|
||
flags.RegisterDatabaseURLFlag(driftCmd, &databaseURL) | ||
return driftCmd | ||
} | ||
|
||
func drift(databaseURL string, logger log.Logger, filename string) error { | ||
db, err := database.Dial(databaseURL, logger) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
description, err := pgutil.DescribeSchema(context.Background(), db) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
b, err := os.ReadFile(filename) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var expected pgutil.SchemaDescription | ||
if err := json.Unmarshal(b, &expected); err != nil { | ||
return err | ||
} | ||
|
||
statements := pgutil.Compare(expected, description) | ||
|
||
if len(statements) == 0 { | ||
fmt.Printf("No drift detected\n") | ||
return nil | ||
} | ||
|
||
for _, d := range statements { | ||
fmt.Printf("%s\n\n", d) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package commands | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/go-nacelle/log/v2" | ||
"github.com/go-nacelle/pgutil/cmd/migrate/internal/database" | ||
"github.com/go-nacelle/pgutil/cmd/migrate/internal/flags" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func UndoCommand(logger log.Logger) *cobra.Command { | ||
var ( | ||
databaseURL string | ||
migrationsDirectory string | ||
) | ||
|
||
undoCmd := &cobra.Command{ | ||
Use: "undo [migration_id]", | ||
Short: "Undo migrations up to and including the specified migration ID", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
migrationID, err := strconv.Atoi(args[0]) | ||
if err != nil { | ||
return fmt.Errorf("invalid migration ID: %v", err) | ||
} | ||
|
||
return undo(databaseURL, migrationsDirectory, logger, migrationID) | ||
}, | ||
} | ||
|
||
flags.RegisterDatabaseURLFlag(undoCmd, &databaseURL) | ||
flags.RegisterMigrationsDirectoryFlag(undoCmd, &migrationsDirectory) | ||
return undoCmd | ||
} | ||
|
||
func undo(databaseURL string, migrationsDirectory string, logger log.Logger, migrationID int) error { | ||
runner, err := database.CreateRunner(databaseURL, migrationsDirectory, logger) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return runner.Undo(context.Background(), migrationID) | ||
} |
Oops, something went wrong.