Skip to content

Commit

Permalink
large chunk of the work necessary to get XML export functional
Browse files Browse the repository at this point in the history
  • Loading branch information
williammoran committed May 2, 2024
1 parent 32279c3 commit f9771bb
Show file tree
Hide file tree
Showing 21 changed files with 949 additions and 146 deletions.
22 changes: 11 additions & 11 deletions lib/dbsteward.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ func (dbsteward *DBSteward) doXmlDataInsert(defFile string, dataFile string) {

defFileModified := defFile + ".xmldatainserted"
dbsteward.Info("Saving modified dbsteward definition as %s", defFileModified)
err = xml.SaveDefinition(defFileModified, defDoc)
err = xml.SaveDefinition(dbsteward.Logger(), defFileModified, defDoc)
dbsteward.FatalIfError(err, "saving file")
}
func (dbsteward *DBSteward) doXmlSort(files []string) {
Expand All @@ -493,7 +493,7 @@ func (dbsteward *DBSteward) doXmlConvert(files []string) {
doc, err := xml.LoadDefintion(file)
dbsteward.FatalIfError(err, "Could not load %s", file)
xml.SqlFormatConvert(doc)
convertedXml, err := xml.FormatXml(doc)
convertedXml, err := xml.FormatXml(dbsteward.Logger(), doc)
dbsteward.FatalIfError(err, "formatting xml")
convertedXml = strings.Replace(convertedXml, "pgdbxml>", "dbsteward>", -1)
err = util.WriteFile(convertedXml, convertedFileName)
Expand All @@ -509,7 +509,7 @@ func (dbsteward *DBSteward) doXmlSlonyId(files []string, slonyOut string) {
outputPrefix := dbsteward.calculateFileOutputPrefix(files)
compositeFile := outputPrefix + "_composite.xml"
dbsteward.Info("Saving composite as %s", compositeFile)
err = xml.SaveDefinition(compositeFile, dbDoc)
err = xml.SaveDefinition(dbsteward.Logger(), compositeFile, dbDoc)
dbsteward.FatalIfError(err, "saving file")

dbsteward.Info("Slony ID numbering any missing attributes")
Expand All @@ -521,7 +521,7 @@ func (dbsteward *DBSteward) doXmlSlonyId(files []string, slonyOut string) {
slonyIdNumberedFile = slonyOut
}
dbsteward.Info("Saving Slony ID numbered XML as %s", slonyIdNumberedFile)
err = xml.SaveDefinition(slonyIdNumberedFile, slonyIdDoc)
err = xml.SaveDefinition(dbsteward.Logger(), slonyIdNumberedFile, slonyIdDoc)
dbsteward.FatalIfError(err, "saving file")
}
func (dbsteward *DBSteward) doBuild(files []string, dataFiles []string, addendums uint) {
Expand All @@ -542,13 +542,13 @@ func (dbsteward *DBSteward) doBuild(files []string, dataFiles []string, addendum
outputPrefix := dbsteward.calculateFileOutputPrefix(files)
compositeFile := outputPrefix + "_composite.xml"
dbsteward.Info("Saving composite as %s", compositeFile)
err = xml.SaveDefinition(compositeFile, dbDoc)
err = xml.SaveDefinition(dbsteward.Logger(), compositeFile, dbDoc)
dbsteward.FatalIfError(err, "saving file")

if addendumsDoc != nil {
addendumsFile := outputPrefix + "_addendums.xml"
dbsteward.Info("Saving addendums as %s", addendumsFile)
err = xml.SaveDefinition(compositeFile, addendumsDoc)
err = xml.SaveDefinition(dbsteward.Logger(), compositeFile, addendumsDoc)
dbsteward.FatalIfError(err, "saving file")
}

Expand All @@ -573,13 +573,13 @@ func (dbsteward *DBSteward) doDiff(oldFiles []string, newFiles []string, dataFil
oldOutputPrefix := dbsteward.calculateFileOutputPrefix(oldFiles)
oldCompositeFile := oldOutputPrefix + "_composite.xml"
dbsteward.Info("Saving composite as %s", oldCompositeFile)
err = xml.SaveDefinition(oldCompositeFile, oldDbDoc)
err = xml.SaveDefinition(dbsteward.Logger(), oldCompositeFile, oldDbDoc)
dbsteward.FatalIfError(err, "saving file")

newOutputPrefix := dbsteward.calculateFileOutputPrefix(newFiles)
newCompositeFile := newOutputPrefix + "_composite.xml"
dbsteward.Info("Saving composite as %s", newCompositeFile)
err = xml.SaveDefinition(newCompositeFile, newDbDoc)
err = xml.SaveDefinition(dbsteward.Logger(), newCompositeFile, newDbDoc)
dbsteward.FatalIfError(err, "saving file")

err = dbsteward.Lookup().OperationsConstructor().BuildUpgrade(
Expand All @@ -591,7 +591,7 @@ func (dbsteward *DBSteward) doDiff(oldFiles []string, newFiles []string, dataFil
func (dbsteward *DBSteward) doExtract(dbHost string, dbPort uint, dbName, dbUser, dbPass string, outputFile string) {
output := dbsteward.Lookup().OperationsConstructor().ExtractSchema(dbHost, dbPort, dbName, dbUser, dbPass)
dbsteward.Info("Saving extracted database schema to %s", outputFile)
err := xml.SaveDefinition(outputFile, output)
err := xml.SaveDefinition(dbsteward.Logger(), outputFile, output)
dbsteward.FatalIfError(err, "saving file")
}
func (dbsteward *DBSteward) doDbDataDiff(files []string, dataFiles []string, addendums uint, dbHost string, dbPort uint, dbName, dbUser, dbPass string) {
Expand All @@ -614,12 +614,12 @@ func (dbsteward *DBSteward) doDbDataDiff(files []string, dataFiles []string, add
outputPrefix := dbsteward.calculateFileOutputPrefix(files)
compositeFile := outputPrefix + "_composite.xml"
dbsteward.Info("Saving composite as %s", compositeFile)
err = xml.SaveDefinition(compositeFile, dbDoc)
err = xml.SaveDefinition(dbsteward.Logger(), compositeFile, dbDoc)
dbsteward.FatalIfError(err, "saving file")

output, err := dbsteward.Lookup().OperationsConstructor().CompareDbData(dbDoc, dbHost, dbPort, dbName, dbUser, dbPass)
dbsteward.FatalIfError(err, "comparing data")
err = xml.SaveDefinition(compositeFile, output)
err = xml.SaveDefinition(dbsteward.Logger(), compositeFile, output)
dbsteward.FatalIfError(err, "saving file")
}
func (dbsteward *DBSteward) doSqlDiff(oldSql, newSql []string, outputFile string) {
Expand Down
63 changes: 59 additions & 4 deletions lib/encoding/xml/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package xml
import (
"encoding/xml"
"fmt"
"log/slog"

"github.com/dbsteward/dbsteward/lib/ir"
)
Expand Down Expand Up @@ -42,18 +43,72 @@ type Column struct {
AfterAddPostStage3 string `xml:"afterAddPostStage3,attr,omitempty"`
}

func ColumnsFromIR(l *slog.Logger, cols []*ir.Column) ([]*Column, error) {
if len(cols) == 0 {
return nil, nil
}
var rv []*Column
for _, c := range cols {
if c != nil {
nc, err := ColumnFromIR(l, c)
if err != nil {
return nil, err
}
rv = append(rv, nc)
}
}
return rv, nil
}

func ColumnFromIR(l *slog.Logger, col *ir.Column) (*Column, error) {
if col == nil {
return nil, nil
}
l = l.With(slog.String("column", col.Name))
l.Debug("converting column")
defer l.Debug("done converting column")
rv := Column{
Name: col.Name,
Type: col.Type,
Nullable: col.Nullable,
Default: col.Default,
Description: col.Description,
Unique: col.Unique,
Check: col.Check,
SerialStart: col.SerialStart,
OldColumnName: col.OldColumnName,
ConvertUsing: col.ConvertUsing,
ForeignSchema: col.ForeignSchema,
ForeignTable: col.ForeignTable,
ForeignColumn: col.ForeignColumn,
ForeignKeyName: col.ForeignKeyName,
ForeignIndexName: col.ForeignIndexName,
ForeignOnUpdate: string(col.ForeignOnUpdate),
ForeignOnDelete: string(col.ForeignOnDelete),
Statistics: col.Statistics,
BeforeAddStage1: col.BeforeAddStage1,
AfterAddStage1: col.AfterAddStage1,
BeforeAddStage2: col.BeforeAddStage2,
AfterAddStage2: col.AfterAddStage2,
BeforeAddStage3: col.BeforeAddStage3,
AfterAddStage3: col.AfterAddStage3,
// Ignoring depricated fields for now
}
return &rv, nil
}

// Implement some custom unmarshalling behavior
func (self *Column) UnmarshalXML(decoder *xml.Decoder, start xml.StartElement) error {
func (col *Column) UnmarshalXML(decoder *xml.Decoder, start xml.StartElement) error {
type colAlias Column // prevents recursion while decoding, as type aliases have no methods
// set defaults
col := &colAlias{
ca := &colAlias{
Nullable: true, // as in SQL NULL
}
err := decoder.DecodeElement(col, &start)
err := decoder.DecodeElement(ca, &start)
if err != nil {
return err
}
*self = Column(*col)
*col = Column(*col)
return nil
}

Expand Down
34 changes: 29 additions & 5 deletions lib/encoding/xml/constraint.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package xml

import (
"fmt"
"log/slog"
"strings"

"github.com/dbsteward/dbsteward/lib/ir"
Expand All @@ -16,6 +17,29 @@ type Constraint struct {
ForeignTable string `xml:"foreignTable,attr,omitempty"`
}

func ConstraintsFromIR(l *slog.Logger, cs []*ir.Constraint) ([]*Constraint, error) {
if len(cs) == 0 {
return nil, nil
}
var rv []*Constraint
for _, c := range cs {
if c != nil {
rv = append(
rv,
&Constraint{
Name: c.Name,
Type: string(c.Type),
Definition: c.Definition,
ForeignIndexName: c.ForeignIndexName,
ForeignSchema: c.ForeignSchema,
ForeignTable: c.ForeignTable,
},
)
}
}
return rv, nil
}

func (c *Constraint) ToIR() (*ir.Constraint, error) {
rv := ir.Constraint{
Name: c.Name,
Expand All @@ -32,17 +56,17 @@ func (c *Constraint) ToIR() (*ir.Constraint, error) {
return &rv, nil
}

func (self *Constraint) IdentityMatches(other *Constraint) bool {
func (c *Constraint) IdentityMatches(other *Constraint) bool {
if other == nil {
return false
}
return strings.EqualFold(self.Name, other.Name)
return strings.EqualFold(c.Name, other.Name)
}

func (self *Constraint) Merge(overlay *Constraint) {
func (c *Constraint) Merge(overlay *Constraint) {
if overlay == nil {
return
}
self.Type = overlay.Type
self.Definition = overlay.Definition
c.Type = overlay.Type
c.Definition = overlay.Definition
}
Loading

0 comments on commit f9771bb

Please sign in to comment.