2020-06-19 23:29:02 +02:00
|
|
|
// Vikunja is a to-do list application to facilitate your life.
|
2021-02-02 20:19:13 +01:00
|
|
|
// Copyright 2018-2021 Vikunja and contributors. All rights reserved.
|
2020-06-19 23:29:02 +02:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2020-12-23 16:41:52 +01:00
|
|
|
// it under the terms of the GNU Affero General Public Licensee as published by
|
2020-06-19 23:29:02 +02:00
|
|
|
// 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
|
2020-12-23 16:41:52 +01:00
|
|
|
// GNU Affero General Public Licensee for more details.
|
2020-06-19 23:29:02 +02:00
|
|
|
//
|
2020-12-23 16:41:52 +01:00
|
|
|
// You should have received a copy of the GNU Affero General Public Licensee
|
2020-06-19 23:29:02 +02:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package db
|
|
|
|
|
2020-11-29 00:08:30 +01:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-07-13 23:43:53 +02:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"code.vikunja.io/api/pkg/log"
|
2020-11-29 00:08:30 +01:00
|
|
|
|
|
|
|
"xorm.io/xorm/schemas"
|
|
|
|
)
|
2020-06-19 23:29:02 +02:00
|
|
|
|
|
|
|
// Dump dumps all database tables
|
|
|
|
func Dump() (data map[string][]byte, err error) {
|
|
|
|
tables, err := x.DBMetas()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data = make(map[string][]byte, len(tables))
|
|
|
|
for _, table := range tables {
|
|
|
|
entries := []map[string]interface{}{}
|
|
|
|
err := x.Table(table.Name).Find(&entries)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
data[table.Name], err = json.Marshal(entries)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2020-06-21 17:30:48 +02:00
|
|
|
|
|
|
|
// Restore restores a table with all its entries
|
|
|
|
func Restore(table string, contents []map[string]interface{}) (err error) {
|
2021-06-03 15:30:31 +02:00
|
|
|
if _, err := x.IsTableExist(table); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-21 17:30:48 +02:00
|
|
|
|
|
|
|
for _, content := range contents {
|
|
|
|
if _, err := x.Table(table).Insert(content); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-13 23:43:53 +02:00
|
|
|
if Type() == schemas.POSTGRES {
|
|
|
|
idSequence := table + "_id_seq"
|
|
|
|
_, err = x.Query("SELECT setval('" + idSequence + "', " + strconv.Itoa(len(contents)) + ", true);")
|
|
|
|
if err != nil {
|
|
|
|
log.Warningf("Could not reset id sequence for %s: %s", idSequence, err)
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-21 17:30:48 +02:00
|
|
|
return
|
|
|
|
}
|
2020-11-29 00:08:30 +01:00
|
|
|
|
|
|
|
// RestoreAndTruncate removes all content from the table before restoring it from the contents map
|
|
|
|
func RestoreAndTruncate(table string, contents []map[string]interface{}) (err error) {
|
|
|
|
if _, err := x.IsTableExist(table); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if x.Dialect().URI().DBType == schemas.SQLITE {
|
|
|
|
if _, err := x.Query("DELETE FROM " + table); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if _, err := x.Query("TRUNCATE TABLE ?", table); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Restore(table, contents)
|
|
|
|
}
|