From c4eacf89b6c1193bc50949d3d6fa69f5d816998e Mon Sep 17 00:00:00 2001 From: Amy Dewaal Date: Thu, 6 Jun 2024 17:11:31 -0400 Subject: [PATCH] PLIN-5295: CLI retrieve and deploy for Structured REST datasources (#104) * account for string pointer struct fields and more complicated data structures * fix tests --- picard.go | 77 +++++++- picard_test.go | 465 ++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 475 insertions(+), 67 deletions(-) diff --git a/picard.go b/picard.go index 33d8854..02f8c60 100644 --- a/picard.go +++ b/picard.go @@ -196,8 +196,9 @@ func (p PersistenceORM) upsert(data interface{}, deleteFilters interface{}) erro if deleteFilters != nil { deletes := []dbchange.Change{} deleteResults, err := p.FilterModel(FilterRequest{ - FilterModel: deleteFilters, - Runner: p.transaction, + FilterModel: deleteFilters, + Runner: p.transaction, + Associations: getAssociations(tableMetadata), }) if err != nil { return err @@ -416,6 +417,19 @@ func (p PersistenceORM) getExistingObjectByID(tableMetadata *tags.TableMetadata, return results[0], nil } +func getAssociations(tableMetadata *tags.TableMetadata) []tags.Association { + assocs := []tags.Association{} + foreignKeys := tableMetadata.GetForeignKeys() + for _, foreignKey := range foreignKeys { + a := tags.Association{ + Name: foreignKey.RelatedFieldName, + Associations: getAssociations(foreignKey.TableMetadata), + } + assocs = append(assocs, a) + } + return assocs +} + func (p PersistenceORM) checkForExisting( data interface{}, tableMetadata *tags.TableMetadata, @@ -574,16 +588,35 @@ func getLookupsForDeploy(data interface{}, tableMetadata *tags.TableMetadata, fo }, lookupsToUse...) } - // Iterate over foreign keys to check in reverse so we can remove items without consequence + // Iterate over foreign keys to check in reverse so we can remove foreign keys that already have values for i := len(foreignKeysToCheck) - 1; i >= 0; i-- { foreignKeyToCheck := foreignKeysToCheck[i] fkValue := item.FieldByName(foreignKeyToCheck.FieldName) - if fkValue.IsValid() && fkValue.String() != "" && foreignKeyToCheck.NeedsLookup { + fkValueType := fkValue.Kind() + fkValueActual := "" + if fkValueType == reflect.String { + fkValueActual = fkValue.String() + } else if fkValueType == reflect.Pointer { + v := fkValue.Elem() + if v.IsValid() { + fkValueActual = v.String() + } + } else { + fkValueActual = fkValue.String() + } + if fkValue.IsValid() && fkValueActual != "" && foreignKeyToCheck.NeedsLookup { + // Take this foreign key out because we already have its id and do not need to look it up foreignKeysToCheck = append(foreignKeysToCheck[:i], foreignKeysToCheck[i+1:]...) lookupsToUse = append(lookupsToUse, tags.Lookup{ MatchDBColumn: foreignKeyToCheck.KeyColumn, MatchObjectProperty: foreignKeyToCheck.FieldName, }) + } else { + // We don't have the id value for this foreign key so it does need a lookup + // But we should only pass on the ones where the original data has the values to lookup + if !hasForeignKeyData(item, foreignKeyToCheck) { + foreignKeysToCheck = append(foreignKeysToCheck[:i], foreignKeysToCheck[i+1:]...) + } } } } @@ -597,6 +630,21 @@ func getLookupsForDeploy(data interface{}, tableMetadata *tags.TableMetadata, fo return lookupsToUse } +func hasForeignKeyData(item reflect.Value, foreignKey tags.ForeignKey) bool { + fk := item.FieldByName(foreignKey.RelatedFieldName) + tableMetadata := foreignKey.TableMetadata + hasData := true + + // We're checking this way because we need to make sure that all lookups have data + // AKA if even one part of the lookup doesn't have data don't use it + for _, lookup := range tableMetadata.GetLookups() { + if fk.FieldByName(lookup.MatchObjectProperty).String() == "" { + hasData = false + } + } + return hasData +} + func getLookupObjectKeys(data interface{}, lookupsToUse []tags.Lookup, foreignKey *tags.ForeignKey) []string { keys := []string{} keyMap := map[string]bool{} @@ -732,7 +780,15 @@ func (p PersistenceORM) performChildUpserts(changeObjects []dbchange.Change, tab value := childValue.Index(i) if child.ForeignKey != "" { keyField := getValueFromLookupString(value, child.ForeignKey) - keyField.SetString(foreignKeyValue.(string)) + keyFieldType := keyField.Kind() + if keyFieldType == reflect.String { + keyField.SetString(foreignKeyValue.(string)) + } else if keyFieldType == reflect.Pointer { + stringValue := foreignKeyValue.(string) + keyField.Set(reflect.ValueOf(&stringValue)) + } else { + keyField.SetString(foreignKeyValue.(string)) + } } data = reflect.Append(data, value) index = index + 1 @@ -1116,7 +1172,16 @@ func getValueFromLookupString(value reflect.Value, lookupString string) reflect. } func getObjectProperty(value reflect.Value, lookupString string) string { - return getValueFromLookupString(value, lookupString).String() + val := getValueFromLookupString(value, lookupString) + valueKind := val.Kind() + if valueKind == reflect.String { + return val.String() + } else if valueKind == reflect.Pointer { + v := val.Elem().String() + return v + } else { + return val.String() + } } func getQueryResults(rows *sql.Rows) ([]map[string]interface{}, error) { diff --git a/picard_test.go b/picard_test.go index 268b733..02d3e7c 100644 --- a/picard_test.go +++ b/picard_test.go @@ -47,10 +47,10 @@ var testChildObjectHelper = ExpectationHelper{ var testChildObjectWithLookupHelper = ExpectationHelper{ FixtureType: testdata.ChildTestObject{}, - LookupFrom: `childtest JOIN testobject as t1 on t1.id::"varchar" = parent_id::"varchar"`, - LookupSelect: "childtest.id, childtest.name as childtest_name, t1.name as t1_name, t1.nullable_lookup as t1_nullable_lookup", - LookupWhere: `COALESCE(childtest.name::"varchar",'') || '|' || COALESCE(t1.name::"varchar",'') || '|' || COALESCE(t1.nullable_lookup::"varchar",'')`, - LookupReturnCols: []string{"id", "childtest_name", "t1_name", "t1_nullable_lookup"}, + LookupFrom: `childtest`, + LookupSelect: "childtest.id, childtest.name as childtest_name", + LookupWhere: `COALESCE(childtest.name::"varchar",'')`, + LookupReturnCols: []string{"id", "childtest_name"}, LookupFields: []string{"Name", "ParentID"}, } @@ -63,6 +63,15 @@ var siblingJunctionHelper = ExpectationHelper{ LookupFields: []string{"ChildID", "SiblingID"}, } +var simpleSiblingJunctionHelper = ExpectationHelper{ + FixtureType: testdata.SiblingJunctionModel{}, + LookupFrom: `siblingjunction JOIN personmodel as t1 on t1.id::"varchar" = sibling_id::"varchar"`, + LookupSelect: "siblingjunction.id, t1.name as t1_name", + LookupWhere: `COALESCE(t1.name::"varchar",'')`, + LookupReturnCols: []string{"id", "t1_name"}, + LookupFields: []string{"ChildID", "SiblingID"}, +} + var siblingJunctionHelperWithChildKey = ExpectationHelper{ FixtureType: testdata.SiblingJunctionModel{}, LookupFrom: `siblingjunction JOIN personmodel as t1 on t1.id::"varchar" = sibling_id::"varchar"`, @@ -821,11 +830,49 @@ func TestDeployments(t *testing.T) { t0.name AS "t0.name", t0.other_info AS "t0.other_info", t0.parent_id AS "t0.parent_id", - t0.optional_parent_id AS "t0.optional_parent_id" - FROM childtest AS t0 - WHERE t0.organization_id = $1 AND ((t0.parent_id = $2)) + t0.optional_parent_id AS "t0.optional_parent_id", + t1.id AS "t1.id", + t1.organization_id AS "t1.organization_id", + t1.name AS "t1.name", + t1.nullable_lookup AS "t1.nullable_lookup", + t1.type AS "t1.type", + t1.is_active AS "t1.is_active", + t1.parent_id AS "t1.parent_id", + t1.config AS "t1.config", + t1.created_by_id AS "t1.created_by_id", + t1.updated_by_id AS "t1.updated_by_id", + t1.created_at AS "t1.created_at", + t1.updated_at AS "t1.updated_at", + t2.id AS "t2.id", + t2.organization_id AS "t2.organization_id", + t2.name AS "t2.name", + t3.id AS "t3.id", + t3.organization_id AS "t3.organization_id", + t3.name AS "t3.name", + t3.nullable_lookup AS "t3.nullable_lookup", + t3.type AS "t3.type", + t3.is_active AS "t3.is_active", + t3.parent_id AS "t3.parent_id", + t3.config AS "t3.config", + t3.created_by_id AS "t3.created_by_id", + t3.updated_by_id AS "t3.updated_by_id", + t3.created_at AS "t3.created_at", + t3.updated_at AS "t3.updated_at", + t4.id AS "t4.id", + t4.organization_id AS "t4.organization_id", + t4.name AS "t4.name" + FROM childtest AS t0 + LEFT JOIN testobject AS t1 + ON (t1.id = t0.parent_id AND t1.organization_id = $1) + LEFT JOIN parenttest AS t2 + ON (t2.id = t1.parent_id AND t2.organization_id = $2) + LEFT JOIN testobject AS t3 + ON (t3.id = t0.optional_parent_id AND t3.organization_id = $3) + LEFT JOIN parenttest AS t4 + ON (t4.id = t3.parent_id AND t4.organization_id = $4) + WHERE t0.organization_id = $5 AND ((t0.parent_id = $6)) `)). - WithArgs(sampleOrgID, parentIDs[0]). + WithArgs(sampleOrgID, sampleOrgID, sampleOrgID, sampleOrgID, sampleOrgID, parentIDs[0]). WillReturnRows( sqlmock.NewRows([]string{"t0.name", "t0.id", "t0.parent_id"}). AddRow("Orphan1", "00000000-0000-0000-0000-000000000001", parentIDs[0]). @@ -900,11 +947,49 @@ func TestDeployments(t *testing.T) { t0.name AS "t0.name", t0.other_info AS "t0.other_info", t0.parent_id AS "t0.parent_id", - t0.optional_parent_id AS "t0.optional_parent_id" - FROM childtest AS t0 - WHERE t0.organization_id = $1 AND ((t0.parent_id = $2)) + t0.optional_parent_id AS "t0.optional_parent_id", + t1.id AS "t1.id", + t1.organization_id AS "t1.organization_id", + t1.name AS "t1.name", + t1.nullable_lookup AS "t1.nullable_lookup", + t1.type AS "t1.type", + t1.is_active AS "t1.is_active", + t1.parent_id AS "t1.parent_id", + t1.config AS "t1.config", + t1.created_by_id AS "t1.created_by_id", + t1.updated_by_id AS "t1.updated_by_id", + t1.created_at AS "t1.created_at", + t1.updated_at AS "t1.updated_at", + t2.id AS "t2.id", + t2.organization_id AS "t2.organization_id", + t2.name AS "t2.name", + t3.id AS "t3.id", + t3.organization_id AS "t3.organization_id", + t3.name AS "t3.name", + t3.nullable_lookup AS "t3.nullable_lookup", + t3.type AS "t3.type", + t3.is_active AS "t3.is_active", + t3.parent_id AS "t3.parent_id", + t3.config AS "t3.config", + t3.created_by_id AS "t3.created_by_id", + t3.updated_by_id AS "t3.updated_by_id", + t3.created_at AS "t3.created_at", + t3.updated_at AS "t3.updated_at", + t4.id AS "t4.id", + t4.organization_id AS "t4.organization_id", + t4.name AS "t4.name" + FROM childtest AS t0 + LEFT JOIN testobject AS t1 + ON (t1.id = t0.parent_id AND t1.organization_id = $1) + LEFT JOIN parenttest AS t2 + ON (t2.id = t1.parent_id AND t2.organization_id = $2) + LEFT JOIN testobject AS t3 + ON (t3.id = t0.optional_parent_id AND t3.organization_id = $3) + LEFT JOIN parenttest AS t4 + ON (t4.id = t3.parent_id AND t4.organization_id = $4) + WHERE t0.organization_id = $5 AND ((t0.parent_id = $6)) `)). - WithArgs(sampleOrgID, parentIDs[0]). + WithArgs(sampleOrgID, sampleOrgID, sampleOrgID, sampleOrgID, sampleOrgID, parentIDs[0]). WillReturnRows( sqlmock.NewRows([]string{"t0.name", "t0.id"}). AddRow("Orphan1", "00000000-0000-0000-0000-000000000001"). @@ -921,11 +1006,49 @@ func TestDeployments(t *testing.T) { t0.name AS "t0.name", t0.other_info AS "t0.other_info", t0.parent_id AS "t0.parent_id", - t0.optional_parent_id AS "t0.optional_parent_id" - FROM childtest AS t0 - WHERE t0.organization_id = $1 AND ((t0.parent_id = $2)) + t0.optional_parent_id AS "t0.optional_parent_id", + t1.id AS "t1.id", + t1.organization_id AS "t1.organization_id", + t1.name AS "t1.name", + t1.nullable_lookup AS "t1.nullable_lookup", + t1.type AS "t1.type", + t1.is_active AS "t1.is_active", + t1.parent_id AS "t1.parent_id", + t1.config AS "t1.config", + t1.created_by_id AS "t1.created_by_id", + t1.updated_by_id AS "t1.updated_by_id", + t1.created_at AS "t1.created_at", + t1.updated_at AS "t1.updated_at", + t2.id AS "t2.id", + t2.organization_id AS "t2.organization_id", + t2.name AS "t2.name", + t3.id AS "t3.id", + t3.organization_id AS "t3.organization_id", + t3.name AS "t3.name", + t3.nullable_lookup AS "t3.nullable_lookup", + t3.type AS "t3.type", + t3.is_active AS "t3.is_active", + t3.parent_id AS "t3.parent_id", + t3.config AS "t3.config", + t3.created_by_id AS "t3.created_by_id", + t3.updated_by_id AS "t3.updated_by_id", + t3.created_at AS "t3.created_at", + t3.updated_at AS "t3.updated_at", + t4.id AS "t4.id", + t4.organization_id AS "t4.organization_id", + t4.name AS "t4.name" + FROM childtest AS t0 + LEFT JOIN testobject AS t1 + ON (t1.id = t0.parent_id AND t1.organization_id = $1) + LEFT JOIN parenttest AS t2 + ON (t2.id = t1.parent_id AND t2.organization_id = $2) + LEFT JOIN testobject AS t3 + ON (t3.id = t0.optional_parent_id AND t3.organization_id = $3) + LEFT JOIN parenttest AS t4 + ON (t4.id = t3.parent_id AND t4.organization_id = $4) + WHERE t0.organization_id = $5 AND ((t0.parent_id = $6)) `)). - WithArgs(sampleOrgID, parentIDs[0]). + WithArgs(sampleOrgID, sampleOrgID, sampleOrgID, sampleOrgID, sampleOrgID, parentIDs[0]). WillReturnRows( sqlmock.NewRows([]string{"t0.name", "t0.id"}). AddRow("Orphan1", "00000000-0000-0000-0000-000000000001"). @@ -1045,11 +1168,49 @@ func TestDeployments(t *testing.T) { t0.name AS "t0.name", t0.other_info AS "t0.other_info", t0.parent_id AS "t0.parent_id", - t0.optional_parent_id AS "t0.optional_parent_id" - FROM childtest AS t0 - WHERE t0.organization_id = $1 AND ((t0.parent_id = $2)) + t0.optional_parent_id AS "t0.optional_parent_id", + t1.id AS "t1.id", + t1.organization_id AS "t1.organization_id", + t1.name AS "t1.name", + t1.nullable_lookup AS "t1.nullable_lookup", + t1.type AS "t1.type", + t1.is_active AS "t1.is_active", + t1.parent_id AS "t1.parent_id", + t1.config AS "t1.config", + t1.created_by_id AS "t1.created_by_id", + t1.updated_by_id AS "t1.updated_by_id", + t1.created_at AS "t1.created_at", + t1.updated_at AS "t1.updated_at", + t2.id AS "t2.id", + t2.organization_id AS "t2.organization_id", + t2.name AS "t2.name", + t3.id AS "t3.id", + t3.organization_id AS "t3.organization_id", + t3.name AS "t3.name", + t3.nullable_lookup AS "t3.nullable_lookup", + t3.type AS "t3.type", + t3.is_active AS "t3.is_active", + t3.parent_id AS "t3.parent_id", + t3.config AS "t3.config", + t3.created_by_id AS "t3.created_by_id", + t3.updated_by_id AS "t3.updated_by_id", + t3.created_at AS "t3.created_at", + t3.updated_at AS "t3.updated_at", + t4.id AS "t4.id", + t4.organization_id AS "t4.organization_id", + t4.name AS "t4.name" + FROM childtest AS t0 + LEFT JOIN testobject AS t1 + ON (t1.id = t0.parent_id AND t1.organization_id = $1) + LEFT JOIN parenttest AS t2 + ON (t2.id = t1.parent_id AND t2.organization_id = $2) + LEFT JOIN testobject AS t3 + ON (t3.id = t0.optional_parent_id AND t3.organization_id = $3) + LEFT JOIN parenttest AS t4 + ON (t4.id = t3.parent_id AND t4.organization_id = $4) + WHERE t0.organization_id = $5 AND ((t0.parent_id = $6)) `)). - WithArgs(sampleOrgID, parentIDs[0]). + WithArgs(sampleOrgID, sampleOrgID, sampleOrgID, sampleOrgID, sampleOrgID, parentIDs[0]). WillReturnRows( sqlmock.NewRows([]string{"t0.name", "t0.id", "t0.parent_id"}). AddRow("ChildRecord", "00000000-0000-0000-0000-000000000001", parentIDs[0]). @@ -1065,11 +1226,49 @@ func TestDeployments(t *testing.T) { t0.name AS "t0.name", t0.other_info AS "t0.other_info", t0.parent_id AS "t0.parent_id", - t0.optional_parent_id AS "t0.optional_parent_id" - FROM childtest AS t0 - WHERE t0.organization_id = $1 AND ((t0.parent_id = $2)) + t0.optional_parent_id AS "t0.optional_parent_id", + t1.id AS "t1.id", + t1.organization_id AS "t1.organization_id", + t1.name AS "t1.name", + t1.nullable_lookup AS "t1.nullable_lookup", + t1.type AS "t1.type", + t1.is_active AS "t1.is_active", + t1.parent_id AS "t1.parent_id", + t1.config AS "t1.config", + t1.created_by_id AS "t1.created_by_id", + t1.updated_by_id AS "t1.updated_by_id", + t1.created_at AS "t1.created_at", + t1.updated_at AS "t1.updated_at", + t2.id AS "t2.id", + t2.organization_id AS "t2.organization_id", + t2.name AS "t2.name", + t3.id AS "t3.id", + t3.organization_id AS "t3.organization_id", + t3.name AS "t3.name", + t3.nullable_lookup AS "t3.nullable_lookup", + t3.type AS "t3.type", + t3.is_active AS "t3.is_active", + t3.parent_id AS "t3.parent_id", + t3.config AS "t3.config", + t3.created_by_id AS "t3.created_by_id", + t3.updated_by_id AS "t3.updated_by_id", + t3.created_at AS "t3.created_at", + t3.updated_at AS "t3.updated_at", + t4.id AS "t4.id", + t4.organization_id AS "t4.organization_id", + t4.name AS "t4.name" + FROM childtest AS t0 + LEFT JOIN testobject AS t1 + ON (t1.id = t0.parent_id AND t1.organization_id = $1) + LEFT JOIN parenttest AS t2 + ON (t2.id = t1.parent_id AND t2.organization_id = $2) + LEFT JOIN testobject AS t3 + ON (t3.id = t0.optional_parent_id AND t3.organization_id = $3) + LEFT JOIN parenttest AS t4 + ON (t4.id = t3.parent_id AND t4.organization_id = $4) + WHERE t0.organization_id = $5 AND ((t0.parent_id = $6)) `)). - WithArgs(sampleOrgID, parentIDs[0]). + WithArgs(sampleOrgID, sampleOrgID, sampleOrgID, sampleOrgID, sampleOrgID, parentIDs[0]). WillReturnRows( sqlmock.NewRows([]string{"t0.name", "t0.id", "t0.parent_id"}). AddRow("Orphan1", "00000000-0000-0000-0000-000000000001", parentIDs[0]). @@ -1158,13 +1357,49 @@ func TestDeployments(t *testing.T) { t0.name AS "t0.name", t0.other_info AS "t0.other_info", t0.parent_id AS "t0.parent_id", - t0.optional_parent_id AS "t0.optional_parent_id" - FROM childtest AS t0 - WHERE - t0.organization_id = $1 AND - ((t0.parent_id = $2) OR (t0.parent_id = $3)) + t0.optional_parent_id AS "t0.optional_parent_id", + t1.id AS "t1.id", + t1.organization_id AS "t1.organization_id", + t1.name AS "t1.name", + t1.nullable_lookup AS "t1.nullable_lookup", + t1.type AS "t1.type", + t1.is_active AS "t1.is_active", + t1.parent_id AS "t1.parent_id", + t1.config AS "t1.config", + t1.created_by_id AS "t1.created_by_id", + t1.updated_by_id AS "t1.updated_by_id", + t1.created_at AS "t1.created_at", + t1.updated_at AS "t1.updated_at", + t2.id AS "t2.id", + t2.organization_id AS "t2.organization_id", + t2.name AS "t2.name", + t3.id AS "t3.id", + t3.organization_id AS "t3.organization_id", + t3.name AS "t3.name", + t3.nullable_lookup AS "t3.nullable_lookup", + t3.type AS "t3.type", + t3.is_active AS "t3.is_active", + t3.parent_id AS "t3.parent_id", + t3.config AS "t3.config", + t3.created_by_id AS "t3.created_by_id", + t3.updated_by_id AS "t3.updated_by_id", + t3.created_at AS "t3.created_at", + t3.updated_at AS "t3.updated_at", + t4.id AS "t4.id", + t4.organization_id AS "t4.organization_id", + t4.name AS "t4.name" + FROM childtest AS t0 + LEFT JOIN testobject AS t1 + ON (t1.id = t0.parent_id AND t1.organization_id = $1) + LEFT JOIN parenttest AS t2 + ON (t2.id = t1.parent_id AND t2.organization_id = $2) + LEFT JOIN testobject AS t3 + ON (t3.id = t0.optional_parent_id AND t3.organization_id = $3) + LEFT JOIN parenttest AS t4 + ON (t4.id = t3.parent_id AND t4.organization_id = $4) + WHERE t0.organization_id = $5 AND ((t0.parent_id = $6) OR (t0.parent_id = $7)) `)). - WithArgs(sampleOrgID, parentIDs[0], parentIDs[1]). + WithArgs(sampleOrgID, sampleOrgID, sampleOrgID, sampleOrgID, sampleOrgID, parentIDs[0], parentIDs[1]). WillReturnRows( sqlmock.NewRows([]string{"t0.name", "t0.id", "t0.parent_id"}). AddRow("ChildRecord", "00000000-0000-0000-0000-000000000001", parentIDs[0]). @@ -1186,12 +1421,48 @@ func TestDeployments(t *testing.T) { t0.name AS "t0.name", t0.other_info AS "t0.other_info", t0.parent_id AS "t0.parent_id", - t0.optional_parent_id AS "t0.optional_parent_id" - FROM childtest AS t0 - WHERE - t0.organization_id = $1 AND ((t0.parent_id = $2)) + t0.optional_parent_id AS "t0.optional_parent_id", + t1.id AS "t1.id", + t1.organization_id AS "t1.organization_id", + t1.name AS "t1.name", + t1.nullable_lookup AS "t1.nullable_lookup", + t1.type AS "t1.type", + t1.is_active AS "t1.is_active", + t1.parent_id AS "t1.parent_id", + t1.config AS "t1.config", + t1.created_by_id AS "t1.created_by_id", + t1.updated_by_id AS "t1.updated_by_id", + t1.created_at AS "t1.created_at", + t1.updated_at AS "t1.updated_at", + t2.id AS "t2.id", + t2.organization_id AS "t2.organization_id", + t2.name AS "t2.name", + t3.id AS "t3.id", + t3.organization_id AS "t3.organization_id", + t3.name AS "t3.name", + t3.nullable_lookup AS "t3.nullable_lookup", + t3.type AS "t3.type", + t3.is_active AS "t3.is_active", + t3.parent_id AS "t3.parent_id", + t3.config AS "t3.config", + t3.created_by_id AS "t3.created_by_id", + t3.updated_by_id AS "t3.updated_by_id", + t3.created_at AS "t3.created_at", + t3.updated_at AS "t3.updated_at", + t4.id AS "t4.id", + t4.organization_id AS "t4.organization_id", + t4.name AS "t4.name" + FROM childtest AS t0 + LEFT JOIN testobject AS t1 + ON (t1.id = t0.parent_id AND t1.organization_id = $1) + LEFT JOIN parenttest AS t2 + ON (t2.id = t1.parent_id AND t2.organization_id = $2) + LEFT JOIN testobject AS t3 + ON (t3.id = t0.optional_parent_id AND t3.organization_id = $3) + LEFT JOIN parenttest AS t4 + ON (t4.id = t3.parent_id AND t4.organization_id = $4) WHERE t0.organization_id = $5 AND ((t0.parent_id = $6)) `)). - WithArgs(sampleOrgID, parentIDs[0]). + WithArgs(sampleOrgID, sampleOrgID, sampleOrgID, sampleOrgID, sampleOrgID, parentIDs[0]). WillReturnRows( sqlmock.NewRows([]string{"t0.name", "t0.id", "t0.parent_id"}). AddRow("Orphan1", "00000000-0000-0000-0000-000000000001", parentIDs[0]). @@ -1338,18 +1609,55 @@ func TestDeployments(t *testing.T) { // Expect the lookup to find orphans to delete for the first child field ExpectQuery(mock, testdata.FmtSQLRegex(` - SELECT - t0.id AS "t0.id", - t0.organization_id AS "t0.organization_id", - t0.name AS "t0.name", - t0.other_info AS "t0.other_info", - t0.parent_id AS "t0.parent_id", - t0.optional_parent_id AS "t0.optional_parent_id" - FROM childtest AS t0 - WHERE - t0.organization_id = $1 AND ((t0.parent_id = $2) OR (t0.parent_id = $3)) + SELECT + t0.id AS "t0.id", + t0.organization_id AS "t0.organization_id", + t0.name AS "t0.name", + t0.other_info AS "t0.other_info", + t0.parent_id AS "t0.parent_id", + t0.optional_parent_id AS "t0.optional_parent_id", + t1.id AS "t1.id", + t1.organization_id AS "t1.organization_id", + t1.name AS "t1.name", + t1.nullable_lookup AS "t1.nullable_lookup", + t1.type AS "t1.type", + t1.is_active AS "t1.is_active", + t1.parent_id AS "t1.parent_id", + t1.config AS "t1.config", + t1.created_by_id AS "t1.created_by_id", + t1.updated_by_id AS "t1.updated_by_id", + t1.created_at AS "t1.created_at", + t1.updated_at AS "t1.updated_at", + t2.id AS "t2.id", + t2.organization_id AS "t2.organization_id", + t2.name AS "t2.name", + t3.id AS "t3.id", + t3.organization_id AS "t3.organization_id", + t3.name AS "t3.name", + t3.nullable_lookup AS "t3.nullable_lookup", + t3.type AS "t3.type", + t3.is_active AS "t3.is_active", + t3.parent_id AS "t3.parent_id", + t3.config AS "t3.config", + t3.created_by_id AS "t3.created_by_id", + t3.updated_by_id AS "t3.updated_by_id", + t3.created_at AS "t3.created_at", + t3.updated_at AS "t3.updated_at", + t4.id AS "t4.id", + t4.organization_id AS "t4.organization_id", + t4.name AS "t4.name" + FROM childtest AS t0 + LEFT JOIN testobject AS t1 + ON (t1.id = t0.parent_id AND t1.organization_id = $1) + LEFT JOIN parenttest AS t2 + ON (t2.id = t1.parent_id AND t2.organization_id = $2) + LEFT JOIN testobject AS t3 + ON (t3.id = t0.optional_parent_id AND t3.organization_id = $3) + LEFT JOIN parenttest AS t4 + ON (t4.id = t3.parent_id AND t4.organization_id = $4) + WHERE t0.organization_id = $5 AND ((t0.parent_id = $6) OR (t0.parent_id = $7)) `)). - WithArgs(sampleOrgID, parentIDs[0], parentIDs[1]). + WithArgs(sampleOrgID, sampleOrgID, sampleOrgID, sampleOrgID, sampleOrgID, parentIDs[0], parentIDs[1]). WillReturnRows( sqlmock.NewRows([]string{"t0.name", "t0.id", "t0.parent_id"}). AddRow("ChildRecord", "00000000-0000-0000-0000-000000000001", parentIDs[0]). @@ -1371,12 +1679,49 @@ func TestDeployments(t *testing.T) { t0.name AS "t0.name", t0.other_info AS "t0.other_info", t0.parent_id AS "t0.parent_id", - t0.optional_parent_id AS "t0.optional_parent_id" - FROM childtest AS t0 - WHERE - t0.organization_id = $1 AND ((t0.parent_id = $2)) + t0.optional_parent_id AS "t0.optional_parent_id", + t1.id AS "t1.id", + t1.organization_id AS "t1.organization_id", + t1.name AS "t1.name", + t1.nullable_lookup AS "t1.nullable_lookup", + t1.type AS "t1.type", + t1.is_active AS "t1.is_active", + t1.parent_id AS "t1.parent_id", + t1.config AS "t1.config", + t1.created_by_id AS "t1.created_by_id", + t1.updated_by_id AS "t1.updated_by_id", + t1.created_at AS "t1.created_at", + t1.updated_at AS "t1.updated_at", + t2.id AS "t2.id", + t2.organization_id AS "t2.organization_id", + t2.name AS "t2.name", + t3.id AS "t3.id", + t3.organization_id AS "t3.organization_id", + t3.name AS "t3.name", + t3.nullable_lookup AS "t3.nullable_lookup", + t3.type AS "t3.type", + t3.is_active AS "t3.is_active", + t3.parent_id AS "t3.parent_id", + t3.config AS "t3.config", + t3.created_by_id AS "t3.created_by_id", + t3.updated_by_id AS "t3.updated_by_id", + t3.created_at AS "t3.created_at", + t3.updated_at AS "t3.updated_at", + t4.id AS "t4.id", + t4.organization_id AS "t4.organization_id", + t4.name AS "t4.name" + FROM childtest AS t0 + LEFT JOIN testobject AS t1 + ON (t1.id = t0.parent_id AND t1.organization_id = $1) + LEFT JOIN parenttest AS t2 + ON (t2.id = t1.parent_id AND t2.organization_id = $2) + LEFT JOIN testobject AS t3 + ON (t3.id = t0.optional_parent_id AND t3.organization_id = $3) + LEFT JOIN parenttest AS t4 + ON (t4.id = t3.parent_id AND t4.organization_id = $4) + WHERE t0.organization_id = $5 AND ((t0.parent_id = $6)) `)). - WithArgs(sampleOrgID, parentIDs[0]). + WithArgs(sampleOrgID, sampleOrgID, sampleOrgID, sampleOrgID, sampleOrgID, parentIDs[0]). WillReturnRows( sqlmock.NewRows([]string{"t0.name", "t0.id", "t0.parent_id"}). AddRow("Orphan1", "00000000-0000-0000-0000-000000000001", parentIDs[0]). @@ -1400,12 +1745,10 @@ func TestDeployments(t *testing.T) { []driver.Value{ childUUID, "ChildItem", - "Simple", - "", }, } - ExpectLookup(mock, testChildObjectWithLookupHelper, []string{"ChildItem|Simple|"}, returnData) + ExpectLookup(mock, testChildObjectWithLookupHelper, []string{"ChildItem"}, returnData) // Expect the foreign key lookup next ExpectLookup(mock, testObjectHelper, []string{"Simple|"}, [][]driver.Value{ @@ -1438,7 +1781,7 @@ func TestDeployments(t *testing.T) { func(mock *sqlmock.Sqlmock, fixturesAbstract interface{}) { parentUUID := uuid.NewV4().String() fixtures := fixturesAbstract.([]testdata.ChildTestObject) - lookupKeys := []string{"ChildItem|Simple|"} + lookupKeys := []string{"ChildItem"} returnData := [][]driver.Value{} childObjects := []testdata.ChildTestObject{} @@ -1523,7 +1866,7 @@ func TestDeployments(t *testing.T) { parentUUID := uuid.NewV4().String() optionalParentUUID := uuid.NewV4().String() fixtures := fixturesAbstract.([]testdata.ChildTestObject) - lookupKeys := []string{"ChildItem|Simple|"} + lookupKeys := []string{"ChildItem"} returnData := [][]driver.Value{} childObjects := []testdata.ChildTestObject{} @@ -1573,7 +1916,7 @@ func TestDeployments(t *testing.T) { testdata.ChildTestObject{}, 100, func(mock *sqlmock.Sqlmock, fixturesAbstract interface{}) { - lookupKeys := []string{"ChildItem|Simple|"} + lookupKeys := []string{"ChildItem"} returnData := [][]driver.Value{} ExpectLookup(mock, testChildObjectWithLookupHelper, lookupKeys, returnData) @@ -1590,7 +1933,7 @@ func TestDeployments(t *testing.T) { testdata.ChildTestObject{}, 100, func(mock *sqlmock.Sqlmock, fixturesAbstract interface{}) { - lookupKeys := []string{"ChildItem|Simple|", "ChildItem2|Simple2|"} + lookupKeys := []string{"ChildItem", "ChildItem2"} returnData := [][]driver.Value{} ExpectLookup(mock, testChildObjectWithLookupHelper, lookupKeys, returnData) @@ -1607,7 +1950,7 @@ func TestDeployments(t *testing.T) { testdata.ChildTestObject{}, 100, func(mock *sqlmock.Sqlmock, fixturesAbstract interface{}) { - lookupKeys := []string{"ChildItem|Simple|"} + lookupKeys := []string{"ChildItem"} returnData := [][]driver.Value{} ExpectLookup(mock, testChildObjectWithLookupHelper, lookupKeys, returnData) @@ -1660,7 +2003,7 @@ func TestDeployments(t *testing.T) { testdata.SiblingJunctionModel{}, 100, func(mock *sqlmock.Sqlmock, fixtures interface{}) { - junctionHelper := siblingJunctionHelper + junctionHelper := simpleSiblingJunctionHelper personHelper := personModelHelper personWithPKHelper := personModelWithIDHelper junctionReturnData := GetReturnDataForLookup(junctionHelper, nil) @@ -1677,7 +2020,7 @@ func TestDeployments(t *testing.T) { Name: "Fred", }, }) - junctionLookupKeys := []string{"|Fred"} + junctionLookupKeys := []string{"Fred"} ExpectLookup(mock, junctionHelper, junctionLookupKeys, junctionReturnData)