|
| 1 | +package testingutils |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "github.com/ft-t/go-money/pkg/boilerplate" |
| 7 | + "github.com/jackc/pgx/v5" |
| 8 | + "github.com/juju/fslock" |
| 9 | + "github.com/rs/zerolog/log" |
| 10 | + "os" |
| 11 | + "path" |
| 12 | + "strings" |
| 13 | +) |
| 14 | + |
| 15 | +func getLock() *fslock.Lock { |
| 16 | + return fslock.New(path.Join(os.TempDir(), "go_fs_lock")) |
| 17 | +} |
| 18 | + |
| 19 | +func FlushAllTables(config boilerplate.DbConfig) error { |
| 20 | + return flushInternal(config, nil) |
| 21 | +} |
| 22 | + |
| 23 | +func ensureThatItsLocal(config boilerplate.DbConfig) { |
| 24 | + allowedHosts := []string{"localhost", "127.0.0.1", "postgres"} |
| 25 | + |
| 26 | + for _, h := range allowedHosts { |
| 27 | + if strings.EqualFold(h, config.Host) { |
| 28 | + return |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + if strings.HasPrefix(config.Host, "192.168.") { // home network |
| 33 | + return |
| 34 | + } |
| 35 | + |
| 36 | + panic("host is not allowed, please check config for postgres") |
| 37 | +} |
| 38 | + |
| 39 | +func flushInternal(config boilerplate.DbConfig, tables []string) error { |
| 40 | + ensureThatItsLocal(config) |
| 41 | + |
| 42 | + rawStr, _ := boilerplate.GetDbConnectionString(config) |
| 43 | + |
| 44 | + conn, err := pgx.Connect(context.Background(), rawStr) |
| 45 | + |
| 46 | + if err != nil { |
| 47 | + return err |
| 48 | + } |
| 49 | + |
| 50 | + defer conn.Close(context.Background()) |
| 51 | + |
| 52 | + res, err := conn.Query(context.Background(), "SELECT table_schema, table_name FROM information_schema.tables where table_schema != 'pg_catalog' and table_schema != 'information_schema';") |
| 53 | + |
| 54 | + if err != nil { |
| 55 | + return err |
| 56 | + } |
| 57 | + |
| 58 | + existing := map[string]bool{} |
| 59 | + |
| 60 | + for res.Next() { |
| 61 | + values := res.RawValues() |
| 62 | + |
| 63 | + resultPath := fmt.Sprintf("%v.%v", string(values[0]), string(values[1])) |
| 64 | + existing[resultPath] = true |
| 65 | + } |
| 66 | + |
| 67 | + builder := strings.Builder{} |
| 68 | + |
| 69 | + builder.WriteString("BEGIN TRANSACTION; ") |
| 70 | + |
| 71 | + if tables != nil { |
| 72 | + for _, table := range tables { |
| 73 | + if strings.ToLower(table) == "public.migrations" { |
| 74 | + continue |
| 75 | + } |
| 76 | + |
| 77 | + if _, ok := existing[strings.ToLower(table)]; ok { |
| 78 | + |
| 79 | + builder.WriteString(fmt.Sprintf(" truncate table %v CASCADE; ", strings.ToLower(table))) |
| 80 | + } else { |
| 81 | + log.Warn().Msgf("table %v does not exists", table) |
| 82 | + } |
| 83 | + } |
| 84 | + } else { |
| 85 | + for name := range existing { |
| 86 | + if strings.ToLower(name) == "public.migrations" { |
| 87 | + continue |
| 88 | + } |
| 89 | + builder.WriteString(fmt.Sprintf(" truncate table %v CASCADE; ", strings.ToLower(name))) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + builder.WriteString(" COMMIT;") |
| 94 | + |
| 95 | + _, err = conn.Exec(context.Background(), builder.String()) |
| 96 | + |
| 97 | + if err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + |
| 101 | + return nil |
| 102 | +} |
| 103 | + |
| 104 | +func EnsurePostgresDbExists(config boilerplate.DbConfig) error { |
| 105 | + lock := getLock() |
| 106 | + |
| 107 | + if err := lock.Lock(); err != nil { |
| 108 | + return err |
| 109 | + } |
| 110 | + |
| 111 | + defer func() { |
| 112 | + _ = lock.Unlock() |
| 113 | + }() |
| 114 | + |
| 115 | + oldDbName := config.Db |
| 116 | + |
| 117 | + config.Db = "postgres" |
| 118 | + rawStr, _ := boilerplate.GetDbConnectionString(config) |
| 119 | + |
| 120 | + conn, err := pgx.Connect(context.Background(), rawStr) |
| 121 | + config.Db = oldDbName |
| 122 | + |
| 123 | + if err != nil { |
| 124 | + return err |
| 125 | + } |
| 126 | + |
| 127 | + defer conn.Close(context.Background()) |
| 128 | + |
| 129 | + r, err := conn.Query(context.Background(), fmt.Sprintf("SELECT * FROM pg_database WHERE datname='%v'", oldDbName)) |
| 130 | + |
| 131 | + if err != nil { |
| 132 | + return err |
| 133 | + } |
| 134 | + |
| 135 | + r.Next() |
| 136 | + |
| 137 | + exists := true |
| 138 | + |
| 139 | + if len(r.RawValues()) == 0 { |
| 140 | + exists = false |
| 141 | + } |
| 142 | + |
| 143 | + if !exists { |
| 144 | + _, err = conn.Exec(context.Background(), fmt.Sprintf("CREATE DATABASE %v;", oldDbName)) |
| 145 | + |
| 146 | + if err != nil { |
| 147 | + return err |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + return nil |
| 152 | +} |
0 commit comments