waveterm/wavesrv/pkg/sstore/migrate.go

228 lines
5.5 KiB
Go
Raw Normal View History

2023-10-17 06:31:13 +02:00
// Copyright 2023, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0
package sstore
import (
"errors"
"fmt"
"io"
"io/fs"
2022-10-31 20:40:45 +01:00
"log"
"os"
"strconv"
"time"
_ "github.com/golang-migrate/migrate/v4/database/sqlite3"
_ "github.com/golang-migrate/migrate/v4/source/file"
"github.com/golang-migrate/migrate/v4/source/iofs"
_ "github.com/mattn/go-sqlite3"
sh2db "github.com/wavetermdev/waveterm/wavesrv/db"
"github.com/golang-migrate/migrate/v4"
)
Allow dots in alias and add port to canonical name (#209) * allow numerical start and allow dots in ssh alias We previously restricted the ssh alias to start with an alphabetic character and did not allow dots within it. This caused problems with users wanting to use an ip address as an alias. This lifts that restriction so both can freely be used. Note that while it is common to use the hostname as the alias, hostnames are not allowed to use the underscore character. However, we do allow the alias to contain an underscore. I do not think we can remove this from aliases now but it is something to watch out for in the future. * allow backslash in username This involved converting the regexp string into a raw string to able to use the backslash character. this is important. * Revert "allow backslash in username" This reverts commit cafe2812411cfba15b7ece1250a6def29d092366. Upon reflection, more time is needed to evaluate that this actually corrects the issue. It will be performed with proper diligence at a later time. * add port to end of canoncial names when not 22 The canonical name is the identifying key in the database, so it causes problems if another remote entry has the same canonical name. By adding the port number to the end of this, it is possible to differentiate the two. * add db migrations for adding port to canonicalname The up migration adds the port to the existing canonical id if it exists and is not 22. The down migration strips the port off the canonical name and deletes extra remotes with the same canonical name. If all remotes with that canonical name have been archived, it keeps the first one added to the database. If any have not been archived, it keeps the first added to the database from the non-archived group. * remove ability to edit port number via ssh import Previously, ssh imports could edit the port number since it was possible to change them in the config file without changin the canonical name. Now that the canonical name contains the port, a change in the port will simply create a new database entry. For this reason, the ability to modify the ssh port is dead code and has been removed. * allow backslash in username This involved converting the regexp string into a raw string to able to use the backslash character. this is important. * simplify up migration logic The previous up migration was suboptimal because it was accounting for a corner case not found in production. That case no longer needs to be considered, so the procedure was simplified.
2024-01-04 19:16:26 +01:00
const MaxMigration = 29
const MigratePrimaryScreenVersion = 9
const CmdScreenSpecialMigration = 13
2023-07-31 02:16:43 +02:00
const CmdLineSpecialMigration = 20
func MakeMigrate() (*migrate.Migrate, error) {
fsVar, err := iofs.New(sh2db.MigrationFS, "migrations")
if err != nil {
return nil, fmt.Errorf("opening iofs: %w", err)
}
// migrationPathUrl := fmt.Sprintf("file://%s", path.Join(wd, "db", "migrations"))
dbUrl := fmt.Sprintf("sqlite3://%s", GetDBName())
m, err := migrate.NewWithSourceInstance("iofs", fsVar, dbUrl)
// m, err := migrate.New(migrationPathUrl, dbUrl)
if err != nil {
return nil, fmt.Errorf("making migration db[%s]: %w", GetDBName(), err)
}
return m, nil
}
func copyFile(srcFile string, dstFile string, notFoundOk bool) error {
if srcFile == dstFile {
return fmt.Errorf("cannot copy %s to itself", srcFile)
}
srcFd, err := os.Open(srcFile)
if notFoundOk && err != nil && errors.Is(err, fs.ErrNotExist) {
return nil
}
if err != nil {
2023-12-18 08:46:53 +01:00
return fmt.Errorf("cannot open %s: %v", srcFile, err)
}
defer srcFd.Close()
dstFd, err := os.OpenFile(dstFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
2023-12-18 08:46:53 +01:00
return fmt.Errorf("cannot open destination file %s: %v", dstFile, err)
}
_, err = io.Copy(dstFd, srcFd)
if err != nil {
dstFd.Close()
return fmt.Errorf("error copying file: %v", err)
}
return dstFd.Close()
}
func MigrateUpStep(m *migrate.Migrate, newVersion uint) error {
startTime := time.Now()
err := m.Migrate(newVersion)
if err != nil {
return err
}
if newVersion == CmdScreenSpecialMigration {
2023-07-31 02:16:43 +02:00
mErr := RunMigration13()
if mErr != nil {
2023-07-31 02:16:43 +02:00
return fmt.Errorf("migrating to v%d: %w", newVersion, mErr)
}
}
if newVersion == CmdLineSpecialMigration {
mErr := RunMigration20()
if mErr != nil {
return fmt.Errorf("migrating to v%d: %w", newVersion, mErr)
}
}
log.Printf("[db] migration v%d, elapsed %v\n", newVersion, time.Since(startTime))
return nil
}
func MigrateUp(targetVersion uint) error {
m, err := MakeMigrate()
if err != nil {
return err
}
curVersion, dirty, err := MigrateVersion(m)
if dirty {
return fmt.Errorf("cannot migrate up, database is dirty")
}
if err != nil {
return fmt.Errorf("cannot get current migration version: %v", err)
}
if curVersion >= targetVersion {
return nil
}
log.Printf("[db] migrating from %d to %d\n", curVersion, targetVersion)
log.Printf("[db] backing up database %s to %s\n", DBFileName, DBFileNameBackup)
os.Remove(GetDBBackupName()) // don't report error
os.Remove(GetDBWALBackupName()) // don't report error
err = copyFile(GetDBName(), GetDBBackupName(), false)
if err != nil {
return fmt.Errorf("error creating database backup: %v", err)
}
err = copyFile(GetDBWALName(), GetDBWALBackupName(), true)
if err != nil {
return fmt.Errorf("error creating database(wal) backup: %v", err)
}
for newVersion := curVersion + 1; newVersion <= targetVersion; newVersion++ {
err = MigrateUpStep(m, newVersion)
if err != nil {
return fmt.Errorf("during migration v%d: %w", newVersion, err)
}
}
log.Printf("[db] migration done, new version = %d\n", targetVersion)
return nil
}
// returns curVersion, dirty, error
func MigrateVersion(m *migrate.Migrate) (uint, bool, error) {
if m == nil {
var err error
m, err = MakeMigrate()
if err != nil {
return 0, false, err
}
}
curVersion, dirty, err := m.Version()
if err == migrate.ErrNilVersion {
return 0, false, nil
}
return curVersion, dirty, err
}
func MigrateDown() error {
m, err := MakeMigrate()
if err != nil {
return err
}
err = m.Down()
if err != nil {
return err
}
return nil
}
func MigrateGoto(n uint) error {
curVersion, _, _ := MigrateVersion(nil)
if curVersion == n {
return nil
}
if curVersion < n {
return MigrateUp(n)
}
m, err := MakeMigrate()
if err != nil {
return err
}
err = m.Migrate(n)
if err != nil {
return err
}
return nil
}
func TryMigrateUp() error {
curVersion, _, _ := MigrateVersion(nil)
log.Printf("[db] db version = %d\n", curVersion)
if curVersion >= MaxMigration {
return nil
}
err := MigrateUp(MaxMigration)
if err != nil {
return err
}
return MigratePrintVersion()
}
func MigratePrintVersion() error {
version, dirty, err := MigrateVersion(nil)
if err != nil {
return fmt.Errorf("error getting db version: %v", err)
}
if dirty {
return fmt.Errorf("error db is dirty, version=%d", version)
}
2022-10-31 20:40:45 +01:00
log.Printf("[db] version=%d\n", version)
return nil
}
func MigrateCommandOpts(opts []string) error {
var err error
if opts[0] == "--migrate-up" {
fmt.Printf("migrate-up %v\n", GetDBName())
time.Sleep(3 * time.Second)
err = MigrateUp(MaxMigration)
} else if opts[0] == "--migrate-down" {
fmt.Printf("migrate-down %v\n", GetDBName())
time.Sleep(3 * time.Second)
err = MigrateDown()
} else if opts[0] == "--migrate-goto" {
n, err := strconv.Atoi(opts[1])
if err == nil {
fmt.Printf("migrate-goto %v => %d\n", GetDBName(), n)
time.Sleep(3 * time.Second)
err = MigrateGoto(uint(n))
}
} else {
err = fmt.Errorf("invalid migration command")
}
if err != nil && err.Error() == migrate.ErrNoChange.Error() {
err = nil
}
if err != nil {
return err
}
return MigratePrintVersion()
}