-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DB model extended for visited countries. DB model adapted for new key…
… format. Insertion and retrieval for v1/v2 non international working with visited countries.
- Loading branch information
1 parent
0f0c6fa
commit c5ee830
Showing
18 changed files
with
399 additions
and
43 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
...backend-sdk-data/src/main/java/db/migration/hsqldb/V2_0_1__SetVisitedForExistingKeys.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,61 @@ | ||
/* | ||
* Copyright (c) 2020 Ubique Innovation AG <https://www.ubique.ch> | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
* | ||
* SPDX-License-Identifier: MPL-2.0 | ||
*/ | ||
|
||
package db.migration.hsqldb; | ||
|
||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.Statement; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.flywaydb.core.api.migration.BaseJavaMigration; | ||
import org.flywaydb.core.api.migration.Context; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class V2_0_1__SetVisitedForExistingKeys extends BaseJavaMigration { | ||
|
||
private static final Logger logger = | ||
LoggerFactory.getLogger(V2_0_1__SetVisitedForExistingKeys.class); | ||
|
||
@Override | ||
public void migrate(Context context) throws Exception { | ||
|
||
// HSQLDB: For Testing purposes only, set origin to CH | ||
String originCountry = "CH"; | ||
|
||
// Get all key ids | ||
List<Integer> keyIds = new ArrayList<>(); | ||
try (Statement select = context.getConnection().createStatement()) { | ||
try (ResultSet rows = select.executeQuery("select pk_exposed_id from t_gaen_exposed")) { | ||
while (rows.next()) { | ||
int id = rows.getInt(1); | ||
keyIds.add(id); | ||
} | ||
} | ||
} | ||
logger.info("Found " + keyIds.size() + " keys for migration"); | ||
|
||
// For each key, insert origin country as visited country | ||
if (!keyIds.isEmpty()) { | ||
try (PreparedStatement insertVisitedCountries = | ||
context | ||
.getConnection() | ||
.prepareStatement("insert into t_visited(pfk_exposed_id, country) values(?, ?)")) { | ||
for (Integer keyId : keyIds) { | ||
insertVisitedCountries.setInt(1, keyId); | ||
insertVisitedCountries.setString(2, originCountry); | ||
insertVisitedCountries.addBatch(); | ||
} | ||
insertVisitedCountries.executeBatch(); | ||
} | ||
} | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...-backend-sdk-data/src/main/java/db/migration/pgsql/V2_0_1__SetVisitedForExistingKeys.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,67 @@ | ||
/* | ||
* Copyright (c) 2020 Ubique Innovation AG <https://www.ubique.ch> | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
* | ||
* SPDX-License-Identifier: MPL-2.0 | ||
*/ | ||
|
||
package db.migration.pgsql; | ||
|
||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.Statement; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.flywaydb.core.api.migration.BaseJavaMigration; | ||
import org.flywaydb.core.api.migration.Context; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class V2_0_1__SetVisitedForExistingKeys extends BaseJavaMigration { | ||
|
||
private static final String ORIGIN_COUNTRY_SYS_VAR = "ws.origin.country"; | ||
|
||
private static final Logger logger = | ||
LoggerFactory.getLogger(V2_0_1__SetVisitedForExistingKeys.class); | ||
|
||
@Override | ||
public void migrate(Context context) throws Exception { | ||
|
||
String originCountry = System.getProperty(ORIGIN_COUNTRY_SYS_VAR); | ||
if (originCountry == null || originCountry.isBlank()) { | ||
throw new IllegalArgumentException( | ||
"For successfull migration to the DP3T V2 database schema the country of origin must be" | ||
+ " specified as system variable: " | ||
+ ORIGIN_COUNTRY_SYS_VAR | ||
+ " (for example: java -jar dp3t-sdk.jar -Dws.origin.country=CH ... )"); | ||
} | ||
|
||
// Get all key ids | ||
List<Integer> keyIds = new ArrayList<>(); | ||
try (Statement select = context.getConnection().createStatement()) { | ||
try (ResultSet rows = select.executeQuery("select pk_exposed_id from t_gaen_exposed")) { | ||
while (rows.next()) { | ||
int id = rows.getInt(1); | ||
keyIds.add(id); | ||
} | ||
} | ||
} | ||
logger.info("Found " + keyIds.size() + " keys for migration"); | ||
|
||
// For each key, insert origin country as visited country | ||
try (PreparedStatement insertVisitedCountries = | ||
context | ||
.getConnection() | ||
.prepareStatement("insert into t_visited(pfk_exposed_id, country) values(?, ?)")) { | ||
for (Integer keyId : keyIds) { | ||
insertVisitedCountries.setInt(1, keyId); | ||
insertVisitedCountries.setString(2, originCountry); | ||
insertVisitedCountries.addBatch(); | ||
} | ||
insertVisitedCountries.executeBatch(); | ||
} | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...-sdk-data/src/main/java/db/migration/pgsql_cluster/V2_0_1__SetVisitedForExistingKeys.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,67 @@ | ||
/* | ||
* Copyright (c) 2020 Ubique Innovation AG <https://www.ubique.ch> | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
* | ||
* SPDX-License-Identifier: MPL-2.0 | ||
*/ | ||
|
||
package db.migration.pgsql_cluster; | ||
|
||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.Statement; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.flywaydb.core.api.migration.BaseJavaMigration; | ||
import org.flywaydb.core.api.migration.Context; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class V2_0_1__SetVisitedForExistingKeys extends BaseJavaMigration { | ||
|
||
private static final String ORIGIN_COUNTRY_SYS_VAR = "ws.origin.country"; | ||
|
||
private static final Logger logger = | ||
LoggerFactory.getLogger(V2_0_1__SetVisitedForExistingKeys.class); | ||
|
||
@Override | ||
public void migrate(Context context) throws Exception { | ||
|
||
String originCountry = System.getProperty(ORIGIN_COUNTRY_SYS_VAR); | ||
if (originCountry == null || originCountry.isBlank()) { | ||
throw new IllegalArgumentException( | ||
"For successfull migration to the DP3T V2 database schema the country of origin must be" | ||
+ " specified as system variable: " | ||
+ ORIGIN_COUNTRY_SYS_VAR | ||
+ " (for example: java -jar dp3t-sdk.jar -Dws.origin.country=CH ... )"); | ||
} | ||
|
||
// Get all key ids | ||
List<Integer> keyIds = new ArrayList<>(); | ||
try (Statement select = context.getConnection().createStatement()) { | ||
try (ResultSet rows = select.executeQuery("select pk_exposed_id from t_gaen_exposed")) { | ||
while (rows.next()) { | ||
int id = rows.getInt(1); | ||
keyIds.add(id); | ||
} | ||
} | ||
} | ||
logger.info("Found " + keyIds.size() + " keys for migration"); | ||
|
||
// For each key, insert origin country as visited country | ||
try (PreparedStatement insertVisitedCountries = | ||
context | ||
.getConnection() | ||
.prepareStatement("insert into t_visited(pfk_exposed_id, country) values(?, ?)")) { | ||
for (Integer keyId : keyIds) { | ||
insertVisitedCountries.setInt(1, keyId); | ||
insertVisitedCountries.setString(2, originCountry); | ||
insertVisitedCountries.addBatch(); | ||
} | ||
insertVisitedCountries.executeBatch(); | ||
} | ||
} | ||
} |
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
23 changes: 23 additions & 0 deletions
23
...k/dpppt-backend-sdk-data/src/main/resources/db/migration/hsqldb/V2_0_0__international.sql
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,23 @@ | ||
/* | ||
* Created by Ubique Innovation AG | ||
* https://www.ubique.ch | ||
* Copyright (c) 2020. All rights reserved. | ||
*/ | ||
|
||
alter table t_gaen_exposed drop column transmission_risk_level; | ||
|
||
alter table t_gaen_exposed add column report_type varchar(30); | ||
alter table t_gaen_exposed add column days_since_onset_of_symptoms integer; | ||
|
||
CREATE TABLE t_visited | ||
( | ||
pfk_exposed_id integer NOT NULL, | ||
country varchar(10) NOT NULL, | ||
CONSTRAINT PK_t_visited PRIMARY KEY ( pfk_exposed_id, country ), | ||
CONSTRAINT r_gaen_exposed_visited FOREIGN KEY ( pfk_exposed_id ) REFERENCES t_gaen_exposed ( pk_exposed_id ) ON DELETE CASCADE | ||
); | ||
|
||
CREATE INDEX idx_visited_exposed_id ON t_visited | ||
( | ||
pfk_exposed_id | ||
); |
23 changes: 23 additions & 0 deletions
23
...dk/dpppt-backend-sdk-data/src/main/resources/db/migration/pgsql/V2_0_0__international.sql
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,23 @@ | ||
/* | ||
* Created by Ubique Innovation AG | ||
* https://www.ubique.ch | ||
* Copyright (c) 2020. All rights reserved. | ||
*/ | ||
|
||
alter table t_gaen_exposed drop column transmission_risk_level; | ||
|
||
alter table t_gaen_exposed add column report_type varchar(30); | ||
alter table t_gaen_exposed add column days_since_onset_of_symptoms integer; | ||
|
||
CREATE TABLE t_visited | ||
( | ||
pfk_exposed_id integer NOT NULL, | ||
country varchar(10) NOT NULL, | ||
CONSTRAINT PK_t_visited PRIMARY KEY ( pfk_exposed_id, country ), | ||
CONSTRAINT r_gaen_exposed_visited FOREIGN KEY ( pfk_exposed_id ) REFERENCES t_gaen_exposed ( pk_exposed_id ) ON DELETE CASCADE | ||
); | ||
|
||
CREATE INDEX idx_visited_exposed_id ON t_visited | ||
( | ||
pfk_exposed_id | ||
); |
Oops, something went wrong.