Skip to content

Commit

Permalink
Update CleanupChainTables shell cmd to specify schema (#11409)
Browse files Browse the repository at this point in the history
* Update CleanupChainTables shell cmd to specify schema

* Update testdb naming checks to match

* Add test that checks if cleanup chain table shell cmd works

* Fix shell cmd test db checks to match CI

* Adjust CleanupChainTables test to handle heavyweight db testdb naming

* Change CleanupChainTables to account for duplicate table names
  • Loading branch information
ilija42 authored Nov 29, 2023
1 parent 7e7b84b commit e546a7e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
27 changes: 20 additions & 7 deletions core/cmd/shell_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -996,23 +996,36 @@ func (s *Shell) CleanupChainTables(c *cli.Context) error {
if err != nil {
return s.errorOut(errors.Wrap(err, "error connecting to the database"))
}

defer db.Close()

tablesToDeleteFromQuery := `SELECT table_name FROM information_schema.columns WHERE "column_name"=$1;`
// some tables with evm_chain_id (mostly job specs) are in public schema
tablesToDeleteFromQuery := `SELECT table_name, table_schema FROM information_schema.columns WHERE "column_name"=$1;`
// Delete rows from each table based on the chain_id.
if strings.EqualFold("EVM", c.String("type")) {
var tables []string
if err = db.Select(&tables, tablesToDeleteFromQuery, "evm_chain_id"); err != nil {
rows, err := db.Query(tablesToDeleteFromQuery, "evm_chain_id")
if err != nil {
return err
} else if rows.Err() != nil {
return rows.Err()
}
for _, tableName := range tables {

var tablesToDeleteFrom []string
for rows.Next() {
var name string
var schema string
if err = rows.Scan(&name, &schema); err != nil {
return err
}
tablesToDeleteFrom = append(tablesToDeleteFrom, schema+"."+name)
}

for _, tableName := range tablesToDeleteFrom {
query := fmt.Sprintf(`DELETE FROM %s WHERE "evm_chain_id"=$1;`, tableName)
_, err = db.Exec(query, c.String("id"))
if err != nil {
fmt.Printf("Error deleting rows from %s: %v\n", tableName, err)
fmt.Printf("Error deleting rows containing evm_chain_id from %s: %v\n", tableName, err)
} else {
fmt.Printf("Rows with chain_id %s deleted from %s.\n", c.String("id"), tableName)
fmt.Printf("Rows with evm_chain_id %s deleted from %s.\n", c.String("id"), tableName)
}
}
} else {
Expand Down
20 changes: 20 additions & 0 deletions core/cmd/shell_local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,3 +492,23 @@ func TestShell_RebroadcastTransactions_AddressCheck(t *testing.T) {
})
}
}

func TestShell_CleanupChainTables(t *testing.T) {
// Just check if it doesn't error, command itself shouldn't be changed unless major schema changes were made.
// It would be really hard to write a test that accounts for schema changes, so this should be enough to alarm us that something broke.
config, _ := heavyweight.FullTestDBV2(t, func(c *chainlink.Config, s *chainlink.Secrets) { c.Database.Dialect = dialects.Postgres })
client := cmd.Shell{
Config: config,
Logger: logger.TestLogger(t),
}

set := flag.NewFlagSet("test", 0)
flagSetApplyFromAction(client.CleanupChainTables, set, "")
require.NoError(t, set.Set("id", testutils.FixtureChainID.String()))
require.NoError(t, set.Set("type", "EVM"))
// heavyweight creates test db named chainlink_test_uid, while usual naming is chainlink_test
// CleanupChainTables handles test db name with chainlink_test, but because of heavyweight test db naming we have to set danger flag
require.NoError(t, set.Set("danger", "true"))
c := cli.NewContext(nil, set, nil)
require.NoError(t, client.CleanupChainTables(c))
}

0 comments on commit e546a7e

Please sign in to comment.