Fixed bun/gorm writer logic for multi files.
Some checks failed
CI / Test (1.24) (push) Successful in -25m56s
CI / Test (1.25) (push) Successful in -25m50s
CI / Build (push) Successful in -26m31s
CI / Lint (push) Failing after -26m21s

This commit is contained in:
2025-12-19 21:47:11 +02:00
parent 289715ba44
commit 79effe6921
2 changed files with 72 additions and 12 deletions

View File

@@ -41,12 +41,7 @@ func NewWriter(options *writers.WriterOptions) *Writer {
// WriteDatabase writes a complete database as Bun models
func (w *Writer) WriteDatabase(db *models.Database) error {
// Check if multi-file mode is enabled
multiFile := false
if w.options.Metadata != nil {
if mf, ok := w.options.Metadata["multi_file"].(bool); ok {
multiFile = mf
}
}
multiFile := w.shouldUseMultiFile()
if multiFile {
return w.writeMultiFile(db)
@@ -346,6 +341,41 @@ func (w *Writer) writeOutput(content string) error {
return nil
}
// shouldUseMultiFile determines whether to use multi-file mode based on metadata or output path
func (w *Writer) shouldUseMultiFile() bool {
// Check if multi_file is explicitly set in metadata
if w.options.Metadata != nil {
if mf, ok := w.options.Metadata["multi_file"].(bool); ok {
return mf
}
}
// Auto-detect based on output path
if w.options.OutputPath == "" {
// No output path means stdout (single file)
return false
}
// Check if path ends with .go (explicit file)
if strings.HasSuffix(w.options.OutputPath, ".go") {
return false
}
// Check if path ends with directory separator
if strings.HasSuffix(w.options.OutputPath, "/") || strings.HasSuffix(w.options.OutputPath, "\\") {
return true
}
// Check if path exists and is a directory
info, err := os.Stat(w.options.OutputPath)
if err == nil && info.IsDir() {
return true
}
// Default to single file for ambiguous cases
return false
}
// createDatabaseRef creates a shallow copy of database without schemas to avoid circular references
func (w *Writer) createDatabaseRef(db *models.Database) *models.Database {
return &models.Database{

View File

@@ -41,12 +41,7 @@ func NewWriter(options *writers.WriterOptions) *Writer {
// WriteDatabase writes a complete database as GORM models
func (w *Writer) WriteDatabase(db *models.Database) error {
// Check if multi-file mode is enabled
multiFile := false
if w.options.Metadata != nil {
if mf, ok := w.options.Metadata["multi_file"].(bool); ok {
multiFile = mf
}
}
multiFile := w.shouldUseMultiFile()
if multiFile {
return w.writeMultiFile(db)
@@ -340,6 +335,41 @@ func (w *Writer) writeOutput(content string) error {
return nil
}
// shouldUseMultiFile determines whether to use multi-file mode based on metadata or output path
func (w *Writer) shouldUseMultiFile() bool {
// Check if multi_file is explicitly set in metadata
if w.options.Metadata != nil {
if mf, ok := w.options.Metadata["multi_file"].(bool); ok {
return mf
}
}
// Auto-detect based on output path
if w.options.OutputPath == "" {
// No output path means stdout (single file)
return false
}
// Check if path ends with .go (explicit file)
if strings.HasSuffix(w.options.OutputPath, ".go") {
return false
}
// Check if path ends with directory separator
if strings.HasSuffix(w.options.OutputPath, "/") || strings.HasSuffix(w.options.OutputPath, "\\") {
return true
}
// Check if path exists and is a directory
info, err := os.Stat(w.options.OutputPath)
if err == nil && info.IsDir() {
return true
}
// Default to single file for ambiguous cases
return false
}
// createDatabaseRef creates a shallow copy of database without schemas to avoid circular references
func (w *Writer) createDatabaseRef(db *models.Database) *models.Database {
return &models.Database{