Database migration
How Hanzo IAM handles database schema and data migrations.
Hanzo IAM uses xorm for database access. Xorm handles many schema changes automatically, but renaming columns and some data migrations must be done manually via the xorm migrate package.
:::note See the xorm documentation for schema operation details. :::
How it works
Xorm does not rename columns automatically. To rename a field (e.g. p_type → ptype), you add a migration that copies data and then drops the old column.
Example migration:
migrations := []*migrate.Migration{
{
ID: "CasbinRule--fill ptype field with p",
Migrate: func(tx *xorm.Engine) error {
_, err := tx.Cols("ptype").Update(&xormadapter.CasbinRule{
Ptype: "p",
})
return err
},
Rollback: func(tx *xorm.Engine) error {
return tx.DropTables(&xormadapter.CasbinRule{})
},
},
}
m.Migrate()
Here the goal is to rename `p_type` to `ptype`: copy values into `ptype`, then drop `p_type` (handled elsewhere). The migration **ID** is stored in the database; on subsequent starts, migrations with an existing ID are skipped.How is this guide?
Last updated on