Compare commits

...

3 Commits

2 changed files with 96 additions and 1 deletions

View File

@ -61,12 +61,14 @@ func CreateDBEngine() (engine *xorm.Engine, err error) {
if err != nil {
return
}
} else {
} else if config.DatabaseType.GetString() == "sqlite" {
// Otherwise use sqlite
engine, err = initSqliteEngine()
if err != nil {
return
}
} else {
log.Fatalf("Unknown database type %s", config.DatabaseType.GetString())
}
engine.SetMapper(core.GonicMapper{})

View File

@ -0,0 +1,93 @@
// 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 + `_ts DATETIME null;
UPDATE ` + table + ` SET ` + column + `_ts = FROM_UNIXTIME(` + column + `);
ALTER TABLE ` + table + ` DROP COLUMN ` + column + `;
ALTER TABLE ` + table + ` CHANGE ` + column + `_ts ` + column + ` 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
}
_, 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
},
})
}