|
| 1 | +package diff |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "sort" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/jackc/pgconn" |
| 11 | + "github.com/jackc/pgx/v4" |
| 12 | + "github.com/supabase/cli/internal/utils" |
| 13 | + "github.com/supabase/cli/pkg/pgxv5" |
| 14 | +) |
| 15 | + |
| 16 | +const SELECT_VIEW_RELOPTIONS = `SELECT n.nspname AS nspname, |
| 17 | + c.relname AS relname, |
| 18 | + c.relkind::text AS relkind, |
| 19 | + COALESCE(c.reloptions, ARRAY[]::text[]) AS reloptions |
| 20 | + FROM pg_class c |
| 21 | + JOIN pg_namespace n ON c.relnamespace = n.oid |
| 22 | + WHERE c.relkind IN ('v','m') |
| 23 | + ORDER BY n.nspname, c.relname` |
| 24 | + |
| 25 | +type viewReloptionKey struct { |
| 26 | + schema string |
| 27 | + name string |
| 28 | + relkind string |
| 29 | +} |
| 30 | + |
| 31 | +type viewReloptionRow struct { |
| 32 | + Nspname string `db:"nspname"` |
| 33 | + Relname string `db:"relname"` |
| 34 | + Relkind string `db:"relkind"` |
| 35 | + Reloptions []string `db:"reloptions"` |
| 36 | +} |
| 37 | + |
| 38 | +func appendViewReloptionDiff(ctx context.Context, sql string, source, target pgconn.Config, schema []string, options ...func(*pgx.ConnConfig)) string { |
| 39 | + sourceConn, err := utils.ConnectByConfig(ctx, source, options...) |
| 40 | + if err != nil { |
| 41 | + fmt.Fprintln(os.Stderr, utils.Yellow("WARNING:"), "could not connect to source database to diff view reloptions:", err) |
| 42 | + return sql |
| 43 | + } |
| 44 | + defer sourceConn.Close(context.Background()) |
| 45 | + targetConn, err := utils.ConnectByConfig(ctx, target, options...) |
| 46 | + if err != nil { |
| 47 | + fmt.Fprintln(os.Stderr, utils.Yellow("WARNING:"), "could not connect to target database to diff view reloptions:", err) |
| 48 | + return sql |
| 49 | + } |
| 50 | + defer targetConn.Close(context.Background()) |
| 51 | + sourceReloptions, err := selectViewReloptions(ctx, sourceConn) |
| 52 | + if err != nil { |
| 53 | + fmt.Fprintln(os.Stderr, utils.Yellow("WARNING:"), "could not read source view reloptions:", err) |
| 54 | + return sql |
| 55 | + } |
| 56 | + targetReloptions, err := selectViewReloptions(ctx, targetConn) |
| 57 | + if err != nil { |
| 58 | + fmt.Fprintln(os.Stderr, utils.Yellow("WARNING:"), "could not read target view reloptions:", err) |
| 59 | + return sql |
| 60 | + } |
| 61 | + return appendDiffSQL(sql, buildViewReloptionDiff(sourceReloptions, targetReloptions, schema)) |
| 62 | +} |
| 63 | + |
| 64 | +func selectViewReloptions(ctx context.Context, conn *pgx.Conn) (map[viewReloptionKey][]string, error) { |
| 65 | + rows, err := conn.Query(ctx, SELECT_VIEW_RELOPTIONS) |
| 66 | + if err != nil { |
| 67 | + return nil, err |
| 68 | + } |
| 69 | + collected, err := pgxv5.CollectRows[viewReloptionRow](rows) |
| 70 | + if err != nil { |
| 71 | + return nil, err |
| 72 | + } |
| 73 | + out := make(map[viewReloptionKey][]string, len(collected)) |
| 74 | + for _, r := range collected { |
| 75 | + out[viewReloptionKey{schema: r.Nspname, name: r.Relname, relkind: r.Relkind}] = r.Reloptions |
| 76 | + } |
| 77 | + return out, nil |
| 78 | +} |
| 79 | + |
| 80 | +func buildViewReloptionDiff(source, target map[viewReloptionKey][]string, schema []string) string { |
| 81 | + if len(source) == 0 || len(target) == 0 { |
| 82 | + return "" |
| 83 | + } |
| 84 | + includeSchema := schemaFilter(schema) |
| 85 | + keys := make([]viewReloptionKey, 0, len(target)) |
| 86 | + for key := range target { |
| 87 | + if !includeSchema(key.schema) { |
| 88 | + continue |
| 89 | + } |
| 90 | + if _, ok := source[key]; ok { |
| 91 | + keys = append(keys, key) |
| 92 | + } |
| 93 | + } |
| 94 | + sort.Slice(keys, func(i, j int) bool { |
| 95 | + if keys[i].schema != keys[j].schema { |
| 96 | + return keys[i].schema < keys[j].schema |
| 97 | + } |
| 98 | + if keys[i].name != keys[j].name { |
| 99 | + return keys[i].name < keys[j].name |
| 100 | + } |
| 101 | + return keys[i].relkind < keys[j].relkind |
| 102 | + }) |
| 103 | + var statements []string |
| 104 | + for _, key := range keys { |
| 105 | + statements = append(statements, buildAlterViewReloptions(key, source[key], target[key])...) |
| 106 | + } |
| 107 | + return strings.Join(statements, "") |
| 108 | +} |
| 109 | + |
| 110 | +func schemaFilter(schema []string) func(string) bool { |
| 111 | + if len(schema) > 0 { |
| 112 | + included := make(map[string]bool, len(schema)) |
| 113 | + for _, name := range schema { |
| 114 | + included[name] = true |
| 115 | + } |
| 116 | + return func(name string) bool { |
| 117 | + return included[name] |
| 118 | + } |
| 119 | + } |
| 120 | + excluded := make(map[string]bool, len(managedSchemas)+2) |
| 121 | + for _, name := range managedSchemas { |
| 122 | + excluded[name] = true |
| 123 | + } |
| 124 | + excluded["information_schema"] = true |
| 125 | + excluded["pg_catalog"] = true |
| 126 | + return func(name string) bool { |
| 127 | + return !excluded[name] && !strings.HasPrefix(name, "pg_") |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func buildAlterViewReloptions(key viewReloptionKey, source, target []string) []string { |
| 132 | + sourceOpts := reloptionsByName(source) |
| 133 | + targetOpts := reloptionsByName(target) |
| 134 | + var setNames []string |
| 135 | + for name, targetOpt := range targetOpts { |
| 136 | + if sourceOpt, ok := sourceOpts[name]; !ok || sourceOpt.raw != targetOpt.raw { |
| 137 | + setNames = append(setNames, name) |
| 138 | + } |
| 139 | + } |
| 140 | + var resetNames []string |
| 141 | + for name := range sourceOpts { |
| 142 | + if _, ok := targetOpts[name]; !ok { |
| 143 | + resetNames = append(resetNames, name) |
| 144 | + } |
| 145 | + } |
| 146 | + sort.Strings(setNames) |
| 147 | + sort.Strings(resetNames) |
| 148 | + alterPrefix := "ALTER VIEW " |
| 149 | + if key.relkind == "m" { |
| 150 | + alterPrefix = "ALTER MATERIALIZED VIEW " |
| 151 | + } |
| 152 | + viewName := quoteIdentifier(key.schema) + "." + quoteIdentifier(key.name) |
| 153 | + var statements []string |
| 154 | + if len(setNames) > 0 { |
| 155 | + opts := make([]string, len(setNames)) |
| 156 | + for i, name := range setNames { |
| 157 | + opts[i] = targetOpts[name].raw |
| 158 | + } |
| 159 | + statements = append(statements, fmt.Sprintf("%s%s SET (%s);\n", alterPrefix, viewName, strings.Join(opts, ", "))) |
| 160 | + } |
| 161 | + if len(resetNames) > 0 { |
| 162 | + statements = append(statements, fmt.Sprintf("%s%s RESET (%s);\n", alterPrefix, viewName, strings.Join(resetNames, ", "))) |
| 163 | + } |
| 164 | + return statements |
| 165 | +} |
| 166 | + |
| 167 | +type reloption struct { |
| 168 | + raw string |
| 169 | +} |
| 170 | + |
| 171 | +func reloptionsByName(options []string) map[string]reloption { |
| 172 | + out := make(map[string]reloption, len(options)) |
| 173 | + for _, raw := range options { |
| 174 | + name, _, _ := strings.Cut(raw, "=") |
| 175 | + if name == "" { |
| 176 | + continue |
| 177 | + } |
| 178 | + out[name] = reloption{raw: raw} |
| 179 | + } |
| 180 | + return out |
| 181 | +} |
| 182 | + |
| 183 | +func appendDiffSQL(sql, extra string) string { |
| 184 | + if extra == "" { |
| 185 | + return sql |
| 186 | + } |
| 187 | + if strings.TrimSpace(sql) == "" { |
| 188 | + return extra |
| 189 | + } |
| 190 | + if strings.HasSuffix(sql, "\n") { |
| 191 | + return sql + extra |
| 192 | + } |
| 193 | + return sql + "\n" + extra |
| 194 | +} |
| 195 | + |
| 196 | +func quoteIdentifier(identifier string) string { |
| 197 | + return `"` + strings.ReplaceAll(identifier, `"`, `""`) + `"` |
| 198 | +} |
0 commit comments