fix: fix and improve primary keys on projections (#3708)

* fix: org_domain projection

* fix: projection reset

* fix test

* improve foreign keys on suffixed tables
This commit is contained in:
Livio Amstutz
2022-05-25 14:15:13 +02:00
committed by GitHub
parent 79452da7d6
commit 737e01bfd2
20 changed files with 403 additions and 156 deletions

View File

@@ -90,7 +90,7 @@ func TestIDPProjection_reduces(t *testing.T) {
executer: &testExecuter{
executions: []execution{
{
expectedStmt: "UPDATE projections.idps SET (name, styling_type, auto_register, change_date, sequence) = ($1, $2, $3, $4, $5) WHERE (id = $6)",
expectedStmt: "UPDATE projections.idps SET (name, styling_type, auto_register, change_date, sequence) = ($1, $2, $3, $4, $5) WHERE (id = $6) AND (instance_id = $7)",
expectedArgs: []interface{}{
"custom-zitadel-instance",
domain.IDPConfigStylingTypeGoogle,
@@ -98,6 +98,7 @@ func TestIDPProjection_reduces(t *testing.T) {
anyArg{},
uint64(15),
"idp-config-id",
"instance-id",
},
},
},
@@ -124,12 +125,13 @@ func TestIDPProjection_reduces(t *testing.T) {
executer: &testExecuter{
executions: []execution{
{
expectedStmt: "UPDATE projections.idps SET (state, change_date, sequence) = ($1, $2, $3) WHERE (id = $4)",
expectedStmt: "UPDATE projections.idps SET (state, change_date, sequence) = ($1, $2, $3) WHERE (id = $4) AND (instance_id = $5)",
expectedArgs: []interface{}{
domain.IDPConfigStateInactive,
anyArg{},
uint64(15),
"idp-config-id",
"instance-id",
},
},
},
@@ -156,12 +158,13 @@ func TestIDPProjection_reduces(t *testing.T) {
executer: &testExecuter{
executions: []execution{
{
expectedStmt: "UPDATE projections.idps SET (state, change_date, sequence) = ($1, $2, $3) WHERE (id = $4)",
expectedStmt: "UPDATE projections.idps SET (state, change_date, sequence) = ($1, $2, $3) WHERE (id = $4) AND (instance_id = $5)",
expectedArgs: []interface{}{
domain.IDPConfigStateActive,
anyArg{},
uint64(15),
"idp-config-id",
"instance-id",
},
},
},
@@ -188,9 +191,10 @@ func TestIDPProjection_reduces(t *testing.T) {
executer: &testExecuter{
executions: []execution{
{
expectedStmt: "DELETE FROM projections.idps WHERE (id = $1)",
expectedStmt: "DELETE FROM projections.idps WHERE (id = $1) AND (instance_id = $2)",
expectedArgs: []interface{}{
"idp-config-id",
"instance-id",
},
},
},
@@ -229,18 +233,20 @@ func TestIDPProjection_reduces(t *testing.T) {
executer: &testExecuter{
executions: []execution{
{
expectedStmt: "UPDATE projections.idps SET (change_date, sequence, type) = ($1, $2, $3) WHERE (id = $4)",
expectedStmt: "UPDATE projections.idps SET (change_date, sequence, type) = ($1, $2, $3) WHERE (id = $4) AND (instance_id = $5)",
expectedArgs: []interface{}{
anyArg{},
uint64(15),
domain.IDPConfigTypeOIDC,
"idp-config-id",
"instance-id",
},
},
{
expectedStmt: "INSERT INTO projections.idps_oidc_config (idp_id, client_id, client_secret, issuer, scopes, display_name_mapping, username_mapping, authorization_endpoint, token_endpoint) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)",
expectedStmt: "INSERT INTO projections.idps_oidc_config (idp_id, instance_id, client_id, client_secret, issuer, scopes, display_name_mapping, username_mapping, authorization_endpoint, token_endpoint) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)",
expectedArgs: []interface{}{
"idp-config-id",
"instance-id",
"client-id",
anyArg{},
"issuer",
@@ -287,15 +293,16 @@ func TestIDPProjection_reduces(t *testing.T) {
executer: &testExecuter{
executions: []execution{
{
expectedStmt: "UPDATE projections.idps SET (change_date, sequence) = ($1, $2) WHERE (id = $3)",
expectedStmt: "UPDATE projections.idps SET (change_date, sequence) = ($1, $2) WHERE (id = $3) AND (instance_id = $4)",
expectedArgs: []interface{}{
anyArg{},
uint64(15),
"idp-config-id",
"instance-id",
},
},
{
expectedStmt: "UPDATE projections.idps_oidc_config SET (client_id, client_secret, issuer, authorization_endpoint, token_endpoint, scopes, display_name_mapping, username_mapping) = ($1, $2, $3, $4, $5, $6, $7, $8) WHERE (idp_id = $9)",
expectedStmt: "UPDATE projections.idps_oidc_config SET (client_id, client_secret, issuer, authorization_endpoint, token_endpoint, scopes, display_name_mapping, username_mapping) = ($1, $2, $3, $4, $5, $6, $7, $8) WHERE (idp_id = $9) AND (instance_id = $10)",
expectedArgs: []interface{}{
"client-id",
anyArg{},
@@ -306,6 +313,7 @@ func TestIDPProjection_reduces(t *testing.T) {
domain.OIDCMappingFieldUnspecified,
domain.OIDCMappingFieldPreferredLoginName,
"idp-config-id",
"instance-id",
},
},
},
@@ -356,18 +364,20 @@ func TestIDPProjection_reduces(t *testing.T) {
executer: &testExecuter{
executions: []execution{
{
expectedStmt: "UPDATE projections.idps SET (change_date, sequence, type) = ($1, $2, $3) WHERE (id = $4)",
expectedStmt: "UPDATE projections.idps SET (change_date, sequence, type) = ($1, $2, $3) WHERE (id = $4) AND (instance_id = $5)",
expectedArgs: []interface{}{
anyArg{},
uint64(15),
domain.IDPConfigTypeJWT,
"idp-config-id",
"instance-id",
},
},
{
expectedStmt: "INSERT INTO projections.idps_jwt_config (idp_id, endpoint, issuer, keys_endpoint, header_name) VALUES ($1, $2, $3, $4, $5)",
expectedStmt: "INSERT INTO projections.idps_jwt_config (idp_id, instance_id, endpoint, issuer, keys_endpoint, header_name) VALUES ($1, $2, $3, $4, $5, $6)",
expectedArgs: []interface{}{
"idp-config-id",
"instance-id",
"https://api.zitadel.ch/jwt",
"issuer",
"https://api.zitadel.ch/keys",
@@ -402,21 +412,23 @@ func TestIDPProjection_reduces(t *testing.T) {
executer: &testExecuter{
executions: []execution{
{
expectedStmt: "UPDATE projections.idps SET (change_date, sequence) = ($1, $2) WHERE (id = $3)",
expectedStmt: "UPDATE projections.idps SET (change_date, sequence) = ($1, $2) WHERE (id = $3) AND (instance_id = $4)",
expectedArgs: []interface{}{
anyArg{},
uint64(15),
"idp-config-id",
"instance-id",
},
},
{
expectedStmt: "UPDATE projections.idps_jwt_config SET (endpoint, issuer, keys_endpoint, header_name) = ($1, $2, $3, $4) WHERE (idp_id = $5)",
expectedStmt: "UPDATE projections.idps_jwt_config SET (endpoint, issuer, keys_endpoint, header_name) = ($1, $2, $3, $4) WHERE (idp_id = $5) AND (instance_id = $6)",
expectedArgs: []interface{}{
"https://api.zitadel.ch/jwt",
"issuer",
"https://api.zitadel.ch/keys",
"hodor",
"idp-config-id",
"instance-id",
},
},
},
@@ -509,7 +521,7 @@ func TestIDPProjection_reduces(t *testing.T) {
executer: &testExecuter{
executions: []execution{
{
expectedStmt: "UPDATE projections.idps SET (name, styling_type, auto_register, change_date, sequence) = ($1, $2, $3, $4, $5) WHERE (id = $6)",
expectedStmt: "UPDATE projections.idps SET (name, styling_type, auto_register, change_date, sequence) = ($1, $2, $3, $4, $5) WHERE (id = $6) AND (instance_id = $7)",
expectedArgs: []interface{}{
"custom-zitadel-instance",
domain.IDPConfigStylingTypeGoogle,
@@ -517,6 +529,7 @@ func TestIDPProjection_reduces(t *testing.T) {
anyArg{},
uint64(15),
"idp-config-id",
"instance-id",
},
},
},
@@ -543,12 +556,13 @@ func TestIDPProjection_reduces(t *testing.T) {
executer: &testExecuter{
executions: []execution{
{
expectedStmt: "UPDATE projections.idps SET (state, change_date, sequence) = ($1, $2, $3) WHERE (id = $4)",
expectedStmt: "UPDATE projections.idps SET (state, change_date, sequence) = ($1, $2, $3) WHERE (id = $4) AND (instance_id = $5)",
expectedArgs: []interface{}{
domain.IDPConfigStateInactive,
anyArg{},
uint64(15),
"idp-config-id",
"instance-id",
},
},
},
@@ -575,12 +589,13 @@ func TestIDPProjection_reduces(t *testing.T) {
executer: &testExecuter{
executions: []execution{
{
expectedStmt: "UPDATE projections.idps SET (state, change_date, sequence) = ($1, $2, $3) WHERE (id = $4)",
expectedStmt: "UPDATE projections.idps SET (state, change_date, sequence) = ($1, $2, $3) WHERE (id = $4) AND (instance_id = $5)",
expectedArgs: []interface{}{
domain.IDPConfigStateActive,
anyArg{},
uint64(15),
"idp-config-id",
"instance-id",
},
},
},
@@ -607,9 +622,10 @@ func TestIDPProjection_reduces(t *testing.T) {
executer: &testExecuter{
executions: []execution{
{
expectedStmt: "DELETE FROM projections.idps WHERE (id = $1)",
expectedStmt: "DELETE FROM projections.idps WHERE (id = $1) AND (instance_id = $2)",
expectedArgs: []interface{}{
"idp-config-id",
"instance-id",
},
},
},
@@ -648,18 +664,20 @@ func TestIDPProjection_reduces(t *testing.T) {
executer: &testExecuter{
executions: []execution{
{
expectedStmt: "UPDATE projections.idps SET (change_date, sequence, type) = ($1, $2, $3) WHERE (id = $4)",
expectedStmt: "UPDATE projections.idps SET (change_date, sequence, type) = ($1, $2, $3) WHERE (id = $4) AND (instance_id = $5)",
expectedArgs: []interface{}{
anyArg{},
uint64(15),
domain.IDPConfigTypeOIDC,
"idp-config-id",
"instance-id",
},
},
{
expectedStmt: "INSERT INTO projections.idps_oidc_config (idp_id, client_id, client_secret, issuer, scopes, display_name_mapping, username_mapping, authorization_endpoint, token_endpoint) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)",
expectedStmt: "INSERT INTO projections.idps_oidc_config (idp_id, instance_id, client_id, client_secret, issuer, scopes, display_name_mapping, username_mapping, authorization_endpoint, token_endpoint) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)",
expectedArgs: []interface{}{
"idp-config-id",
"instance-id",
"client-id",
anyArg{},
"issuer",
@@ -706,15 +724,16 @@ func TestIDPProjection_reduces(t *testing.T) {
executer: &testExecuter{
executions: []execution{
{
expectedStmt: "UPDATE projections.idps SET (change_date, sequence) = ($1, $2) WHERE (id = $3)",
expectedStmt: "UPDATE projections.idps SET (change_date, sequence) = ($1, $2) WHERE (id = $3) AND (instance_id = $4)",
expectedArgs: []interface{}{
anyArg{},
uint64(15),
"idp-config-id",
"instance-id",
},
},
{
expectedStmt: "UPDATE projections.idps_oidc_config SET (client_id, client_secret, issuer, authorization_endpoint, token_endpoint, scopes, display_name_mapping, username_mapping) = ($1, $2, $3, $4, $5, $6, $7, $8) WHERE (idp_id = $9)",
expectedStmt: "UPDATE projections.idps_oidc_config SET (client_id, client_secret, issuer, authorization_endpoint, token_endpoint, scopes, display_name_mapping, username_mapping) = ($1, $2, $3, $4, $5, $6, $7, $8) WHERE (idp_id = $9) AND (instance_id = $10)",
expectedArgs: []interface{}{
"client-id",
anyArg{},
@@ -725,6 +744,7 @@ func TestIDPProjection_reduces(t *testing.T) {
domain.OIDCMappingFieldUnspecified,
domain.OIDCMappingFieldPreferredLoginName,
"idp-config-id",
"instance-id",
},
},
},
@@ -775,18 +795,20 @@ func TestIDPProjection_reduces(t *testing.T) {
executer: &testExecuter{
executions: []execution{
{
expectedStmt: "UPDATE projections.idps SET (change_date, sequence, type) = ($1, $2, $3) WHERE (id = $4)",
expectedStmt: "UPDATE projections.idps SET (change_date, sequence, type) = ($1, $2, $3) WHERE (id = $4) AND (instance_id = $5)",
expectedArgs: []interface{}{
anyArg{},
uint64(15),
domain.IDPConfigTypeJWT,
"idp-config-id",
"instance-id",
},
},
{
expectedStmt: "INSERT INTO projections.idps_jwt_config (idp_id, endpoint, issuer, keys_endpoint, header_name) VALUES ($1, $2, $3, $4, $5)",
expectedStmt: "INSERT INTO projections.idps_jwt_config (idp_id, instance_id, endpoint, issuer, keys_endpoint, header_name) VALUES ($1, $2, $3, $4, $5, $6)",
expectedArgs: []interface{}{
"idp-config-id",
"instance-id",
"https://api.zitadel.ch/jwt",
"issuer",
"https://api.zitadel.ch/keys",
@@ -821,21 +843,23 @@ func TestIDPProjection_reduces(t *testing.T) {
executer: &testExecuter{
executions: []execution{
{
expectedStmt: "UPDATE projections.idps SET (change_date, sequence) = ($1, $2) WHERE (id = $3)",
expectedStmt: "UPDATE projections.idps SET (change_date, sequence) = ($1, $2) WHERE (id = $3) AND (instance_id = $4)",
expectedArgs: []interface{}{
anyArg{},
uint64(15),
"idp-config-id",
"instance-id",
},
},
{
expectedStmt: "UPDATE projections.idps_jwt_config SET (endpoint, issuer, keys_endpoint, header_name) = ($1, $2, $3, $4) WHERE (idp_id = $5)",
expectedStmt: "UPDATE projections.idps_jwt_config SET (endpoint, issuer, keys_endpoint, header_name) = ($1, $2, $3, $4) WHERE (idp_id = $5) AND (instance_id = $6)",
expectedArgs: []interface{}{
"https://api.zitadel.ch/jwt",
"issuer",
"https://api.zitadel.ch/keys",
"hodor",
"idp-config-id",
"instance-id",
},
},
},