forked from GoogleCloudPlatform/pgadapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbchangelog.xml
498 lines (469 loc) · 18.2 KB
/
dbchangelog.xml
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:pro="http://www.liquibase.org/xml/ns/pro"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd
http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-latest.xsd">
<!-- Add a tag to the empty database, so we can roll back to this version later if we want -->
<changeSet id="tag-v0.0" author="loite">
<tagDatabase tag="v0.0"/>
</changeSet>
<!--
Each change set is applied in a separate transaction. Cloud Spanner however does not support
DDL transactions, only DDL batches. A DDL batch is not guaranteed to be atomic. If a statement
in a DDL batch fails, the preceding statements in the batch might have been applied to the
database.
Applying multiple DDL statements in a single batch is a lot more efficient than executing each
statement separately. It is therefore recommended creating one large change set with multiple
DDL statements instead of multiple small change sets with separate DDL statements whenever
possible.
-->
<changeSet id="1 - create singers and albums" author="loite">
<createTable tableName="singers">
<column name="singer_id" type="varchar(36)">
<!-- Setting the primary key name to 'pk_<table_name>' is required! -->
<constraints
primaryKey="true"
primaryKeyName="pk_singers"
nullable="false"/>
</column>
<column name="first_name" type="varchar(200)" />
<column name="last_name" type="varchar(200)">
<constraints nullable="false" />
</column>
<column
name="full_name"
type="varchar(400)"
generationType="ALWAYS"
defaultValueComputed="GENERATED ALWAYS AS (coalesce(first_name || ' ', '') || last_name) STORED">
<constraints nullable="true" />
</column>
<column name="active" type="boolean" defaultValueBoolean="true"/>
</createTable>
<createIndex indexName="idx_singers_last_name" tableName="singers">
<column name="last_name" />
</createIndex>
<createTable tableName="albums">
<column name="album_id" type="varchar(36)">
<!-- Setting the primary key name to 'pk_<table_name>' is required! -->
<constraints
primaryKey="true"
primaryKeyName="pk_albums"
nullable="false"/>
</column>
<column name="title" type="varchar(200)">
<constraints nullable="false" />
</column>
<column name="singer_id" type="varchar(36)">
<constraints
nullable="false"
foreignKeyName="fk_albums_singers"
references="singers (singer_id)" />
</column>
</createTable>
</changeSet>
<!--
Create a table that is interleaved in a parent table.
The 'INTERLEAVE IN PARENT' keyword is a Spanner-specific extension to the PostgreSQL DDL dialect.
We therefore need to modify the SQL manually.
-->
<changeSet id="2 - create tracks" author="loite">
<createTable tableName="tracks">
<!-- Cloud Spanner does not allow adding a primary key after the table has been created, so
you must add the primary key definition to all columns when creating the table. -->
<column name="album_id" type="varchar(36)">
<constraints
primaryKey="true"
primaryKeyName="pk_tracks"
nullable="false"/>
</column>
<column name="track_number" type="bigint">
<constraints
primaryKey="true"
primaryKeyName="pk_tracks"
nullable="false"/>
</column>
<column name="title" type="varchar(200)">
<constraints nullable="false" />
</column>
</createTable>
<!-- Append custom sql to the previous CREATE TABLE statement to interleave it in albums. -->
<modifySql dbms="postgresql">
<append value=" interleave in parent albums on delete cascade"/>
</modifySql>
</changeSet>
<!--
Load data changes can safely be split into multiple change sets. Each change set will use its
own read/write transaction and is guaranteed to be atomic.
-->
<changeSet id="3 - load singers" author="loite">
<loadData
encoding="UTF-8"
file="data/singers.csv"
quotchar='"'
relativeToChangelogFile="true"
separator=";"
tableName="singers"
usePreparedStatements="true">
<column
name="singer_id"
type="string"/>
<column
name="first_name"
type="string"/>
<column
name="last_name"
type="string"/>
<column
name="active"
type="boolean"/>
</loadData>
<rollback>
<!-- No specific rollback -->
</rollback>
</changeSet>
<changeSet id="4 - load albums" author="loite">
<loadData
encoding="UTF-8"
file="data/albums.csv"
quotchar='"'
relativeToChangelogFile="true"
separator=";"
tableName="albums"
usePreparedStatements="true">
<column
name="album_id"
type="string"/>
<column
name="singer_id"
type="string"/>
<column
name="title"
type="string"/>
</loadData>
<rollback>
<!-- No specific rollback -->
</rollback>
</changeSet>
<changeSet id="5 - load tracks" author="loite">
<loadData
encoding="UTF-8"
file="data/tracks.csv"
quotchar='"'
relativeToChangelogFile="true"
separator=";"
tableName="tracks"
usePreparedStatements="true">
<column
name="album_id"
type="string"/>
<column
name="track_number"
type="numeric"/> <!-- Note: The type here is the internal Liquibase type, not the PG / Spanner NUMERIC data type. -->
<column
name="title"
type="string"/>
</loadData>
<rollback>
<!-- No specific rollback -->
</rollback>
</changeSet>
<!-- Add a tag to the database, so we can roll back to this version later if we want -->
<changeSet id="tag-v1.0" author="loite">
<tagDatabase tag="v1.0"/>
</changeSet>
<!-- Add a non-null column to an existing table. -->
<changeSet id="6 - add duration column to tracks" author="loite">
<addColumn tableName="tracks">
<!--
Add a non-null column. Note that we are using 'defaultValue' instead of 'defaultValueNumeric here.
The reason for that is that 'defaultValueNumeric' would be automatically converted to '0', but we
want '0.0' to be conserved, as only that is a valid NUMERIC default value literal.
-->
<column name="duration" type="numeric" defaultValue="0.0">
<constraints nullable="false" />
</column>
</addColumn>
</changeSet>
<!-- Add a tag to the database, so we can roll back to this version later if we want -->
<changeSet id="tag-v1.1" author="loite">
<tagDatabase tag="v1.1"/>
</changeSet>
<!-- Create a table that contains a column with each supported data type. -->
<changeSet id="7 - add table with all types" author="loite">
<createTable tableName="all_types">
<column name="col_bigint" type="bigint">
<!-- NOTE: Setting the primary key name to 'pk_<table_name>' is REQUIRED -->
<constraints nullable="false" primaryKey="true" primaryKeyName="pk_all_types" />
</column>
<column name="col_bool" type="boolean"/>
<column name="col_bytea" type="bytea"/>
<column name="col_float8" type="float8"/>
<column name="col_int" type="int"/> <!-- Note that this will automatically be converted to bigint. -->
<column name="col_numeric" type="numeric"/>
<column name="col_timestamptz" type="timestamptz"/>
<column name="col_date" type="date"/>
<column name="col_varchar" type="varchar(100)"/>
<column name="col_text" type="text"/>
</createTable>
</changeSet>
<!-- Load data into a table with all supported data types. -->
<changeSet id="8 - load data for all data types" author="loite">
<loadData
encoding="UTF-8"
file="data/all_types.csv"
quotchar='"'
relativeToChangelogFile="true"
separator=";"
tableName="all_types"
usePreparedStatements="true">
<column
name="col_bigint"
type="numeric"/> <!-- Note: The type here is the internal Liquibase type, not the PG / Spanner NUMERIC data type. -->
<column
name="col_bool"
type="boolean"/>
<column
name="col_bytea"
type="other"/> <!-- We cannot use 'blob', as that is not supported by Cloud Spanner. -->
<column
name="col_float8"
type="numeric"/>
<column
name="col_int"
type="numeric"/>
<column
name="col_numeric"
type="numeric"/>
<column
name="col_timestamptz"
type="other"/> <!-- Use 'other' to ensure that the backend will infer the type of this value. -->
<column
name="col_date"
type="date"/>
<column
name="col_varchar"
type="string"/>
<column
name="col_text"
type="string"/>
</loadData>
<rollback>
<!-- No specific rollback -->
</rollback>
</changeSet>
<!-- Add a tag to the database, so we can roll back to this version later if we want -->
<changeSet id="tag-v2.0" author="loite">
<tagDatabase tag="v2.0"/>
</changeSet>
<changeSet id="9 - custom SQL change" author="loite">
<sql
splitStatements="true"
endDelimiter=";"><![CDATA[
create table venues (
venue_id varchar(36) not null primary key,
name varchar(100)
);
create table concerts (
concert_id varchar(36) not null primary key,
venue_id varchar(36) not null,
start_time timestamptz,
end_time timestamptz,
singer_id varchar(36) not null,
constraint fk_concerts_singers foreign key (singer_id) references singers (singer_id),
constraint chk_concert_end_time_after_start_time check (
coalesce(start_time, '0001-01-01T00:00:00Z'::timestamptz) <
coalesce(end_time, '9999-12-31T23:59:59Z'::timestamptz)
)
);
]]>
</sql>
<rollback>
<sql
splitStatements="true"
endDelimiter=";"><![CDATA[
drop table concerts;
drop table venues;
]]>
</sql>
</rollback>
</changeSet>
<changeSet id="9.1 - add index for concert times" author="loite">
<createIndex indexName="idx_concerts_singer_start_time" tableName="concerts" unique="true">
<column name="singer_id" />
<column name="start_time" descending="true" />
</createIndex>
</changeSet>
<!-- Add a tag to the database, so we can roll back to this version later if we want -->
<changeSet id="tag-v3.0" author="loite">
<tagDatabase tag="v3.0"/>
</changeSet>
<changeSet id="9.2 - fix check constraint" author="loite">
<sql
splitStatements="true"
endDelimiter=";"><![CDATA[
alter table concerts drop constraint chk_concert_end_time_after_start_time;
alter table concerts add constraint chk_concert_end_time_after_start_time check (
coalesce(start_time, '0001-01-01 00:00:00+00'::timestamptz) <
coalesce(end_time, '9999-12-31 23:59:59+00'::timestamptz)
);
]]>
</sql>
<rollback>
<sql
splitStatements="true"
endDelimiter=";"><![CDATA[
alter table concerts drop constraint chk_concert_end_time_after_start_time;
alter table concerts add constraint chk_concert_end_time_after_start_time check (
coalesce(start_time, '0001-01-01T00:00:00Z'::timestamptz) <
coalesce(end_time, '9999-12-31T23:59:59Z'::timestamptz)
);
]]>
</sql>
</rollback>
</changeSet>
<!-- Add a tag to the database, so we can roll back to this version later if we want -->
<changeSet id="tag-v3.1" author="loite">
<tagDatabase tag="v3.1"/>
</changeSet>
<changeSet id="10 - insert data with SQL" author="loite">
<sql
splitStatements="true"
endDelimiter=";">
insert into venues values ('45850c71-c2ea-404b-98b8-bcf985ec3a8c', 'Venue 1');
insert into concerts values ('149e4411-fa1e-46a1-8b95-d5da83d7e7a7', '45850c71-c2ea-404b-98b8-bcf985ec3a8c', null, null, '014a1c27-a8af-4911-bb71-6da16a540df1');
insert into concerts values ('89b2cf6f-322d-45e8-8228-89544e948908', '45850c71-c2ea-404b-98b8-bcf985ec3a8c', '2022-08-31 20:30:00+02:00'::timestamptz, '2022-09-01 02:00:00+02:00'::timestamptz, '014a1c27-a8af-4911-bb71-6da16a540df1');
</sql>
<rollback>
<sql
splitStatements="true"
endDelimiter=";">
delete from concerts where concert_id='89b2cf6f-322d-45e8-8228-89544e948908';
delete from concerts where concert_id='149e4411-fa1e-46a1-8b95-d5da83d7e7a7';
delete from venues where venue_id='45850c71-c2ea-404b-98b8-bcf985ec3a8c';
</sql>
</rollback>
</changeSet>
<!-- Add a tag to the database, so we can roll back to this version later if we want -->
<changeSet id="tag-v3.2" author="loite">
<tagDatabase tag="v3.2"/>
</changeSet>
<changeSet id="11 - create singers view" author="loite">
<!-- Set fullDefinition="true" to ensure the `sql security invoker` clause is included. -->
<createView viewName="singers_by_last_name" fullDefinition="true" replaceIfExists="true">
create view singers_by_last_name sql security invoker as
select singer_id, full_name from singers order by last_name
</createView>
</changeSet>
<!-- Add a tag to the database, so we can roll back to this version later if we want -->
<changeSet id="tag-v3.3" author="loite">
<tagDatabase tag="v3.3"/>
</changeSet>
<changeSet id="12 - drop duration column from tracks" author="loite">
<dropColumn tableName="tracks" columnName="duration" />
<rollback changeSetId="6 - add duration column to tracks" changeSetAuthor="loite" />
</changeSet>
<changeSet id="13 - drop concerts index" author="loite">
<dropIndex tableName="concerts" indexName="idx_concerts_singer_start_time" />
<rollback changeSetId="9.1 - add index for concert times" changeSetAuthor="loite"/>
</changeSet>
<changeSet id="14 - drop all_types table" author="loite">
<dropTable tableName="all_types" />
<rollback changeSetId="7 - add table with all types" changeSetAuthor="loite" />
</changeSet>
<changeSet id="15 - drop singers view" author="loite">
<dropView viewName="singers_by_last_name" />
<rollback changeSetId="11 - create singers view" changeSetAuthor="loite" />
</changeSet>
<changeSet id="16 - add venue name default value" author="loite">
<addDefaultValue tableName="venues" columnName="name" defaultValue="'my-venue'" />
</changeSet>
<changeSet id="16.1 - drop default value for venues(name)" author="loite">
<dropDefaultValue tableName="venues" columnName="name" />
<rollback changeSetId="16 - add venue name default value" changeSetAuthor="loite" />
</changeSet>
<changeSet id="17 - add foreign key concerts - venues" author="loite">
<addForeignKeyConstraint
baseColumnNames="venue_id"
baseTableName="concerts"
baseTableSchemaName="public"
constraintName="fk_concerts_venues"
referencedColumnNames="venue_id"
referencedTableName="venues"
referencedTableSchemaName="public"/>
</changeSet>
<changeSet id="18 - drop foreign key concerts - venues" author="loite">
<dropForeignKeyConstraint
baseTableName="concerts"
constraintName="fk_concerts_venues" />
<rollback changeSetId="17 - add foreign key concerts - venues" changeSetAuthor="loite" />
</changeSet>
<changeSet id="19 - add an extra track" author="loite">
<sql
splitStatements="true"
endDelimiter=";"><![CDATA[
insert into tracks (album_id, track_number, title) values ('0e3f2ac2-28e9-47b2-ba24-3086b04bd6f3',8,'Track 8');
]]>
</sql>
<rollback>
<sql
splitStatements="true"
endDelimiter=";"><![CDATA[
delete from tracks where album_id='0e3f2ac2-28e9-47b2-ba24-3086b04bd6f3' and track_number=8;
]]>
</sql>
</rollback>
</changeSet>
<changeSet id="20 - delete track 8" author="loite">
<delete tableName="tracks">
<where>album_id='0e3f2ac2-28e9-47b2-ba24-3086b04bd6f3' and track_number=8</where>
</delete>
<rollback changeSetId="19 - add an extra track" changeSetAuthor="loite" />
</changeSet>
<changeSet id="21 - re-insert track 8" author="loite">
<insert tableName="tracks">
<column name="album_id" value="0e3f2ac2-28e9-47b2-ba24-3086b04bd6f3"/>
<column name="track_number" value="8" />
<column name="title" value="Track 8" />
</insert>
<rollback changeSetId="20 - delete track 8" changeSetAuthor="loite" />
</changeSet>
<changeSet id="22.0 - create address columns" author="loite">
<addColumn tableName="singers">
<column name="street" type="varchar" />
<column name="house_number" type="varchar" />
</addColumn>
<update tableName="singers">
<column name="street" value="my street"/>
<column name="house_number" value="1"/>
</update>
<rollback>
<dropColumn tableName="singers" columnName="street" />
<dropColumn tableName="singers" columnName="house_number" />
</rollback>
</changeSet>
<changeSet id="22.1 - merge street and house number" author="loite">
<mergeColumns
column1Name="street"
column2Name="house_number"
finalColumnName="address"
finalColumnType="varchar"
joinString=" "
tableName="singers"/>
<rollback>
<dropColumn tableName="singers" columnName="address" />
</rollback>
<rollback changeSetId="22.0 - create address columns" changeSetAuthor="loite" />
</changeSet>
<changeSet id="23 - remove all addresses" author="loite">
<update tableName="singers">
<column name="address" value="null"/>
</update>
<!-- There's no rollback for this change -->
<rollback />
</changeSet>
</databaseChangeLog>