Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: delete sample instance data if the version isn't matched (#8720)

Danny Xu8c7627fb2eb0
diff --git a/backend/resources/postgres/postgres.go b/backend/resources/postgres/postgres.go
index cb47822976..3bed1f34da 100644
--- a/backend/resources/postgres/postgres.go
+++ b/backend/resources/postgres/postgres.go
@@ -44,7 +44,8 @@ const (
// SampleUser is the user name for the sample database.
SampleUser = "bbsample"
// SampleDatabase is the sample database name.
- SampleDatabase = "employee"
+ SampleDatabase = "employee"
+ currentPGVersion = "14"
)
// Install will extract the postgres and utility tar in resourceDir.
@@ -231,6 +232,18 @@ func InitDB(pgBinDir, pgDataDir, pgUser string) error {
return nil
}
+func getPGVersion(pgDataPath string) (string, error) {
+ versionPath := filepath.Join(pgDataPath, "PG_VERSION")
+ data, err := os.ReadFile(versionPath)
+ if err != nil {
+ if os.IsNotExist(err) {
+ return "", nil
+ }
+ return "", errors.Wrapf(err, "failed to check postgres version in data directory path %q", versionPath)
+ }
+ return string(data), nil
+}
+
func shouldSwitchUser() (int, int, bool, error) {
sameUser := true
bytebaseUser, err := user.Current()
@@ -419,6 +432,16 @@ func prepareDemoDatabase(ctx context.Context, pgUser, host, port, database strin
// StartSampleInstance starts a postgres sample instance.
func StartSampleInstance(ctx context.Context, pgBinDir, pgDataDir string, port int, mode common.ReleaseMode) error {
+ v, err := getPGVersion(pgDataDir)
+ if err != nil {
+ return err
+ }
+ if v != currentPGVersion {
+ err := os.RemoveAll(pgDataDir)
+ if err != nil {
+ return err
+ }
+ }
if err := InitDB(pgBinDir, pgDataDir, SampleUser); err != nil {
return errors.Wrapf(err, "failed to init sample instance")
}