func (m *sqlNamespaceManager) GetRewrite(ctx context.Context, namespace, relation string) (*aclpb.Rewrite, error) {
	cfg, err := m.GetConfig(ctx, namespace)
	if err != nil {
		return nil, err
	}
	if cfg == nil {
		return nil, nil
	}
	relations := cfg.GetRelations()
	for _, r := range relations {
		if r.GetName() == relation {
			rewrite := r.GetRewrite()
			if rewrite == nil {
				rewrite = &aclpb.Rewrite{
					RewriteOperation: &aclpb.Rewrite_Union{
						Union: &aclpb.SetOperation{
							Children: []*aclpb.SetOperation_Child{
								{ChildType: &aclpb.SetOperation_Child_This_{}},
							},
						},
					},
				}
			}
			return rewrite, nil
		}
	}
	return nil, nil
}
GetRewrite fetches the latest namespace config rewrite for the (namespace, relation) pair. If no namespace
config exists for the provided namespace 'nil' is returned. If any error occurs with the underlying txn
an error is returned.
package postgres
import (
	"context"
	"database/sql"
	"fmt"
	"time"
	"github.com/doug-martin/goqu/v9"
	"google.golang.org/protobuf/encoding/protojson"
	aclpb "github.com/authorizer-tech/access-controller/genprotos/authorizer/accesscontroller/v1alpha1"
	namespacemgr "github.com/authorizer-tech/access-controller/internal/namespace-manager"
)
type txnKey struct{}
type sqlNamespaceManager struct {
	db *sql.DB
}
func NewNamespaceManager(db *sql.DB) (namespacemgr.NamespaceManager, error) {
	m := sqlNamespaceManager{
		db,
	}
	return &m, nil
}
func (m *sqlNamespaceManager) UpsertConfig(ctx context.Context, cfg *aclpb.NamespaceConfig) error {
	jsonConfig, err := protojson.Marshal(cfg)
	if err != nil {
		return err
	}
	err = m.withTransaction(ctx, func(ctx context.Context, txn *sql.Tx) error {
		row := txn.QueryRow(`SELECT COUNT(namespace) FROM "namespace-configs" WHERE namespace=$1`, cfg.GetName())
		var count int
		if err := row.Scan(&count); err != nil {
			return err
		}
		var operation namespacemgr.NamespaceOperation
		if count > 0 {
			operation = namespacemgr.UpdateNamespace
		} else {
			operation = namespacemgr.AddNamespace
			stmt := fmt.Sprintf(`CREATE TABLE IF NOT EXISTS %s (
				object text,
				relation text,
				subject text,
				PRIMARY KEY (object, relation, subject)
			)`, cfg.GetName())
			_, err := txn.Exec(stmt)
			if err != nil {
				return err
			}
		}
		ins1 := goqu.Insert("namespace-configs").
			Cols("namespace", "config", "timestamp").
			Vals(
				goqu.Vals{cfg.GetName(), jsonConfig, goqu.L("NOW()")},
			)
		ins2 := goqu.Insert("namespace-changelog").
			Cols("namespace", "operation", "config", "timestamp").
			Vals(
				goqu.Vals{cfg.GetName(), operation, jsonConfig, goqu.L("NOW()")},  
			)
		sql1, args1, err := ins1.ToSQL()
		if err != nil {
			return err
		}
		sql2, args2, err := ins2.ToSQL()
		if err != nil {
			return err
		}
		_, err = txn.Exec(sql1, args1...)
		if err != nil {
			return err
		}
		_, err = txn.Exec(sql2, args2...)
		if err != nil {
			return err
		}
		return nil
	})
	if err != nil {
		return err
	}
	return nil
}
func (m *sqlNamespaceManager) GetConfig(ctx context.Context, namespace string) (*aclpb.NamespaceConfig, error) {
	query := `SELECT config FROM "namespace-configs" WHERE namespace = $1 AND timestamp = (SELECT MAX(timestamp) FROM "namespace-configs" WHERE namespace = $1)`
	var jsonConfig string
	err := m.withTransaction(ctx, func(ctx context.Context, txn *sql.Tx) error {
		row := txn.QueryRow(query, namespace)
		if err := row.Scan(&jsonConfig); err != nil {
			if err == sql.ErrNoRows {
				return namespacemgr.ErrNamespaceDoesntExist
			}
			return err
		}
		return nil
	})
	if err != nil {
		return nil, err
	}
	var config aclpb.NamespaceConfig
	if err := protojson.Unmarshal([]byte(jsonConfig), &config); err != nil {
		return nil, err
	}
	return &config, nil
}