From 3fcb295d72155f0a88031e677fb45e95c979cdfa Mon Sep 17 00:00:00 2001 From: Felipe Martin <812088+fmartingr@users.noreply.github.com> Date: Sun, 27 Nov 2022 15:39:27 +0100 Subject: [PATCH] Make migrations transparent to the user (#530) * refactor: remove migrate command * reafactor: avoid migration errors on no changes --- docs/Usage.md | 11 +---------- internal/cmd/migrate.go | 21 --------------------- internal/cmd/root.go | 7 ++++++- internal/database/mysql.go | 6 +++++- internal/database/pg.go | 6 +++++- internal/database/sqlite.go | 6 +++++- 6 files changed, 22 insertions(+), 35 deletions(-) delete mode 100644 internal/cmd/migrate.go diff --git a/docs/Usage.md b/docs/Usage.md index 0a6a1a9..3efe9cc 100644 --- a/docs/Usage.md +++ b/docs/Usage.md @@ -4,8 +4,7 @@ Before using `shiori`, make sure it has been installed on your system. By defaul - [Running Docker Container](#running-docker-container) - [Using Command Line Interface](#using-command-line-interface) - - [Search syntax](#search-syntax) -- [Running migrations](#running-migrations) + - [Search syntax](#search-syntax) - [Using Web Interface](#using-web-interface) - [Improved import from Pocket](#improved-import-from-pocket) - [Import from Wallabag](#import-from-wallabag) @@ -43,14 +42,6 @@ Now you can use `shiori` like normal. If you've finished, you can stop and remov docker stop shiori ``` -## Running migrations - -If this is a fresh install or you're upgrading versions, you'll need to migrate the database to apply any required -changes for Shiori to work properly: - -- If you're using the binary version: `shiori migrate` -- If you're running the containerized version: `docker run --rm -v $(pwd):/shiori ghcr.io/go-shiori/shiori migrate` (you can also start a shell on the running container and perform the migration there). - ## Using Command Line Interface Shiori is composed by several subcommands. To see the documentation, run `shiori -h` : diff --git a/internal/cmd/migrate.go b/internal/cmd/migrate.go deleted file mode 100644 index dfcdc17..0000000 --- a/internal/cmd/migrate.go +++ /dev/null @@ -1,21 +0,0 @@ -package cmd - -import ( - "github.com/spf13/cobra" -) - -func migrateCmd() *cobra.Command { - cmd := &cobra.Command{ - Use: "migrate", - Short: "Migrates the database to the latest version", - Run: migrateHandler, - } - - return cmd -} - -func migrateHandler(cmd *cobra.Command, args []string) { - if err := db.Migrate(); err != nil { - cError.Printf("Error during migration: %s", err) - } -} diff --git a/internal/cmd/root.go b/internal/cmd/root.go index 2f582c2..ead193b 100644 --- a/internal/cmd/root.go +++ b/internal/cmd/root.go @@ -38,7 +38,6 @@ func ShioriCmd() *cobra.Command { pocketCmd(), serveCmd(), checkCmd(), - migrateCmd(), ) return rootCmd @@ -68,6 +67,12 @@ func preRunRootHandler(cmd *cobra.Command, args []string) { cError.Printf("Failed to open database: %v\n", err) os.Exit(1) } + + // Migrate + if err := db.Migrate(); err != nil { + cError.Printf("Error running migration: %s\n", err) + os.Exit(1) + } } func getDataDir(portableMode bool) (string, error) { diff --git a/internal/database/mysql.go b/internal/database/mysql.go index 9f9119a..d1e01f9 100644 --- a/internal/database/mysql.go +++ b/internal/database/mysql.go @@ -58,7 +58,11 @@ func (db *MySQLDatabase) Migrate() error { return errors.WithStack(err) } - return migration.Up() + if err := migration.Up(); err != nil && !errors.Is(err, migrate.ErrNoChange) { + return err + } + + return nil } // SaveBookmarks saves new or updated bookmarks to database. diff --git a/internal/database/pg.go b/internal/database/pg.go index b749d84..21addeb 100644 --- a/internal/database/pg.go +++ b/internal/database/pg.go @@ -59,7 +59,11 @@ func (db *PGDatabase) Migrate() error { return errors.WithStack(err) } - return migration.Up() + if err := migration.Up(); err != nil && !errors.Is(err, migrate.ErrNoChange) { + return err + } + + return nil } // SaveBookmarks saves new or updated bookmarks to database. diff --git a/internal/database/sqlite.go b/internal/database/sqlite.go index f319872..96e3760 100644 --- a/internal/database/sqlite.go +++ b/internal/database/sqlite.go @@ -67,7 +67,11 @@ func (db *SQLiteDatabase) Migrate() error { return errors.WithStack(err) } - return migration.Up() + if err := migration.Up(); err != nil && !errors.Is(err, migrate.ErrNoChange) { + return err + } + + return nil } // SaveBookmarks saves new or updated bookmarks to database.