Skip to content

Commit

Permalink
Merge pull request #564 from azdagron/datastore-cleanup
Browse files Browse the repository at this point in the history
sql datastore cleanup
  • Loading branch information
azdagron authored Aug 24, 2018
2 parents 3181fdf + 27a9baf commit 2a5b0fc
Show file tree
Hide file tree
Showing 5 changed files with 1,054 additions and 871 deletions.
30 changes: 17 additions & 13 deletions pkg/server/plugin/datastore/sql/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@ const (
func migrateDB(db *gorm.DB) (err error) {
isNew := !db.HasTable(&Bundle{})
if err := db.Error; err != nil {
return err
return sqlError.Wrap(err)
}

if isNew {
return initDB(db)
}

if err := db.AutoMigrate(&Migration{}).Error; err != nil {
return err
return sqlError.Wrap(err)
}

migration := new(Migration)
if err := db.Assign(Migration{}).FirstOrCreate(migration).Error; err != nil {
return err
return sqlError.Wrap(err)
}
version := migration.Version

if version > codeVersion {
err = fmt.Errorf("backwards migration not supported! (current=%d, code=%d)", version, codeVersion)
err = sqlError.New("backwards migration not supported! (current=%d, code=%d)", version, codeVersion)
logrus.Error(err)
return err
}
Expand All @@ -46,15 +46,15 @@ func migrateDB(db *gorm.DB) (err error) {
for version < codeVersion {
tx := db.Begin()
if err := tx.Error; err != nil {
return err
return sqlError.Wrap(err)
}
version, err = migrateVersion(tx, version)
if err != nil {
tx.Rollback()
return err
}
if err := tx.Commit().Error; err != nil {
return err
return sqlError.Wrap(err)
}
}

Expand All @@ -66,22 +66,26 @@ func initDB(db *gorm.DB) (err error) {
logrus.Infof("initializing database.")
tx := db.Begin()
if err := tx.Error; err != nil {
return err
return sqlError.Wrap(err)
}

if err := tx.AutoMigrate(&Bundle{}, &CACert{}, &AttestedNodeEntry{},
&NodeResolverMapEntry{}, &RegisteredEntry{}, &JoinToken{},
&Selector{}, &Migration{}).Error; err != nil {
tx.Rollback()
return err
return sqlError.Wrap(err)
}

if err := tx.Assign(Migration{Version: codeVersion}).FirstOrCreate(&Migration{}).Error; err != nil {
tx.Rollback()
return err
return sqlError.Wrap(err)
}

if err := tx.Commit().Error; err != nil {
return sqlError.Wrap(err)
}

return tx.Commit().Error
return nil
}

func migrateVersion(tx *gorm.DB, version int) (versionOut int, err error) {
Expand All @@ -95,15 +99,15 @@ func migrateVersion(tx *gorm.DB, version int) (versionOut int, err error) {
case 0:
err = migrateToV1(tx)
default:
err = fmt.Errorf("no migration support for version %d", version)
err = sqlError.New("no migration support for version %d", version)
}
if err != nil {
return version, err
}

nextVersion := version + 1
if err := tx.Model(&Migration{}).Updates(Migration{Version: nextVersion}).Error; err != nil {
return version, err
return version, sqlError.Wrap(err)
}

return nextVersion, nil
Expand All @@ -126,7 +130,7 @@ func migrateToV1(tx *gorm.DB) error {
// sqlite3).
for _, table := range v0tables {
if err := tx.Exec(fmt.Sprintf("DELETE FROM %s WHERE deleted_at IS NOT NULL;", table)).Error; err != nil {
return err
return sqlError.Wrap(err)
}
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/server/plugin/datastore/sql/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type postgres struct{}
func (p postgres) connect(connectionString string) (*gorm.DB, error) {
db, err := gorm.Open("postgres", connectionString)
if err != nil {
return nil, err
return nil, sqlError.Wrap(err)
}
return db, nil

Expand Down
Loading

0 comments on commit 2a5b0fc

Please sign in to comment.