Skip to content

Commit

Permalink
Merge pull request #1232 from Permify/postgres-improvements
Browse files Browse the repository at this point in the history
refactor: postgres improvements
  • Loading branch information
tolgaOzen committed May 1, 2024
2 parents 14b5ce0 + 7b78c7e commit e75ac7f
Show file tree
Hide file tree
Showing 29 changed files with 274 additions and 249 deletions.
2 changes: 1 addition & 1 deletion docs/api-reference/apidocs.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"info": {
"title": "Permify API",
"description": "Permify is an open source authorization service for creating fine-grained and scalable authorization systems.",
"version": "v0.8.1",
"version": "v0.8.2",
"contact": {
"name": "API Support",
"url": "https://github.com/Permify/permify/issues",
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ require (
github.com/containerd/containerd v1.7.12 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/cpuguy83/dockercfg v0.3.1 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/docker/docker v25.0.5+incompatible // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
Expand Down
8 changes: 5 additions & 3 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,10 @@ type (

// Database contains configuration for the database.
Database struct {
Engine string `mapstructure:"engine"` // Database engine type (e.g., "postgres" or "memory")
URI string `mapstructure:"uri"` // Database connection URI
AutoMigrate bool `mapstructure:"auto_migrate"` // Whether to enable automatic migration
Engine string `mapstructure:"engine"` // Database engine type (e.g., "postgres" or "memory")
URI string `mapstructure:"uri"` // Database connection URI
AutoMigrate bool `mapstructure:"auto_migrate"` // Whether to enable automatic migration
SimpleMode bool `mapstructure:"simple_mode"`
MaxOpenConnections int `mapstructure:"max_open_connections"` // Maximum number of open connections to the database
MaxIdleConnections int `mapstructure:"max_idle_connections"` // Maximum number of idle connections to the database
MaxConnectionLifetime time.Duration `mapstructure:"max_connection_lifetime"` // Maximum duration a connection can be reused
Expand Down Expand Up @@ -317,6 +318,7 @@ func DefaultConfig() *Config {
Database: Database{
Engine: "memory",
AutoMigrate: true,
SimpleMode: false,
MaxDataPerWrite: 1000,
MaxRetries: 10,
WatchBufferSize: 100,
Expand Down
4 changes: 3 additions & 1 deletion internal/factories/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ func DatabaseFactory(conf config.Database) (db database.Database, err error) {
PQDatabase.WatchBufferSize(conf.WatchBufferSize),
PQDatabase.MaxDataPerWrite(conf.MaxDataPerWrite),
PQDatabase.MaxRetries(conf.MaxRetries),
PQDatabase.SimpleMode(conf.SimpleMode),
)
if err != nil {
return nil, err
}

// check postgres version
_, err = utils.EnsureDBVersion(db.(*PQDatabase.Postgres).DB)
_, err = utils.EnsureDBVersion(db.(*PQDatabase.Postgres).ReadPool)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var Identifier = ""
*/
const (
// Version is the last release of the Permify (e.g. v0.1.0)
Version = "v0.8.1"
Version = "v0.8.2"
)

// Function to create a single line of the ASCII art with centered content and color
Expand Down
1 change: 1 addition & 0 deletions internal/servers/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ func (s *Container) Run(
opts := []grpc.ServerOption{
grpc.ChainUnaryInterceptor(unaryInterceptors...),
grpc.ChainStreamInterceptor(streamingInterceptors...),
grpc.StatsHandler(otelgrpc.NewServerHandler()),
}

if srv.GRPC.TLSConfig.Enabled {
Expand Down
28 changes: 19 additions & 9 deletions internal/storage/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"log"

"github.com/jackc/pgx/v5/stdlib"

"github.com/pressly/goose/v3"

"github.com/Permify/permify/internal/config"
Expand All @@ -29,15 +31,15 @@ func Migrate(conf config.Database) (err error) {
case database.POSTGRES.String():
// Create a new Postgres database connection
var db *PQDatabase.Postgres
db, err = PQDatabase.New(conf.URI)
db, err = PQDatabase.New(conf.URI, PQDatabase.SimpleMode(conf.SimpleMode))
if err != nil {
return err
}
// Ensure database connection is closed when function returns
defer closeDB(db)

// check postgres version
_, err = utils.EnsureDBVersion(db.DB)
_, err = utils.EnsureDBVersion(db.ReadPool)
if err != nil {
return err
}
Expand All @@ -53,8 +55,10 @@ func Migrate(conf config.Database) (err error) {
// Set file system for migration scripts
goose.SetBaseFS(postgresMigrations)

pool := stdlib.OpenDBFromPool(db.WritePool)

// Perform migration
if err = goose.Up(db.DB, postgresMigrationDir); err != nil {
if err = goose.Up(pool, postgresMigrationDir); err != nil {
return err
}

Expand Down Expand Up @@ -86,8 +90,9 @@ func MigrateUp(engine, uri string) (err error) {
}

goose.SetBaseFS(postgresMigrations)
pool := stdlib.OpenDBFromPool(db.WritePool)

if err = goose.Up(db.DB, postgresMigrationDir); err != nil {
if err = goose.Up(pool, postgresMigrationDir); err != nil {
return err
}

Expand Down Expand Up @@ -117,8 +122,9 @@ func MigrateUpTo(engine, uri string, p int64) (err error) {
}

goose.SetBaseFS(postgresMigrations)
pool := stdlib.OpenDBFromPool(db.WritePool)

if err = goose.UpTo(db.DB, postgresMigrationDir, p); err != nil {
if err = goose.UpTo(pool, postgresMigrationDir, p); err != nil {
return err
}

Expand Down Expand Up @@ -148,8 +154,9 @@ func MigrateDown(engine, uri string) (err error) {
}

goose.SetBaseFS(postgresMigrations)
pool := stdlib.OpenDBFromPool(db.WritePool)

if err = goose.Down(db.DB, postgresMigrationDir); err != nil {
if err = goose.Down(pool, postgresMigrationDir); err != nil {
return err
}

Expand Down Expand Up @@ -179,8 +186,9 @@ func MigrateDownTo(engine, uri string, p int64) (err error) {
}

goose.SetBaseFS(postgresMigrations)
pool := stdlib.OpenDBFromPool(db.WritePool)

if err = goose.DownTo(db.DB, postgresMigrationDir, p); err != nil {
if err = goose.DownTo(pool, postgresMigrationDir, p); err != nil {
return err
}

Expand Down Expand Up @@ -210,8 +218,9 @@ func MigrateReset(engine, uri string) (err error) {
}

goose.SetBaseFS(postgresMigrations)
pool := stdlib.OpenDBFromPool(db.WritePool)

if err = goose.Reset(db.DB, postgresMigrationDir); err != nil {
if err = goose.Reset(pool, postgresMigrationDir); err != nil {
return err
}

Expand Down Expand Up @@ -241,8 +250,9 @@ func MigrateStatus(engine, uri string) (err error) {
}

goose.SetBaseFS(postgresMigrations)
pool := stdlib.OpenDBFromPool(db.WritePool)

if err = goose.Status(db.DB, postgresMigrationDir); err != nil {
if err = goose.Status(pool, postgresMigrationDir); err != nil {
return err
}

Expand Down
13 changes: 7 additions & 6 deletions internal/storage/postgres/bundleReader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package postgres

import (
"context"
"database/sql"
"errors"
"log/slog"
"strings"

"github.com/jackc/pgx/v5"

"github.com/Masterminds/squirrel"
"github.com/golang/protobuf/jsonpb"
"go.opentelemetry.io/otel/codes"
Expand All @@ -18,13 +19,13 @@ import (

type BundleReader struct {
database *db.Postgres
txOptions sql.TxOptions
txOptions pgx.TxOptions
}

func NewBundleReader(database *db.Postgres) *BundleReader {
return &BundleReader{
database: database,
txOptions: sql.TxOptions{Isolation: sql.LevelReadCommitted, ReadOnly: false},
txOptions: pgx.TxOptions{IsoLevel: pgx.ReadCommitted, AccessMode: pgx.ReadWrite},
}
}

Expand All @@ -46,13 +47,13 @@ func (b *BundleReader) Read(ctx context.Context, tenantID, name string) (bundle

slog.Debug("executing sql query", slog.Any("query", query), slog.Any("arguments", args))

var row *sql.Row
row = b.database.DB.QueryRowContext(ctx, query, args...)
var row pgx.Row
row = b.database.WritePool.QueryRow(ctx, query, args...)

var jsonData string
err = row.Scan(&jsonData)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
if errors.Is(err, pgx.ErrNoRows) {
return nil, errors.New(base.ErrorCode_ERROR_CODE_BUNDLE_NOT_FOUND.String())
}
return nil, utils.HandleError(ctx, span, err, base.ErrorCode_ERROR_CODE_SCAN)
Expand Down
11 changes: 6 additions & 5 deletions internal/storage/postgres/bundleWriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package postgres

import (
"context"
"database/sql"
"log/slog"

"github.com/jackc/pgx/v5"

"github.com/Masterminds/squirrel"
"github.com/golang/protobuf/jsonpb"

Expand All @@ -17,13 +18,13 @@ import (
type BundleWriter struct {
database *db.Postgres
// options
txOptions sql.TxOptions
txOptions pgx.TxOptions
}

func NewBundleWriter(database *db.Postgres) *BundleWriter {
return &BundleWriter{
database: database,
txOptions: sql.TxOptions{Isolation: sql.LevelReadCommitted, ReadOnly: false},
txOptions: pgx.TxOptions{IsoLevel: pgx.ReadCommitted, AccessMode: pgx.ReadWrite},
}
}

Expand Down Expand Up @@ -60,7 +61,7 @@ func (b *BundleWriter) Write(ctx context.Context, bundles []storage.Bundle) (nam

slog.Debug("executing sql insert query", slog.Any("query", query), slog.Any("arguments", args))

_, err = b.database.DB.ExecContext(ctx, query, args...)
_, err = b.database.WritePool.Exec(ctx, query, args...)
if err != nil {
return nil, utils.HandleError(ctx, span, err, base.ErrorCode_ERROR_CODE_EXECUTION)
}
Expand All @@ -86,7 +87,7 @@ func (b *BundleWriter) Delete(ctx context.Context, tenantID, name string) (err e
return utils.HandleError(ctx, span, err, base.ErrorCode_ERROR_CODE_SQL_BUILDER)
}

_, err = b.database.DB.ExecContext(ctx, query, args...)
_, err = b.database.WritePool.Exec(ctx, query, args...)
if err != nil {
return utils.HandleError(ctx, span, err, base.ErrorCode_ERROR_CODE_EXECUTION)
}
Expand Down

0 comments on commit e75ac7f

Please sign in to comment.