-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathv3.0_CreateAddressTable.xml
43 lines (35 loc) · 1.56 KB
/
v3.0_CreateAddressTable.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
<?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"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.20.xsd">
<changeSet id="1" author="aromanow" labels="v3.0">
<sql>
CREATE TABLE address
(
id SERIAL PRIMARY KEY,
city VARCHAR NOT NULL
);
CREATE UNIQUE INDEX idx_address_city ON address (city);
INSERT INTO address (city)
SELECT DISTINCT usr.location FROM users usr;
ALTER TABLE users ADD COLUMN address_id INT
CONSTRAINT fk_users_address_id REFERENCES address (id);
CREATE INDEX idx_users_address_id ON users (address_id);
UPDATE users SET address_id = (SELECT addr.id FROM address addr WHERE addr.city = location);
ALTER TABLE users DROP COLUMN location;
</sql>
<rollback>
<sql>
ALTER TABLE users ADD COLUMN location VARCHAR;
UPDATE users SET location = (SELECT addr.city FROM address addr WHERE addr.id = users.address_id);
ALTER TABLE users DROP COLUMN address_id;
DROP TABLE address;
</sql>
</rollback>
</changeSet>
<changeSet id="2" author="aromanow" labels="v3.0">
<tagDatabase tag="v3.0"/>
</changeSet>
</databaseChangeLog>