Skip to content

Commit

Permalink
Merge pull request #1010 from berkeli/feat-error-for-unsupported-psql…
Browse files Browse the repository at this point in the history
…-versions

feat: error for unsupported psql versions
  • Loading branch information
tolgaOzen authored Jan 17, 2024
2 parents 4ddc002 + e7bb4b9 commit a747784
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
7 changes: 7 additions & 0 deletions internal/factories/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/Permify/permify/internal/config"
"github.com/Permify/permify/internal/storage/memory/migrations"
"github.com/Permify/permify/internal/storage/postgres/utils"
"github.com/Permify/permify/pkg/database"
IMDatabase "github.com/Permify/permify/pkg/database/memory"
PQDatabase "github.com/Permify/permify/pkg/database/postgres"
Expand Down Expand Up @@ -37,6 +38,12 @@ func DatabaseFactory(conf config.Database) (db database.Database, err error) {
if err != nil {
return nil, err
}
// check postgres version
_, err = utils.EnsureDBVersion(db.(*PQDatabase.Postgres).DB)
if err != nil {
return nil, err
}

return
case database.MEMORY.String():
db, err = IMDatabase.New(migrations.Schema)
Expand Down
14 changes: 11 additions & 3 deletions internal/storage/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ import (
"github.com/pressly/goose/v3"

"github.com/Permify/permify/internal/config"
"github.com/Permify/permify/internal/storage/postgres/utils"
"github.com/Permify/permify/pkg/database"
PQDatabase "github.com/Permify/permify/pkg/database/postgres"
)

const (
postgresMigrationDir = "postgres/migrations"
postgresDialect = "postgres"
migrationsTable = "migrations"
postgresMigrationDir = "postgres/migrations"
postgresDialect = "postgres"
migrationsTable = "migrations"
earliestPostgresVersion = 130008
)

//go:embed postgres/migrations/*.sql
Expand All @@ -34,6 +36,12 @@ func Migrate(conf config.Database) (err error) {
// Ensure database connection is closed when function returns
defer closeDB(db)

// check postgres version
_, err = utils.EnsureDBVersion(db.DB)
if err != nil {
return err
}

// Set table name for migrations
goose.SetTableName(migrationsTable)

Expand Down
24 changes: 24 additions & 0 deletions internal/storage/postgres/utils/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package utils

import (
"database/sql"
"fmt"
)

const (
// The earliest supported version of PostgreSQL is 13.8
earliestPostgresVersion = 130008
)

// EnsureDBVersion checks the version of the given database connection and returns an error if the version is not
// supported.
func EnsureDBVersion(db *sql.DB) (version int, err error) {
err = db.QueryRow("SHOW server_version_num;").Scan(&version)
if err != nil {
return
}
if version < earliestPostgresVersion {
err = fmt.Errorf("unsupported postgres version: %d, expected >= %d", version, earliestPostgresVersion)
}
return
}

0 comments on commit a747784

Please sign in to comment.