Skip to content

Commit 931648d

Browse files
authored
CORE-16183 Ledger data model simplifications (part 1) (#1270)
This PR contains the AGREED suggestions from this page: https://r3-cev.atlassian.net/wiki/spaces/CB/pages/4578214028/UTXO+Ledger+Data+Model+Simplifications#Remove-created-field-from-utxo_transaction_component-table-Agreed. These are: - Merge utxo_transaction_status table into utxo_transaction (then delete the utxo_transaction_status table) and rename utxo_transaction_output table to utxo_visible_transaction_output - Merge utxo_visible_transaction_state table into utxo_transaction_output (then delete the utxo_visible_transaction_state table) - Removal of utxo_transaction_sources table - Removal of utxo_transaction_cpk and utxo_cpk tables - Remove created field from utxo_transaction_component table
1 parent 9403890 commit 931648d

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

data/db-schema/src/main/resources/net/corda/db/schema/vnode-vault/migration/ledger-utxo-creation-v5.1.xml

+88
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,94 @@
3333
tableName="utxo_transaction_output">
3434
<column name="token_notary_x500_name"/>
3535
</createIndex>
36+
37+
<!-- Add new status column to utxo_transaction -->
38+
<addColumn tableName="utxo_transaction">
39+
<column name="status" type="VARCHAR(16)">
40+
<constraints nullable="true"/>
41+
</column>
42+
<column name="updated" type="TIMESTAMP">
43+
<constraints nullable="true"/>
44+
</column>
45+
</addColumn>
46+
47+
<!-- Migrate data from the old status table -->
48+
<update tableName="utxo_transaction">
49+
<column name="status"
50+
valueComputed="(select status from utxo_transaction_status uts where uts.transaction_id=id)"/>
51+
<column name="updated"
52+
valueComputed="(select updated from utxo_transaction_status uts where uts.transaction_id=id)"/>
53+
</update>
54+
55+
<!-- Drop the old status table -->
56+
<dropTable tableName="utxo_transaction_status"/>
57+
58+
<!-- After status data migration we can make the status/updated column non-nullable -->
59+
<addNotNullConstraint tableName="utxo_transaction" columnName="status" columnDataType="VARCHAR(16)"/>
60+
<addNotNullConstraint tableName="utxo_transaction" columnName="updated" columnDataType="TIMESTAMP"/>
61+
62+
<!-- Add new custom_representation and consumed columns to utxo_transaction_output -->
63+
<addColumn tableName="utxo_transaction_output">
64+
<column name="custom_representation" type="${json.column.type}">
65+
<constraints nullable="true"/>
66+
</column>
67+
<column name="consumed" type="TIMESTAMP">
68+
<constraints nullable="true"/>
69+
</column>
70+
</addColumn>
71+
72+
<!--
73+
Migrate data from the old visible states table
74+
NOTE: We can't use <update> here because both tables have the same column naming
75+
(transaction_id, group_idx etc.) and the SQL statement generated will not work in
76+
Postgres, and we have no way to alias the base table.
77+
-->
78+
<sql>
79+
UPDATE utxo_transaction_output txo
80+
SET
81+
custom_representation = (
82+
SELECT custom_representation from utxo_visible_transaction_state vts
83+
WHERE vts.transaction_id=txo.transaction_id
84+
AND vts.group_idx=txo.group_idx
85+
AND vts.leaf_idx=txo.leaf_idx
86+
),
87+
consumed = (
88+
SELECT consumed from utxo_visible_transaction_state vts
89+
WHERE vts.transaction_id=txo.transaction_id
90+
AND vts.group_idx=txo.group_idx
91+
AND vts.leaf_idx=txo.leaf_idx
92+
);
93+
</sql>
94+
95+
<!-- After visible states data migration we can make the custom_representation column non-nullable -->
96+
<addNotNullConstraint tableName="utxo_transaction_output"
97+
columnName="custom_representation"
98+
columnDataType="${json.column.type}"/>
99+
100+
<!-- Drop the utxo_visible_transaction_state table after data migration -->
101+
<dropTable tableName="utxo_visible_transaction_state"/>
102+
103+
<!-- Rename utxo_transaction_output to utxo_visible_transaction_output -->
104+
<renameTable newTableName="utxo_visible_transaction_output" oldTableName="utxo_transaction_output"/>
105+
106+
<!-- Re-create the index on the consumed column in the new table -->
107+
<createIndex
108+
indexName="utxo_visible_transaction_output_idx_consumed"
109+
tableName="utxo_visible_transaction_output">
110+
<column name="consumed"/>
111+
</createIndex>
112+
113+
<!-- Drop the created column from the utxo_transaction_component table -->
114+
<dropColumn columnName="created" tableName="utxo_transaction_component"/>
115+
116+
<!-- Drop the utxo_transaction_sources table -->
117+
<dropTable tableName="utxo_transaction_sources"/>
118+
119+
<!-- Drop the utxo_transaction_cpk table -->
120+
<dropTable tableName="utxo_transaction_cpk"/>
121+
122+
<!-- Drop the utxo_cpk table -->
123+
<dropTable tableName="utxo_cpk"/>
36124
</changeSet>
37125

38126
</databaseChangeLog>

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ cordaProductVersion = 5.1.0
99
# NOTE: update this each time this module contains a breaking change
1010
## NOTE: currently this is a top level revision, so all API versions will line up, but this could be moved to
1111
## a per module property in which case module versions can change independently.
12-
cordaApiRevision = 29
12+
cordaApiRevision = 30
1313

1414
# Main
1515
kotlinVersion = 1.8.21

0 commit comments

Comments
 (0)