Skip to content

Commit

Permalink
fix migration from v0.3.3 (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
tigerinus authored Aug 31, 2022
1 parent 9afe211 commit 6c73a4b
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 66 deletions.
6 changes: 3 additions & 3 deletions build/scripts/migration/service.d/user-service/migration.list
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
LEGACY_WITHOUT_VERSION v0.3.6-alpha5
v0.3.5 v0.3.6-alpha5
v0.3.5.1 v0.3.6-alpha5
LEGACY_WITHOUT_VERSION v0.3.6-alpha6
v0.3.5 v0.3.6-alpha6
v0.3.5.1 v0.3.6-alpha6
47 changes: 44 additions & 3 deletions cmd/migration-tool/migration_032_and_older.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"database/sql"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -94,16 +95,56 @@ func (u *migrationTool1) Migrate() error {
}

func (u *migrationTool1) PostMigrate() error {
_logger.Info("Deleting legacy `user` section in %s...", version.LegacyCasaOSConfigFilePath)

legacyConfigFile, err := ini.Load(version.LegacyCasaOSConfigFilePath)
if err != nil {
return err
}

_logger.Info("Deleting legacy `user` section in %s...", version.LegacyCasaOSConfigFilePath)

legacyConfigFile.DeleteSection("user")

return legacyConfigFile.SaveTo(version.LegacyCasaOSConfigFilePath)
if err := legacyConfigFile.SaveTo(version.LegacyCasaOSConfigFilePath); err != nil {
return err
}

dbPath := legacyConfigFile.Section("app").Key("DBPath").String()

dbFile := filepath.Join(dbPath, "db", "casaOS.db")

if _, err := os.Stat(dbFile); err != nil {
dbFile = filepath.Join(defaultDBPath, "db", "casaOS.db")

if _, err := os.Stat(dbFile); err != nil {
return nil
}
}

legacyDB, err := sql.Open("sqlite3", dbFile)
if err != nil {
return err
}

defer legacyDB.Close()

for _, tableName := range []string{"o_users", "o_user"} {
tableExists, err := isTableExist(legacyDB, tableName)
if err != nil {
return err
}

if !tableExists {
continue
}

_logger.Info("Dropping `%s` table in legacy database...", tableName)

if _, err = legacyDB.Exec("DROP TABLE " + tableName); err != nil {
_logger.Error("Failed to drop `%s` table in legacy database: %s", tableName, err)
}
}

return nil
}

func NewMigrationToolFor032AndOlder() interfaces.MigrationTool {
Expand Down
104 changes: 61 additions & 43 deletions cmd/migration-tool/migration_033_034_035.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ func (u *migrationTool2) PostMigrate() error {
return err
}

_logger.Info("Deleting legacy `user` section in %s...", version.LegacyCasaOSConfigFilePath)

legacyConfigFile.DeleteSection("user")

if err := legacyConfigFile.SaveTo(version.LegacyCasaOSConfigFilePath); err != nil {
return err
}

dbPath := legacyConfigFile.Section("app").Key("DBPath").String()

dbFile := filepath.Join(dbPath, "db", "casaOS.db")
Expand All @@ -133,7 +141,7 @@ func (u *migrationTool2) PostMigrate() error {
dbFile = filepath.Join(defaultDBPath, "db", "casaOS.db")

if _, err := os.Stat(dbFile); err != nil {
return err
return nil
}
}

Expand All @@ -144,21 +152,24 @@ func (u *migrationTool2) PostMigrate() error {

defer legacyDB.Close()

if tableExists, err := isTableExist(legacyDB, "o_users"); err != nil {
return err
} else if tableExists {
_logger.Info("Dropping `o_users` table in legacy database...")
for _, tableName := range []string{"o_users", "o_user"} {
tableExists, err := isTableExist(legacyDB, tableName)
if err != nil {
return err
}

if _, err = legacyDB.Exec("DROP TABLE o_users"); err != nil {
_logger.Error("Failed to drop `o_users` table in legacy database: %s", err)
if !tableExists {
continue
}
}

_logger.Info("Deleting legacy `user` section in %s...", version.LegacyCasaOSConfigFilePath)
_logger.Info("Dropping `%s` table in legacy database...", tableName)

legacyConfigFile.DeleteSection("user")
if _, err = legacyDB.Exec("DROP TABLE " + tableName); err != nil {
_logger.Error("Failed to drop `%s` table in legacy database: %s", tableName, err)
}
}

return legacyConfigFile.SaveTo(version.LegacyCasaOSConfigFilePath)
return nil
}

func NewMigrationToolFor033_034_035() interfaces.MigrationTool {
Expand Down Expand Up @@ -220,47 +231,54 @@ func migrateUser2(legacyConfigFile *ini.File) error {

defer legacyDB.Close()

if tableExists, err := isTableExist(legacyDB, "o_users"); err != nil {
return err
} else if !tableExists {
_logger.Info("Table `o_users` not found in legacy database. Skipping...")
return nil
}

sqlStatement := "SELECT id, username, password, role, email, nickname, avatar, description, created_at FROM o_users ORDER BY id ASC"

rows, err := legacyDB.Query(sqlStatement)
if err != nil {
return err
}

defer rows.Close()

newDB := sqlite.GetDb(config.AppInfo.DBPath)
userService := service.NewUserService(newDB)

for rows.Next() {
if err := rows.Scan(
&user.Id,
&user.Username,
&user.Password,
&user.Role,
&user.Email,
&user.Nickname,
&user.Avatar,
&user.Description,
&user.CreatedAt,
); err != nil {
// create an inline map from string to string
sqlTableStatementMap := make(map[string]string)
sqlTableStatementMap["o_users"] = "SELECT id, username, password, role, email, nickname, avatar, description, created_at FROM o_users ORDER BY id ASC"
sqlTableStatementMap["o_user"] = "SELECT id, user_name, password, role, email, nick_name, avatar, description, created_at FROM o_user ORDER BY id ASC"

// historically there were two names for user table: o_users and users
for tableName, sqlStatement := range sqlTableStatementMap {
tableExists, err := isTableExist(legacyDB, tableName)
if err != nil {
return err
}

if userService.GetUserAllInfoByName(user.Username).Id > 0 {
_logger.Info("User %s already exists in user database at %s, skipping...", user.Username, config.AppInfo.DBPath)
if !tableExists {
continue
}
rows, err := legacyDB.Query(sqlStatement)
if err != nil {
return err
}

_logger.Info("Creating user %s in user database...", user.Username)
user = userService.CreateUser(user)
defer rows.Close()

for rows.Next() {
if err := rows.Scan(
&user.Id,
&user.Username,
&user.Password,
&user.Role,
&user.Email,
&user.Nickname,
&user.Avatar,
&user.Description,
&user.CreatedAt,
); err != nil {
return err
}

if userService.GetUserAllInfoByName(user.Username).Id > 0 {
_logger.Info("User %s already exists in user database at %s, skipping...", user.Username, config.AppInfo.DBPath)
continue
}

_logger.Info("Creating user %s in user database...", user.Username)
user = userService.CreateUser(user)
}
}

return nil
Expand Down
23 changes: 6 additions & 17 deletions pkg/sqlite/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,19 @@ func GetDb(dbPath string) *gorm.DB {
if gdb != nil {
return gdb
}
// Refer https://github.com/go-sql-driver/mysql#dsn-data-source-name
// dsn := fmt.Sprintf("%v:%v@tcp(%v:%v)/%v?charset=utf8mb4&parseTime=True&loc=Local", m.User, m.PWD, m.IP, m.Port, m.DBName)
// db, err := gorm.Open(mysql2.Open(dsn), &gorm.Config{})

file.IsNotExistMkDir(dbPath)
db, err := gorm.Open(sqlite.Open(dbPath+"/user.db"), &gorm.Config{})
if err != nil {
panic(err)
}

c, _ := db.DB()
c.SetMaxIdleConns(10)
c.SetMaxOpenConns(100)
c.SetConnMaxIdleTime(time.Second * 1000)
if err != nil {
logger.Error("sqlite connect error", zap.Any("db connect error", err))
panic("sqlite connect error")
}
gdb = db

db.Exec(`alter table o_user rename to old_user;
create table o_users ( id integer primary key,username text,password text,role text,email text,nickname text,avatar text,description text,created_at datetime,updated_at datetime);
insert into o_users select id,user_name,password,role,email,nick_name,avatar,description,created_at,updated_at from old_user;
drop table old_user;
drop table o_user;
`)
gdb = db

err = db.AutoMigrate(model2.UserDBModel{})
if err != nil {
Expand Down

0 comments on commit 6c73a4b

Please sign in to comment.