From 11facd7e6f9bf797da414ff168fea837d0e962d4 Mon Sep 17 00:00:00 2001 From: Stefan Benz <46600784+stebenz@users.noreply.github.com> Date: Mon, 31 Mar 2025 12:06:40 +0200 Subject: [PATCH] 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 (cherry picked from commit 2eb187f1410a25e80ff86255923d534041c8d139) --- cmd/setup/52.go | 24 ++++++++++++++++++++++-- cmd/setup/{52.sql => 52/alter.sql} | 2 +- cmd/setup/52/check.sql | 4 ++++ 3 files changed, 27 insertions(+), 3 deletions(-) rename cmd/setup/{52.sql => 52/alter.sql} (86%) create mode 100644 cmd/setup/52/check.sql diff --git a/cmd/setup/52.go b/cmd/setup/52.go index 5b86ba1bad..f5fc238c93 100644 --- a/cmd/setup/52.go +++ b/cmd/setup/52.go @@ -2,15 +2,19 @@ package setup import ( "context" + "database/sql" _ "embed" + "errors" "github.com/zitadel/zitadel/internal/database" "github.com/zitadel/zitadel/internal/eventstore" ) var ( - //go:embed 52.sql + //go:embed 52/alter.sql renameTableIfNotExisting string + //go:embed 52/check.sql + checkIfTableIsExisting string ) type IDPTemplate6LDAP2 struct { @@ -18,7 +22,23 @@ type IDPTemplate6LDAP2 struct { } func (mig *IDPTemplate6LDAP2) Execute(ctx context.Context, _ eventstore.Event) error { - _, err := mig.dbClient.ExecContext(ctx, renameTableIfNotExisting) + 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 } diff --git a/cmd/setup/52.sql b/cmd/setup/52/alter.sql similarity index 86% rename from cmd/setup/52.sql rename to cmd/setup/52/alter.sql index 1414b6386c..66df66d4e9 100644 --- a/cmd/setup/52.sql +++ b/cmd/setup/52/alter.sql @@ -1,2 +1,2 @@ ALTER TABLE IF EXISTS projections.idp_templates6_ldap3 RENAME COLUMN rootCA TO root_ca; -ALTER TABLE IF EXISTS projections.idp_templates6_ldap3 RENAME TO idp_templates6_ldap2; +ALTER TABLE IF EXISTS projections.idp_templates6_ldap3 RENAME TO idp_templates6_ldap2; \ No newline at end of file diff --git a/cmd/setup/52/check.sql b/cmd/setup/52/check.sql new file mode 100644 index 0000000000..f5eb07a341 --- /dev/null +++ b/cmd/setup/52/check.sql @@ -0,0 +1,4 @@ +SELECT 1 +FROM information_schema.tables +WHERE table_schema = 'projections' + AND table_name = 'idp_templates6_ldap2';