2023-10-19 12:19:10 +02:00
|
|
|
package handler
|
2022-03-23 09:02:39 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2022-08-31 09:52:43 +02:00
|
|
|
"github.com/jackc/pgconn"
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/logging"
|
2022-03-23 09:02:39 +01:00
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
errs "github.com/zitadel/zitadel/internal/errors"
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler"
|
2022-03-23 09:02:39 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Table struct {
|
2023-10-19 12:19:10 +02:00
|
|
|
columns []*InitColumn
|
2022-03-28 10:05:09 +02:00
|
|
|
primaryKey PrimaryKey
|
|
|
|
indices []*Index
|
|
|
|
constraints []*Constraint
|
2022-05-25 14:15:13 +02:00
|
|
|
foreignKeys []*ForeignKey
|
2022-03-23 09:02:39 +01:00
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func NewTable(columns []*InitColumn, key PrimaryKey, opts ...TableOption) *Table {
|
2022-03-28 10:05:09 +02:00
|
|
|
t := &Table{
|
2022-03-23 09:02:39 +01:00
|
|
|
columns: columns,
|
|
|
|
primaryKey: key,
|
|
|
|
}
|
2022-03-28 10:05:09 +02:00
|
|
|
for _, opt := range opts {
|
|
|
|
opt(t)
|
|
|
|
}
|
|
|
|
return t
|
2022-03-23 09:02:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type SuffixedTable struct {
|
|
|
|
Table
|
|
|
|
suffix string
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func NewSuffixedTable(columns []*InitColumn, key PrimaryKey, suffix string, opts ...TableOption) *SuffixedTable {
|
2022-03-23 09:02:39 +01:00
|
|
|
return &SuffixedTable{
|
2022-03-28 10:05:09 +02:00
|
|
|
Table: *NewTable(columns, key, opts...),
|
2022-03-23 09:02:39 +01:00
|
|
|
suffix: suffix,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-28 10:05:09 +02:00
|
|
|
type TableOption func(*Table)
|
|
|
|
|
|
|
|
func WithIndex(index *Index) TableOption {
|
|
|
|
return func(table *Table) {
|
|
|
|
table.indices = append(table.indices, index)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithConstraint(constraint *Constraint) TableOption {
|
|
|
|
return func(table *Table) {
|
|
|
|
table.constraints = append(table.constraints, constraint)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-25 14:15:13 +02:00
|
|
|
func WithForeignKey(key *ForeignKey) TableOption {
|
|
|
|
return func(table *Table) {
|
|
|
|
table.foreignKeys = append(table.foreignKeys, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
type InitColumn struct {
|
2022-03-23 09:02:39 +01:00
|
|
|
Name string
|
|
|
|
Type ColumnType
|
|
|
|
nullable bool
|
|
|
|
defaultValue interface{}
|
|
|
|
deleteCascade string
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
type ColumnOption func(*InitColumn)
|
2022-03-23 09:02:39 +01:00
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func NewColumn(name string, columnType ColumnType, opts ...ColumnOption) *InitColumn {
|
|
|
|
column := &InitColumn{
|
2022-03-23 09:02:39 +01:00
|
|
|
Name: name,
|
|
|
|
Type: columnType,
|
|
|
|
nullable: false,
|
|
|
|
defaultValue: nil,
|
|
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(column)
|
|
|
|
}
|
|
|
|
return column
|
|
|
|
}
|
|
|
|
|
|
|
|
func Nullable() ColumnOption {
|
2023-10-19 12:19:10 +02:00
|
|
|
return func(c *InitColumn) {
|
2022-03-23 09:02:39 +01:00
|
|
|
c.nullable = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Default(value interface{}) ColumnOption {
|
2023-10-19 12:19:10 +02:00
|
|
|
return func(c *InitColumn) {
|
2022-03-23 09:02:39 +01:00
|
|
|
c.defaultValue = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteCascade(column string) ColumnOption {
|
2023-10-19 12:19:10 +02:00
|
|
|
return func(c *InitColumn) {
|
2022-03-23 09:02:39 +01:00
|
|
|
c.deleteCascade = column
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type PrimaryKey []string
|
|
|
|
|
|
|
|
func NewPrimaryKey(columnNames ...string) PrimaryKey {
|
|
|
|
return columnNames
|
|
|
|
}
|
|
|
|
|
|
|
|
type ColumnType int32
|
|
|
|
|
|
|
|
const (
|
|
|
|
ColumnTypeText ColumnType = iota
|
|
|
|
ColumnTypeTextArray
|
|
|
|
ColumnTypeJSONB
|
|
|
|
ColumnTypeBytes
|
|
|
|
ColumnTypeTimestamp
|
2023-02-15 02:52:11 +01:00
|
|
|
ColumnTypeInterval
|
2022-03-23 09:02:39 +01:00
|
|
|
ColumnTypeEnum
|
|
|
|
ColumnTypeEnumArray
|
|
|
|
ColumnTypeInt64
|
|
|
|
ColumnTypeBool
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewIndex(name string, columns []string, opts ...indexOpts) *Index {
|
|
|
|
i := &Index{
|
2023-10-19 12:19:10 +02:00
|
|
|
Name: name,
|
|
|
|
Columns: columns,
|
2022-03-23 09:02:39 +01:00
|
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(i)
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
type Index struct {
|
2023-10-19 12:19:10 +02:00
|
|
|
Name string
|
|
|
|
Columns []string
|
|
|
|
includes []string
|
2022-03-23 09:02:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type indexOpts func(*Index)
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func WithInclude(columns ...string) indexOpts {
|
2022-03-23 09:02:39 +01:00
|
|
|
return func(i *Index) {
|
2023-10-19 12:19:10 +02:00
|
|
|
i.includes = columns
|
2022-03-23 09:02:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-28 10:05:09 +02:00
|
|
|
func NewConstraint(name string, columns []string) *Constraint {
|
|
|
|
i := &Constraint{
|
|
|
|
Name: name,
|
|
|
|
Columns: columns,
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
type Constraint struct {
|
|
|
|
Name string
|
|
|
|
Columns []string
|
|
|
|
}
|
|
|
|
|
2022-05-25 14:15:13 +02:00
|
|
|
func NewForeignKey(name string, columns []string, refColumns []string) *ForeignKey {
|
|
|
|
i := &ForeignKey{
|
|
|
|
Name: name,
|
|
|
|
Columns: columns,
|
|
|
|
RefColumns: refColumns,
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2022-11-30 17:01:17 +01:00
|
|
|
func NewForeignKeyOfPublicKeys() *ForeignKey {
|
2022-05-25 14:15:13 +02:00
|
|
|
return &ForeignKey{
|
2022-11-30 17:01:17 +01:00
|
|
|
Name: "",
|
2022-05-25 14:15:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ForeignKey struct {
|
|
|
|
Name string
|
|
|
|
Columns []string
|
|
|
|
RefColumns []string
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
type initializer interface {
|
|
|
|
Init() *handler.Check
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) Init(ctx context.Context) error {
|
|
|
|
check, ok := h.projection.(initializer)
|
|
|
|
if !ok || check.Init().IsNoop() {
|
2022-11-04 10:21:58 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
tx, err := h.client.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
2023-10-19 12:19:10 +02:00
|
|
|
return errs.ThrowInternal(err, "CRDB-SAdf2", "begin failed")
|
2022-11-04 10:21:58 +01:00
|
|
|
}
|
2023-10-19 12:19:10 +02:00
|
|
|
for i, execute := range check.Init().Executes {
|
|
|
|
logging.WithFields("projection", h.projection.Name(), "execute", i).Debug("executing check")
|
|
|
|
next, err := execute(tx, h.projection.Name())
|
2022-03-23 09:02:39 +01:00
|
|
|
if err != nil {
|
2023-10-19 12:19:10 +02:00
|
|
|
logging.OnError(tx.Rollback()).Debug("unable to rollback")
|
2022-03-23 09:02:39 +01:00
|
|
|
return err
|
|
|
|
}
|
2022-11-04 10:21:58 +01:00
|
|
|
if !next {
|
2023-10-19 12:19:10 +02:00
|
|
|
logging.WithFields("projection", h.projection.Name(), "execute", i).Debug("projection set up")
|
2022-11-04 10:21:58 +01:00
|
|
|
break
|
|
|
|
}
|
2022-03-23 09:02:39 +01:00
|
|
|
}
|
2022-11-04 10:21:58 +01:00
|
|
|
return tx.Commit()
|
2022-03-23 09:02:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewTableCheck(table *Table, opts ...execOption) *handler.Check {
|
|
|
|
config := execConfig{}
|
|
|
|
create := func(config execConfig) string {
|
|
|
|
return createTableStatement(table, config.tableName, "")
|
|
|
|
}
|
|
|
|
executes := make([]func(handler.Executer, string) (bool, error), len(table.indices)+1)
|
|
|
|
executes[0] = execNextIfExists(config, create, opts, true)
|
|
|
|
for i, index := range table.indices {
|
2022-11-30 17:01:17 +01:00
|
|
|
executes[i+1] = execNextIfExists(config, createIndexCheck(index), opts, true)
|
2022-03-23 09:02:39 +01:00
|
|
|
}
|
|
|
|
return &handler.Check{
|
|
|
|
Executes: executes,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMultiTableCheck(primaryTable *Table, secondaryTables ...*SuffixedTable) *handler.Check {
|
|
|
|
config := execConfig{}
|
|
|
|
create := func(config execConfig) string {
|
|
|
|
stmt := createTableStatement(primaryTable, config.tableName, "")
|
|
|
|
for _, table := range secondaryTables {
|
|
|
|
stmt += createTableStatement(&table.Table, config.tableName, "_"+table.suffix)
|
|
|
|
}
|
|
|
|
return stmt
|
|
|
|
}
|
|
|
|
|
|
|
|
return &handler.Check{
|
|
|
|
Executes: []func(handler.Executer, string) (bool, error){
|
|
|
|
execNextIfExists(config, create, nil, true),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewViewCheck(selectStmt string, secondaryTables ...*SuffixedTable) *handler.Check {
|
|
|
|
config := execConfig{}
|
|
|
|
create := func(config execConfig) string {
|
|
|
|
var stmt string
|
|
|
|
for _, table := range secondaryTables {
|
|
|
|
stmt += createTableStatement(&table.Table, config.tableName, "_"+table.suffix)
|
|
|
|
}
|
|
|
|
stmt += createViewStatement(config.tableName, selectStmt)
|
|
|
|
return stmt
|
|
|
|
}
|
|
|
|
|
|
|
|
return &handler.Check{
|
|
|
|
Executes: []func(handler.Executer, string) (bool, error){
|
|
|
|
execNextIfExists(config, create, nil, false),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func execNextIfExists(config execConfig, q query, opts []execOption, executeNext bool) func(handler.Executer, string) (bool, error) {
|
|
|
|
return func(handler handler.Executer, name string) (bool, error) {
|
|
|
|
err := exec(config, q, opts)(handler, name)
|
|
|
|
if isErrAlreadyExists(err) {
|
|
|
|
return executeNext, nil
|
|
|
|
}
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func isErrAlreadyExists(err error) bool {
|
2023-10-19 12:19:10 +02:00
|
|
|
caosErr := &errs.CaosError{}
|
2022-03-23 09:02:39 +01:00
|
|
|
if !errors.As(err, &caosErr) {
|
|
|
|
return false
|
|
|
|
}
|
2023-10-19 12:19:10 +02:00
|
|
|
pgErr := new(pgconn.PgError)
|
|
|
|
if errors.As(caosErr.Parent, &pgErr) {
|
|
|
|
return pgErr.Code == "42P07"
|
2022-03-23 09:02:39 +01:00
|
|
|
}
|
2023-10-19 12:19:10 +02:00
|
|
|
return false
|
2022-03-23 09:02:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func createTableStatement(table *Table, tableName string, suffix string) string {
|
2022-08-31 09:52:43 +02:00
|
|
|
stmt := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s (%s, PRIMARY KEY (%s)",
|
2022-03-23 09:02:39 +01:00
|
|
|
tableName+suffix,
|
|
|
|
createColumnsStatement(table.columns, tableName),
|
|
|
|
strings.Join(table.primaryKey, ", "),
|
|
|
|
)
|
2022-05-25 14:15:13 +02:00
|
|
|
for _, key := range table.foreignKeys {
|
|
|
|
ref := tableName
|
|
|
|
if len(key.RefColumns) > 0 {
|
|
|
|
ref += fmt.Sprintf("(%s)", strings.Join(key.RefColumns, ","))
|
|
|
|
}
|
|
|
|
if len(key.Columns) == 0 {
|
|
|
|
key.Columns = table.primaryKey
|
|
|
|
}
|
2022-11-30 17:01:17 +01:00
|
|
|
stmt += fmt.Sprintf(", CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s ON DELETE CASCADE", foreignKeyName(key.Name, tableName, suffix), strings.Join(key.Columns, ","), ref)
|
2022-05-25 14:15:13 +02:00
|
|
|
}
|
2022-03-28 10:05:09 +02:00
|
|
|
for _, constraint := range table.constraints {
|
2022-11-30 17:01:17 +01:00
|
|
|
stmt += fmt.Sprintf(", CONSTRAINT %s UNIQUE (%s)", constraintName(constraint.Name, tableName, suffix), strings.Join(constraint.Columns, ","))
|
2022-03-28 10:05:09 +02:00
|
|
|
}
|
2022-08-31 09:52:43 +02:00
|
|
|
|
|
|
|
stmt += ");"
|
|
|
|
|
|
|
|
for _, index := range table.indices {
|
2022-11-30 17:01:17 +01:00
|
|
|
stmt += createIndexStatement(index, tableName+suffix)
|
2022-08-31 09:52:43 +02:00
|
|
|
}
|
|
|
|
return stmt
|
2022-03-23 09:02:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func createViewStatement(viewName string, selectStmt string) string {
|
|
|
|
return fmt.Sprintf("CREATE VIEW %s AS %s",
|
|
|
|
viewName,
|
|
|
|
selectStmt,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-11-30 17:01:17 +01:00
|
|
|
func createIndexCheck(index *Index) func(config execConfig) string {
|
2022-03-23 09:02:39 +01:00
|
|
|
return func(config execConfig) string {
|
2022-11-30 17:01:17 +01:00
|
|
|
return createIndexStatement(index, config.tableName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createIndexStatement(index *Index, tableName string) string {
|
|
|
|
stmt := fmt.Sprintf("CREATE INDEX IF NOT EXISTS %s ON %s (%s)",
|
|
|
|
indexName(index.Name, tableName),
|
|
|
|
tableName,
|
|
|
|
strings.Join(index.Columns, ","),
|
|
|
|
)
|
2023-10-19 12:19:10 +02:00
|
|
|
if len(index.includes) > 0 {
|
|
|
|
stmt += " INCLUDE (" + strings.Join(index.includes, ", ") + ")"
|
2022-11-30 17:01:17 +01:00
|
|
|
}
|
2023-10-19 12:19:10 +02:00
|
|
|
return stmt + ";"
|
2022-11-30 17:01:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func foreignKeyName(name, tableName, suffix string) string {
|
|
|
|
if name == "" {
|
|
|
|
key := "fk" + suffix + "_ref_" + tableNameWithoutSchema(tableName)
|
|
|
|
return key
|
2022-03-23 09:02:39 +01:00
|
|
|
}
|
2022-11-30 17:01:17 +01:00
|
|
|
return "fk_" + tableNameWithoutSchema(tableName+suffix) + "_" + name
|
|
|
|
}
|
|
|
|
func constraintName(name, tableName, suffix string) string {
|
|
|
|
return tableNameWithoutSchema(tableName+suffix) + "_" + name + "_unique"
|
|
|
|
}
|
|
|
|
func indexName(name, tableName string) string {
|
|
|
|
return tableNameWithoutSchema(tableName) + "_" + name + "_idx"
|
|
|
|
}
|
|
|
|
|
|
|
|
func tableNameWithoutSchema(name string) string {
|
|
|
|
return name[strings.LastIndex(name, ".")+1:]
|
2022-03-23 09:02:39 +01:00
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func createColumnsStatement(cols []*InitColumn, tableName string) string {
|
2022-03-23 09:02:39 +01:00
|
|
|
columns := make([]string, len(cols))
|
|
|
|
for i, col := range cols {
|
|
|
|
column := col.Name + " " + columnType(col.Type)
|
|
|
|
if !col.nullable {
|
|
|
|
column += " NOT NULL"
|
|
|
|
}
|
|
|
|
if col.defaultValue != nil {
|
|
|
|
column += " DEFAULT " + defaultValue(col.defaultValue)
|
|
|
|
}
|
2022-03-28 10:05:09 +02:00
|
|
|
if len(col.deleteCascade) != 0 {
|
2022-03-23 09:02:39 +01:00
|
|
|
column += fmt.Sprintf(" REFERENCES %s (%s) ON DELETE CASCADE", tableName, col.deleteCascade)
|
|
|
|
}
|
|
|
|
columns[i] = column
|
|
|
|
}
|
|
|
|
return strings.Join(columns, ",")
|
|
|
|
}
|
|
|
|
|
|
|
|
func defaultValue(value interface{}) string {
|
|
|
|
switch v := value.(type) {
|
|
|
|
case string:
|
|
|
|
return "'" + v + "'"
|
2023-04-19 11:46:02 +03:00
|
|
|
case fmt.Stringer:
|
|
|
|
return fmt.Sprintf("%#v", v)
|
2022-03-23 09:02:39 +01:00
|
|
|
default:
|
|
|
|
return fmt.Sprintf("%v", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func columnType(columnType ColumnType) string {
|
|
|
|
switch columnType {
|
|
|
|
case ColumnTypeText:
|
|
|
|
return "TEXT"
|
|
|
|
case ColumnTypeTextArray:
|
|
|
|
return "TEXT[]"
|
|
|
|
case ColumnTypeTimestamp:
|
|
|
|
return "TIMESTAMPTZ"
|
2023-02-15 02:52:11 +01:00
|
|
|
case ColumnTypeInterval:
|
|
|
|
return "INTERVAL"
|
2022-03-23 09:02:39 +01:00
|
|
|
case ColumnTypeEnum:
|
|
|
|
return "SMALLINT"
|
|
|
|
case ColumnTypeEnumArray:
|
|
|
|
return "SMALLINT[]"
|
|
|
|
case ColumnTypeInt64:
|
|
|
|
return "BIGINT"
|
|
|
|
case ColumnTypeBool:
|
|
|
|
return "BOOLEAN"
|
|
|
|
case ColumnTypeJSONB:
|
|
|
|
return "JSONB"
|
|
|
|
case ColumnTypeBytes:
|
2022-08-31 09:52:43 +02:00
|
|
|
return "BYTEA"
|
2022-03-23 09:02:39 +01:00
|
|
|
default:
|
2022-05-16 16:35:49 +02:00
|
|
|
panic("unknown column type")
|
2022-03-23 09:02:39 +01:00
|
|
|
}
|
|
|
|
}
|