| 1 | # SQL Asset |
| 2 | |
| 3 | Provides SQL persistence for assets. |
| 4 | |
| 5 | ## Prerequisites |
| 6 | |
| 7 | Please apply this [schema](src/main/resources/asset-index-schema.sql) to your SQL database. |
| 8 | |
| 9 | ## Entity Diagram |
| 10 | |
| 11 |  |
| 12 | <!-- |
| 13 | ```plantuml |
| 14 | @startuml |
| 15 | entity edc_asset { |
| 16 | * asset_id_fk: string <<PK>> |
| 17 | -- |
| 18 | } |
| 19 | |
| 20 | entity edc_asset_dataaddress { |
| 21 | * asset_id_fk: string <<PK>> |
| 22 | * properties: string <<json>> |
| 23 | -- |
| 24 | } |
| 25 | |
| 26 | entity edc_asset_property { |
| 27 | * asset_id_fk: string <<PK>> |
| 28 | * property_name: string |
| 29 | * property_value: string |
| 30 | * property_type: string |
| 31 | -- |
| 32 | } |
| 33 | |
| 34 | edc_asset ||--|| edc_asset_dataaddress |
| 35 | edc_asset ||--o{ edc_asset_property |
| 36 | @enduml |
| 37 | ``` |
| 38 | --> |
| 39 | |
| 40 | ## Migrate from 0.3.1 to 0.3.2 |
| 41 | |
| 42 | This table structure has been changed, from 3 tables (one for the asset, one for properties and one for data address) to |
| 43 | a single table with json fields. |
| 44 | To migrate an existing database, please first add the 3 new columns in the `edc_asset` table: |
| 45 | |
| 46 | ```sql |
| 47 | ALTER 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 | |
| 53 | Then migrate the existing data in the new format: |
| 54 | |
| 55 | ```sql |
| 56 | UPDATE edc_asset |
| 57 | SET 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 | |
| 68 | After the migration, the tables `edc_asset_dataaddress` and `edc_asset_property` can be deleted. |