aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/resources/db/changelog/schema/01-init-schema.xml
blob: b10cd00046f82bd73c1700e1814a44c99aeb6181 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?xml version="1.0" encoding="UTF-8"?>
<!--
  ============LICENSE_START=======================================================
  Copyright (c) 2021 Bell Canada.
  ================================================================================
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
  ============LICENSE_END=========================================================
-->

<databaseChangeLog
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
            http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.3.xsd">

    <changeSet id="1.1" author="cps">
        <comment>Create network data timescale table</comment>
        <createTable tableName="network_data"
                remarks="Table storing network configuration or state data along with its timestamp.">
            <column name="timestamp" type="TIMESTAMP WITH TIMEZONE"
                    remarks="The timestamp when configuration or state has been observed.">
                <constraints nullable="false"/>
            </column>
            <column name="dataspace" type="VARCHAR(255)"
                    remarks="The dataspace, configuration or state belongs to. Refers to CPS Core dataspace.">
                <constraints nullable="false"/>
            </column>
            <column name="anchor" type="VARCHAR(255)"
                    remarks="The anchor, configuration or state is attached to. Refers to CPS Core anchor.">
                <constraints nullable="false"/>
            </column>
            <column name="schema_set" type="VARCHAR(255)"
                    remarks="The schema set, configuration or state complies to. Refers to CPS Core schema set.">
                <constraints nullable="false"/>
            </column>
            <column name="payload" type="JSONB"
                    remarks="The complete json payload representing configuration or state data.">
                <constraints nullable="false"/>
            </column>
            <column name="version" type="TIMESTAMP WITH TIMEZONE"
                    remarks="The system version of the record, automatically set by the application with the timestamp when the record is persisted.">
                <constraints nullable="false"/>
            </column>
        </createTable>
        <sql>
            SELECT create_hypertable('network_data', 'timestamp', chunk_time_interval => interval '1 minute');
        </sql>
        <rollback>
            <sql>
                DROP table network_data;
            </sql>
        </rollback>
    </changeSet>

    <changeSet id="1.2" author="cps">
        <comment>Create indexes on network data</comment>
        <createIndex indexName="network_data_dataspace_anchor_timestamp_idx" tableName="network_data"
                unique="true">
            <column name="dataspace"/>
            <column name="anchor"/>
            <column name="timestamp" descending="true"/>
        </createIndex>
        <createIndex indexName="network_data_dataspace_schema_set_timestamp_idx" tableName="network_data">
            <column name="dataspace"/>
            <column name="schema_set"/>
            <column name="timestamp" descending="true"/>
        </createIndex>
    </changeSet>

    <changeSet id="1.3" author="cps">
        <comment>Create GIN index on network data payload</comment>
        <sql>
            CREATE INDEX network_data_payload_idx ON network_data USING GIN (payload);
        </sql>
        <rollback>
            <sql>
                DROP INDEX network_data_payload_idx;
            </sql>
        </rollback>
    </changeSet>

</databaseChangeLog>