2019-03-29 18:54:35 +01:00
|
|
|
---
|
|
|
|
date: "2019-03-29:00:00+02:00"
|
|
|
|
title: "Database migrations"
|
|
|
|
draft: false
|
|
|
|
type: "doc"
|
|
|
|
menu:
|
|
|
|
sidebar:
|
|
|
|
parent: "development"
|
|
|
|
---
|
|
|
|
|
|
|
|
# Database Migrations
|
|
|
|
|
|
|
|
Vikunja runs all database migrations automatically on each start if needed.
|
|
|
|
Additionally, they can also be run directly by using the `migrate` command.
|
|
|
|
|
|
|
|
We use [xormigrate](https://github.com/techknowlogick/xormigrate) to handle migrations,
|
|
|
|
which is based on gormigrate.
|
|
|
|
|
2020-09-03 17:34:44 +02:00
|
|
|
{{< table_of_contents >}}
|
|
|
|
|
2019-03-29 18:54:35 +01:00
|
|
|
## Add a new migration
|
|
|
|
|
|
|
|
All migrations are stored in `pkg/migrations` and files should have the same name as their id.
|
|
|
|
|
|
|
|
Each migration should have a function to apply and roll it back, as well as a numeric id (the datetime)
|
|
|
|
and a more in-depth description of what the migration actually does.
|
|
|
|
|
|
|
|
To easily get a new id, run the following on any unix system:
|
|
|
|
|
2019-03-31 21:54:17 +02:00
|
|
|
{{< highlight bash >}}
|
2019-03-29 18:54:35 +01:00
|
|
|
date +%Y%m%d%H%M%S
|
2019-03-31 21:54:17 +02:00
|
|
|
{{< /highlight >}}
|
2019-03-29 18:54:35 +01:00
|
|
|
|
|
|
|
New migrations should be added via the `init()` function to the `migrations` variable.
|
|
|
|
All migrations are sorted before being executed, since `init()` does not guarantee the order.
|
|
|
|
|
|
|
|
When you're adding a new struct, you also need to add it to the `models.GetTables()` function
|
|
|
|
to ensure it will be created on new installations.
|
|
|
|
|
|
|
|
### Example
|
|
|
|
|
2019-03-31 21:54:17 +02:00
|
|
|
{{< highlight golang >}}
|
2019-03-29 18:54:35 +01:00
|
|
|
package migration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/go-xorm/xorm"
|
|
|
|
"src.techknowlogick.com/xormigrate"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Used for rollback
|
|
|
|
type teamMembersMigration20190328074430 struct {
|
|
|
|
Updated int64 `xorm:"updated"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (teamMembersMigration20190328074430) TableName() string {
|
|
|
|
return "team_members"
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
migrations = append(migrations, &xormigrate.Migration{
|
|
|
|
ID: "20190328074430",
|
|
|
|
Description: "Remove updated from team_members",
|
|
|
|
Migrate: func(tx *xorm.Engine) error {
|
|
|
|
return dropTableColum(tx, "team_members", "updated")
|
|
|
|
},
|
|
|
|
Rollback: func(tx *xorm.Engine) error {
|
|
|
|
return tx.Sync2(teamMembersMigration20190328074430{})
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2019-03-31 21:54:17 +02:00
|
|
|
{{< /highlight >}}
|
2019-03-29 18:54:35 +01:00
|
|
|
|
|
|
|
You should always copy the changed parts of the struct you're changing when adding migraitons.
|