Skip to content

Commit

Permalink
Update CleanupChainTables shell cmd to specify schema
Browse files Browse the repository at this point in the history
  • Loading branch information
ilija42 committed Nov 29, 2023
1 parent 2ed60e4 commit a6394b7
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions core/cmd/shell_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -996,23 +996,37 @@ 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 tableNameAndSchema = make(map[string]string)
for rows.Next() {
var name string
var schema string
if err = rows.Scan(&name, &schema); err != nil {
return err
}
tableNameAndSchema[name] = schema
}

for name, schema := range tableNameAndSchema {
tableName := schema + "." + name
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

0 comments on commit a6394b7

Please sign in to comment.