-
Notifications
You must be signed in to change notification settings - Fork 20
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
base: master
Are you sure you want to change the base?
Conversation
…inked to Instance
# Conflicts: # src/main/resources/templates/db_scripts/schema.json
|
||
for (var subject : newInstance.getSubjects()) { | ||
if (subject.getSourceId() == null) { | ||
instanceRepository.unlinkInstanceFromSubjectSource(instanceId); |
There was a problem hiding this comment.
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.
Co-authored-by: Pavlo Smahin <[email protected]>
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); |
There was a problem hiding this comment.
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:
- Get all source/type ids from new instance subjects.
- Get all source/type ids from old instance subjects.
- Identify distinction: what should be created, what should be deleted.
- Do queries for creating and deleting these relations in batch and not one by one.
There was a problem hiding this 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?
Quality Gate passedIssues Measures |
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
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)