190 lines
4.6 KiB
Go
190 lines
4.6 KiB
Go
// Vikunja is a to-do list application to facilitate your life.
|
|
// Copyright 2018-2020 Vikunja and contributors. All rights reserved.
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
package migration
|
|
|
|
import (
|
|
"fmt"
|
|
"src.techknowlogick.com/xormigrate"
|
|
"strings"
|
|
"xorm.io/xorm"
|
|
"xorm.io/xorm/schemas"
|
|
)
|
|
|
|
func init() {
|
|
migrations = append(migrations, &xormigrate.Migration{
|
|
ID: "20200621214452",
|
|
Description: "Make all dates to iso time",
|
|
Migrate: func(tx *xorm.Engine) error {
|
|
|
|
convertTime := func(table, column string) error {
|
|
|
|
var sql []string
|
|
colOld := "`" + column + "`"
|
|
colTmp := "`" + column + `_ts` + "`"
|
|
// If the column namme ends with "_unix", we want to directly remove that since the timestamp
|
|
// isn't a unix one anymore.
|
|
var colFinal = colOld
|
|
if strings.HasSuffix(column, "_unix") {
|
|
colFinal = "`" + column[:len(column)-5] + "`"
|
|
}
|
|
|
|
switch tx.Dialect().URI().DBType {
|
|
case schemas.POSTGRES:
|
|
sql = []string{
|
|
"ALTER TABLE " + table + " DROP COLUMN IF EXISTS " + colTmp + ";",
|
|
"ALTER TABLE " + table + " ADD COLUMN " + colTmp + " TIMESTAMP WITHOUT TIME ZONE NULL;",
|
|
}
|
|
if colFinal != colOld {
|
|
sql = append(sql, "ALTER TABLE "+table+" ADD COLUMN "+colFinal+" TIMESTAMP WITHOUT TIME ZONE NULL;")
|
|
}
|
|
sql = append(sql,
|
|
"UPDATE "+table+" SET "+colTmp+" = TIMESTAMP 'epoch' + "+colOld+" * INTERVAL '1 second';",
|
|
"ALTER TABLE "+table+" ALTER COLUMN "+colFinal+" TYPE TIMESTAMP USING "+colTmp+";",
|
|
"ALTER TABLE "+table+" DROP COLUMN "+colTmp+";",
|
|
)
|
|
if colFinal != colOld {
|
|
sql = append(sql, "ALTER TABLE "+table+" DROP COLUMN "+colOld+";")
|
|
}
|
|
case schemas.MYSQL:
|
|
sql = []string{
|
|
"ALTER TABLE " + table + " DROP COLUMN IF EXISTS " + colTmp + ";",
|
|
"ALTER TABLE " + table + " ADD COLUMN " + colTmp + " DATETIME NULL;",
|
|
"UPDATE " + table + " SET " + colTmp + " = FROM_UNIXTIME(" + colOld + ");",
|
|
"ALTER TABLE " + table + " DROP COLUMN " + colOld + ";",
|
|
"ALTER TABLE " + table + " CHANGE " + colTmp + " " + colFinal + " DATETIME NOT NULL;",
|
|
}
|
|
case schemas.SQLITE:
|
|
// welp
|
|
default:
|
|
return fmt.Errorf("unsupported dbms: %s", tx.Dialect().URI().DBType)
|
|
}
|
|
|
|
sess := tx.NewSession()
|
|
if err := sess.Begin(); err != nil {
|
|
return err
|
|
}
|
|
for _, s := range sql {
|
|
_, err := sess.Exec(s)
|
|
if err != nil {
|
|
_ = sess.Rollback()
|
|
return err
|
|
}
|
|
}
|
|
if err := sess.Commit(); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
for table, columns := range map[string][]string{
|
|
"buckets": {
|
|
"created",
|
|
"updated",
|
|
},
|
|
"files": {
|
|
"created_unix",
|
|
},
|
|
"label_task": {
|
|
"created",
|
|
},
|
|
"labels": {
|
|
"created",
|
|
"updated",
|
|
},
|
|
"link_sharing": {
|
|
"created",
|
|
"updated",
|
|
},
|
|
"list": {
|
|
"created",
|
|
"updated",
|
|
},
|
|
"migration_status": {
|
|
"created_unix",
|
|
},
|
|
"namespaces": {
|
|
"created",
|
|
"updated",
|
|
},
|
|
"task_assignees": {
|
|
"created",
|
|
},
|
|
"task_attachments": {
|
|
"created",
|
|
},
|
|
"task_comments": {
|
|
"created",
|
|
"updated",
|
|
},
|
|
"task_relations": {
|
|
"created",
|
|
},
|
|
"task_reminders": {
|
|
"created",
|
|
"reminder_unix",
|
|
},
|
|
"tasks": {
|
|
"done_at_unix",
|
|
"due_date_unix",
|
|
"start_date_unix",
|
|
"end_date_unix",
|
|
"created",
|
|
"updated",
|
|
},
|
|
"team_list": {
|
|
"created",
|
|
"updated",
|
|
},
|
|
"team_members": {
|
|
"created",
|
|
},
|
|
"team_namespaces": {
|
|
"created",
|
|
"updated",
|
|
},
|
|
"teams": {
|
|
"created",
|
|
"updated",
|
|
},
|
|
"users": {
|
|
"created",
|
|
"updated",
|
|
},
|
|
"users_list": {
|
|
"created",
|
|
"updated",
|
|
},
|
|
"users_namespace": {
|
|
"created",
|
|
"updated",
|
|
},
|
|
} {
|
|
for _, column := range columns {
|
|
if err := convertTime(table, column); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
},
|
|
Rollback: func(tx *xorm.Engine) error {
|
|
return nil
|
|
},
|
|
})
|
|
}
|