Skip to content

Commit

Permalink
[MODFQMMGR-618] Organizations migration id/name/code fixes (#574)
Browse files Browse the repository at this point in the history
* [MODFQMMGR-618] Restore ID field for querying

* Update migration strategy to handle name, too

* coverage
  • Loading branch information
ncovercash authored Jan 6, 2025
1 parent 75825af commit e644f8c
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ public interface OrganizationsClient {

public record OrganizationsResponse(List<Organization> organizations) {}

public record Organization(String id, String code) {}
public record Organization(String id, String code, String name) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.folio.fqm.client.PatronGroupsClient;
import org.folio.fqm.migration.strategies.V0POCMigration;
import org.folio.fqm.migration.strategies.V10OrganizationStatusValueChange;
import org.folio.fqm.migration.strategies.V11OrganizationCodeOperatorChange;
import org.folio.fqm.migration.strategies.V11OrganizationNameCodeOperatorChange;
import org.folio.fqm.migration.strategies.V12PurchaseOrderIdFieldRemoval;
import org.folio.fqm.migration.strategies.V1ModeOfIssuanceConsolidation;
import org.folio.fqm.migration.strategies.V2ResourceTypeConsolidation;
Expand Down Expand Up @@ -48,7 +48,7 @@ public MigrationStrategyRepository(
new V8LocationValueChange(locationsClient),
new V9LocLibraryValueChange(locationUnitsClient),
new V10OrganizationStatusValueChange(),
new V11OrganizationCodeOperatorChange(organizationsClient),
new V11OrganizationNameCodeOperatorChange(organizationsClient),
new V12PurchaseOrderIdFieldRemoval()
// adding a strategy? be sure to update the `CURRENT_VERSION` in MigrationConfiguration!
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
import org.folio.fqm.migration.warnings.Warning;

/**
* Version 11 -> 12, handles a change in operators for the organization code field.
* Version 11 -> 12, handles a change in operators for the organization code and name fields.
*
* Previously, organization codes were considered a plain text field, with $eq/$ne and $regex
* Previously, organization codes/names were considered a plain text field, with $eq/$ne and $regex
* (backing "contains" and "starts with" functionality). In Ramsons, this was changed to have a
* value source of the codes themselves, using a dropdown in the UI. The values used in the query
* are now the UUIDs, too, rather than just the plain text.
* value source of the codes/names themselves, using a dropdown in the UI. The values used in the
* query are now the UUIDs, too, rather than just the plain text.
*
* Therefore, we need to (for $eq/$ne) update queries to use the corresponding UUIDs, and will
* be removing $regex queries (the alternative was to pull all that matched these, but a query
Expand All @@ -35,20 +35,20 @@
*/
@Log4j2
@RequiredArgsConstructor
public class V11OrganizationCodeOperatorChange implements MigrationStrategy {
public class V11OrganizationNameCodeOperatorChange implements MigrationStrategy {

public static final String SOURCE_VERSION = "11";
public static final String TARGET_VERSION = "12";

private static final UUID ORGANIZATIONS_ENTITY_TYPE_ID = UUID.fromString("b5ffa2e9-8080-471a-8003-a8c5a1274503");
private static final String FIELD_NAME = "code";
private static final List<String> FIELD_NAMES = List.of("code", "name");

private final ObjectMapper objectMapper = new ObjectMapper();
private final OrganizationsClient organizationsClient;

@Override
public String getLabel() {
return "V11 -> V12 Organizations code operator/value transformation (MODFQMMGR-606)";
return "V11 -> V12 Organizations name/code operator/value transformation (MODFQMMGR-606)";
}

@Override
Expand All @@ -68,7 +68,7 @@ public MigratableQueryInformation apply(FqlService fqlService, MigratableQueryIn
query.fqlQuery(),
originalVersion -> TARGET_VERSION,
(result, key, value) -> {
if (!ORGANIZATIONS_ENTITY_TYPE_ID.equals(query.entityTypeId()) || !FIELD_NAME.equals(key)) {
if (!ORGANIZATIONS_ENTITY_TYPE_ID.equals(query.entityTypeId()) || !FIELD_NAMES.contains(key)) {
result.set(key, value); // no-op
return;
}
Expand All @@ -91,15 +91,21 @@ public MigratableQueryInformation apply(FqlService fqlService, MigratableQueryIn
records
.get()
.stream()
.filter(org -> org.code().equalsIgnoreCase(entry.getValue().textValue()))
.filter(org ->
// our earlier condition ensures that the key is either "code" or "name",
// so no need to check both here.
"code".equals(key)
? org.code().equalsIgnoreCase(entry.getValue().textValue())
: org.name().equalsIgnoreCase(entry.getValue().textValue())
)
.findAny()
.ifPresentOrElse(
org -> transformed.set(entry.getKey(), TextNode.valueOf(org.id())),
() ->
warnings.add(
ValueBreakingWarning
.builder()
.field(FIELD_NAME)
.field(key)
.value(entry.getValue().asText())
.fql(
objectMapper.createObjectNode().set(entry.getKey(), entry.getValue()).toPrettyString()
Expand All @@ -111,17 +117,18 @@ public MigratableQueryInformation apply(FqlService fqlService, MigratableQueryIn
case "$regex" -> warnings.add(
OperatorBreakingWarning
.builder()
.field(FIELD_NAME)
.field(key)
.operator(entry.getKey())
.fql(objectMapper.createObjectNode().set(entry.getKey(), entry.getValue()).toPrettyString())
.build()
);
case "$empty" -> transformed.set(entry.getKey(), entry.getValue());
default -> {
log.warn(
"Unknown operator {}: {} found for organizations' code field, not migrating...",
"Unknown operator {}: {} found for organizations' {} field, not migrating...",
entry.getKey(),
entry.getValue()
entry.getValue(),
key
);
transformed.set(entry.getKey(), entry.getValue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,26 @@
dataType: {
dataType: 'rangedUUIDType',
},
isIdColumn: true,
isIdColumn: false,
queryable: true,
visibleByDefault: false,
essential: true,
valueGetter: ':sourceAlias.id',
},
// used as id columns for name/code
{
name: '_id',
sourceAlias: 'org',
dataType: {
dataType: 'rangedUUIDType',
},
isIdColumn: true,
queryable: false,
hidden: true,
visibleByDefault: false,
essential: true,
valueGetter: ':sourceAlias.id',
},
{
name: 'created_at',
sourceAlias: 'org',
Expand Down Expand Up @@ -89,7 +103,7 @@
valueGetter: ":sourceAlias.jsonb->>'name'",
filterValueGetter: "lower(${tenant_id}_mod_organizations_storage.f_unaccent(:sourceAlias.jsonb ->> 'name'::text))",
valueFunction: 'lower(${tenant_id}_mod_organizations_storage.f_unaccent(:value))',
idColumnName: 'id',
idColumnName: '_id',
source: {
entityTypeId: 'b5ffa2e9-8080-471a-8003-a8c5a1274503',
columnName: 'name',
Expand All @@ -113,7 +127,7 @@
valueGetter: ":sourceAlias.jsonb->>'code'",
filterValueGetter: "lower(${tenant_id}_mod_organizations_storage.f_unaccent(:sourceAlias.jsonb ->> 'code'::text))",
valueFunction: 'lower(${tenant_id}_mod_organizations_storage.f_unaccent(:value))',
idColumnName: 'id',
idColumnName: '_id',
source: {
entityTypeId: 'b5ffa2e9-8080-471a-8003-a8c5a1274503',
columnName: 'code',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ public void setup() {
lenient()
.when(organizationsClient.getOrganizations())
.thenReturn(
new OrganizationsResponse(List.of(new Organization("id1", "code1"), new Organization("id2", "code2")))
new OrganizationsResponse(
List.of(new Organization("id1", "code1", "name1"), new Organization("id2", "code2", "name2"))
)
);
}

@Override
public MigrationStrategy getStrategy() {
return new V11OrganizationCodeOperatorChange(organizationsClient);
return new V11OrganizationNameCodeOperatorChange(organizationsClient);
}

@Override
Expand All @@ -61,16 +63,18 @@ public List<Arguments> getExpectedTransformations() {
.builder()
.entityTypeId(UUID.fromString("b5ffa2e9-8080-471a-8003-a8c5a1274503"))
.fqlQuery("""
{ "code": { "$eq": "code1" } }
{ "code": { "$eq": "code1" }, "name": { "$eq": "name1" } }
""")
.fields(List.of())
.build(),
MigratableQueryInformation
.builder()
.entityTypeId(UUID.fromString("b5ffa2e9-8080-471a-8003-a8c5a1274503"))
.fqlQuery("""
{ "_version": "12", "code": { "$eq": "id1" } }
""")
.fqlQuery(
"""
{ "_version": "12", "code": { "$eq": "id1" }, "name": { "$eq": "id1" } }
"""
)
.fields(List.of())
.build()
),
Expand Down
1 change: 1 addition & 0 deletions translations/mod-fqm-manager/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,7 @@
"entityType.simple_mode_of_issuance.id": "UUID",
"entityType.simple_mode_of_issuance.name": "Name",
"entityType.simple_organization": "Organizations",
"entityType.simple_organization._id": "UUID",
"entityType.simple_organization.access_provider": "Access provider",
"entityType.simple_organization.accounting_code": "Accounting code",
"entityType.simple_organization.accounts": "Accounts",
Expand Down

0 comments on commit e644f8c

Please sign in to comment.