Skip to content

Commit

Permalink
Transformation of data migration models with RemovePK to scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
suarezgpablo committed Nov 20, 2024
1 parent c573542 commit c8791df
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 15 deletions.
2 changes: 2 additions & 0 deletions modevo-script/dat/bmk/testCustomV9RemovePK-script.cql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FOR $1,$2,$3 = SELECT idAuthor, idBook, title FROM table1BeforeChange
INSERT INTO table1(idAuthor, idBook, title) VALUES ($1, $2, $3);
20 changes: 20 additions & 0 deletions modevo-script/dat/inp/creationSchema.cql
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,26 @@ CREATE TABLE "testCustomV8JoinColumn".table1 (
PRIMARY KEY (idauthor, idbook)
) WITH CLUSTERING ORDER BY ( idbook ASC );

-- Export of keyspace testCustomV9RemovePK
CREATE KEYSPACE "testCustomV9RemovePK"
WITH durable_writes = true
AND replication = {
'class' : 'SimpleStrategy',
'replication_factor' : 1
};

CREATE TABLE "testCustomV9RemovePK".table1beforechange (
idauthor text,
idbook text,
title text,
PRIMARY KEY (idauthor, idbook, title)
) WITH CLUSTERING ORDER BY ( idbook ASC );
CREATE TABLE "testCustomV9RemovePK".table1 (
idauthor text,
idbook text,
title text,
PRIMARY KEY (idauthor, idbook)
) WITH CLUSTERING ORDER BY ( idbook ASC );

-- Export of keyspace testMindsV10NewTableMigrationFromOneTable
CREATE KEYSPACE "testMindsV10NewTableMigrationFromOneTable"
Expand Down
5 changes: 5 additions & 0 deletions modevo-script/dat/inp/testCustomV9RemovePK-initDB.cql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
TRUNCATE "testCustomV9RemovePK".table1beforechange;
TRUNCATE "testCustomV9RemovePK".table1;
Insert into "testCustomV9RemovePK".table1beforechange (idauthor, idbook, title) values ('1', '2', 'lotr');
Insert into "testCustomV9RemovePK".table1beforechange (idauthor, idbook, title) values ('3', '4', 'asoiaf');
Insert into "testCustomV9RemovePK".table1beforechange (idauthor, idbook, title) values ('5', '6', 'frieren');
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ else if (mt.migrationSplitColumn(se)) {
else if (mt.migrationJoinColumn(se)) {
scripts.add(migrationJoinColumn (schema, se, mt));
}
else if (mt.migrationFromRemovePK(se, mt)) {
scripts.add(migrationNewTable (schema, se, mt, false));
}
else {
return new ArrayList<>(); //Scenarios not implemented
}
Expand Down Expand Up @@ -198,7 +201,8 @@ private Script migrationFromSeveralTables(Schema schema, SchemaEvolution se, Mig
return script;
}
/**
* Creates the script needed for the migration of a new table.
* Creates the script needed for the migration of a new table. This includes the schema modification RemovePK
* as it creates a new table based on the original one but without the removed PK
*/
private Script migrationNewTableAll(Schema schema, SchemaEvolution se, MigrationTable mt) {
log.info("New Table Script from one source table. Target table: %s", mt.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ public void testCustomV7SplitTable () {
public void testCustomV8JoinColumn () {
testScript(name.getMethodName(), connection);
}
@Test
public void testCustomV9RemovePK () {
testScript(name.getMethodName(), connection);
}
public static void executeCQLFile(String path){
try{
// Open the file that is the first
Expand Down
12 changes: 1 addition & 11 deletions modevo-transform/dat/inp/testCustomV9RemovePK-schema.xmi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<xmi:XMI xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="Schema" xsi:schemaLocation="Schema ../../src/main/java/giis/modevo/transformations/atl/Metamodels/Schema.ecore">
<Table xmi:id="t1"
name="table1">
name="table1BeforeChange">
<Column xmi:id="c1"
name="idAuthor"
tableColumn="t1"
Expand All @@ -22,15 +22,5 @@
key="true"
nameEntity="Book"/>
</Table>
<Table xmi:id="t2"
name="table2">
<Column xmi:id="c4"
name="idAuthor"
tableColumn="t1"
nameAttribute="id"
key="true"
nameEntity="Author"
pk="true"/>
</Table>

</xmi:XMI>
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ public Map<String, List<MigrationColumn>> classifyMCsBySourceTable(List<Migratio
public boolean migrationFromRemovePK(SchemaEvolution se, MigrationTable mt) {
for (SchemaChange sc : se.getChanges()) {
if (sc instanceof RemovePK rpk) {
rpk.getNamePreviousTable();
for (MigrationColumn mc : mt.getMigrationColumns()) {
if (mc.getColFrom().getTable().equalsIgnoreCase(rpk.getNamePreviousTable())) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ rule removePK {
j: SchemaEvolution!RemovePK
using{
tableTarget: Schema!Table = Schema!Table.allInstancesFrom('LM') --Main table to get the data is the previous version
->any (t | t.name = j.table);
->any (t | t.name = j.previous);
}
to
d: DataMigration!MigrationTable(
name<- tableTarget.name,
name<- j.table,
migcol <- tableTarget.Column -> collect (c | thisModule.columnMigRemovePK(c, tableTarget, j.previous))
)
}
Expand Down

0 comments on commit c8791df

Please sign in to comment.