File size: 7,211 Bytes
1e92f2d |
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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
package pgdump
import (
"context"
"fmt"
"strings"
"github.com/target/goalert/devtools/pgdump-lite/pgd"
)
type Schema struct {
Extensions []Extension
Functions []Function
Tables []Table
Enums []Enum
Sequences []Sequence
}
func (s Schema) String() string {
var b strings.Builder
b.WriteString("-- Extensions\n\n")
for _, e := range s.Extensions {
b.WriteString(e.String())
b.WriteString("\n\n")
}
b.WriteString("-- Enums\n\n")
for _, e := range s.Enums {
b.WriteString(e.String())
b.WriteString("\n\n")
}
b.WriteString("-- Functions\n\n")
for _, e := range s.Functions {
b.WriteString(e.String())
b.WriteString("\n\n")
}
b.WriteString("-- Tables\n\n")
for _, e := range s.Tables {
b.WriteString(e.String())
b.WriteString("\n\n")
}
b.WriteString("-- Sequences\n\n")
for _, e := range s.Sequences {
b.WriteString(e.String())
b.WriteString("\n\n")
}
return b.String()
}
type Index struct {
Name string
Def string
}
func (idx Index) EntityName() string { return idx.Name }
func (idx Index) String() string { return idx.Def + ";" }
type Trigger struct {
Name string
Def string
}
func (t Trigger) EntityName() string { return t.Name }
func (t Trigger) String() string { return t.Def + ";" }
type Sequence struct {
Name string
StartValue int64
Increment int64
MinValue int64
MaxValue int64
Cache int64
OwnedBy string
}
func (s Sequence) EntityName() string { return s.Name }
func (s Sequence) String() string {
def := fmt.Sprintf("CREATE SEQUENCE %s\n\tSTART WITH %d\n\tINCREMENT BY %d\n\tMINVALUE %d\n\tMAXVALUE %d\n\tCACHE %d",
s.Name, s.StartValue, s.Increment, s.MinValue, s.MaxValue, s.Cache)
if s.OwnedBy == "" {
return def + ";"
}
return fmt.Sprintf("%s\n\tOWNED BY%s;",
def,
s.OwnedBy,
)
}
type Extension struct {
Name string
}
func (e Extension) EntityName() string { return e.Name }
func (e Extension) String() string {
return fmt.Sprintf("CREATE EXTENSION IF NOT EXISTS %s;", e.Name)
}
type Function struct {
Name string
Def string
}
func (f Function) EntityName() string { return f.Name }
func (f Function) String() string { return f.Def + ";" }
type Enum struct {
Name string
Values []string
}
func (e Enum) EntityName() string { return e.Name }
func (e Enum) String() string {
return fmt.Sprintf("CREATE TYPE %s AS ENUM (\n\t'%s'\n);", e.Name, strings.Join(e.Values, "',\n\t'"))
}
type Table struct {
Name string
Columns []Column
Constraints []Constraint
Indexes []Index
Triggers []Trigger
Sequences []Sequence
}
func (t Table) EntityName() string { return t.Name }
func (t Table) String() string {
var lines []string
for _, c := range t.Columns {
lines = append(lines, c.String())
}
for _, c := range t.Constraints {
lines = append(lines, c.String())
}
var b strings.Builder
fmt.Fprintf(&b, "CREATE TABLE %s (\n\t%s\n);\n", t.Name, strings.Join(lines, ",\n\t"))
if len(t.Indexes) > 0 {
b.WriteString("\n")
}
for _, idx := range t.Indexes {
b.WriteString(idx.String())
b.WriteString("\n")
}
if len(t.Triggers) > 0 {
b.WriteString("\n")
}
for _, trg := range t.Triggers {
b.WriteString(trg.String())
b.WriteString("\n")
}
return b.String()
}
type Constraint struct {
Name string
Def string
}
func (c Constraint) EntityName() string { return c.Name }
func (c Constraint) String() string {
return fmt.Sprintf("CONSTRAINT %s %s", c.Name, c.Def)
}
type Column struct {
Name string
Type string
NotNull bool
DefaultValue string
}
func (c Column) EntityName() string { return c.Name }
func (c Column) String() string {
var def string
if c.DefaultValue != "" {
def = fmt.Sprintf(" DEFAULT %s", c.DefaultValue)
}
if c.NotNull {
def += " NOT NULL"
}
return fmt.Sprintf("%s %s%s", c.Name, c.Type, def)
}
func DumpSchema(ctx context.Context, conn pgd.DBTX) (*Schema, error) {
db := pgd.New(conn)
var s Schema
// list extensions
exts, err := db.ListExtensions(ctx)
if err != nil {
return nil, fmt.Errorf("list extensions: %w", err)
}
for _, e := range exts {
s.Extensions = append(s.Extensions, Extension{Name: e.ExtName})
}
// list enums
enums, err := db.ListEnums(ctx)
if err != nil {
return nil, fmt.Errorf("list types: %w", err)
}
for _, e := range enums {
s.Enums = append(s.Enums, Enum{
Name: e.EnumName,
Values: strings.Split(string(e.EnumValues), ","),
})
}
// list functions
funcs, err := db.ListFunctions(ctx)
if err != nil {
return nil, fmt.Errorf("list functions: %w", err)
}
for _, f := range funcs {
s.Functions = append(s.Functions, Function{
Name: f.FunctionName,
Def: f.FuncDef,
})
}
seqs, err := db.ListSequences(ctx)
if err != nil {
return nil, fmt.Errorf("list sequences: %w", err)
}
for _, seq := range seqs {
if seq.TableName != "" {
continue
}
s.Sequences = append(s.Sequences, Sequence{
Name: seq.SequenceName,
StartValue: seq.StartValue.Int64,
Increment: seq.Increment.Int64,
MinValue: seq.MinValue.Int64,
MaxValue: seq.MaxValue.Int64,
Cache: seq.Cache.Int64,
})
}
cols, err := db.ListColumns(ctx)
if err != nil {
return nil, fmt.Errorf("list columns: %w", err)
}
var tables []string
for _, c := range cols {
// these are always sorted by schema, then table
if len(tables) == 0 || tables[len(tables)-1] != c.TableName {
tables = append(tables, c.TableName)
}
}
cstr, err := db.ListConstraints(ctx)
if err != nil {
return nil, fmt.Errorf("list constraints: %w", err)
}
idxs, err := db.ListIndexes(ctx)
if err != nil {
return nil, fmt.Errorf("list indexes: %w", err)
}
trgs, err := db.ListTriggers(ctx)
if err != nil {
return nil, fmt.Errorf("list triggers: %w", err)
}
for _, tbl := range tables {
t := Table{Name: tbl}
for _, c := range cols {
if c.TableName != tbl {
continue
}
t.Columns = append(t.Columns, Column{
Name: c.ColumnName,
Type: c.ColumnType,
NotNull: c.NotNull,
DefaultValue: c.ColumnDefault,
})
}
for _, c := range cstr {
if c.TableName != tbl {
continue
}
if strings.HasPrefix(c.ConstraintDefinition, "TRIGGER") {
// skip triggers
continue
}
t.Constraints = append(t.Constraints, Constraint{
Name: c.ConstraintName,
Def: c.ConstraintDefinition,
})
}
for _, idx := range idxs {
if idx.TableName != tbl {
continue
}
t.Indexes = append(t.Indexes, Index{
Name: idx.IndexName,
Def: idx.IndexDefinition,
})
}
for _, trg := range trgs {
if trg.TableName != tbl {
continue
}
t.Triggers = append(t.Triggers, Trigger{
Name: trg.TriggerName,
Def: trg.TriggerDefinition,
})
}
for _, seq := range seqs {
if seq.TableName != tbl {
continue
}
t.Sequences = append(t.Sequences, Sequence{
Name: seq.SequenceName,
StartValue: seq.StartValue.Int64,
Increment: seq.Increment.Int64,
MinValue: seq.MinValue.Int64,
MaxValue: seq.MaxValue.Int64,
Cache: seq.Cache.Int64,
OwnedBy: seq.TableName + "." + seq.ColumnName,
})
}
s.Tables = append(s.Tables, t)
}
return &s, nil
}
|