Skip to content

Commit

Permalink
Add operation flag to ensure truncate table occurs
Browse files Browse the repository at this point in the history
  • Loading branch information
asteel-gsa committed May 30, 2024
1 parent 465d266 commit 5b10a88
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
29 changes: 25 additions & 4 deletions cmd/db_to_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
var (
source_db string
dest_db string
operation string
)

func get_table_and_schema_names_db(source_creds vcap.Credentials) map[string]string {
Expand Down Expand Up @@ -57,14 +58,24 @@ func get_table_and_schema_names_db(source_creds vcap.Credentials) map[string]str
func LocalDatabaseSync(
source_db_creds vcap.Credentials,
dest_db_creds vcap.Credentials,
table_names []string) {
table_names []string,
operation string) {
var BACKUP_ALL = len(table_names) == 0

logging.Logger.Println("DBTODB " + source_db_creds.Get("name").String() + " to " + dest_db_creds.Get("name").String() + " starting")
table_to_schema := get_table_and_schema_names_db(source_db_creds)
//pg_dump -t table_to_copy source_db | psql target_db
for table, schema := range table_to_schema {
if slices.Contains(table_names, table) || BACKUP_ALL {
switch operation {
case "initial":
logging.Logger.Printf("Initial db2db operation, truncate not required")
case "backup":
fallthrough
case "restore":
truncate_tables(dest_db_creds, []string{table})
}

psql_write := pipes.Psql(
pipes.PG_Dump_Table(source_db_creds, schema, table),
dest_db_creds,
Expand All @@ -82,14 +93,24 @@ func LocalDatabaseSync(
func CgovDatabaseSync(
source_db_creds vcap.Credentials,
dest_db_creds vcap.Credentials,
table_names []string) {
table_names []string,
operation string) {
var BACKUP_ALL = len(table_names) == 0

logging.Logger.Println("DBTODB " + source_db_creds.Get("name").String() + " to " + dest_db_creds.Get("name").String() + " starting")
table_to_schema := get_table_and_schema_names_db(source_db_creds)
//pg_dump -t table_to_copy source_db | psql target_db
for table, schema := range table_to_schema {
if slices.Contains(table_names, table) || BACKUP_ALL {
switch operation {
case "initial":
logging.Logger.Printf("Initial db2db operation, truncate not required")
case "backup":
fallthrough
case "restore":
truncate_tables(dest_db_creds, []string{table})
}

psql_write := pipes.Psql(
pipes.PG_Dump_Table(source_db_creds, schema, table),
dest_db_creds,
Expand Down Expand Up @@ -117,10 +138,10 @@ var DbToDb = &cobra.Command{

ch := structs.Choice{
Local: func() {
LocalDatabaseSync(source_db_creds, dest_db_creds, table_names)
LocalDatabaseSync(source_db_creds, dest_db_creds, table_names, operation)
},
Remote: func() {
CgovDatabaseSync(source_db_creds, dest_db_creds, table_names)
CgovDatabaseSync(source_db_creds, dest_db_creds, table_names, operation)
}}
runLocalOrRemote(ch)

Expand Down
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ func parseFlags(cmd_name string, cmd *cobra.Command) {
fmt.Println("RUNNING DB_TO_DB FLAGS")
cmd.Flags().StringVarP(&source_db, "src_db", "", "", "source database name")
cmd.Flags().StringVarP(&dest_db, "dest_db", "", "", "destination database name")
cmd.Flags().StringVarP(&operation, "operation", "", "", "operation (initial/backup/restore)")
cmd.MarkFlagRequired("src_db")
cmd.MarkFlagRequired("dest_db")
cmd.MarkFlagRequired("operation")
case "truncate":
fmt.Println("RUNNING TRUNCATE FLAGS")
cmd.Flags().StringVarP(&truncate_db, "db", "", "", "target database name")
Expand Down

0 comments on commit 5b10a88

Please sign in to comment.