Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MODINVSTOR-1284 User can delete local Subject types/sources when it linked to Instance #1119

Open
wants to merge 28 commits into
base: master
Choose a base branch
from

Conversation

JavokhirAbdullayev
Copy link
Contributor

@JavokhirAbdullayev JavokhirAbdullayev commented Nov 27, 2024

Purpose

MODINVSTOR-1284 User can delete local Subject types/sources when it linked to Instance

Approach

add validation to check if subject source/type linked with instance

Changes Checklist

  • API Changes: Document any API paths, methods, request or response bodies changed, added, or removed.
  • Database Schema Changes: Indicate any database schema changes and their impact. Confirm that migration scripts were created.
  • Interface Version Changes: Indicate any changes to interface versions.
  • Interface Dependencies: Document added or removed dependencies.
  • Permissions: Document any changes to permissions.
  • Logging: Confirm that logging is appropriately handled.
  • Unit Testing: Confirm that changed classes were covered by unit tests.
  • Integration Testing: Confirm that changed logic was covered by integration tests.
  • Manual Testing: Confirm that changes were tested on local or dev environment.
  • NEWS: Confirm that the NEWS file is updated with relevant information about the changes made in this pull request.

Related Issues

List any Jira issues related to this pull request.

Learning and Resources (if applicable)

Discuss any research conducted during the development of this pull request. Include links to relevant blog posts, patterns, libraries, or addons that were used to solve the problem.

Screenshots (if applicable)

image

# Conflicts:
#	src/main/resources/templates/db_scripts/schema.json
NEWS.md Outdated Show resolved Hide resolved
src/main/java/org/folio/persist/InstanceRepository.java Outdated Show resolved Hide resolved
src/main/java/org/folio/persist/InstanceRepository.java Outdated Show resolved Hide resolved

for (var subject : newInstance.getSubjects()) {
if (subject.getSourceId() == null) {
instanceRepository.unlinkInstanceFromSubjectSource(instanceId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you unlink all subjects from the instance, but should unlink only one based on your logic.

Comment on lines 47 to 98
public void linkInstanceWithSubjectSourceAndType(Instance instance) {
instance.getSubjects().forEach(subject -> {
var sql = new StringBuilder();
if (subject.getSourceId() != null) {
sql.append(" INSERT INTO ");
sql.append(postgresClientFuturized.getFullTableName(INSTANCE_SUBJECT_SOURCE_TABLE));
sql.append(" (instance_id, source_id) ");
sql.append(" VALUES ");
sql.append(String.format("( '%s' , '%s' ) ON CONFLICT DO NOTHING; ", instance.getId(), subject.getSourceId()));
}
if (subject.getTypeId() != null) {
sql.append(" INSERT INTO ");
sql.append(postgresClientFuturized.getFullTableName(INSTANCE_SUBJECT_TYPE_TABLE));
sql.append(" (instance_id, type_id) ");
sql.append(" VALUES ");
sql.append(String.format("( '%s' , '%s' ) ON CONFLICT DO NOTHING; ", instance.getId(), subject.getTypeId()));
}
var sqlString = sql.toString();
if (!isBlank(sqlString)) {
postgresClient.execute(sql.toString());
}
});
}

public void unlinkInstanceFromSubjectSource(String instanceId) {
var sql = unlinkInstanceFromSubjectSql(INSTANCE_SUBJECT_SOURCE_TABLE, instanceId);
postgresClient.execute(sql);
}

public void unlinkInstanceFromSubjectType(String instanceId) {
var sql = unlinkInstanceFromSubjectSql(INSTANCE_SUBJECT_TYPE_TABLE, instanceId);
postgresClient.execute(sql);
}

private String unlinkInstanceFromSubjectSql(String table, String id) {
return String.format("DELETE FROM %s WHERE instance_id = '%s'; ", postgresClientFuturized.getFullTableName(table), id);
}

public void unlinkSubjectSource(String instanceId, String sourceId) {
var sql = "DELETE FROM " +
postgresClientFuturized.getFullTableName(INSTANCE_SUBJECT_SOURCE_TABLE) +
String.format(" WHERE instance_id = '%s' AND source_id = '%s';",
instanceId, sourceId);
postgresClient.execute(sql);
}

public void unlinkSubjectType(String instanceId, String typeId) {
var sql = "DELETE FROM " +
postgresClientFuturized.getFullTableName(INSTANCE_SUBJECT_TYPE_TABLE) +
String.format(" WHERE instance_id = '%s' AND type_id = '%s';", instanceId,
typeId);
postgresClient.execute(sql);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basicaly the logic should be like following:

  1. Get all source/type ids from new instance subjects.
  2. Get all source/type ids from old instance subjects.
  3. Identify distinction: what should be created, what should be deleted.
  4. Do queries for creating and deleting these relations in batch and not one by one.

Copy link
Contributor

@psmagin psmagin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you ensure consistency? Specifically, what happens if an instance is saved successfully but the related data fails to save? Do you have any rollback mechanisms for the instance?
Are you considering the use of transactions?

Copy link

sonarcloud bot commented Dec 14, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants