zitadel/cmd/setup/52.go
Stefan Benz 2eb187f141
fix(migration): check if ldap2 already exists (#9674)
# Which Problems Are Solved

With v2.71.0 the `idp_templates6_ldap3` projection was created but never
filled, as it was a subtable. To fix this we altered the
`idp_templates6_ldap3` to `idp_templates6_ldap2` with v2.71.5.
This was unfortunately without a check that the `idp_templates_ldap2`was
already existing, which resulted in an error in the setup step.

# How the Problems Are Solved

Add check if `idp_templates6_ldap2` is already existing, before renaming
`idp_templates6_ldap3` -> `idp_templates6_ldap2`.

# Additional Changes

None

# Additional Context

Closes #9669
2025-03-31 10:06:40 +00:00

48 lines
897 B
Go

package setup
import (
"context"
"database/sql"
_ "embed"
"errors"
"github.com/zitadel/zitadel/internal/database"
"github.com/zitadel/zitadel/internal/eventstore"
)
var (
//go:embed 52/alter.sql
renameTableIfNotExisting string
//go:embed 52/check.sql
checkIfTableIsExisting string
)
type IDPTemplate6LDAP2 struct {
dbClient *database.DB
}
func (mig *IDPTemplate6LDAP2) Execute(ctx context.Context, _ eventstore.Event) error {
var count int
err := mig.dbClient.QueryRowContext(ctx,
func(row *sql.Row) error {
if err := row.Scan(&count); err != nil {
return err
}
return row.Err()
},
checkIfTableIsExisting,
)
if err == nil {
return nil
}
if !errors.Is(err, sql.ErrNoRows) {
return err
}
_, err = mig.dbClient.ExecContext(ctx, renameTableIfNotExisting)
return err
}
func (mig *IDPTemplate6LDAP2) String() string {
return "52_idp_templates6_ldap2"
}