package cmd import ( "github.com/urfave/cli/v2" "github.com/mayswind/ezbookkeeping/pkg/core" "github.com/mayswind/ezbookkeeping/pkg/datastore" "github.com/mayswind/ezbookkeeping/pkg/log" "github.com/mayswind/ezbookkeeping/pkg/models" ) // Database represents the database command var Database = &cli.Command{ Name: "database", Usage: "ezBookkeeping database maintenance", Subcommands: []*cli.Command{ { Name: "update", Usage: "Update database structure", Action: bindAction(updateDatabaseStructure), }, }, } func updateDatabaseStructure(c *core.CliContext) error { _, err := initializeSystem(c) if err != nil { return err } log.BootInfof(c, "[database.updateDatabaseStructure] starting maintaining") err = updateAllDatabaseTablesStructure(c) if err != nil { log.BootErrorf(c, "[database.updateDatabaseStructure] update database table structure failed, because %s", err.Error()) return err } log.BootInfof(c, "[database.updateDatabaseStructure] all tables maintained successfully") return nil } func updateAllDatabaseTablesStructure(c *core.CliContext) error { var err error err = datastore.Container.UserStore.SyncStructs(new(models.User)) if err != nil { return err } log.BootInfof(c, "[database.updateAllDatabaseTablesStructure] user table maintained successfully") err = datastore.Container.UserStore.SyncStructs(new(models.TwoFactor)) if err != nil { return err } log.BootInfof(c, "[database.updateAllDatabaseTablesStructure] two-factor table maintained successfully") err = datastore.Container.UserStore.SyncStructs(new(models.TwoFactorRecoveryCode)) if err != nil { return err } log.BootInfof(c, "[database.updateAllDatabaseTablesStructure] two-factor recovery code table maintained successfully") err = datastore.Container.TokenStore.SyncStructs(new(models.TokenRecord)) if err != nil { return err } log.BootInfof(c, "[database.updateAllDatabaseTablesStructure] token record table maintained successfully") err = datastore.Container.UserDataStore.SyncStructs(new(models.Account)) if err != nil { return err } log.BootInfof(c, "[database.updateAllDatabaseTablesStructure] account table maintained successfully") err = datastore.Container.UserDataStore.SyncStructs(new(models.Transaction)) if err != nil { return err } log.BootInfof(c, "[database.updateAllDatabaseTablesStructure] transaction table maintained successfully") err = datastore.Container.UserDataStore.SyncStructs(new(models.TransactionCategory)) if err != nil { return err } log.BootInfof(c, "[database.updateAllDatabaseTablesStructure] transaction category table maintained successfully") err = datastore.Container.UserDataStore.SyncStructs(new(models.TransactionTag)) if err != nil { return err } log.BootInfof(c, "[database.updateAllDatabaseTablesStructure] transaction tag table maintained successfully") err = datastore.Container.UserDataStore.SyncStructs(new(models.TransactionTagIndex)) if err != nil { return err } log.BootInfof(c, "[database.updateAllDatabaseTablesStructure] transaction tag index table maintained successfully") err = datastore.Container.UserDataStore.SyncStructs(new(models.TransactionTemplate)) if err != nil { return err } log.BootInfof(c, "[database.updateAllDatabaseTablesStructure] transaction template table maintained successfully") return nil }