Preserve existing model files when not using --clean

This commit is contained in:
2026-03-29 18:43:32 +02:00
parent 5c40cc142f
commit 987e55de10
3 changed files with 43 additions and 148 deletions

View File

@@ -1,64 +0,0 @@
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
package dbmodel
import (
"git.ma-al.com/goc_marek/gormcol"
"time"
)
const TableNamePsStore = "ps_store"
// PsStore mapped from table <ps_store>
type PsStore struct {
IDStore int32 `gorm:"column:id_store;primaryKey;autoIncrement:true" json:"id_store"`
IDCountry int32 `gorm:"column:id_country;not null" json:"id_country"`
IDState *int32 `gorm:"column:id_state" json:"id_state"`
City string `gorm:"column:city;not null" json:"city"`
Postcode string `gorm:"column:postcode;not null" json:"postcode"`
Latitude *float64 `gorm:"column:latitude" json:"latitude"`
Longitude *float64 `gorm:"column:longitude" json:"longitude"`
Phone *string `gorm:"column:phone" json:"phone"`
Fax *string `gorm:"column:fax" json:"fax"`
Email *string `gorm:"column:email" json:"email"`
Active bool `gorm:"column:active;not null" json:"active"`
DateAdd time.Time `gorm:"column:date_add;not null" json:"date_add"`
DateUpd time.Time `gorm:"column:date_upd;not null" json:"date_upd"`
}
// TableName PsStore's table name
func (*PsStore) TableName() string {
return TableNamePsStore
}
var PsStoreCols = struct {
IDStore gormcol.Field
IDCountry gormcol.Field
IDState gormcol.Field
City gormcol.Field
Postcode gormcol.Field
Latitude gormcol.Field
Longitude gormcol.Field
Phone gormcol.Field
Fax gormcol.Field
Email gormcol.Field
Active gormcol.Field
DateAdd gormcol.Field
DateUpd gormcol.Field
}{
IDStore: gormcol.Field{}.Set((&PsStore{}).TableName(), "id_store"),
IDCountry: gormcol.Field{}.Set((&PsStore{}).TableName(), "id_country"),
IDState: gormcol.Field{}.Set((&PsStore{}).TableName(), "id_state"),
City: gormcol.Field{}.Set((&PsStore{}).TableName(), "city"),
Postcode: gormcol.Field{}.Set((&PsStore{}).TableName(), "postcode"),
Latitude: gormcol.Field{}.Set((&PsStore{}).TableName(), "latitude"),
Longitude: gormcol.Field{}.Set((&PsStore{}).TableName(), "longitude"),
Phone: gormcol.Field{}.Set((&PsStore{}).TableName(), "phone"),
Fax: gormcol.Field{}.Set((&PsStore{}).TableName(), "fax"),
Email: gormcol.Field{}.Set((&PsStore{}).TableName(), "email"),
Active: gormcol.Field{}.Set((&PsStore{}).TableName(), "active"),
DateAdd: gormcol.Field{}.Set((&PsStore{}).TableName(), "date_add"),
DateUpd: gormcol.Field{}.Set((&PsStore{}).TableName(), "date_upd"),
}

127
gen.go
View File

