feat(settings_relational): adding settings table for relational db (#10769)

# Which Problems Are Solved

As part of https://github.com/zitadel/zitadel/issues/9599 to push to
move to a relational db table model, this is the migration of the
settings

# Additional Changes

I had to add 2 non source code files
`backend/v3/storage/database/events_testing/font.otf` and
`backend/v3/storage/database/events_testing/picture.png`, these are used
as part of the reduce tests, hence why they're sourced inside
`backend/v3/storage/database/events_testing/settings_instance_test.go`

- Closes https://github.com/zitadel/zitadel/issues/10207

---------

Co-authored-by: adlerhurst <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Tim Möhlmann <[email protected]>
Co-authored-by: Stefan Benz <[email protected]>
This commit is contained in:
Iraq
2025-12-02 08:16:21 +01:00
committed by GitHub
co-authored by adlerhurst Copilot Tim Möhlmann Stefan Benz
parent 6c8619e2f0
commit 911967c865
30 changed files with 12316 additions and 27 deletions
@@ -51,6 +51,9 @@ func wrapPgError(err *pgconn.PgError) error {
// 23502: not_null_violation - A value violates a NOT NULL constraint.
case "23502":
return database.NewNotNullError(err.TableName, err.ConstraintName, err)
// 22P02: integrity violation - A value is being inserted into a enum that is not valid
case "22P02":
return database.NewCheckError(err.TableName, err.ConstraintName, err)
default:
return database.NewUnknownError(err)
}
@@ -0,0 +1,16 @@
package migration
import (
_ "embed"
)
var (
//go:embed 009_settings_table/up.sql
up009SettingsTable string
//go:embed 009_settings_table/down.sql
down009SettingsTable string
)
func init() {
registerSQLMigration(9, up009SettingsTable, down009SettingsTable)
}
@@ -0,0 +1,3 @@
DROP TABLE IF EXISTS zitadel.settings;
DROP TYPE IF EXISTS zitadel.settings_type;
DROP TYPE IF EXISTS zitadel.settings_state;
@@ -0,0 +1,95 @@
CREATE TYPE zitadel.settings_type AS ENUM (
'login',
'branding',
'password_complexity',
'password_expiry',
'domain',
'lockout',
'security',
'organization',
'notification',
'legal_and_support'
);
CREATE TYPE zitadel.settings_state AS ENUM (
'active',
'preview'
);
CREATE TABLE zitadel.settings (
instance_id TEXT NOT NULL
, organization_id TEXT
, id TEXT NOT NULL DEFAULT gen_random_uuid()
, type zitadel.settings_type NOT NULL
, state zitadel.settings_state NOT NULL
, settings JSONB -- the storage does not really care about what is configured so we store it as json
, created_at TIMESTAMPTZ NOT NULL DEFAULT now()
, updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
, PRIMARY KEY (instance_id, id, type, state)
, FOREIGN KEY (instance_id) REFERENCES zitadel.instances(id) ON DELETE CASCADE
, FOREIGN KEY (instance_id, organization_id) REFERENCES zitadel.organizations(instance_id, id) ON DELETE CASCADE
, UNIQUE (instance_id, organization_id, type, state)
);
CREATE UNIQUE INDEX idx_settings_unique_type ON zitadel.settings (instance_id, organization_id, type, state) NULLS NOT DISTINCT;
CREATE TRIGGER trigger_set_updated_at
BEFORE UPDATE ON zitadel.settings
FOR EACH ROW
WHEN (NEW.updated_at IS NULL)
EXECUTE FUNCTION zitadel.set_updated_at();
CREATE OR REPLACE FUNCTION zitadel.jsonb_array_remove(
source JSONB
, path TEXT[]
, value anyelement
, outt out jsonb
)
language 'plpgsql'
AS $$
BEGIN
outt = jsonb_set(source, path,
(CASE WHEN (SELECT source ?& path) then
coalesce(
(SELECT jsonb_agg(v)
FROM jsonb_array_elements(source #>path) AS elem(v)
WHERE v::text <> value::text),
jsonb_build_array())
ELSe
jsonb_build_array()
END)::jsonb, TRUE);
END;
$$;
CREATE OR REPLACE FUNCTION zitadel.jsonb_array_append(
source jsonb
, path text[]
, value anyelement
, outt out jsonb
)
language 'plpgsql'
AS $$
BEGIN
outt := jsonb_insert(
source,
(CASE WHEN (SELECT source ?& path) then
array_append(path, '-1')
ELSE
path
END)::text[],
(CASE WHEN (select source ?& path) then
value::TEXT::jsonb
ELSE
jsonb_build_array(value)
END),
TRUE
);
END;
$$;