-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'change-modification-order-when-deleted' of https://gith…
…ub.com/gridsuite/network-modification-server into change-modification-order-when-deleted
- Loading branch information
Showing
9 changed files
with
124 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
src/main/java/org/gridsuite/modification/server/migration/ModificationOrderMigration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package org.gridsuite.modification.server.migration; | ||
|
||
import liquibase.change.custom.CustomTaskChange; | ||
import liquibase.database.Database; | ||
import liquibase.database.jvm.JdbcConnection; | ||
import liquibase.exception.CustomChangeException; | ||
import liquibase.exception.DatabaseException; | ||
import liquibase.exception.SetupException; | ||
import liquibase.exception.ValidationErrors; | ||
import liquibase.resource.ResourceAccessor; | ||
import org.apache.commons.lang3.tuple.Pair; | ||
|
||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.UUID; | ||
import java.util.stream.IntStream; | ||
|
||
public class ModificationOrderMigration implements CustomTaskChange { | ||
|
||
@Override | ||
public void execute(Database database) throws CustomChangeException { | ||
JdbcConnection connection = (JdbcConnection) database.getConnection(); | ||
try { | ||
ResultSet groupIds = connection.createStatement().executeQuery("select distinct group_id from modification"); | ||
while (groupIds.next()) { | ||
UUID groupId = UUID.fromString(groupIds.getString(1)); | ||
reorderNetworkModifications(groupId, true, connection); | ||
reorderNetworkModifications(groupId, false, connection); | ||
} | ||
} catch (SQLException | DatabaseException e) { | ||
throw new CustomChangeException(e); | ||
} | ||
} | ||
|
||
private void reorderNetworkModifications(UUID groupId, boolean stashed, JdbcConnection connection) throws SQLException, DatabaseException { | ||
List<UUID> entities = findAllByGroupId(groupId, stashed, connection); | ||
List<Pair<UUID, Integer>> entitiesToUpdate = new ArrayList<>(); | ||
if (!entities.isEmpty()) { | ||
if (Boolean.TRUE.equals(stashed)) { | ||
IntStream.range(1, entities.size() + 1) | ||
.forEach(i -> entitiesToUpdate.add(Pair.of(entities.get(i - 1), -i))); | ||
} else { | ||
IntStream.range(0, entities.size()) | ||
.forEach(i -> entitiesToUpdate.add(Pair.of(entities.get(i), i))); | ||
} | ||
} | ||
saveAll(entitiesToUpdate, connection); | ||
} | ||
|
||
private List<UUID> findAllByGroupId(UUID groupId, boolean stashed, JdbcConnection connection) throws DatabaseException, SQLException { | ||
ResultSet resultSet = connection.createStatement().executeQuery(String.format("SELECT id FROM modification m WHERE m.group_id = '%s' AND m.stashed = %b order by modifications_order", groupId, stashed)); | ||
List<UUID> entities = new ArrayList<>(); | ||
while (resultSet.next()) { | ||
entities.add(UUID.fromString(resultSet.getString(1))); | ||
} | ||
return entities; | ||
} | ||
|
||
private void saveAll(List<Pair<UUID, Integer>> entities, JdbcConnection connection) throws DatabaseException, SQLException { | ||
Statement statement = connection.createStatement(); | ||
entities.forEach(pair -> { | ||
try { | ||
statement.addBatch(String.format("UPDATE modification SET modifications_order=%d WHERE id = '%s'", pair.getValue(), pair.getKey())); | ||
} catch (SQLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
}); | ||
statement.executeBatch(); | ||
} | ||
|
||
@Override | ||
public String getConfirmationMessage() { | ||
return ""; | ||
} | ||
|
||
@Override | ||
public void setUp() throws SetupException { | ||
|
||
} | ||
|
||
@Override | ||
public void setFileOpener(ResourceAccessor resourceAccessor) { | ||
|
||
} | ||
|
||
@Override | ||
public ValidationErrors validate(Database database) { | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/main/resources/db/changelog/changesets/changelog_20241015T130742Z.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.1" encoding="UTF-8" standalone="no"?> | ||
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd"> | ||
<changeSet author="etienne lesot" id="1726146482277-3"> | ||
<customChange class="org.gridsuite.modification.server.migration.ModificationOrderMigration"/> | ||
</changeSet> | ||
</databaseChangeLog> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters