Add --clean flag to control file deletion, default preserves existing models
This commit is contained in:
31
app/model/dbmodel/ps_module_shop.go
Normal file
31
app/model/dbmodel/ps_module_shop.go
Normal file
@@ -0,0 +1,31 @@
|
||||
// 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"
|
||||
|
||||
const TableNamePsModuleShop = "ps_module_shop"
|
||||
|
||||
// PsModuleShop mapped from table <ps_module_shop>
|
||||
type PsModuleShop struct {
|
||||
IDModule int32 `gorm:"column:id_module;primaryKey" json:"id_module"`
|
||||
IDShop int32 `gorm:"column:id_shop;primaryKey;index:id_shop,priority:1" json:"id_shop"`
|
||||
EnableDevice bool `gorm:"column:enable_device;not null;default:7" json:"enable_device"`
|
||||
}
|
||||
|
||||
// TableName PsModuleShop's table name
|
||||
func (*PsModuleShop) TableName() string {
|
||||
return TableNamePsModuleShop
|
||||
}
|
||||
|
||||
var PsModuleShopCols = struct {
|
||||
IDModule gormcol.Field
|
||||
IDShop gormcol.Field
|
||||
EnableDevice gormcol.Field
|
||||
}{
|
||||
IDModule: gormcol.Field{}.Set((&PsModuleShop{}).TableName(), "id_module"),
|
||||
IDShop: gormcol.Field{}.Set((&PsModuleShop{}).TableName(), "id_shop"),
|
||||
EnableDevice: gormcol.Field{}.Set((&PsModuleShop{}).TableName(), "enable_device"),
|
||||
}
|
||||
@@ -1,190 +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 TableNamePsProduct = "ps_product"
|
||||
|
||||
// PsProduct mapped from table <ps_product>
|
||||
type PsProduct struct {
|
||||
IDProduct int32 `gorm:"column:id_product;primaryKey;autoIncrement:true;index:product_manufacturer,priority:2" json:"id_product"`
|
||||
IDSupplier *int32 `gorm:"column:id_supplier;index:product_supplier,priority:1" json:"id_supplier"`
|
||||
IDManufacturer *int32 `gorm:"column:id_manufacturer;index:idx_product_manufacturer,priority:1;index:product_manufacturer,priority:1" json:"id_manufacturer"`
|
||||
IDCategoryDefault *int32 `gorm:"column:id_category_default;index:id_category_default,priority:1" json:"id_category_default"`
|
||||
IDShopDefault int32 `gorm:"column:id_shop_default;not null;default:1" json:"id_shop_default"`
|
||||
IDTaxRulesGroup int32 `gorm:"column:id_tax_rules_group;not null" json:"id_tax_rules_group"`
|
||||
OnSale bool `gorm:"column:on_sale;not null" json:"on_sale"`
|
||||
OnlineOnly bool `gorm:"column:online_only;not null" json:"online_only"`
|
||||
Ean13 *string `gorm:"column:ean13" json:"ean13"`
|
||||
Isbn *string `gorm:"column:isbn" json:"isbn"`
|
||||
Upc *string `gorm:"column:upc" json:"upc"`
|
||||
Ecotax float64 `gorm:"column:ecotax;not null;default:0.000000" json:"ecotax"`
|
||||
Quantity int32 `gorm:"column:quantity;not null" json:"quantity"`
|
||||
MinimalQuantity int32 `gorm:"column:minimal_quantity;not null;default:1" json:"minimal_quantity"`
|
||||
LowStockThreshold *int32 `gorm:"column:low_stock_threshold" json:"low_stock_threshold"`
|
||||
LowStockAlert bool `gorm:"column:low_stock_alert;not null" json:"low_stock_alert"`
|
||||
Price float64 `gorm:"column:price;not null;default:0.000000" json:"price"`
|
||||
WholesalePrice float64 `gorm:"column:wholesale_price;not null;default:0.000000" json:"wholesale_price"`
|
||||
Unity *string `gorm:"column:unity" json:"unity"`
|
||||
UnitPriceRatio float64 `gorm:"column:unit_price_ratio;not null;default:0.000000" json:"unit_price_ratio"`
|
||||
IDUnit int32 `gorm:"column:id_unit;not null" json:"id_unit"`
|
||||
AdditionalShippingCost float64 `gorm:"column:additional_shipping_cost;not null;default:0.00" json:"additional_shipping_cost"`
|
||||
Reference *string `gorm:"column:reference" json:"reference"`
|
||||
SupplierReference *string `gorm:"column:supplier_reference" json:"supplier_reference"`
|
||||
Location *string `gorm:"column:location" json:"location"`
|
||||
Width float64 `gorm:"column:width;not null;default:0.000000" json:"width"`
|
||||
Height float64 `gorm:"column:height;not null;default:0.000000" json:"height"`
|
||||
Depth float64 `gorm:"column:depth;not null;default:0.000000" json:"depth"`
|
||||
Weight float64 `gorm:"column:weight;not null;default:0.000000" json:"weight"`
|
||||
OutOfStock int32 `gorm:"column:out_of_stock;not null;default:2" json:"out_of_stock"`
|
||||
AdditionalDeliveryTimes int32 `gorm:"column:additional_delivery_times;not null;default:1" json:"additional_delivery_times"`
|
||||
QuantityDiscount *bool `gorm:"column:quantity_discount" json:"quantity_discount"`
|
||||
Customizable int32 `gorm:"column:customizable;not null" json:"customizable"`
|
||||
UploadableFiles int32 `gorm:"column:uploadable_files;not null" json:"uploadable_files"`
|
||||
TextFields int32 `gorm:"column:text_fields;not null" json:"text_fields"`
|
||||
Active bool `gorm:"column:active;not null" json:"active"`
|
||||
RedirectType string `gorm:"column:redirect_type;not null" json:"redirect_type"`
|
||||
IDTypeRedirected int32 `gorm:"column:id_type_redirected;not null" json:"id_type_redirected"`
|
||||
AvailableForOrder bool `gorm:"column:available_for_order;not null;default:1" json:"available_for_order"`
|
||||
AvailableDate *time.Time `gorm:"column:available_date" json:"available_date"`
|
||||
ShowCondition bool `gorm:"column:show_condition;not null" json:"show_condition"`
|
||||
Condition string `gorm:"column:condition;not null;default:new" json:"condition"`
|
||||
ShowPrice bool `gorm:"column:show_price;not null;default:1" json:"show_price"`
|
||||
Indexed bool `gorm:"column:indexed;not null;index:indexed,priority:1" json:"indexed"`
|
||||
Visibility string `gorm:"column:visibility;not null;index:idx_product_visibility,priority:1;index:idx_ps_product_visibility,priority:1;index:idx_visibility,priority:1;default:both" json:"visibility"`
|
||||
CacheIsPack bool `gorm:"column:cache_is_pack;not null" json:"cache_is_pack"`
|
||||
CacheHasAttachments bool `gorm:"column:cache_has_attachments;not null" json:"cache_has_attachments"`
|
||||
IsVirtual bool `gorm:"column:is_virtual;not null" json:"is_virtual"`
|
||||
CacheDefaultAttribute *int32 `gorm:"column:cache_default_attribute" json:"cache_default_attribute"`
|
||||
DateAdd time.Time `gorm:"column:date_add;not null;index:date_add,priority:1" json:"date_add"`
|
||||
DateUpd time.Time `gorm:"column:date_upd;not null;index:state,priority:2" json:"date_upd"`
|
||||
AdvancedStockManagement bool `gorm:"column:advanced_stock_management;not null" json:"advanced_stock_management"`
|
||||
PackStockType int32 `gorm:"column:pack_stock_type;not null;default:3" json:"pack_stock_type"`
|
||||
State int32 `gorm:"column:state;not null;index:state,priority:1;default:1" json:"state"`
|
||||
DeliveryDays *int32 `gorm:"column:delivery_days" json:"delivery_days"`
|
||||
}
|
||||
|
||||
// TableName PsProduct's table name
|
||||
func (*PsProduct) TableName() string {
|
||||
return TableNamePsProduct
|
||||
}
|
||||
|
||||
var PsProductCols = struct {
|
||||
IDProduct gormcol.Field
|
||||
IDSupplier gormcol.Field
|
||||
IDManufacturer gormcol.Field
|
||||
IDCategoryDefault gormcol.Field
|
||||
IDShopDefault gormcol.Field
|
||||
IDTaxRulesGroup gormcol.Field
|
||||
OnSale gormcol.Field
|
||||
OnlineOnly gormcol.Field
|
||||
Ean13 gormcol.Field
|
||||
Isbn gormcol.Field
|
||||
Upc gormcol.Field
|
||||
Ecotax gormcol.Field
|
||||
Quantity gormcol.Field
|
||||
MinimalQuantity gormcol.Field
|
||||
LowStockThreshold gormcol.Field
|
||||
LowStockAlert gormcol.Field
|
||||
Price gormcol.Field
|
||||
WholesalePrice gormcol.Field
|
||||
Unity gormcol.Field
|
||||
UnitPriceRatio gormcol.Field
|
||||
IDUnit gormcol.Field
|
||||
AdditionalShippingCost gormcol.Field
|
||||
Reference gormcol.Field
|
||||
SupplierReference gormcol.Field
|
||||
Location gormcol.Field
|
||||
Width gormcol.Field
|
||||
Height gormcol.Field
|
||||
Depth gormcol.Field
|
||||
Weight gormcol.Field
|
||||
OutOfStock gormcol.Field
|
||||
AdditionalDeliveryTimes gormcol.Field
|
||||
QuantityDiscount gormcol.Field
|
||||
Customizable gormcol.Field
|
||||
UploadableFiles gormcol.Field
|
||||
TextFields gormcol.Field
|
||||
Active gormcol.Field
|
||||
RedirectType gormcol.Field
|
||||
IDTypeRedirected gormcol.Field
|
||||
AvailableForOrder gormcol.Field
|
||||
AvailableDate gormcol.Field
|
||||
ShowCondition gormcol.Field
|
||||
Condition gormcol.Field
|
||||
ShowPrice gormcol.Field
|
||||
Indexed gormcol.Field
|
||||
Visibility gormcol.Field
|
||||
CacheIsPack gormcol.Field
|
||||
CacheHasAttachments gormcol.Field
|
||||
IsVirtual gormcol.Field
|
||||
CacheDefaultAttribute gormcol.Field
|
||||
DateAdd gormcol.Field
|
||||
DateUpd gormcol.Field
|
||||
AdvancedStockManagement gormcol.Field
|
||||
PackStockType gormcol.Field
|
||||
State gormcol.Field
|
||||
DeliveryDays gormcol.Field
|
||||
}{
|
||||
IDProduct: gormcol.Field{}.Set((&PsProduct{}).TableName(), "id_product"),
|
||||
IDSupplier: gormcol.Field{}.Set((&PsProduct{}).TableName(), "id_supplier"),
|
||||
IDManufacturer: gormcol.Field{}.Set((&PsProduct{}).TableName(), "id_manufacturer"),
|
||||
IDCategoryDefault: gormcol.Field{}.Set((&PsProduct{}).TableName(), "id_category_default"),
|
||||
IDShopDefault: gormcol.Field{}.Set((&PsProduct{}).TableName(), "id_shop_default"),
|
||||
IDTaxRulesGroup: gormcol.Field{}.Set((&PsProduct{}).TableName(), "id_tax_rules_group"),
|
||||
OnSale: gormcol.Field{}.Set((&PsProduct{}).TableName(), "on_sale"),
|
||||
OnlineOnly: gormcol.Field{}.Set((&PsProduct{}).TableName(), "online_only"),
|
||||
Ean13: gormcol.Field{}.Set((&PsProduct{}).TableName(), "ean13"),
|
||||
Isbn: gormcol.Field{}.Set((&PsProduct{}).TableName(), "isbn"),
|
||||
Upc: gormcol.Field{}.Set((&PsProduct{}).TableName(), "upc"),
|
||||
Ecotax: gormcol.Field{}.Set((&PsProduct{}).TableName(), "ecotax"),
|
||||
Quantity: gormcol.Field{}.Set((&PsProduct{}).TableName(), "quantity"),
|
||||
MinimalQuantity: gormcol.Field{}.Set((&PsProduct{}).TableName(), "minimal_quantity"),
|
||||
LowStockThreshold: gormcol.Field{}.Set((&PsProduct{}).TableName(), "low_stock_threshold"),
|
||||
LowStockAlert: gormcol.Field{}.Set((&PsProduct{}).TableName(), "low_stock_alert"),
|
||||
Price: gormcol.Field{}.Set((&PsProduct{}).TableName(), "price"),
|
||||
WholesalePrice: gormcol.Field{}.Set((&PsProduct{}).TableName(), "wholesale_price"),
|
||||
Unity: gormcol.Field{}.Set((&PsProduct{}).TableName(), "unity"),
|
||||
UnitPriceRatio: gormcol.Field{}.Set((&PsProduct{}).TableName(), "unit_price_ratio"),
|
||||
IDUnit: gormcol.Field{}.Set((&PsProduct{}).TableName(), "id_unit"),
|
||||
AdditionalShippingCost: gormcol.Field{}.Set((&PsProduct{}).TableName(), "additional_shipping_cost"),
|
||||
Reference: gormcol.Field{}.Set((&PsProduct{}).TableName(), "reference"),
|
||||
SupplierReference: gormcol.Field{}.Set((&PsProduct{}).TableName(), "supplier_reference"),
|
||||
Location: gormcol.Field{}.Set((&PsProduct{}).TableName(), "location"),
|
||||
Width: gormcol.Field{}.Set((&PsProduct{}).TableName(), "width"),
|
||||
Height: gormcol.Field{}.Set((&PsProduct{}).TableName(), "height"),
|
||||
Depth: gormcol.Field{}.Set((&PsProduct{}).TableName(), "depth"),
|
||||
Weight: gormcol.Field{}.Set((&PsProduct{}).TableName(), "weight"),
|
||||
OutOfStock: gormcol.Field{}.Set((&PsProduct{}).TableName(), "out_of_stock"),
|
||||
AdditionalDeliveryTimes: gormcol.Field{}.Set((&PsProduct{}).TableName(), "additional_delivery_times"),
|
||||
QuantityDiscount: gormcol.Field{}.Set((&PsProduct{}).TableName(), "quantity_discount"),
|
||||
Customizable: gormcol.Field{}.Set((&PsProduct{}).TableName(), "customizable"),
|
||||
UploadableFiles: gormcol.Field{}.Set((&PsProduct{}).TableName(), "uploadable_files"),
|
||||
TextFields: gormcol.Field{}.Set((&PsProduct{}).TableName(), "text_fields"),
|
||||
Active: gormcol.Field{}.Set((&PsProduct{}).TableName(), "active"),
|
||||
RedirectType: gormcol.Field{}.Set((&PsProduct{}).TableName(), "redirect_type"),
|
||||
IDTypeRedirected: gormcol.Field{}.Set((&PsProduct{}).TableName(), "id_type_redirected"),
|
||||
AvailableForOrder: gormcol.Field{}.Set((&PsProduct{}).TableName(), "available_for_order"),
|
||||
AvailableDate: gormcol.Field{}.Set((&PsProduct{}).TableName(), "available_date"),
|
||||
ShowCondition: gormcol.Field{}.Set((&PsProduct{}).TableName(), "show_condition"),
|
||||
Condition: gormcol.Field{}.Set((&PsProduct{}).TableName(), "condition"),
|
||||
ShowPrice: gormcol.Field{}.Set((&PsProduct{}).TableName(), "show_price"),
|
||||
Indexed: gormcol.Field{}.Set((&PsProduct{}).TableName(), "indexed"),
|
||||
Visibility: gormcol.Field{}.Set((&PsProduct{}).TableName(), "visibility"),
|
||||
CacheIsPack: gormcol.Field{}.Set((&PsProduct{}).TableName(), "cache_is_pack"),
|
||||
CacheHasAttachments: gormcol.Field{}.Set((&PsProduct{}).TableName(), "cache_has_attachments"),
|
||||
IsVirtual: gormcol.Field{}.Set((&PsProduct{}).TableName(), "is_virtual"),
|
||||
CacheDefaultAttribute: gormcol.Field{}.Set((&PsProduct{}).TableName(), "cache_default_attribute"),
|
||||
DateAdd: gormcol.Field{}.Set((&PsProduct{}).TableName(), "date_add"),
|
||||
DateUpd: gormcol.Field{}.Set((&PsProduct{}).TableName(), "date_upd"),
|
||||
AdvancedStockManagement: gormcol.Field{}.Set((&PsProduct{}).TableName(), "advanced_stock_management"),
|
||||
PackStockType: gormcol.Field{}.Set((&PsProduct{}).TableName(), "pack_stock_type"),
|
||||
State: gormcol.Field{}.Set((&PsProduct{}).TableName(), "state"),
|
||||
DeliveryDays: gormcol.Field{}.Set((&PsProduct{}).TableName(), "delivery_days"),
|
||||
}
|
||||
31
app/model/dbmodel/ps_reassurance.go
Normal file
31
app/model/dbmodel/ps_reassurance.go
Normal file
@@ -0,0 +1,31 @@
|
||||
// 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"
|
||||
|
||||
const TableNamePsReassurance = "ps_reassurance"
|
||||
|
||||
// PsReassurance mapped from table <ps_reassurance>
|
||||
type PsReassurance struct {
|
||||
IDReassurance int32 `gorm:"column:id_reassurance;primaryKey;autoIncrement:true" json:"id_reassurance"`
|
||||
IDShop int32 `gorm:"column:id_shop;not null" json:"id_shop"`
|
||||
FileName string `gorm:"column:file_name;not null" json:"file_name"`
|
||||
}
|
||||
|
||||
// TableName PsReassurance's table name
|
||||
func (*PsReassurance) TableName() string {
|
||||
return TableNamePsReassurance
|
||||
}
|
||||
|
||||
var PsReassuranceCols = struct {
|
||||
IDReassurance gormcol.Field
|
||||
IDShop gormcol.Field
|
||||
FileName gormcol.Field
|
||||
}{
|
||||
IDReassurance: gormcol.Field{}.Set((&PsReassurance{}).TableName(), "id_reassurance"),
|
||||
IDShop: gormcol.Field{}.Set((&PsReassurance{}).TableName(), "id_shop"),
|
||||
FileName: gormcol.Field{}.Set((&PsReassurance{}).TableName(), "file_name"),
|
||||
}
|
||||
64
app/model/dbmodel/ps_store.go
Normal file
64
app/model/dbmodel/ps_store.go
Normal file
@@ -0,0 +1,64 @@
|
||||
// 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"),
|
||||
}
|
||||
@@ -24,6 +24,7 @@ func main() {
|
||||
dsn := flag.String("dsn", "", "database DSN (e.g. user:pass@tcp(host:3306)/dbname)")
|
||||
filter := flag.String("filter", "", "regex to match table names (triggers batch mode)")
|
||||
all := flag.Bool("all", false, "generate all tables matching filter (shows confirmation)")
|
||||
clean := flag.Bool("clean", false, "remove existing model files before generation")
|
||||
outDir := flag.String("out", "./app/model/dbmodel", "output directory for generated files")
|
||||
pkgName := flag.String("pkg", "dbmodel", "Go package name for generated files")
|
||||
flag.Usage = func() {
|
||||
@@ -68,6 +69,7 @@ func main() {
|
||||
OutputDir: *outDir,
|
||||
PkgName: *pkgName,
|
||||
TableFilter: *filter,
|
||||
Clean: *clean,
|
||||
}
|
||||
|
||||
// Modes:
|
||||
|
||||
109
gen.go
109
gen.go
@@ -32,6 +32,9 @@ type GenConfig struct {
|
||||
// SelectedTables is a list of specific table names to generate.
|
||||
// When set, TableFilter is ignored.
|
||||
SelectedTables []string
|
||||
// Clean determines whether to remove existing model files before generation.
|
||||
// When false, only updates or creates models without deleting existing files.
|
||||
Clean bool
|
||||
}
|
||||
|
||||
// defaultConfig returns the default configuration values.
|
||||
@@ -40,6 +43,7 @@ func defaultConfig() GenConfig {
|
||||
OutputDir: "./app/model/dbmodel",
|
||||
PkgName: "dbmodel",
|
||||
TableFilter: "ps_.*",
|
||||
Clean: true,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,6 +73,7 @@ func NewWithConfig(db *gorm.DB, cfg GenConfig) *GormGen {
|
||||
if len(cfg.SelectedTables) > 0 {
|
||||
d.SelectedTables = cfg.SelectedTables
|
||||
}
|
||||
d.Clean = cfg.Clean
|
||||
return &GormGen{db: db, cfg: d}
|
||||
}
|
||||
|
||||
@@ -84,9 +89,28 @@ func ConnectDSN(dsn string) (*gorm.DB, error) {
|
||||
}
|
||||
|
||||
// GenModels generates GORM model files and column descriptors for matched tables.
|
||||
// It cleans the output directory, 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.
|
||||
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)
|
||||
}
|
||||
|
||||
if err := m.cleanOutputDir(); err != nil {
|
||||
return fmt.Errorf("failed to clean output dir: %w", err)
|
||||
}
|
||||
@@ -140,6 +164,13 @@ 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)
|
||||
}
|
||||
}
|
||||
|
||||
if err := m.generateCols(); err != nil {
|
||||
return fmt.Errorf("failed to generate column descriptors: %w", err)
|
||||
}
|
||||
@@ -147,6 +178,77 @@ 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)
|
||||
}
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
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)
|
||||
restored++
|
||||
}
|
||||
}
|
||||
if restored > 0 {
|
||||
fmt.Printf("Restored %d files from backup\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
|
||||
// or creates it if it doesn't exist.
|
||||
func (m *GormGen) cleanOutputDir() error {
|
||||
@@ -168,6 +270,11 @@ 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
|
||||
|
||||
Reference in New Issue
Block a user