mirror of
https://github.com/portainer/portainer.git
synced 2025-07-23 07:19:41 +02:00
fix(migration) datastore always marked new and migrations skipped EE-1775 (#5788)
* fix issue with broken store init * minor logic improvement * Remove fileexists logic as its redundant and handled implicitely by bolt.Open * Added re-open test on IsNew flag. Essential for migrations to be able to run
This commit is contained in:
parent
fc4ff59bfd
commit
6bd72d21a8
4 changed files with 20 additions and 25 deletions
|
@ -90,20 +90,13 @@ func (store *Store) edition() portainer.SoftwareEdition {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewStore initializes a new Store and the associated services
|
// NewStore initializes a new Store and the associated services
|
||||||
func NewStore(storePath string, fileService portainer.FileService) (*Store, error) {
|
func NewStore(storePath string, fileService portainer.FileService) *Store {
|
||||||
store := &Store{
|
return &Store{
|
||||||
path: storePath,
|
path: storePath,
|
||||||
fileService: fileService,
|
fileService: fileService,
|
||||||
isNew: true,
|
isNew: true,
|
||||||
connection: &internal.DbConnection{},
|
connection: &internal.DbConnection{},
|
||||||
}
|
}
|
||||||
|
|
||||||
databasePath := path.Join(storePath, databaseFileName)
|
|
||||||
if _, err := fileService.FileExists(databasePath); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return store, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open opens and initializes the BoltDB database.
|
// Open opens and initializes the BoltDB database.
|
||||||
|
@ -120,10 +113,9 @@ func (store *Store) Open() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
//if failed to retrieve DBVersion from database
|
// if we have DBVersion in the database then ensure we flag this as NOT a new store
|
||||||
//treat it as a new store
|
if _, err := store.VersionService.DBVersion(); err == nil {
|
||||||
if _, err := store.VersionService.DBVersion(); err != nil {
|
store.isNew = false
|
||||||
store.isNew = true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -17,12 +17,23 @@ func testVersion(store *Store, versionWant int, t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMigrateData(t *testing.T) {
|
func TestMigrateData(t *testing.T) {
|
||||||
t.Run("MigrateData for New Store", func(t *testing.T) {
|
t.Run("MigrateData for New Store & Re-Open Check", func(t *testing.T) {
|
||||||
store, teardown := MustNewTestStore(false)
|
store, teardown := MustNewTestStore(false)
|
||||||
defer teardown()
|
defer teardown()
|
||||||
|
|
||||||
|
if !store.IsNew() {
|
||||||
|
t.Error("Expect a new DB")
|
||||||
|
}
|
||||||
|
|
||||||
store.MigrateData(false)
|
store.MigrateData(false)
|
||||||
|
|
||||||
testVersion(store, portainer.DBVersion, t)
|
testVersion(store, portainer.DBVersion, t)
|
||||||
store.Close()
|
store.Close()
|
||||||
|
|
||||||
|
store.Open()
|
||||||
|
if store.IsNew() {
|
||||||
|
t.Error("Expect store to NOT be new DB")
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
|
|
|
@ -35,11 +35,7 @@ func NewTestStore(init bool) (*Store, func(), error) {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
store, err := NewStore(dataStorePath, fileService)
|
store := NewStore(dataStorePath, fileService)
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = store.Open()
|
err = store.Open()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
|
|
|
@ -57,12 +57,8 @@ func initFileService(dataStorePath string) portainer.FileService {
|
||||||
}
|
}
|
||||||
|
|
||||||
func initDataStore(dataStorePath string, rollback bool, fileService portainer.FileService, shutdownCtx context.Context) portainer.DataStore {
|
func initDataStore(dataStorePath string, rollback bool, fileService portainer.FileService, shutdownCtx context.Context) portainer.DataStore {
|
||||||
store, err := bolt.NewStore(dataStorePath, fileService)
|
store := bolt.NewStore(dataStorePath, fileService)
|
||||||
if err != nil {
|
err := store.Open()
|
||||||
log.Fatalf("failed creating data store: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = store.Open()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("failed opening store: %v", err)
|
log.Fatalf("failed opening store: %v", err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue