More Roundtrip tests
This commit is contained in:
@@ -29,7 +29,6 @@ func (w *Writer) WriteDatabase(db *models.Database) error {
|
||||
return os.WriteFile(w.options.OutputPath, []byte(content), 0644)
|
||||
}
|
||||
|
||||
// If no output path, print to stdout
|
||||
fmt.Print(content)
|
||||
return nil
|
||||
}
|
||||
@@ -48,7 +47,7 @@ func (w *Writer) WriteSchema(schema *models.Schema) error {
|
||||
|
||||
// WriteTable writes a Table model to DBML format
|
||||
func (w *Writer) WriteTable(table *models.Table) error {
|
||||
content := w.tableToDBML(table, table.Schema)
|
||||
content := w.tableToDBML(table)
|
||||
|
||||
if w.options.OutputPath != "" {
|
||||
return os.WriteFile(w.options.OutputPath, []byte(content), 0644)
|
||||
@@ -60,70 +59,63 @@ func (w *Writer) WriteTable(table *models.Table) error {
|
||||
|
||||
// databaseToDBML converts a Database to DBML format string
|
||||
func (w *Writer) databaseToDBML(d *models.Database) string {
|
||||
var result string
|
||||
var sb strings.Builder
|
||||
|
||||
// Add database comment if exists
|
||||
if d.Description != "" {
|
||||
result += fmt.Sprintf("// %s\n", d.Description)
|
||||
sb.WriteString(fmt.Sprintf("// %s\n", d.Description))
|
||||
}
|
||||
if d.Comment != "" {
|
||||
result += fmt.Sprintf("// %s\n", d.Comment)
|
||||
sb.WriteString(fmt.Sprintf("// %s\n", d.Comment))
|
||||
}
|
||||
if d.Description != "" || d.Comment != "" {
|
||||
result += "\n"
|
||||
sb.WriteString("\n")
|
||||
}
|
||||
|
||||
// Process each schema
|
||||
for _, schema := range d.Schemas {
|
||||
result += w.schemaToDBML(schema)
|
||||
sb.WriteString(w.schemaToDBML(schema))
|
||||
}
|
||||
|
||||
// Add relationships
|
||||
result += "\n// Relationships\n"
|
||||
sb.WriteString("\n// Relationships\n")
|
||||
for _, schema := range d.Schemas {
|
||||
for _, table := range schema.Tables {
|
||||
for _, constraint := range table.Constraints {
|
||||
if constraint.Type == models.ForeignKeyConstraint {
|
||||
result += w.constraintToDBML(constraint, schema.Name, table.Name)
|
||||
sb.WriteString(w.constraintToDBML(constraint, table))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// schemaToDBML converts a Schema to DBML format string
|
||||
func (w *Writer) schemaToDBML(schema *models.Schema) string {
|
||||
var result string
|
||||
var sb strings.Builder
|
||||
|
||||
if schema.Description != "" {
|
||||
result += fmt.Sprintf("// Schema: %s - %s\n", schema.Name, schema.Description)
|
||||
sb.WriteString(fmt.Sprintf("// Schema: %s - %s\n", schema.Name, schema.Description))
|
||||
}
|
||||
|
||||
// Process tables
|
||||
for _, table := range schema.Tables {
|
||||
result += w.tableToDBML(table, schema.Name)
|
||||
result += "\n"
|
||||
sb.WriteString(w.tableToDBML(table))
|
||||
sb.WriteString("\n")
|
||||
}
|
||||
|
||||
return result
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// tableToDBML converts a Table to DBML format string
|
||||
func (w *Writer) tableToDBML(t *models.Table, schemaName string) string {
|
||||
var result string
|
||||
func (w *Writer) tableToDBML(t *models.Table) string {
|
||||
var sb strings.Builder
|
||||
|
||||
// Table definition
|
||||
tableName := fmt.Sprintf("%s.%s", schemaName, t.Name)
|
||||
result += fmt.Sprintf("Table %s {\n", tableName)
|
||||
tableName := fmt.Sprintf("%s.%s", t.Schema, t.Name)
|
||||
sb.WriteString(fmt.Sprintf("Table %s {\n", tableName))
|
||||
|
||||
// Add columns
|
||||
for _, column := range t.Columns {
|
||||
result += fmt.Sprintf(" %s %s", column.Name, column.Type)
|
||||
sb.WriteString(fmt.Sprintf(" %s %s", column.Name, column.Type))
|
||||
|
||||
// Add column attributes
|
||||
attrs := make([]string, 0)
|
||||
var attrs []string
|
||||
if column.IsPrimaryKey {
|
||||
attrs = append(attrs, "pk")
|
||||
}
|
||||
@@ -134,77 +126,74 @@ func (w *Writer) tableToDBML(t *models.Table, schemaName string) string {
|
||||
attrs = append(attrs, "increment")
|
||||
}
|
||||
if column.Default != nil {
|
||||
attrs = append(attrs, fmt.Sprintf("default: %v", column.Default))
|
||||
attrs = append(attrs, fmt.Sprintf("default: `%v`", column.Default))
|
||||
}
|
||||
|
||||
if len(attrs) > 0 {
|
||||
result += fmt.Sprintf(" [%s]", strings.Join(attrs, ", "))
|
||||
sb.WriteString(fmt.Sprintf(" [%s]", strings.Join(attrs, ", ")))
|
||||
}
|
||||
|
||||
if column.Comment != "" {
|
||||
result += fmt.Sprintf(" // %s", column.Comment)
|
||||
sb.WriteString(fmt.Sprintf(" // %s", column.Comment))
|
||||
}
|
||||
result += "\n"
|
||||
sb.WriteString("\n")
|
||||
}
|
||||
|
||||
// Add indexes
|
||||
indexCount := 0
|
||||
for _, index := range t.Indexes {
|
||||
if indexCount == 0 {
|
||||
result += "\n indexes {\n"
|
||||
}
|
||||
indexAttrs := make([]string, 0)
|
||||
if index.Unique {
|
||||
indexAttrs = append(indexAttrs, "unique")
|
||||
}
|
||||
if index.Name != "" {
|
||||
indexAttrs = append(indexAttrs, fmt.Sprintf("name: '%s'", index.Name))
|
||||
}
|
||||
if index.Type != "" {
|
||||
indexAttrs = append(indexAttrs, fmt.Sprintf("type: %s", index.Type))
|
||||
}
|
||||
if len(t.Indexes) > 0 {
|
||||
sb.WriteString("\n indexes {\n")
|
||||
for _, index := range t.Indexes {
|
||||
var indexAttrs []string
|
||||
if index.Unique {
|
||||
indexAttrs = append(indexAttrs, "unique")
|
||||
}
|
||||
if index.Name != "" {
|
||||
indexAttrs = append(indexAttrs, fmt.Sprintf("name: '%s'", index.Name))
|
||||
}
|
||||
if index.Type != "" {
|
||||
indexAttrs = append(indexAttrs, fmt.Sprintf("type: %s", index.Type))
|
||||
}
|
||||
|
||||
result += fmt.Sprintf(" (%s)", strings.Join(index.Columns, ", "))
|
||||
if len(indexAttrs) > 0 {
|
||||
result += fmt.Sprintf(" [%s]", strings.Join(indexAttrs, ", "))
|
||||
sb.WriteString(fmt.Sprintf(" (%s)", strings.Join(index.Columns, ", ")))
|
||||
if len(indexAttrs) > 0 {
|
||||
sb.WriteString(fmt.Sprintf(" [%s]", strings.Join(indexAttrs, ", ")))
|
||||
}
|
||||
sb.WriteString("\n")
|
||||
}
|
||||
result += "\n"
|
||||
indexCount++
|
||||
}
|
||||
if indexCount > 0 {
|
||||
result += " }\n"
|
||||
sb.WriteString(" }\n")
|
||||
}
|
||||
|
||||
// Add table note
|
||||
if t.Description != "" || t.Comment != "" {
|
||||
note := t.Description
|
||||
if note != "" && t.Comment != "" {
|
||||
note += " - "
|
||||
}
|
||||
note += t.Comment
|
||||
result += fmt.Sprintf("\n Note: '%s'\n", note)
|
||||
note := strings.TrimSpace(t.Description + " " + t.Comment)
|
||||
if note != "" {
|
||||
sb.WriteString(fmt.Sprintf("\n Note: '%s'\n", note))
|
||||
}
|
||||
|
||||
result += "}\n"
|
||||
return result
|
||||
sb.WriteString("}\n")
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// constraintToDBML converts a Constraint to DBML format string
|
||||
func (w *Writer) constraintToDBML(c *models.Constraint, schemaName, tableName string) string {
|
||||
func (w *Writer) constraintToDBML(c *models.Constraint, t *models.Table) string {
|
||||
if c.Type != models.ForeignKeyConstraint || c.ReferencedTable == "" {
|
||||
return ""
|
||||
}
|
||||
|
||||
fromTable := fmt.Sprintf("%s.%s", schemaName, tableName)
|
||||
fromTable := fmt.Sprintf("%s.%s", c.Schema, c.Table)
|
||||
toTable := fmt.Sprintf("%s.%s", c.ReferencedSchema, c.ReferencedTable)
|
||||
|
||||
// Determine relationship cardinality
|
||||
// For foreign keys, it's typically many-to-one
|
||||
relationship := ">"
|
||||
relationship := ">" // Default to many-to-one
|
||||
for _, index := range t.Indexes {
|
||||
if index.Unique && strings.Join(index.Columns, ",") == strings.Join(c.Columns, ",") {
|
||||
relationship = "-" // one-to-one
|
||||
break
|
||||
}
|
||||
}
|
||||
for _, column := range c.Columns {
|
||||
if t.Columns[column].IsPrimaryKey {
|
||||
relationship = "-" // one-to-one
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// Build from and to column references
|
||||
// For single columns: table.column
|
||||
// For multiple columns: table.(col1, col2)
|
||||
var fromRef, toRef string
|
||||
if len(c.Columns) == 1 {
|
||||
fromRef = fmt.Sprintf("%s.%s", fromTable, c.Columns[0])
|
||||
@@ -218,20 +207,18 @@ func (w *Writer) constraintToDBML(c *models.Constraint, schemaName, tableName st
|
||||
toRef = fmt.Sprintf("%s.(%s)", toTable, strings.Join(c.ReferencedColumns, ", "))
|
||||
}
|
||||
|
||||
result := fmt.Sprintf("Ref: %s %s %s", fromRef, relationship, toRef)
|
||||
|
||||
// Add actions
|
||||
actions := make([]string, 0)
|
||||
var actions []string
|
||||
if c.OnDelete != "" {
|
||||
actions = append(actions, fmt.Sprintf("ondelete: %s", c.OnDelete))
|
||||
actions = append(actions, fmt.Sprintf("delete: %s", c.OnDelete))
|
||||
}
|
||||
if c.OnUpdate != "" {
|
||||
actions = append(actions, fmt.Sprintf("onupdate: %s", c.OnUpdate))
|
||||
}
|
||||
if len(actions) > 0 {
|
||||
result += fmt.Sprintf(" [%s]", strings.Join(actions, ", "))
|
||||
actions = append(actions, fmt.Sprintf("update: %s", c.OnUpdate))
|
||||
}
|
||||
|
||||
result += "\n"
|
||||
return result
|
||||
}
|
||||
refLine := fmt.Sprintf("Ref: %s %s %s", fromRef, relationship, toRef)
|
||||
if len(actions) > 0 {
|
||||
refLine += fmt.Sprintf(" [%s]", strings.Join(actions, ", "))
|
||||
}
|
||||
|
||||
return refLine + "\n"
|
||||
}
|
||||
Reference in New Issue
Block a user