mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-14 03:54:21 +00:00
56df515e5f
* chore: use pgx v5 * chore: update go version * remove direct pq dependency * remove unnecessary type * scan test * map scanner * converter * uint8 number array * duration * most unit tests work * unit tests work * chore: coverage * go 1.21 * linting * int64 gopfertammi * retry go 1.22 * retry go 1.22 * revert to go v1.21.5 * update go toolchain to 1.21.8 * go 1.21.8 * remove test flag * go 1.21.5 * linting * update toolchain * use correct array * use correct array * add byte array * correct value * correct error message * go 1.21 compatible
74 lines
1.6 KiB
Go
74 lines
1.6 KiB
Go
package setup
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"embed"
|
|
"errors"
|
|
"strings"
|
|
|
|
"github.com/jackc/pgx/v5/pgconn"
|
|
"github.com/zitadel/logging"
|
|
|
|
"github.com/zitadel/zitadel/internal/database"
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
)
|
|
|
|
var (
|
|
//go:embed 14/cockroach/*.sql
|
|
//go:embed 14/postgres/*.sql
|
|
newEventsTable embed.FS
|
|
)
|
|
|
|
type NewEventsTable struct {
|
|
dbClient *database.DB
|
|
}
|
|
|
|
func (mig *NewEventsTable) Execute(ctx context.Context, _ eventstore.Event) error {
|
|
migrations, err := newEventsTable.ReadDir("14/" + mig.dbClient.Type())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// if events already exists events2 is created during a setup job
|
|
var count int
|
|
err = mig.dbClient.QueryRow(
|
|
func(row *sql.Row) error {
|
|
if err = row.Scan(&count); err != nil {
|
|
return err
|
|
}
|
|
return row.Err()
|
|
},
|
|
"SELECT count(*) FROM information_schema.tables WHERE table_schema = 'eventstore' AND table_name like 'events2'",
|
|
)
|
|
if err != nil || count == 1 {
|
|
return err
|
|
}
|
|
for _, migration := range migrations {
|
|
stmt, err := readStmt(newEventsTable, "14", mig.dbClient.Type(), migration.Name())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
stmt = strings.ReplaceAll(stmt, "{{.username}}", mig.dbClient.Username())
|
|
|
|
logging.WithFields("migration", mig.String(), "file", migration.Name()).Debug("execute statement")
|
|
|
|
_, err = mig.dbClient.ExecContext(ctx, stmt)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (mig *NewEventsTable) String() string {
|
|
return "14_events_push"
|
|
}
|
|
|
|
func (mig *NewEventsTable) ContinueOnErr(err error) bool {
|
|
pgErr := new(pgconn.PgError)
|
|
if errors.As(err, &pgErr) {
|
|
return pgErr.Code == "42P01"
|
|
}
|
|
return false
|
|
}
|