diff --git a/storage/engine.go b/storage/engine.go index 9c223d7742..5de4376d4f 100644 --- a/storage/engine.go +++ b/storage/engine.go @@ -183,11 +183,16 @@ func (e *engine) initSQLDatabase() error { return err } migrations, err := migrate.NewWithInstance("iofs", sourceDriver, e.sqlDB.Name(), databaseDriver) - migrations.Log = sqlMigrationLogger{} if err != nil { return err } - return migrations.Up() + migrations.Log = sqlMigrationLogger{} + err = migrations.Up() + if errors.Is(err, migrate.ErrNoChange) { + // There was nothing to migrate + return nil + } + return err } type provider struct { diff --git a/storage/engine_test.go b/storage/engine_test.go index 3260d7d932..a91aa4c6b1 100644 --- a/storage/engine_test.go +++ b/storage/engine_test.go @@ -26,6 +26,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.uber.org/mock/gomock" + "os" "path" "testing" ) @@ -103,14 +104,32 @@ func Test_engine_sqlDatabase(t *testing.T) { t.Run("defaults to SQLite in data directory", func(t *testing.T) { e := New() dataDir := io.TestDirectory(t) - err := e.Configure(core.ServerConfig{Datadir: dataDir}) + require.NoError(t, e.Configure(core.ServerConfig{Datadir: dataDir})) require.NoError(t, e.Start()) t.Cleanup(func() { _ = e.Shutdown() }) - require.NoError(t, err) assert.FileExists(t, path.Join(dataDir, "sqlite.db")) }) + t.Run("unable to open SQLite database", func(t *testing.T) { + dataDir := io.TestDirectory(t) + require.NoError(t, os.Remove(dataDir)) + e := New() + require.NoError(t, e.Configure(core.ServerConfig{Datadir: dataDir})) + err := e.Start() + assert.EqualError(t, err, "failed to initialize SQL database: unable to open database file") + }) + t.Run("nothing to migrate (already migrated)", func(t *testing.T) { + dataDir := io.TestDirectory(t) + e := New() + require.NoError(t, e.Configure(core.ServerConfig{Datadir: dataDir})) + require.NoError(t, e.Start()) + require.NoError(t, e.Shutdown()) + e = New() + require.NoError(t, e.Configure(core.ServerConfig{Datadir: dataDir})) + require.NoError(t, e.Start()) + require.NoError(t, e.Shutdown()) + }) t.Run("runs migrations", func(t *testing.T) { e := New().(*engine) e.config.SQL.ConnectionString = SQLiteInMemoryConnectionString @@ -128,4 +147,5 @@ func Test_engine_sqlDatabase(t *testing.T) { assert.NoError(t, row.Scan(&count)) assert.Equal(t, 1, count) }) + }