-
Notifications
You must be signed in to change notification settings - Fork 31
/
database.go
71 lines (61 loc) · 2.19 KB
/
database.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"fmt"
"github.com/spf13/viper"
)
// Execute cleanup of demo database before loading if needed
func executeDemoDatabasePreCleanup() {
// NOTE: This is a hidden feature used mainly by test cases, not used widely
// so use this with caution, since it will cleanup all the tables in the said database
shouldWeDrop := viper.GetBool("MOCK_DATA_TEST_RUNNER")
if shouldWeDrop {
Infof("Executing the cleanup of all objects in the database: %s", cmdOptions.Database)
_, err := ExecuteDB(dropDemoDatabase())
if err != nil {
errMsg := fmt.Sprintf("%s", err)
failureMsg := fmt.Sprintf("Failure in pre cleanup a demo tables in the database %s, err: %v",
cmdOptions.Database, err)
IgnoreError(errMsg, "does not exist", failureMsg)
}
}
}
// ExecuteDemoDatabase creates a demo database based on the flavour of postgres i.e
// native postgres or greenplum
func ExecuteDemoDatabase() {
Infof("Create demo tables in the database: %s", cmdOptions.Database)
// pre cleanup script
executeDemoDatabasePreCleanup()
// Execute the demo database dump
var err error
if GreenplumOrPostgres == "postgres" {
_, err = ExecuteDB(demoDatabasePostgres())
} else {
_, err = ExecuteDB(demoDatabaseGreenplum())
}
if err != nil {
errMsg := fmt.Sprintf("%s", err)
failureMsg := fmt.Sprintf("Failure in creating a demo tables in the database %s, err: %v",
cmdOptions.Database, err)
IgnoreError(errMsg, "does not exist", failureMsg)
}
Infof("Completed creating demo tables in the database: %s", cmdOptions.Database)
}
// MockDatabase mocks the whole database
func MockDatabase() {
// Get the table list that we have to mock the data
Infof("Starting the program to mock full database")
tableList := dbExtractTables("")
MockTable(tableList)
}
// Extract all the tables in the database
func dbExtractTables(whereClause string) []DBTables {
Infof("Extracting the tables in the database: %s", cmdOptions.Database)
// Obtain all the tables in the database
var tables []DBTables
if GreenplumOrPostgres == "postgres" { // Use postgres specific query
tables = allTablesPostgres(whereClause)
} else { // Greenplum flavor postgres database
tables = allTablesGPDB(whereClause)
}
return tables
}