Skip to content

Commit

Permalink
Add forceful testdb creation (#13640)
Browse files Browse the repository at this point in the history
* Add forceful testdb creation

* fix tests
  • Loading branch information
krehermann authored Jun 21, 2024
1 parent b584df5 commit c9f6816
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 6 deletions.
6 changes: 5 additions & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ setup-testdb: ## Setup the test database.
testdb: ## Prepares the test database.
go run . local db preparetest

.PHONY: testdb
.PHONY: testdb-force
testdb-force: ## Prepares the test database, drops any pesky user connections that stand in the the way.
go run . local db preparetest --force

.PHONY: testdb-user-only
testdb-user-only: ## Prepares the test database with user only.
go run . local db preparetest --user-only

Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,14 @@ source .dbenv
make testdb
```

If you encounter the error `database accessed by other users (SQLSTATE 55006) exit status 1`
and you want force the database creation then use
```
source .dbenv
make testdb-force
```


7. Run tests:

```bash
Expand Down
22 changes: 18 additions & 4 deletions core/cmd/shell_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ func initLocalSubCmds(s *Shell, safe bool) []cli.Command {
Name: "dangerWillRobinson",
Usage: "set to true to enable dropping non-test databases",
},
cli.BoolFlag{
Name: "force",
Usage: "set to true to force the reset by dropping any existing connections to the database",
},
},
},
{
Expand All @@ -186,6 +190,10 @@ func initLocalSubCmds(s *Shell, safe bool) []cli.Command {
Name: "user-only",
Usage: "only include test user fixture",
},
cli.BoolFlag{
Name: "force",
Usage: "set to true to force the reset by dropping any existing connections to the database",
},
},
},
{
Expand Down Expand Up @@ -748,15 +756,15 @@ func (s *Shell) ResetDatabase(c *cli.Context) error {
}

dangerMode := c.Bool("dangerWillRobinson")

force := c.Bool("force")
dbname := parsed.Path[1:]
if !dangerMode && !strings.HasSuffix(dbname, "_test") {
return s.errorOut(fmt.Errorf("cannot reset database named `%s`. This command can only be run against databases with a name that ends in `_test`, to prevent accidental data loss. If you REALLY want to reset this database, pass in the -dangerWillRobinson option", dbname))
}
lggr := s.Logger
lggr.Infof("Resetting database: %#v", parsed.String())
lggr.Debugf("Dropping and recreating database: %#v", parsed.String())
if err := dropAndCreateDB(parsed); err != nil {
if err := dropAndCreateDB(parsed, force); err != nil {
return s.errorOut(err)
}
lggr.Debugf("Migrating database: %#v", parsed.String())
Expand Down Expand Up @@ -1079,7 +1087,7 @@ func newConnection(cfg dbConfig) (*sqlx.DB, error) {
return pg.NewConnection(parsed.String(), cfg.Dialect(), cfg)
}

func dropAndCreateDB(parsed url.URL) (err error) {
func dropAndCreateDB(parsed url.URL, force bool) (err error) {
// Cannot drop the database if we are connected to it, so we must connect
// to a different one. template1 should be present on all postgres installations
dbname := parsed.Path[1:]
Expand All @@ -1093,7 +1101,13 @@ func dropAndCreateDB(parsed url.URL) (err error) {
err = multierr.Append(err, cerr)
}
}()

if force {
// supports pg < 13. https://stackoverflow.com/questions/17449420/postgresql-unable-to-drop-database-because-of-some-auto-connections-to-db
_, err = db.Exec(fmt.Sprintf("SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = '%s';", dbname))
if err != nil {
return fmt.Errorf("unable to terminate connections to postgres database: %v", err)
}
}
_, err = db.Exec(fmt.Sprintf(`DROP DATABASE IF EXISTS "%s"`, dbname))
if err != nil {
return fmt.Errorf("unable to drop postgres database: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion core/scripts/setup_testdb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ echo $db_url
repo=$(git rev-parse --show-toplevel)
pushd $repo
export $db_url
make testdb || exit_error "Failed to create test database"
make testdb-force || exit_error "Failed to create test database"
popd

# Set the database URL in the .dbenv file
Expand Down
1 change: 1 addition & 0 deletions testdata/scripts/node/db/preparetest/help.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ USAGE:

OPTIONS:
--user-only only include test user fixture
--force set to true to force the reset by dropping any existing connections to the database

1 change: 1 addition & 0 deletions testdata/scripts/node/db/reset/help.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ USAGE:

OPTIONS:
--dangerWillRobinson set to true to enable dropping non-test databases
--force set to true to force the reset by dropping any existing connections to the database

0 comments on commit c9f6816

Please sign in to comment.