diff --git a/go-backend/cmd/server/main.go b/go-backend/cmd/server/main.go index 5d8317d3..011afa02 100644 --- a/go-backend/cmd/server/main.go +++ b/go-backend/cmd/server/main.go @@ -16,6 +16,7 @@ import ( "github.com/xingrin/go-backend/internal/database" "github.com/xingrin/go-backend/internal/handler" "github.com/xingrin/go-backend/internal/middleware" + "github.com/xingrin/go-backend/internal/model" "github.com/xingrin/go-backend/internal/pkg" "github.com/xingrin/go-backend/internal/repository" "github.com/xingrin/go-backend/internal/service" @@ -56,6 +57,57 @@ func main() { zap.String("name", cfg.Database.Name), ) + // Auto migrate database schema + pkg.Info("Running database migrations...") + if err := db.AutoMigrate( + // Core models (no dependencies) + &model.Organization{}, + &model.User{}, + &model.Target{}, + &model.ScanEngine{}, + &model.WorkerNode{}, + &model.Wordlist{}, + &model.NucleiTemplateRepo{}, + &model.BlacklistRule{}, + &model.NotificationSettings{}, + + // Scan related (depends on Target, ScanEngine) + &model.Scan{}, + &model.ScanInputTarget{}, + &model.ScanLog{}, + &model.ScheduledScan{}, + + // Asset models (depends on Target, Scan) + &model.Subdomain{}, + &model.HostPortMapping{}, + &model.Website{}, + &model.Endpoint{}, + &model.Directory{}, + &model.Screenshot{}, + &model.Vulnerability{}, + + // Snapshot models (depends on Scan) + &model.SubdomainSnapshot{}, + &model.HostPortMappingSnapshot{}, + &model.WebsiteSnapshot{}, + &model.EndpointSnapshot{}, + &model.DirectorySnapshot{}, + &model.ScreenshotSnapshot{}, + &model.VulnerabilitySnapshot{}, + + // Statistics + &model.AssetStatistics{}, + &model.StatisticsHistory{}, + &model.Notification{}, + + // Auth + &model.Session{}, + &model.SubfinderProviderSettings{}, + ); err != nil { + pkg.Fatal("Failed to migrate database", zap.Error(err)) + } + pkg.Info("Database migrations completed") + // Initialize Redis (optional) var redisClient *redis.Client if cfg.Redis.Host != "" { diff --git a/go-backend/internal/model/website.go b/go-backend/internal/model/website.go index 1a60ce42..ed6e2425 100644 --- a/go-backend/internal/model/website.go +++ b/go-backend/internal/model/website.go @@ -6,8 +6,8 @@ import ( "github.com/lib/pq" ) -// WebSite represents a website asset -type WebSite struct { +// Website represents a website asset +type Website struct { ID int `gorm:"primaryKey;autoIncrement" json:"id"` TargetID int `gorm:"column:target_id;not null;index:idx_website_target;uniqueIndex:unique_website_url_target,priority:2" json:"targetId"` URL string `gorm:"column:url;type:text;index:idx_website_url;uniqueIndex:unique_website_url_target,priority:1" json:"url"` @@ -28,7 +28,7 @@ type WebSite struct { Target *Target `gorm:"foreignKey:TargetID" json:"target,omitempty"` } -// TableName returns the table name for WebSite -func (WebSite) TableName() string { +// TableName returns the table name for Website +func (Website) TableName() string { return "website" }