Start adding migration for time.Time

This commit is contained in:
kolaente 2020-06-21 22:59:37 +02:00
parent 1a5e245b9e
commit d3cf78cb8d
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
1 changed files with 92 additions and 0 deletions

View File

@ -0,0 +1,92 @@
// 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"
"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
switch tx.Dialect().URI().DBType {
case schemas.POSTGRES:
sql = `
ALTER TABLE ` + table + ` DROP COLUMN IF EXISTS ` + column + `_ts;
ALTER TABLE ` + table + ` ADD COLUMN ` + column + `_ts TIMESTAMP WITHOUT TIME ZONE NULL;
UPDATE ` + table + ` SET ` + column + `_ts = TIMESTAMP 'epoch' + ` + column + ` * INTERVAL '1 second';
ALTER TABLE ` + table + ` ALTER COLUMN ` + column + ` TYPE TIMESTAMP USING ` + column + `_ts;
ALTER TABLE ` + table + ` DROP COLUMN ` + column + `_ts;
`
case schemas.MYSQL:
sql = `
ALTER TABLE ` + table + ` DROP COLUMN IF EXISTS ` + column + `_ts;
ALTER TABLE ` + table + ` ADD COLUMN ` + column + `_ts DATETIME;
UPDATE ` + table + ` SET ` + column + `_ts = TIMESTAMP 'epoch' + ` + column + ` * INTERVAL '1 second';
ALTER TABLE ` + table + ` ALTER COLUMN ` + column + ` TYPE USING ` + column + `_ts;
ALTER TABLE ` + table + ` DROP COLUMN ` + column + `_ts;
`
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
}
_, err := sess.Exec(sql)
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",
},
} {
for _, column := range columns {
if err := convertTime(table, column); err != nil {
return err
}
}
}
return nil
},
Rollback: func(tx *xorm.Engine) error {
return nil
},
})
}