style: Apply code formatting with go fmt
- Run 'make format' to ensure all Go code follows standard formatting - Maintains consistent code style across the entire codebase - No functional changes, only whitespace and formatting improvements
This commit is contained in:
parent
4fd0846127
commit
2bffa2c418
19 changed files with 543 additions and 527 deletions
|
|
@ -192,17 +192,17 @@ func GetMigrations() []Migration {
|
|||
},
|
||||
// Future migrations will be added here
|
||||
}
|
||||
|
||||
|
||||
// Calculate checksums
|
||||
for i := range migrations {
|
||||
migrations[i].Checksum = calculateChecksum(migrations[i].Up)
|
||||
}
|
||||
|
||||
|
||||
// Sort by version
|
||||
sort.Slice(migrations, func(i, j int) bool {
|
||||
return migrations[i].Version < migrations[j].Version
|
||||
})
|
||||
|
||||
|
||||
return migrations
|
||||
}
|
||||
|
||||
|
|
@ -212,19 +212,19 @@ func (m *Migrator) MigrateToLatest() error {
|
|||
if err != nil {
|
||||
return fmt.Errorf("failed to get current version: %v", err)
|
||||
}
|
||||
|
||||
|
||||
migrations := GetMigrations()
|
||||
|
||||
|
||||
for _, migration := range migrations {
|
||||
if migration.Version <= currentVersion {
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
if err := m.applyMigration(migration); err != nil {
|
||||
return fmt.Errorf("failed to apply migration %d: %v", migration.Version, err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
@ -234,20 +234,20 @@ func (m *Migrator) MigrateTo(targetVersion int) error {
|
|||
if err != nil {
|
||||
return fmt.Errorf("failed to get current version: %v", err)
|
||||
}
|
||||
|
||||
|
||||
if targetVersion == currentVersion {
|
||||
return nil // Already at target version
|
||||
}
|
||||
|
||||
|
||||
migrations := GetMigrations()
|
||||
|
||||
|
||||
if targetVersion > currentVersion {
|
||||
// Forward migration
|
||||
for _, migration := range migrations {
|
||||
if migration.Version <= currentVersion || migration.Version > targetVersion {
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
if err := m.applyMigration(migration); err != nil {
|
||||
return fmt.Errorf("failed to apply migration %d: %v", migration.Version, err)
|
||||
}
|
||||
|
|
@ -258,18 +258,18 @@ func (m *Migrator) MigrateTo(targetVersion int) error {
|
|||
sort.Slice(migrations, func(i, j int) bool {
|
||||
return migrations[i].Version > migrations[j].Version
|
||||
})
|
||||
|
||||
|
||||
for _, migration := range migrations {
|
||||
if migration.Version > currentVersion || migration.Version <= targetVersion {
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
if err := m.rollbackMigration(migration); err != nil {
|
||||
return fmt.Errorf("failed to rollback migration %d: %v", migration.Version, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
@ -279,19 +279,19 @@ func (m *Migrator) GetAppliedMigrations() ([]MigrationRecord, error) {
|
|||
if err := m.ensureSchemaInfoTable(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
query := `
|
||||
SELECT version, description, applied_at, checksum
|
||||
FROM schema_info
|
||||
ORDER BY version
|
||||
`
|
||||
|
||||
|
||||
rows, err := m.conn.Query(query)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to query applied migrations: %v", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
|
||||
var migrations []MigrationRecord
|
||||
for rows.Next() {
|
||||
var migration MigrationRecord
|
||||
|
|
@ -306,7 +306,7 @@ func (m *Migrator) GetAppliedMigrations() ([]MigrationRecord, error) {
|
|||
}
|
||||
migrations = append(migrations, migration)
|
||||
}
|
||||
|
||||
|
||||
return migrations, nil
|
||||
}
|
||||
|
||||
|
|
@ -315,13 +315,13 @@ func (m *Migrator) getCurrentVersion() (int, error) {
|
|||
if err := m.ensureSchemaInfoTable(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
|
||||
var version int
|
||||
err := m.conn.QueryRow(`SELECT COALESCE(MAX(version), 0) FROM schema_info`).Scan(&version)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("failed to get current version: %v", err)
|
||||
}
|
||||
|
||||
|
||||
return version, nil
|
||||
}
|
||||
|
||||
|
|
@ -332,13 +332,13 @@ func (m *Migrator) applyMigration(migration Migration) error {
|
|||
return fmt.Errorf("failed to begin transaction: %v", err)
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
|
||||
// Warn about data loss
|
||||
if migration.DataLoss {
|
||||
// In a real application, this would show a warning to the user
|
||||
// For now, we'll just log it
|
||||
}
|
||||
|
||||
|
||||
// Execute migration SQL
|
||||
statements := strings.Split(migration.Up, ";")
|
||||
for _, stmt := range statements {
|
||||
|
|
@ -346,22 +346,22 @@ func (m *Migrator) applyMigration(migration Migration) error {
|
|||
if stmt == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
if _, err := tx.Exec(stmt); err != nil {
|
||||
return fmt.Errorf("failed to execute migration statement: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Record migration
|
||||
_, err = tx.Exec(`
|
||||
INSERT INTO schema_info (version, description, checksum)
|
||||
VALUES (?, ?, ?)
|
||||
`, migration.Version, migration.Description, migration.Checksum)
|
||||
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to record migration: %v", err)
|
||||
}
|
||||
|
||||
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
|
|
@ -370,13 +370,13 @@ func (m *Migrator) rollbackMigration(migration Migration) error {
|
|||
if migration.Down == "" {
|
||||
return fmt.Errorf("migration %d has no rollback script", migration.Version)
|
||||
}
|
||||
|
||||
|
||||
tx, err := m.conn.Begin()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to begin transaction: %v", err)
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
|
||||
// Execute rollback SQL
|
||||
statements := strings.Split(migration.Down, ";")
|
||||
for _, stmt := range statements {
|
||||
|
|
@ -384,18 +384,18 @@ func (m *Migrator) rollbackMigration(migration Migration) error {
|
|||
if stmt == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
if _, err := tx.Exec(stmt); err != nil {
|
||||
return fmt.Errorf("failed to execute rollback statement: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Remove migration record
|
||||
_, err = tx.Exec(`DELETE FROM schema_info WHERE version = ?`, migration.Version)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to remove migration record: %v", err)
|
||||
}
|
||||
|
||||
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
|
|
@ -416,4 +416,4 @@ func (m *Migrator) ensureSchemaInfoTable() error {
|
|||
func calculateChecksum(content string) string {
|
||||
// Simple checksum - in production, use a proper hash function
|
||||
return fmt.Sprintf("%x", len(content))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue