Skip to content

Commit efb3a74

Browse files
committed
fix: make migrations applicable on existing data.
During adaptation for PostgreSQL, the modifications were made on the existing changesets. It has made them failing on existing data as the checksums were changed. In the new approach, we moved conditions from statements within a changeset to a changeset itself. Correction for the following commits: - c03b2cc - 6d296fd - 7282a7b - d767dab - 11c898b Addressed to #1034
1 parent 03c609f commit efb3a74

5 files changed

+55
-19
lines changed

src/main/resources/liquibase/version/0.3/2014-05-28--release_month_and_day.xml

+8-3
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,21 @@
3232

3333
</changeSet>
3434

35-
<changeSet id="fill-release_year-column-based-on-values-from-released_at" author="php-coder" context="test-data, prod-data">
35+
<changeSet id="fill-release_year-column-based-on-values-from-released_at" author="php-coder" context="test-data, prod-data" dbms="mysql,h2">
3636
<comment>Migrates data from series.released_at to series.release_year</comment>
3737

38-
<sql dbms="mysql,h2">
38+
<sql>
3939
UPDATE series
4040
SET release_year=YEAR(released_at)
4141
WHERE released_at IS NOT NULL
4242
</sql>
4343

44-
<sql dbms="postgresql">
44+
</changeSet>
45+
46+
<changeSet id="fill-release_year-column-based-on-values-from-released_at" author="php-coder" context="test-data, prod-data" dbms="postgresql">
47+
<comment>Migrates data from series.released_at to series.release_year</comment>
48+
49+
<sql>
4550
UPDATE series
4651
SET release_year=EXTRACT(YEAR FROM released_at)
4752
WHERE released_at IS NOT NULL

src/main/resources/liquibase/version/0.3/2014-09-28--collection_slug.xml

+8-3
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414

1515
</changeSet>
1616

17-
<changeSet id="update-collections-slug" author="php-coder" context="test-data,prod-data">
17+
<changeSet id="update-collections-slug" author="php-coder" context="test-data,prod-data" dbms="mysql,h2">
1818
<comment>Sets value of slug field to transformed collection's owner's name</comment>
1919

2020
<!-- Unfortunately, H2 doesn't support UPDATE with JOIN -->
21-
<sql dbms="mysql,h2">
21+
<sql>
2222
UPDATE collections c
2323
SET c.slug = (
2424
SELECT LOWER(REPLACE(u.login, ' ', '-'))
@@ -27,8 +27,13 @@
2727
);
2828
</sql>
2929

30+
</changeSet>
31+
32+
<changeSet id="update-collections-slug" author="php-coder" context="test-data,prod-data" dbms="postgresql">
33+
<comment>Sets value of slug field to transformed collection's owner's name</comment>
34+
3035
<!-- In PostgreSQL it is illegal to prefix columns with table alias in the SET clause -->
31-
<sql dbms="postgresql">
36+
<sql>
3237
UPDATE collections c
3338
SET slug = (
3439
SELECT LOWER(REPLACE(u.login, ' ', '-'))

src/main/resources/liquibase/version/0.4/2015-06-22--image_url.xml

+16-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
</changeSet>
2525

26-
<changeSet id="fill-series_images-table-based-on-values-from-image_url" author="php-coder" context="test-data, prod-data">
26+
<changeSet id="fill-series_images-table-based-on-values-from-image_url" author="php-coder" context="test-data, prod-data" dbms="mysql,h2">
2727
<preConditions>
2828
<sqlCheck expectedResult="0">
2929
SELECT COUNT(*)
@@ -34,14 +34,27 @@
3434

3535
<comment>Migrates data from series.image_url to series_images</comment>
3636

37-
<sql dbms="mysql,h2">
37+
<sql>
3838
INSERT INTO series_images(series_id, image_id)
3939
SELECT id AS series_id, REPLACE(image_url, '/image/', '') AS image_id
4040
FROM series
4141
</sql>
4242

43+
</changeSet>
44+
45+
<changeSet id="fill-series_images-table-based-on-values-from-image_url" author="php-coder" context="test-data, prod-data" dbms="postgresql">
46+
<preConditions>
47+
<sqlCheck expectedResult="0">
48+
SELECT COUNT(*)
49+
FROM series
50+
WHERE image_url IS NULL
51+
</sqlCheck>
52+
</preConditions>
53+
54+
<comment>Migrates data from series.image_url to series_images</comment>
55+
4356
<!-- PostgreSQL requires us to cast a string to an integer explicitly -->
44-
<sql dbms="postgresql">
57+
<sql>
4558
INSERT INTO series_images(series_id, image_id)
4659
SELECT id AS series_id, CAST(REPLACE(image_url, '/image/', '') AS INT) AS image_id
4760
FROM series

src/main/resources/liquibase/version/0.4/2015-07-07--salt_and_hash.xml

+8-7
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,24 @@
1212

1313
</changeSet>
1414

15-
<changeSet id="increase-length-of-hash-column-in-users-table" author="php-coder" context="scheme">
15+
<changeSet id="increase-length-of-hash-column-in-users-table" author="php-coder" context="scheme" dbms="mysql">
1616
<comment>Increases length of hash column in users table</comment>
1717

1818
<!--
1919
We can't use modifyDataType because it looses not null setting:
2020
http://forum.liquibase.org/topic/warning-of-losing-primary-key-autoincrement-for-mysql
2121
-->
22-
<sql dbms="mysql,h2">
22+
<sql>
2323
ALTER TABLE users
2424
MODIFY COLUMN hash VARCHAR(60) NOT NULL
2525
</sql>
2626

27-
<sql dbms="postgresql">
28-
ALTER TABLE users
29-
ALTER COLUMN hash
30-
TYPE VARCHAR(60)
31-
</sql>
27+
</changeSet>
28+
29+
<changeSet id="increase-length-of-hash-column-in-users-table" author="php-coder" context="scheme" dbms="postgresql,h2">
30+
<comment>Increases length of hash column in users table</comment>
31+
32+
<modifyDataType tableName="users" columnName="hash" newDataType="VARCHAR(60)" />
3233

3334
</changeSet>
3435

src/main/resources/liquibase/version/0.4/2018-06-09--add_number_of_stamps_to_collections_series.xml

+15-3
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313

1414
</changeSet>
1515

16-
<changeSet id="fill-collections_series-number_of_stamps-field" author="php-coder" context="prod-data">
16+
<changeSet id="fill-collections_series-number_of_stamps-field" author="php-coder" context="prod-data" dbms="mysql,h2">
1717
<comment>Sets value of number_of_stamps field to number of stamps in a series</comment>
1818

19-
<sql dbms="mysql,h2">
19+
<sql>
2020
UPDATE collections_series cs
2121
SET cs.number_of_stamps = (
2222
SELECT s.quantity
@@ -25,8 +25,20 @@
2525
)
2626
</sql>
2727

28+
<rollback>
29+
<sql>
30+
UPDATE collections_series
31+
SET number_of_stamps = NULL
32+
</sql>
33+
</rollback>
34+
35+
</changeSet>
36+
37+
<changeSet id="fill-collections_series-number_of_stamps-field" author="php-coder" context="prod-data" dbms="postgresql">
38+
<comment>Sets value of number_of_stamps field to number of stamps in a series</comment>
39+
2840
<!-- In PostgreSQL it is illegal to prefix columns with table alias in the SET clause -->
29-
<sql dbms="postgresql">
41+
<sql>
3042
UPDATE collections_series cs
3143
SET number_of_stamps = (
3244
SELECT s.quantity

0 commit comments

Comments
 (0)