Senior
Tell about database migrations: how they were performed, how errors were handled during migration?
sobes.tech AI
Answer from AI
I usually performed database migrations using tools like Goose or Flyway, which allow managing schema versions and applying changes sequentially.
Migration process:
- Created migration scripts with schema changes (creating tables, modifying columns, etc.).
- Tested migrations in a test environment.
- Applied migrations to staging, checked correctness.
- Performed migration on production with minimal downtime.
Error handling:
- Used transactions in migrations so that changes are rolled back in case of errors.
- Logged errors and notified the team.
- In critical errors, rolled back the migration and analyzed the problem.
- In complex cases, wrote scripts for manual data correction.
Example of migration from Goose to Go:
package main
import (
"database/sql"
"github.com/pressly/goose/v3"
)
func Up(tx *sql.Tx) error {
_, err := tx.Exec(`ALTER TABLE users ADD COLUMN age INT;`)
return err
}
func Down(tx *sql.Tx) error {
_, err := tx.Exec(`ALTER TABLE users DROP COLUMN age;`)
return err
}
This approach ensures version control and safety of changes.