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"),
}

133
gen.go
View File

@@ -92,28 +92,18 @@ func ConnectDSN(dsn string) (*gorm.DB, error) {
// 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.
func (m *GormGen) GenModels(ctx context.Context) error {
dir := m.cfg.OutputDir
if !strings.HasPrefix(dir, "./") {
dir = "./" + dir
}
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)
}
// Clean output directory if Clean is true.
if m.cfg.Clean {
if err := m.cleanOutputDir(); err != nil {
return fmt.Errorf("failed to clean output dir: %w", err)
}
}
// When Clean is false, save existing files to restore later.
var existingFiles map[string][]byte
if !m.cfg.Clean {
existingFiles = m.saveExistingFiles()
}
g := gen.NewGenerator(gen.Config{
OutPath: m.cfg.OutputDir,
@@ -164,11 +154,9 @@ func (m *GormGen) GenModels(ctx context.Context) error {
return fmt.Errorf("failed to cleanup generated files: %w", err)
}
// Restore backup if Clean was false.
if backupDir != "" {
if err := m.restoreBackup(absDir, backupDir); err != nil {
return fmt.Errorf("failed to restore backup: %w", err)
}
// Restore existing files that were not regenerated.
if !m.cfg.Clean {
m.restoreExistingFiles(existingFiles)
}
if err := m.generateCols(); err != nil {
@@ -178,75 +166,51 @@ func (m *GormGen) GenModels(ctx context.Context) error {
return nil
}
// backupDir creates a backup copy of the directory and returns the backup path.
func (m *GormGen) backupDir(dir string) (string, error) {
backupPath := dir + ".backup"
if _, err := os.Stat(backupPath); err == nil {
os.RemoveAll(backupPath)
// saveExistingFiles saves the content of existing .go files in the output directory.
func (m *GormGen) saveExistingFiles() map[string][]byte {
dir := m.cfg.OutputDir
if !strings.HasPrefix(dir, "./") {
dir = "./" + dir
}
if err := copyDir(dir, backupPath); err != nil {
return "", err
}
fmt.Printf("Backed up: %s -> %s\n", dir, backupPath)
return backupPath, nil
}
// restoreBackup restores files from backup that don't exist in the target directory.
func (m *GormGen) restoreBackup(dir, backupDir string) error {
entries, err := os.ReadDir(backupDir)
files := make(map[string][]byte)
entries, err := os.ReadDir(dir)
if err != nil {
return err
return files
}
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
}
// restoreExistingFiles restores files that weren't regenerated.
func (m *GormGen) restoreExistingFiles(saved map[string][]byte) {
dir := m.cfg.OutputDir
if !strings.HasPrefix(dir, "./") {
dir = "./" + dir
}
restored := 0
for _, entry := range entries {
if entry.IsDir() {
continue
}
targetPath := filepath.Join(dir, entry.Name())
if _, err := os.Stat(targetPath); os.IsNotExist(err) {
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)
for name, data := range saved {
path := filepath.Join(dir, name)
if _, err := os.Stat(path); os.IsNotExist(err) {
if err := os.WriteFile(path, data, 0644); err == nil {
fmt.Printf("Restored: %s\n", path)
restored++
}
}
}
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
@@ -270,11 +234,6 @@ func (m *GormGen) cleanOutputDir() error {
return nil
}
// Skip cleaning if Clean is false.
if !m.cfg.Clean {
return nil
}
entries, err := os.ReadDir(absDir)
if err != nil {
return err

BIN
gormcol

Binary file not shown.