-
Notifications
You must be signed in to change notification settings - Fork 284
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a flag to allow spicedb to run against non-head migrations
fixes #2135
- Loading branch information
Showing
13 changed files
with
428 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package common | ||
|
||
import ( | ||
"fmt" | ||
"slices" | ||
"strings" | ||
|
||
"github.com/authzed/spicedb/pkg/datastore" | ||
) | ||
|
||
type MigrationValidator struct { | ||
additionalAllowedMigrations []string | ||
headMigration string | ||
} | ||
|
||
func NewMigrationValidator(headMigration string, additionalAllowedMigrations []string) *MigrationValidator { | ||
return &MigrationValidator{ | ||
additionalAllowedMigrations: additionalAllowedMigrations, | ||
headMigration: headMigration, | ||
} | ||
} | ||
|
||
// MigrationReadyState returns the readiness of the datastore for the given version. | ||
func (mv *MigrationValidator) MigrationReadyState(version string) datastore.ReadyState { | ||
if version == mv.headMigration { | ||
return datastore.ReadyState{IsReady: true} | ||
} | ||
if slices.Contains(mv.additionalAllowedMigrations, version) { | ||
return datastore.ReadyState{IsReady: true} | ||
} | ||
var msgBuilder strings.Builder | ||
msgBuilder.WriteString(fmt.Sprintf("datastore is not migrated: currently at revision %q, but requires %q", version, mv.headMigration)) | ||
|
||
if len(mv.additionalAllowedMigrations) > 0 { | ||
msgBuilder.WriteString(fmt.Sprintf(" (additional allowed migrations: %v)", mv.additionalAllowedMigrations)) | ||
} | ||
msgBuilder.WriteString(". Please run \"spicedb datastore migrate\".") | ||
return datastore.ReadyState{ | ||
Message: msgBuilder.String(), | ||
IsReady: false, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package common | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/authzed/spicedb/pkg/datastore" | ||
) | ||
|
||
func TestMigrationReadyState(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
additionalAllowedMigrations []string | ||
headMigration string | ||
version string | ||
want datastore.ReadyState | ||
}{ | ||
{ | ||
name: "matches head migration", | ||
headMigration: "initial", | ||
version: "initial", | ||
want: datastore.ReadyState{IsReady: true}, | ||
}, | ||
{ | ||
name: "matches additional migration", | ||
headMigration: "initial", | ||
additionalAllowedMigrations: []string{"additional"}, | ||
version: "additional", | ||
want: datastore.ReadyState{IsReady: true}, | ||
}, | ||
{ | ||
name: "matches one of several additional migrations", | ||
headMigration: "initial", | ||
additionalAllowedMigrations: []string{"additional", "additional2", "additional3"}, | ||
version: "additional2", | ||
want: datastore.ReadyState{IsReady: true}, | ||
}, | ||
{ | ||
name: "matches head migration when additional migrations are allowed", | ||
headMigration: "initial", | ||
additionalAllowedMigrations: []string{"additional"}, | ||
version: "initial", | ||
want: datastore.ReadyState{IsReady: true}, | ||
}, | ||
{ | ||
name: "doesn't match head migration", | ||
headMigration: "initial", | ||
version: "additional", | ||
want: datastore.ReadyState{IsReady: false, Message: `datastore is not migrated: currently at revision "additional", but requires "initial". Please run "spicedb datastore migrate".`}, | ||
}, | ||
{ | ||
name: "doesn't match head migration or additional migrations", | ||
headMigration: "initial", | ||
additionalAllowedMigrations: []string{"additional", "additional2"}, | ||
version: "plustwo", | ||
want: datastore.ReadyState{IsReady: false, Message: `datastore is not migrated: currently at revision "plustwo", but requires "initial" (additional allowed migrations: [additional additional2]). Please run "spicedb datastore migrate".`}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
mv := &MigrationValidator{ | ||
additionalAllowedMigrations: tt.additionalAllowedMigrations, | ||
headMigration: tt.headMigration, | ||
} | ||
require.Equal(t, tt.want, mv.MigrationReadyState(tt.version)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.