diff options
| author | Shulhan <ms@kilabit.info> | 2021-08-17 23:07:24 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2021-08-17 23:07:24 +0700 |
| commit | f0a2d4794d62267e1a72339b710baec1f97c8b12 (patch) | |
| tree | c3ba7605cd584a8c1c9d41d3f63a52322ca2a0ed | |
| parent | 1fcdc84736b3c62e971b3fd0e92bb173fb98f4ba (diff) | |
| download | pakakeh.go-f0a2d4794d62267e1a72339b710baec1f97c8b12.tar.xz | |
lib/sql: do not run migration if the last file not exist on the list
Previously, if the last migrated file name not found on the migration
directory, we start executing migration start from the first file.
This changes this behaviour to not run any migration at all.
Since we cannot return it as an error, we only log it. In the future
we may return it.
| -rw-r--r-- | lib/sql/client.go | 29 |
1 files changed, 19 insertions, 10 deletions
diff --git a/lib/sql/client.go b/lib/sql/client.go index e6da7508..91bc3698 100644 --- a/lib/sql/client.go +++ b/lib/sql/client.go @@ -104,6 +104,8 @@ func (cl *Client) FetchTableNames() (tableNames []string, err error) { // SQL file name that has been executed and the timestamp. // func (cl *Client) Migrate(fs http.FileSystem) (err error) { + logp := "Migrate" + if reflect.IsNil(fs) { if len(cl.MigrationDir) == 0 { return nil @@ -113,12 +115,12 @@ func (cl *Client) Migrate(fs http.FileSystem) (err error) { root, err := fs.Open("/") if err != nil { - return fmt.Errorf("Migrate: %w", err) + return fmt.Errorf("%s: %w", logp, err) } fis, err := root.Readdir(0) if err != nil { - return fmt.Errorf("Migrate: %w", err) + return fmt.Errorf("%s: %w", logp, err) } sort.SliceStable(fis, func(x, y int) bool { @@ -127,20 +129,27 @@ func (cl *Client) Migrate(fs http.FileSystem) (err error) { lastFile, err := cl.migrateInit() if err != nil { - return fmt.Errorf("Migrate: %w", err) + return fmt.Errorf("%s: %w", logp, err) } - var x int + var ( + x int + lastFileExists bool + ) if len(lastFile) > 0 { for ; x < len(fis); x++ { if fis[x].Name() == lastFile { + lastFileExists = true break } } - if x == len(fis) { - x = 0 - } else { - x++ + x++ + // If the last file not found, there will be no SQL script to + // be executed. Since we cannot return it as an error, we + // only log it here. In the future, we may return it. + if !lastFileExists { + log.Printf("%s: the last file %s not found on the list", + logp, lastFile) } } for ; x < len(fis); x++ { @@ -148,7 +157,7 @@ func (cl *Client) Migrate(fs http.FileSystem) (err error) { sqlRaw, err := loadSQL(fs, fis[x], name) if err != nil { - return fmt.Errorf("Migrate %q: %w", name, err) + return fmt.Errorf("%s: %q: %w", logp, name, err) } if len(sqlRaw) == 0 { continue @@ -156,7 +165,7 @@ func (cl *Client) Migrate(fs http.FileSystem) (err error) { err = cl.migrateApply(name, sqlRaw) if err != nil { - return fmt.Errorf("Migrate %q: %w", name, err) + return fmt.Errorf("%s: %q: %w", logp, name, err) } } return nil |
