|
| 1 | +package postgres |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strconv" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/transferia/transferia/library/go/core/xerrors" |
| 9 | + "github.com/transferia/transferia/pkg/abstract" |
| 10 | +) |
| 11 | + |
| 12 | +func CreateTableQuery(fullTableName string, schema []abstract.ColSchema) (string, error) { |
| 13 | + if err := prepareOriginalTypes(schema); err != nil { |
| 14 | + return "", xerrors.Errorf("failed to prepare original types for parsing: %w", err) |
| 15 | + } |
| 16 | + |
| 17 | + var primaryKeys []string |
| 18 | + b := strings.Builder{} |
| 19 | + b.WriteString(fmt.Sprintf(`CREATE TABLE IF NOT EXISTS %s (`, fullTableName)) |
| 20 | + for idx, col := range schema { |
| 21 | + var queryType string |
| 22 | + if col.OriginalType != "" { |
| 23 | + queryType = strings.TrimPrefix(col.OriginalType, "pg:") |
| 24 | + queryType = strings.ReplaceAll(queryType, "USER-DEFINED", "TEXT") |
| 25 | + } else { |
| 26 | + var err error |
| 27 | + queryType, err = DataToOriginal(col.DataType) |
| 28 | + if err != nil { |
| 29 | + return "", xerrors.Errorf("failed to convert column %q to original type: %w", col.ColumnName, err) |
| 30 | + } |
| 31 | + } |
| 32 | + if strings.HasPrefix(col.Expression, "pg:") { |
| 33 | + queryType += " " + strings.TrimPrefix(col.Expression, "pg:") |
| 34 | + } |
| 35 | + if col.Required { |
| 36 | + queryType += " NOT NULL" |
| 37 | + } |
| 38 | + b.WriteString(fmt.Sprintf(`"%v" %v`, col.ColumnName, queryType)) |
| 39 | + if col.IsKey() { |
| 40 | + primaryKeys = append(primaryKeys, fmt.Sprintf(`"%s"`, col.ColumnName)) |
| 41 | + } |
| 42 | + if idx < len(schema)-1 { |
| 43 | + b.WriteString(",") |
| 44 | + } |
| 45 | + } |
| 46 | + if len(primaryKeys) > 0 { |
| 47 | + b.WriteString(fmt.Sprintf(", primary key (%v)", strings.Join(primaryKeys, ","))) |
| 48 | + } |
| 49 | + b.WriteString(")") |
| 50 | + |
| 51 | + return b.String(), nil |
| 52 | +} |
| 53 | + |
| 54 | +func createEnumQuery(col abstract.ColSchema) (string, error) { |
| 55 | + col, err := prepareOriginalType(col) |
| 56 | + if err != nil { |
| 57 | + return "", xerrors.Errorf("createEnumQuery:failed to prepare original types for parsing: %w", err) |
| 58 | + } |
| 59 | + typeName, err := strconv.Unquote(strings.TrimPrefix(col.OriginalType, "pg:")) |
| 60 | + if err != nil { |
| 61 | + return "", xerrors.Errorf("createEnumQuery:failed to unquote %s: %w", col.OriginalType, err) |
| 62 | + } |
| 63 | + allVals := GetPropertyEnumAllValues(&col) |
| 64 | + |
| 65 | + buf := strings.Builder{} |
| 66 | + for i, v := range allVals { |
| 67 | + if i > 0 { |
| 68 | + buf.WriteString(",") |
| 69 | + } |
| 70 | + buf.WriteString(fmt.Sprintf("'%s'", v)) |
| 71 | + } |
| 72 | + return fmt.Sprintf(` |
| 73 | +DO $$ |
| 74 | +BEGIN |
| 75 | + IF NOT EXISTS ( |
| 76 | + SELECT |
| 77 | + 1 |
| 78 | + FROM |
| 79 | + pg_type |
| 80 | + WHERE |
| 81 | + typname = '%s') THEN |
| 82 | + CREATE TYPE "%s" AS ENUM (%s); |
| 83 | + END IF; |
| 84 | +END; |
| 85 | +$$;`, typeName, typeName, buf.String()), nil |
| 86 | +} |
| 87 | + |
| 88 | +func addEnumValsQuery(currentCol, col abstract.ColSchema) ([]string, error) { |
| 89 | + col, err := prepareOriginalType(col) |
| 90 | + if err != nil { |
| 91 | + return nil, xerrors.Errorf("addColsQuery:failed to prepare original types for parsing: %w", err) |
| 92 | + } |
| 93 | + currValsMap := make(map[string]int) |
| 94 | + currVals := GetPropertyEnumAllValues(¤tCol) |
| 95 | + for i, val := range currVals { |
| 96 | + currValsMap[val] = i |
| 97 | + } |
| 98 | + |
| 99 | + newValsMap := make(map[string]int) |
| 100 | + newVals := GetPropertyEnumAllValues(&col) |
| 101 | + for i, val := range newVals { |
| 102 | + newValsMap[val] = i |
| 103 | + } |
| 104 | + res := make([]string, 0, len(newVals)) |
| 105 | + |
| 106 | + var toAdd []struct { |
| 107 | + val string |
| 108 | + before string |
| 109 | + } |
| 110 | + var toRemove []string |
| 111 | + |
| 112 | + for i, val := range newVals { |
| 113 | + if _, exists := currValsMap[val]; !exists { |
| 114 | + var before string |
| 115 | + if i < len(newVals)-1 && len(currVals) > 0 { |
| 116 | + before = newVals[i+1] |
| 117 | + } |
| 118 | + toAdd = append(toAdd, struct { |
| 119 | + val string |
| 120 | + before string |
| 121 | + }{val, before}) |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + for val := range currValsMap { |
| 126 | + if _, exists := newValsMap[val]; !exists { |
| 127 | + toRemove = append(toRemove, val) |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + if len(toAdd) == 0 && len(toRemove) == 0 { |
| 132 | + return nil, nil |
| 133 | + } |
| 134 | + |
| 135 | + typeName := strings.TrimPrefix(col.OriginalType, "pg:") |
| 136 | + |
| 137 | + for _, add := range toAdd { |
| 138 | + |
| 139 | + if add.before != "" { |
| 140 | + res = append(res, fmt.Sprintf(`ALTER TYPE %s ADD VALUE IF NOT EXISTS '%s' BEFORE '%s'`, |
| 141 | + typeName, add.val, add.before)) |
| 142 | + } else { |
| 143 | + res = append(res, fmt.Sprintf(`ALTER TYPE %s ADD VALUE IF NOT EXISTS '%s'`, |
| 144 | + typeName, add.val)) |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + for _, val := range toRemove { |
| 149 | + res = append(res, fmt.Sprintf(`ALTER TYPE %s DROP VALUE IF EXISTS '%s'`, typeName, val)) |
| 150 | + } |
| 151 | + |
| 152 | + return res, nil |
| 153 | +} |
| 154 | + |
| 155 | +func addColsQuery(ftn string, added []abstract.ColSchema) (string, error) { |
| 156 | + if err := prepareOriginalTypes(added); err != nil { |
| 157 | + return "", xerrors.Errorf("addColsQuery:failed to prepare original types for parsing: %w", err) |
| 158 | + } |
| 159 | + |
| 160 | + b := strings.Builder{} |
| 161 | + b.WriteString(fmt.Sprintf(`ALTER TABLE %s `, ftn)) |
| 162 | + for idx, col := range added { |
| 163 | + var queryType string |
| 164 | + if col.OriginalType != "" { |
| 165 | + queryType = strings.TrimPrefix(col.OriginalType, "pg:") |
| 166 | + queryType = strings.ReplaceAll(queryType, "USER-DEFINED", "TEXT") |
| 167 | + } else { |
| 168 | + var err error |
| 169 | + queryType, err = DataToOriginal(col.DataType) |
| 170 | + if err != nil { |
| 171 | + return "", xerrors.Errorf("addColsQuery:failed to convert column %q to original type: %w", col.ColumnName, err) |
| 172 | + } |
| 173 | + } |
| 174 | + if strings.HasPrefix(col.Expression, "pg:") { |
| 175 | + queryType += " " + strings.TrimPrefix(col.Expression, "pg:") |
| 176 | + } |
| 177 | + if col.Required { |
| 178 | + queryType += " NOT NULL" |
| 179 | + } |
| 180 | + b.WriteString(fmt.Sprintf(`ADD COLUMN IF NOT EXISTS "%v" %v`, col.ColumnName, queryType)) |
| 181 | + if idx < len(added)-1 { |
| 182 | + b.WriteString(",") |
| 183 | + } |
| 184 | + } |
| 185 | + return b.String(), nil |
| 186 | +} |
0 commit comments