Elffuss BraidHub

kikocis/EDC public

main · 2.0 KiBRenderedRawPermalink
1# SQL Asset
2
3Provides SQL persistence for assets.
4
5## Prerequisites
6
7Please apply this [schema](src/main/resources/asset-index-schema.sql) to your SQL database.
8
9## Entity Diagram
10
11![ER Diagram](https://www.plantuml.com/plantuml/png/ZP3D2i8m48JlUOez2ta1AQLtBxv1MDn58crQibiXDBwxGQfKhJ-tm3SpcPr65AEENMiugDS4J0U78gmm6O0DtDxEqnP4emz7gAhzhguBizPSp9lD4IeYKMIHNn653R4VEAfdMT2JzE7R5xCf_P-VNC2Exu9dSiPs_80q3KiortaibBErEQ_V_YBhfvN-fk50PVih)
12<!--
13```plantuml
14@startuml
15entity edc_asset {
16 * asset_id_fk: string <<PK>>
17 --
18}
19
20entity edc_asset_dataaddress {
21 * asset_id_fk: string <<PK>>
22 * properties: string <<json>>
23 --
24}
25
26entity edc_asset_property {
27 * asset_id_fk: string <<PK>>
28 * property_name: string
29 * property_value: string
30 * property_type: string
31 --
32}
33
34edc_asset ||--|| edc_asset_dataaddress
35edc_asset ||--o{ edc_asset_property
36@enduml
37```
38-->
39
40## Migrate from 0.3.1 to 0.3.2
41
42This table structure has been changed, from 3 tables (one for the asset, one for properties and one for data address) to
43a single table with json fields.
44To migrate an existing database, please first add the 3 new columns in the `edc_asset` table:
45
46```sql
47ALTER TABLE edc_asset
48 ADD COLUMN properties json DEFAULT '{}',
49 ADD COLUMN private_properties json DEFAULT '{}',
50 ADD COLUMN data_address json DEFAULT '{}';
51```
52
53Then migrate the existing data in the new format:
54
55```sql
56UPDATE edc_asset
57SET properties=(SELECT JSON_OBJECT_AGG(property_name, property_value)
58 FROM edc_asset_property
59 WHERE asset_id_fk = edc_asset.asset_id
60 AND property_is_private = FALSE),
61 private_properties=(SELECT JSON_OBJECT_AGG(property_name, property_value)
62 FROM edc_asset_property
63 WHERE asset_id_fk = edc_asset.asset_id
64 AND property_is_private = TRUE),
65 data_address=(SELECT properties FROM edc_asset_dataaddress WHERE asset_id_fk = edc_asset.asset_id);
66```
67
68After the migration, the tables `edc_asset_dataaddress` and `edc_asset_property` can be deleted.