feat(dbml): support multiline table notes in DBML
* Add parsing for triple-quoted table notes in DBML * Update writer to format multiline notes correctly * Enhance tests for multiline table notes handling
This commit is contained in:
@@ -434,6 +434,9 @@ func (r *Reader) parseDBML(content string) (*models.Database, error) {
|
||||
var currentSchema string
|
||||
var inIndexes bool
|
||||
var inTable bool
|
||||
var inTableNote bool
|
||||
var tableNoteLines []string
|
||||
tableNoteStartLine := 0
|
||||
var columnSeq uint
|
||||
var lastIndex *models.Index // most recent index in the current Indexes block
|
||||
lineNo := 0
|
||||
@@ -443,7 +446,22 @@ func (r *Reader) parseDBML(content string) (*models.Database, error) {
|
||||
|
||||
for scanner.Scan() {
|
||||
lineNo++
|
||||
line := strings.TrimSpace(scanner.Text())
|
||||
rawLine := scanner.Text()
|
||||
line := strings.TrimSpace(rawLine)
|
||||
|
||||
// A table note can use DBML's triple-quoted form. Its contents must be
|
||||
// consumed before normal parsing, otherwise each prose line is mistaken
|
||||
// for a column declaration.
|
||||
if inTableNote {
|
||||
if line == "'''" {
|
||||
currentTable.Description = strings.TrimSpace(strings.Join(tableNoteLines, "\n"))
|
||||
inTableNote = false
|
||||
tableNoteLines = nil
|
||||
continue
|
||||
}
|
||||
tableNoteLines = append(tableNoteLines, strings.TrimSpace(rawLine))
|
||||
continue
|
||||
}
|
||||
|
||||
// Skip empty lines and comments
|
||||
if line == "" || strings.HasPrefix(line, "//") {
|
||||
@@ -542,6 +560,12 @@ func (r *Reader) parseDBML(content string) (*models.Database, error) {
|
||||
// Parse table note
|
||||
if inTable && currentTable != nil && strings.HasPrefix(line, "Note:") {
|
||||
note := strings.TrimPrefix(line, "Note:")
|
||||
if strings.TrimSpace(note) == "'''" {
|
||||
inTableNote = true
|
||||
tableNoteLines = nil
|
||||
tableNoteStartLine = lineNo
|
||||
continue
|
||||
}
|
||||
note = strings.Trim(note, " '\"")
|
||||
currentTable.Description = note
|
||||
continue
|
||||
@@ -580,6 +604,13 @@ func (r *Reader) parseDBML(content string) (*models.Database, error) {
|
||||
}
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
return nil, fmt.Errorf("failed to scan DBML: %w", err)
|
||||
}
|
||||
if inTableNote {
|
||||
return nil, fmt.Errorf("dbml: line %d: unterminated triple-quoted table note", tableNoteStartLine)
|
||||
}
|
||||
|
||||
// Assign pending constraints to their respective tables
|
||||
for _, constraint := range pendingConstraints {
|
||||
// Find the table this constraint belongs to
|
||||
|
||||
Reference in New Issue
Block a user