feat(go-backend): add database auto-migration and fix Website model naming

- Add comprehensive database auto-migration in main.go with all models organized by dependency order
- Include core models (Organization, User, Target, ScanEngine, WorkerNode, etc.)
- Include scan-related models (Scan, ScanInputTarget, ScanLog, ScheduledScan)
- Include asset models (Subdomain, HostPortMapping, Website, Endpoint, Directory, Screenshot, Vulnerability)
- Include snapshot models for all asset types
- Include statistics and authentication models
- Rename WebSite struct to Website for consistency with Go naming conventions
- Update TableName method to reflect Website naming
- Add migration logging for debugging and monitoring purposes
This commit is contained in:
yyhuni
2026-01-11 22:30:36 +08:00
parent 4aa7b3d68a
commit 0728f3c01d
2 changed files with 56 additions and 4 deletions

View File

@@ -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 != "" {

View File

@@ -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"
}