@@ -92,27 +92,17 @@ func ConnectDSN(dsn string) (*gorm.DB, error) {
// It cleans the output directory (if Clean is true), generates models using gorm.io/gen, // It cleans the output directory (if Clean is true), generates models using gorm.io/gen,
// and appends <Model>Cols variables with type-safe Field descriptors. // and appends <Model>Cols variables with type-safe Field descriptors.
func (m *GormGen) GenModels(ctx context.Context) error { func (m *GormGen) GenModels(ctx context.Context) error {
dir := m.cfg.OutputDir // Clean output directory if Clean is true.
if !strings.HasPrefix(dir, "./") { if m.cfg.Clean {
dir = "./" + dir if err := m.cleanOutputDir(); err != nil {
} return fmt.Errorf("failed to clean output dir: %w", err)
absDir, err := filepath.Abs(dir)
if err != nil {
return err
}
// When Clean is false, backup existing files first.
var backupDir string
if !m.cfg.Clean {
backupDir, err = m.backupDir(absDir)
if err != nil {
return fmt.Errorf("failed to backup existing files: %w", err)
} }
defer os.RemoveAll(backupDir)
} }
if err := m.cleanOutputDir(); err != nil { // When Clean is false, save existing files to restore later.
return fmt.Errorf("failed to clean output dir: %w", err) var existingFiles map[string][]byte
if !m.cfg.Clean {
existingFiles = m.saveExistingFiles()
} }
g := gen.NewGenerator(gen.Config{ g := gen.NewGenerator(gen.Config{
@@ -164,11 +154,9 @@ func (m *GormGen) GenModels(ctx context.Context) error {
return fmt.Errorf("failed to cleanup generated files: %w", err) return fmt.Errorf("failed to cleanup generated files: %w", err)
} }
// Restore backup if Clean was false. // Restore existing files that were not regenerated.
if backupDir != "" { if !m.cfg.Clean {
if err := m.restoreBackup(absDir, backupDir); err != nil { m.restoreExistingFiles(existingFiles)
return fmt.Errorf("failed to restore backup: %w", err)
}
} }
if err := m.generateCols(); err != nil { if err := m.generateCols(); err != nil {
@@ -178,75 +166,51 @@ func (m *GormGen) GenModels(ctx context.Context) error {
return nil return nil
} }
// backupDir creates a backup copy of the directory and returns the backup path. // saveExistingFiles saves the content of existing .go files in the output directory.
func (m *GormGen) backupDir(dir string) (string, error) { func (m *GormGen) saveExistingFiles() map[string][]byte {
backupPath := dir + ".backup" dir := m.cfg.OutputDir
if _, err := os.Stat(backupPath); err == nil { if !strings.HasPrefix(dir, "./") {
os.RemoveAll(backupPath) dir = "./" + dir
} }
if err := copyDir(dir, backupPath); err != nil { files := make(map[string][]byte)
return "", err entries, err := os.ReadDir(dir)
if err != nil {
return files
} }
fmt.Printf("Backed up: %s -> %s\n", dir, backupPath)
return backupPath, nil for _, entry := range entries {
if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".go") {
continue
}
path := filepath.Join(dir, entry.Name())
if data, err := os.ReadFile(path); err == nil {
files[entry.Name()] = data
}
}
return files
} }
// restoreBackup restores files from backup that don't exist in the target directory. // restoreExistingFiles restores files that weren't regenerated.
func (m *GormGen) restoreBackup(dir, backupDir string) error { func (m *GormGen) restoreExistingFiles(saved map[string][]byte) {
entries, err := os.ReadDir(backupDir) dir := m.cfg.OutputDir
if err != nil { if !strings.HasPrefix(dir, "./") {
return err dir = "./" + dir
} }
restored := 0 restored := 0
for _, entry := range entries { for name, data := range saved {
if entry.IsDir() { path := filepath.Join(dir, name)
continue if _, err := os.Stat(path); os.IsNotExist(err) {
} if err := os.WriteFile(path, data, 0644); err == nil {
targetPath := filepath.Join(dir, entry.Name()) fmt.Printf("Restored: %s\n", path)
if _, err := os.Stat(targetPath); os.IsNotExist(err) { restored++
srcPath := filepath.Join(backupDir, entry.Name())
data, err := os.ReadFile(srcPath)
if err != nil {
continue
} }
if err := os.WriteFile(targetPath, data, 0644); err != nil {
return err
}
fmt.Printf("Restored: %s\n", targetPath)
restored++
} }
} }
if restored > 0 { if restored > 0 {
fmt.Printf("Restored %d files from backup\n", restored) fmt.Printf("Restored %d existing files\n", restored)
} }
return nil
}
// copyDir copies a directory recursively.
func copyDir(src, dst string) error {
return filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
rel, _ := filepath.Rel(src, path)
dstPath := filepath.Join(dst, rel)
if info.IsDir() {
return os.MkdirAll(dstPath, info.Mode())
}
return copyFile(path, dstPath)
})
}
// copyFile copies a single file.
func copyFile(src, dst string) error {
data, err := os.ReadFile(src)
if err != nil {
return err
}
return os.WriteFile(dst, data, 0644)
} }
// cleanOutputDir removes existing .go files from the output directory // cleanOutputDir removes existing .go files from the output directory
@@ -270,11 +234,6 @@ func (m *GormGen) cleanOutputDir() error {
return nil return nil
} }
// Skip cleaning if Clean is false.
if !m.cfg.Clean {
return nil
}
entries, err := os.ReadDir(absDir) entries, err := os.ReadDir(absDir)
if err != nil { if err != nil {
return err return err

BIN
gormcol

Binary file not shown.