-
Notifications
You must be signed in to change notification settings - Fork 51
[FEATURE] Add support for --from-dir and providing a dedicated temporary database server
#199
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,89 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "github.com/stripe/pg-schema-diff/internal/pgdump" | ||
| "github.com/stripe/pg-schema-diff/internal/pgengine" | ||
| ) | ||
|
|
||
| func (suite *cmdTestSuite) TestApplyCmd() { | ||
| // Non-comprehensive set of tests for the plan command. Not totally comprehensive to avoid needing to avoid | ||
| // hindering developer velocity when updating the command. | ||
| type testCase struct { | ||
| name string | ||
| // fromDbArg is an optional argument to override the default "--from-dsn" arg. | ||
| fromDbArg func(db *pgengine.DB) []string | ||
| args []string | ||
| // dynamicArgs is function that can be used to build args that are dynamic, i.e., | ||
| // saving schemas to a randomly generated temporary directory. | ||
| dynamicArgs []dArgGenerator | ||
|
|
||
| outputContains []string | ||
| // expectedSchema is the schema that is expected to be in the database after the migration. | ||
| // If nil, the expected schema will be the fromDDL. | ||
| expectedSchemaDDL []string | ||
| // expectErrContains is a list of substrings that are expected to be contained in the error returned by | ||
| // cmd.RunE. This is DISTINCT from stdErr. | ||
| expectErrContains []string | ||
| } | ||
| for _, tc := range []testCase{ | ||
| { | ||
| name: "to dir", | ||
| dynamicArgs: []dArgGenerator{tempSchemaDirDArg("to-dir", []string{"CREATE TABLE foobar();"})}, | ||
|
|
||
| expectedSchemaDDL: []string{"CREATE TABLE foobar();"}, | ||
| }, | ||
| { | ||
| name: "to dsn", | ||
| dynamicArgs: []dArgGenerator{tempDsnDArg(suite.pgEngine, "to-dsn", []string{"CREATE TABLE foobar();"})}, | ||
|
|
||
| expectedSchemaDDL: []string{"CREATE TABLE foobar();"}, | ||
| }, | ||
| { | ||
| name: "from empty dsn", | ||
| fromDbArg: func(db *pgengine.DB) []string { | ||
| tempSetPqEnvVarsForDb(suite.T(), db) | ||
| return []string{"--from-empty-dsn"} | ||
| }, | ||
| dynamicArgs: []dArgGenerator{tempSchemaDirDArg("to-dir", []string{"CREATE TABLE foobar();"})}, | ||
|
|
||
| expectedSchemaDDL: []string{"CREATE TABLE foobar();"}, | ||
| }, | ||
| { | ||
| name: "no to schema provided", | ||
| expectErrContains: []string{"must be set"}, | ||
| }, | ||
| { | ||
| name: "two to schemas provided", | ||
| args: []string{"--to-dir", "some-other-dir", "--to-dsn", "some-dsn"}, | ||
| expectErrContains: []string{"only one of"}, | ||
| }, | ||
| } { | ||
| suite.Run(tc.name, func() { | ||
| fromDb := tempDbWithSchema(suite.T(), suite.pgEngine, nil) | ||
| if tc.fromDbArg == nil { | ||
| tc.fromDbArg = func(db *pgengine.DB) []string { | ||
| return []string{"--from-dsn", db.GetDSN()} | ||
| } | ||
| } | ||
| args := append([]string{ | ||
| "apply", | ||
| "--skip-confirm-prompt", | ||
| }, tc.fromDbArg(fromDb)...) | ||
| args = append(args, tc.args...) | ||
| suite.runCmdWithAssertions(runCmdWithAssertionsParams{ | ||
| args: args, | ||
| dynamicArgs: tc.dynamicArgs, | ||
| outputContains: tc.outputContains, | ||
| expectErrContains: tc.expectErrContains, | ||
| }) | ||
| // The migration should have been successful. Assert it was. | ||
| expectedDb := tempDbWithSchema(suite.T(), suite.pgEngine, tc.expectedSchemaDDL) | ||
| expectedDbDump, err := pgdump.GetDump(expectedDb, pgdump.WithSchemaOnly()) | ||
| suite.Require().NoError(err) | ||
| fromDbDump, err := pgdump.GetDump(fromDb, pgdump.WithSchemaOnly()) | ||
| suite.Require().NoError(err) | ||
|
|
||
| suite.Equal(expectedDbDump, fromDbDump) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -7,29 +7,51 @@ import ( | |
| "github.com/go-logfmt/logfmt" | ||
| "github.com/jackc/pgx/v4" | ||
| "github.com/spf13/cobra" | ||
| "github.com/stripe/pg-schema-diff/pkg/log" | ||
| ) | ||
|
|
||
| type connFlags struct { | ||
| dsn string | ||
| type connectionFlags struct { | ||
| // dsn is the connection string for the database. | ||
| dsn string | ||
| dsnFlagName string | ||
|
|
||
| // isEmptyDsnUsingPq indicates to connect via DSN using the pq environment variables and defaults. | ||
| isEmptyDsnUsingPq bool | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was requested per this PR. This maintains support but makes it more explicit and flexible |
||
| isEmptyDsnUsingPqFlagName string | ||
| } | ||
|
|
||
| func createConnFlags(cmd *cobra.Command) *connFlags { | ||
| flags := &connFlags{} | ||
| func createConnectionFlags(cmd *cobra.Command, prefix string, additionalHelp string) *connectionFlags { | ||
bplunkett-stripe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| var c connectionFlags | ||
|
|
||
| c.dsnFlagName = prefix + "dsn" | ||
| dsnFlagHelp := "Connection string for the database (DB password can be specified through PGPASSWORD environment variable)." | ||
| if additionalHelp != "" { | ||
| dsnFlagHelp += " " + additionalHelp | ||
| } | ||
| cmd.Flags().StringVar(&c.dsn, c.dsnFlagName, "", dsnFlagHelp) | ||
|
|
||
| c.isEmptyDsnUsingPqFlagName = prefix + "empty-dsn" | ||
| isEmptyDsnUsingPqFlagHelp := "Connect with an empty DSN using the pq environment variables and defaults." | ||
| if additionalHelp != "" { | ||
| isEmptyDsnUsingPqFlagHelp += " " + additionalHelp | ||
| } | ||
| cmd.Flags().BoolVar(&c.isEmptyDsnUsingPq, c.isEmptyDsnUsingPqFlagName, false, isEmptyDsnUsingPqFlagHelp) | ||
|
|
||
| cmd.Flags().StringVar(&flags.dsn, "dsn", "", "Connection string for the database (DB password can be specified through PGPASSWORD environment variable)") | ||
| // Don't mark dsn as a required flag. | ||
| // Allow users to use the "PGHOST" etc environment variables like `psql`. | ||
| return &c | ||
| } | ||
|
|
||
| return flags | ||
| func (c *connectionFlags) IsSet() bool { | ||
| return c.dsn != "" || c.isEmptyDsnUsingPq | ||
| } | ||
|
|
||
| func parseConnConfig(c connFlags, logger log.Logger) (*pgx.ConnConfig, error) { | ||
| if c.dsn == "" { | ||
| logger.Warnf("DSN flag not set. Using libpq environment variables and default values.") | ||
| func parseConnectionFlags(flags *connectionFlags) (*pgx.ConnConfig, error) { | ||
| if !flags.isEmptyDsnUsingPq && flags.dsn == "" { | ||
| return nil, fmt.Errorf("must specify either --%s or --%s", flags.dsnFlagName, flags.isEmptyDsnUsingPqFlagName) | ||
| } | ||
|
|
||
| return pgx.ParseConfig(c.dsn) | ||
| connConfig, err := pgx.ParseConfig(flags.dsn) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("could not parse connection string %q: %w", flags.dsn, err) | ||
| } | ||
| return connConfig, nil | ||
| } | ||
|
|
||
| // logFmtToMap parses all LogFmt key/value pairs from the provided string into a | ||
|
|
||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Canonically, all logs should be done through the command. We were just doing this wrong previously. This is how cobra does it's own logging (e.g., printing usage)