mirror of
https://github.com/KaySar12/NextZen-UserService.git
synced 2025-03-15 23:25:35 +07:00
bump version and cleanup migration tool for previous version (#19)
This commit is contained in:
parent
7da057088c
commit
e2d9ecf229
@ -8,7 +8,6 @@ import (
|
||||
|
||||
interfaces "github.com/IceWhaleTech/CasaOS-Common"
|
||||
"github.com/IceWhaleTech/CasaOS-Common/utils/systemctl"
|
||||
"github.com/IceWhaleTech/CasaOS-Common/utils/version"
|
||||
"github.com/IceWhaleTech/CasaOS-UserService/common"
|
||||
)
|
||||
|
||||
@ -20,12 +19,11 @@ const (
|
||||
)
|
||||
|
||||
//go:embedded ../../build/sysroot/etc/casaos/user-service.conf.sample
|
||||
var _userServiceConfigFileSample string
|
||||
//var _userServiceConfigFileSample string
|
||||
|
||||
var (
|
||||
_logger *Logger
|
||||
_status *version.GlobalMigrationStatus
|
||||
)
|
||||
var _logger *Logger
|
||||
|
||||
// var _status *version.GlobalMigrationStatus
|
||||
|
||||
func main() {
|
||||
versionFlag := flag.Bool("v", false, "version")
|
||||
@ -63,8 +61,7 @@ func main() {
|
||||
}
|
||||
|
||||
migrationTools := []interfaces.MigrationTool{
|
||||
NewMigrationToolFor032AndOlder(),
|
||||
NewMigrationToolFor033_034_035(),
|
||||
NewMigrationDummy(),
|
||||
}
|
||||
|
||||
var selectedMigrationTool interfaces.MigrationTool
|
||||
|
@ -1,262 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
interfaces "github.com/IceWhaleTech/CasaOS-Common"
|
||||
"github.com/IceWhaleTech/CasaOS-Common/utils/version"
|
||||
"github.com/IceWhaleTech/CasaOS-UserService/common"
|
||||
"github.com/IceWhaleTech/CasaOS-UserService/pkg/config"
|
||||
"github.com/IceWhaleTech/CasaOS-UserService/pkg/sqlite"
|
||||
"github.com/IceWhaleTech/CasaOS-UserService/pkg/utils/encryption"
|
||||
"github.com/IceWhaleTech/CasaOS-UserService/pkg/utils/file"
|
||||
"github.com/IceWhaleTech/CasaOS-UserService/service"
|
||||
"github.com/IceWhaleTech/CasaOS-UserService/service/model"
|
||||
"gopkg.in/ini.v1"
|
||||
)
|
||||
|
||||
type migrationTool1 struct{}
|
||||
|
||||
func (u *migrationTool1) IsMigrationNeeded() (bool, error) {
|
||||
if status, err := version.GetGlobalMigrationStatus(userServiceNameShort); err == nil {
|
||||
_status = status
|
||||
if status.LastMigratedVersion != "" {
|
||||
_logger.Info("Last migrated version: %s", status.LastMigratedVersion)
|
||||
if r, err := version.Compare(status.LastMigratedVersion, common.Version); err == nil {
|
||||
return r < 0, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := os.Stat(version.LegacyCasaOSConfigFilePath); err != nil {
|
||||
_logger.Info("`%s` not found, migration is not needed.", version.LegacyCasaOSConfigFilePath)
|
||||
return false, nil
|
||||
}
|
||||
|
||||
majorVersion, minorVersion, patchVersion, err := version.DetectLegacyVersion()
|
||||
if err != nil {
|
||||
if err == version.ErrLegacyVersionNotFound {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
return false, err
|
||||
}
|
||||
|
||||
if majorVersion != 0 {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
if minorVersion == 2 {
|
||||
_logger.Info("Migration is needed for a CasaOS version 0.2.x...")
|
||||
return true, nil
|
||||
}
|
||||
|
||||
if minorVersion == 3 && patchVersion < 2 {
|
||||
_logger.Info("Migration is needed for a CasaOS version between 0.3.0 and 0.3.2...")
|
||||
return true, nil
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (u *migrationTool1) PreMigrate() error {
|
||||
if _, err := os.Stat(userServiceConfigDirPath); os.IsNotExist(err) {
|
||||
_logger.Info("Creating %s since it doesn't exists...", userServiceConfigDirPath)
|
||||
if err := os.Mkdir(userServiceConfigDirPath, 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := os.Stat(userServiceConfigFilePath); os.IsNotExist(err) {
|
||||
_logger.Info("Creating %s since it doesn't exist...", userServiceConfigFilePath)
|
||||
|
||||
f, err := os.Create(userServiceConfigFilePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer f.Close()
|
||||
|
||||
if _, err := f.WriteString(_userServiceConfigFileSample); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
extension := "." + time.Now().Format("20060102") + ".bak"
|
||||
|
||||
_logger.Info("Creating a backup %s if it doesn't exist...", version.LegacyCasaOSConfigFilePath+extension)
|
||||
return file.CopySingleFile(version.LegacyCasaOSConfigFilePath, version.LegacyCasaOSConfigFilePath+extension, "skip")
|
||||
}
|
||||
|
||||
func (u *migrationTool1) Migrate() error {
|
||||
_logger.Info("Loading legacy %s...", version.LegacyCasaOSConfigFilePath)
|
||||
legacyConfigFile, err := ini.Load(version.LegacyCasaOSConfigFilePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
migrateConfigurationFile1(legacyConfigFile)
|
||||
|
||||
return migrateUser1(legacyConfigFile)
|
||||
}
|
||||
|
||||
func (u *migrationTool1) PostMigrate() error {
|
||||
defer _status.Done(common.Version)
|
||||
|
||||
legacyConfigFile, err := ini.Load(version.LegacyCasaOSConfigFilePath)
|
||||
if err != nil {
|
||||
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")
|
||||
|
||||
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 {
|
||||
return &migrationTool1{}
|
||||
}
|
||||
|
||||
func migrateConfigurationFile1(legacyConfigFile *ini.File) {
|
||||
_logger.Info("Updating %s with settings from legacy configuration...", config.UserServiceConfigFilePath)
|
||||
config.InitSetup(config.UserServiceConfigFilePath)
|
||||
|
||||
// LogPath
|
||||
if logPath, err := legacyConfigFile.Section("app").GetKey("LogPath"); err == nil {
|
||||
_logger.Info("[app] LogPath = %s", logPath.Value())
|
||||
config.AppInfo.LogPath = logPath.Value()
|
||||
}
|
||||
|
||||
if logPath, err := legacyConfigFile.Section("app").GetKey("LogSavePath"); err == nil {
|
||||
_logger.Info("[app] LogSavePath = %s", logPath.Value())
|
||||
config.AppInfo.LogPath = logPath.Value()
|
||||
}
|
||||
|
||||
// LogFileExt
|
||||
if logFileExt, err := legacyConfigFile.Section("app").GetKey("LogFileExt"); err == nil {
|
||||
_logger.Info("[app] LogFileExt = %s", logFileExt.Value())
|
||||
config.AppInfo.LogFileExt = logFileExt.Value()
|
||||
}
|
||||
|
||||
// UserDataPath
|
||||
if userDataPath, err := legacyConfigFile.Section("app").GetKey("UserDataPath"); err == nil {
|
||||
_logger.Info("[app] UserDataPath = %s", userDataPath.Value())
|
||||
config.AppInfo.UserDataPath = userDataPath.Value()
|
||||
}
|
||||
|
||||
_logger.Info("Saving %s...", config.UserServiceConfigFilePath)
|
||||
config.SaveSetup(config.UserServiceConfigFilePath)
|
||||
}
|
||||
|
||||
func migrateUser1(legacyConfigFile *ini.File) error {
|
||||
_logger.Info("Migrating user from configuration file to database...")
|
||||
|
||||
user := model.UserDBModel{Role: "admin"}
|
||||
|
||||
// UserName
|
||||
if userName, err := legacyConfigFile.Section("user").GetKey("UserName"); err == nil {
|
||||
_logger.Info("[user] UserName = %s", userName.Value())
|
||||
user.Username = userName.Value()
|
||||
}
|
||||
|
||||
// Email
|
||||
if userEmail, err := legacyConfigFile.Section("user").GetKey("Email"); err == nil {
|
||||
_logger.Info("[user] Email = %s", userEmail.Value())
|
||||
user.Email = userEmail.Value()
|
||||
}
|
||||
|
||||
// NickName
|
||||
if userNickName, err := legacyConfigFile.Section("user").GetKey("NickName"); err == nil {
|
||||
_logger.Info("[user] NickName = %s", userNickName.Value())
|
||||
user.Nickname = userNickName.Value()
|
||||
}
|
||||
|
||||
// Password
|
||||
if userPassword, err := legacyConfigFile.Section("user").GetKey("PWD"); err == nil {
|
||||
_logger.Info("[user] Password = %s", strings.Repeat("*", len(userPassword.Value())))
|
||||
user.Password = encryption.GetMD5ByStr(userPassword.Value())
|
||||
}
|
||||
|
||||
newDB := sqlite.GetDb(config.AppInfo.DBPath)
|
||||
userService := service.NewUserService(newDB)
|
||||
|
||||
if len(user.Username) == 0 {
|
||||
_logger.Info("No user found in legacy configuration file. Skipping...")
|
||||
return nil
|
||||
}
|
||||
|
||||
if userService.GetUserInfoByUserName(user.Username).Id > 0 {
|
||||
_logger.Info("User `%s` already exists in user database at %s. Skipping...", user.Username, config.AppInfo.DBPath)
|
||||
return nil
|
||||
}
|
||||
|
||||
_logger.Info("Creating user %s in database at %s...", user.Username, config.AppInfo.DBPath)
|
||||
user = userService.CreateUser(user)
|
||||
if user.Id > 0 {
|
||||
userPath := config.AppInfo.UserDataPath + "/" + strconv.Itoa(user.Id)
|
||||
_logger.Info("Creating user data path: %s", userPath)
|
||||
if err := file.MkDir(userPath); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if legacyProjectPath, err := legacyConfigFile.Section("app").GetKey("ProjectPath"); err == nil {
|
||||
appOrderJSONFile := filepath.Join(legacyProjectPath.Value(), "app_order.json")
|
||||
|
||||
if _, err := os.Stat(appOrderJSONFile); err == nil {
|
||||
_logger.Info("Moving %s to %s...", appOrderJSONFile, userPath)
|
||||
if err := os.Rename(appOrderJSONFile, filepath.Join(userPath, "app_order.json")); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -1,309 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
interfaces "github.com/IceWhaleTech/CasaOS-Common"
|
||||
"github.com/IceWhaleTech/CasaOS-Common/utils/version"
|
||||
"github.com/IceWhaleTech/CasaOS-UserService/common"
|
||||
"github.com/IceWhaleTech/CasaOS-UserService/pkg/config"
|
||||
"github.com/IceWhaleTech/CasaOS-UserService/pkg/sqlite"
|
||||
"github.com/IceWhaleTech/CasaOS-UserService/pkg/utils/file"
|
||||
"github.com/IceWhaleTech/CasaOS-UserService/service"
|
||||
"github.com/IceWhaleTech/CasaOS-UserService/service/model"
|
||||
"gopkg.in/ini.v1"
|
||||
)
|
||||
|
||||
type migrationTool2 struct{}
|
||||
|
||||
const defaultDBPath = "/var/lib/casaos"
|
||||
|
||||
func (u *migrationTool2) IsMigrationNeeded() (bool, error) {
|
||||
if status, err := version.GetGlobalMigrationStatus(userServiceNameShort); err == nil {
|
||||
_status = status
|
||||
if status.LastMigratedVersion != "" {
|
||||
_logger.Info("Last migrated version: %s", status.LastMigratedVersion)
|
||||
if r, err := version.Compare(status.LastMigratedVersion, common.Version); err == nil {
|
||||
return r < 0, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := os.Stat(version.LegacyCasaOSConfigFilePath); err != nil {
|
||||
_logger.Info("`%s` not found, migration is not needed.", version.LegacyCasaOSConfigFilePath)
|
||||
return false, nil
|
||||
}
|
||||
|
||||
majorVersion, minorVersion, patchVersion, err := version.DetectLegacyVersion()
|
||||
if err != nil {
|
||||
if err == version.ErrLegacyVersionNotFound {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
return false, err
|
||||
}
|
||||
|
||||
if majorVersion != 0 {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
if minorVersion != 3 {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
if patchVersion < 3 || patchVersion > 5 {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// legacy version has to be between 0.3.3 and 0.3.5
|
||||
_logger.Info("Migration is needed for a CasaOS version between 0.3.3 and 0.3.5...")
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (u *migrationTool2) PreMigrate() error {
|
||||
if _, err := os.Stat(userServiceConfigDirPath); os.IsNotExist(err) {
|
||||
_logger.Info("Creating %s since it doesn't exists...", userServiceConfigDirPath)
|
||||
if err := os.Mkdir(userServiceConfigDirPath, 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := os.Stat(userServiceConfigFilePath); os.IsNotExist(err) {
|
||||
_logger.Info("Creating %s since it doesn't exist...", userServiceConfigFilePath)
|
||||
|
||||
f, err := os.Create(userServiceConfigFilePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer f.Close()
|
||||
|
||||
if _, err := f.WriteString(_userServiceConfigFileSample); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
extension := "." + time.Now().Format("20060102") + ".bak"
|
||||
|
||||
_logger.Info("Creating a backup %s if it doesn't exist...", version.LegacyCasaOSConfigFilePath+extension)
|
||||
if err := file.CopySingleFile(version.LegacyCasaOSConfigFilePath, version.LegacyCasaOSConfigFilePath+extension, "skip"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
legacyConfigFile, err := ini.Load(version.LegacyCasaOSConfigFilePath)
|
||||
if 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 err
|
||||
}
|
||||
}
|
||||
|
||||
_logger.Info("Creating a backup %s if it doesn't exist...", dbFile+extension)
|
||||
if err := file.CopySingleFile(dbFile, dbFile+extension, "skip"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *migrationTool2) Migrate() error {
|
||||
_logger.Info("Loading legacy %s...", version.LegacyCasaOSConfigFilePath)
|
||||
legacyConfigFile, err := ini.Load(version.LegacyCasaOSConfigFilePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
migrateConfigurationFile2(legacyConfigFile)
|
||||
|
||||
return migrateUser2(legacyConfigFile)
|
||||
}
|
||||
|
||||
func (u *migrationTool2) PostMigrate() error {
|
||||
defer _status.Done(common.Version)
|
||||
|
||||
legacyConfigFile, err := ini.Load(version.LegacyCasaOSConfigFilePath)
|
||||
if err != nil {
|
||||
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")
|
||||
|
||||
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 NewMigrationToolFor033_034_035() interfaces.MigrationTool {
|
||||
return &migrationTool2{}
|
||||
}
|
||||
|
||||
func migrateConfigurationFile2(legacyConfigFile *ini.File) {
|
||||
_logger.Info("Updating %s with settings from legacy configuration...", config.UserServiceConfigFilePath)
|
||||
config.InitSetup(config.UserServiceConfigFilePath)
|
||||
|
||||
// LogPath
|
||||
if logPath, err := legacyConfigFile.Section("app").GetKey("LogPath"); err == nil {
|
||||
_logger.Info("[app] LogPath = %s", logPath.Value())
|
||||
config.AppInfo.LogPath = logPath.Value()
|
||||
}
|
||||
|
||||
// LogFileExt
|
||||
if logFileExt, err := legacyConfigFile.Section("app").GetKey("LogFileExt"); err == nil {
|
||||
_logger.Info("[app] LogFileExt = %s", logFileExt.Value())
|
||||
config.AppInfo.LogFileExt = logFileExt.Value()
|
||||
}
|
||||
|
||||
// DBPath
|
||||
if dbPath, err := legacyConfigFile.Section("app").GetKey("DBPath"); err == nil {
|
||||
_logger.Info("[app] DBPath = %s", dbPath.Value())
|
||||
config.AppInfo.DBPath = dbPath.Value() + "/db"
|
||||
}
|
||||
|
||||
// UserDataPath
|
||||
if userDataPath, err := legacyConfigFile.Section("app").GetKey("UserDataPath"); err == nil {
|
||||
_logger.Info("[app] UserDataPath = %s", userDataPath.Value())
|
||||
config.AppInfo.UserDataPath = userDataPath.Value()
|
||||
}
|
||||
|
||||
_logger.Info("Saving %s...", config.UserServiceConfigFilePath)
|
||||
config.SaveSetup(config.UserServiceConfigFilePath)
|
||||
}
|
||||
|
||||
func migrateUser2(legacyConfigFile *ini.File) error {
|
||||
_logger.Info("Migrating user from legacy database to user database...")
|
||||
|
||||
user := model.UserDBModel{Role: "admin"}
|
||||
|
||||
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 err
|
||||
}
|
||||
}
|
||||
legacyDB, err := sql.Open("sqlite3", dbFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer legacyDB.Close()
|
||||
|
||||
newDB := sqlite.GetDb(config.AppInfo.DBPath)
|
||||
userService := service.NewUserService(newDB)
|
||||
|
||||
// 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 !tableExists {
|
||||
continue
|
||||
}
|
||||
rows, err := legacyDB.Query(sqlStatement)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func isTableExist(legacyDB *sql.DB, tableName string) (bool, error) {
|
||||
rows, err := legacyDB.Query("SELECT name FROM sqlite_master WHERE type='table' AND name= ?", tableName)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
||||
return rows.Next(), nil
|
||||
}
|
27
cmd/migration-tool/migration_dummy.go
Normal file
27
cmd/migration-tool/migration_dummy.go
Normal file
@ -0,0 +1,27 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
interfaces "github.com/IceWhaleTech/CasaOS-Common"
|
||||
)
|
||||
|
||||
type migrationTool struct{}
|
||||
|
||||
func (u *migrationTool) IsMigrationNeeded() (bool, error) {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (u *migrationTool) PreMigrate() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *migrationTool) Migrate() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *migrationTool) PostMigrate() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewMigrationDummy() interfaces.MigrationTool {
|
||||
return &migrationTool{}
|
||||
}
|
@ -1,3 +1,3 @@
|
||||
package common
|
||||
|
||||
const Version = "0.3.7"
|
||||
const Version = "0.3.8"
|
||||
|
10
go.mod
10
go.mod
@ -16,10 +16,8 @@ require (
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/andybalholm/brotli v1.0.1 // indirect
|
||||
github.com/benbjohnson/clock v1.3.0 // indirect
|
||||
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
|
||||
github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5 // indirect
|
||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||
github.com/go-playground/locales v0.14.0 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.0 // indirect
|
||||
@ -27,26 +25,18 @@ require (
|
||||
github.com/goccy/go-json v0.9.10 // indirect
|
||||
github.com/godbus/dbus/v5 v5.0.4 // indirect
|
||||
github.com/golang-jwt/jwt/v4 v4.4.2 // indirect
|
||||
github.com/golang/snappy v0.0.2 // indirect
|
||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
github.com/jinzhu/now v1.1.5 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/klauspost/compress v1.13.6 // indirect
|
||||
github.com/klauspost/pgzip v1.2.5 // indirect
|
||||
github.com/leodido/go-urn v1.2.1 // indirect
|
||||
github.com/mattn/go-isatty v0.0.14 // indirect
|
||||
github.com/mattn/go-sqlite3 v1.14.14 // indirect
|
||||
github.com/mholt/archiver/v3 v3.5.1 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/nwaples/rardecode v1.1.0 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.0.2 // indirect
|
||||
github.com/pierrec/lz4/v4 v4.1.2 // indirect
|
||||
github.com/tidwall/match v1.1.1 // indirect
|
||||
github.com/tidwall/pretty v1.2.0 // indirect
|
||||
github.com/ugorji/go/codec v1.2.7 // indirect
|
||||
github.com/ulikunitz/xz v0.5.9 // indirect
|
||||
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
|
||||
go.uber.org/atomic v1.9.0 // indirect
|
||||
go.uber.org/multierr v1.8.0 // indirect
|
||||
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect
|
||||
|
26
go.sum
26
go.sum
@ -1,8 +1,6 @@
|
||||
github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0=
|
||||
github.com/IceWhaleTech/CasaOS-Common v0.3.7-1 h1:paay9dFGAWjNcjtCrN6ymvuU21KtxPoIpNG3wo10ufk=
|
||||
github.com/IceWhaleTech/CasaOS-Common v0.3.7-1/go.mod h1:2MiivEMzvh41codhEKUcn46WK3Ffesop/04qa9jsvQk=
|
||||
github.com/andybalholm/brotli v1.0.1 h1:KqhlKozYbRtJvsPrrEeXcO+N2l6NYT5A2QAFmSULpEc=
|
||||
github.com/andybalholm/brotli v1.0.1/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
|
||||
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
|
||||
github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A=
|
||||
github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
|
||||
@ -14,9 +12,6 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5 h1:iFaUwBSo5Svw6L7HYpRu/0lE3e0BaElwnNO1qkNQxBY=
|
||||
github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5/go.mod h1:qssHWj60/X5sZFNxpG4HBPDHVqxNm4DfnCKgrbZOT+s=
|
||||
github.com/dsnet/golib v0.0.0-20171103203638-1ea166775780/go.mod h1:Lj+Z9rebOhdfkVLjJ8T6VcRQv3SXugXy999NBtR9aFY=
|
||||
github.com/gin-contrib/gzip v0.0.6 h1:NjcunTcGAj5CO1gn4N8jHOSIeRFHIbn51z6K+xaN4d4=
|
||||
github.com/gin-contrib/gzip v0.0.6/go.mod h1:QOJlmV2xmayAjkNS2Y8NQsMneuRShOU/kjovCXNuzzk=
|
||||
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
|
||||
@ -40,8 +35,6 @@ github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5x
|
||||
github.com/golang-jwt/jwt/v4 v4.4.2 h1:rcc4lwaZgFMCZ5jxF9ABolDcIHdBytAFgqFPbSJQAYs=
|
||||
github.com/golang-jwt/jwt/v4 v4.4.2/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
|
||||
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
||||
github.com/golang/snappy v0.0.2 h1:aeE13tS0IiQgFjYdoL8qN3K1N2bXXtI6Vi51/y7BpMw=
|
||||
github.com/golang/snappy v0.0.2/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
@ -52,13 +45,6 @@ github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
|
||||
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
|
||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||
github.com/klauspost/compress v1.4.1/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
|
||||
github.com/klauspost/compress v1.11.4/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
|
||||
github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc=
|
||||
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
|
||||
github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
|
||||
github.com/klauspost/pgzip v1.2.5 h1:qnWYvvKqedOF2ulHpMG72XQol4ILEJ8k2wwRl/Km8oE=
|
||||
github.com/klauspost/pgzip v1.2.5/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
||||
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
|
||||
@ -74,20 +60,14 @@ github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27k
|
||||
github.com/mattn/go-sqlite3 v1.14.12/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
|
||||
github.com/mattn/go-sqlite3 v1.14.14 h1:qZgc/Rwetq+MtyE18WhzjokPD93dNqLGNT3QJuLvBGw=
|
||||
github.com/mattn/go-sqlite3 v1.14.14/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
|
||||
github.com/mholt/archiver/v3 v3.5.1 h1:rDjOBX9JSF5BvoJGvjqK479aL70qh9DIpZCl+k7Clwo=
|
||||
github.com/mholt/archiver/v3 v3.5.1/go.mod h1:e3dqJ7H78uzsRSEACH1joayhuSyhnonssnDhppzS1L4=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||
github.com/nwaples/rardecode v1.1.0 h1:vSxaY8vQhOcVr4mm5e8XllHWTiM4JF507A0Katqw7MQ=
|
||||
github.com/nwaples/rardecode v1.1.0/go.mod h1:5DzqNKiOdpKKBH87u8VlvAnPZMXcGRhxWkRpHbbfGS0=
|
||||
github.com/pelletier/go-toml/v2 v2.0.1/go.mod h1:r9LEWfGN8R5k0VXJ+0BkIe7MYkRdwZOjgMj2KwnJFUo=
|
||||
github.com/pelletier/go-toml/v2 v2.0.2 h1:+jQXlF3scKIcSEKkdHzXhCTDLPFi5r1wnK6yPS+49Gw=
|
||||
github.com/pelletier/go-toml/v2 v2.0.2/go.mod h1:MovirKjgVRESsAvNZlAjtFwV867yGuwRkXbG66OzopI=
|
||||
github.com/pierrec/lz4/v4 v4.1.2 h1:qvY3YFXRQE/XB8MlLzJH7mSzBs74eA2gg52YTk6jUPM=
|
||||
github.com/pierrec/lz4/v4 v4.1.2/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
|
||||
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
|
||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
@ -116,11 +96,6 @@ github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhso
|
||||
github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M=
|
||||
github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0=
|
||||
github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY=
|
||||
github.com/ulikunitz/xz v0.5.8/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
|
||||
github.com/ulikunitz/xz v0.5.9 h1:RsKRIA2MO8x56wkkcd3LbtcE/uMszhb6DpRf+3uwa3I=
|
||||
github.com/ulikunitz/xz v0.5.9/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
|
||||
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 h1:nIPpBwaJSVYIxUFsDv3M8ofmx9yWTog9BfvIu0q41lo=
|
||||
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8/go.mod h1:HUYIGzjTL3rfEspMxjDjgmT5uz5wzYJKVo23qUhYTos=
|
||||
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
|
||||
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
||||
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
|
||||
@ -199,4 +174,3 @@ gorm.io/driver/sqlite v1.3.6/go.mod h1:Sg1/pvnKtbQ7jLXxfZa+jSHvoX8hoZA8cn4xllOMT
|
||||
gorm.io/gorm v1.23.4/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
|
||||
gorm.io/gorm v1.23.8 h1:h8sGJ+biDgBA1AD1Ha9gFCx7h8npU7AsLdlkX0n2TpE=
|
||||
gorm.io/gorm v1.23.8/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
|
||||
gotest.tools/v3 v3.3.0 h1:MfDY1b1/0xN1CyMlQDac0ziEy9zJQd9CXBRRDHw2jJo=
|
||||
|
Loading…
Reference in New Issue
Block a user