AdSense

網頁

2022/5/24

Liquibase sql-format H2新增資料

使用Liquibase在H2資料庫的資料表新增資料。


範例環境:

  • macOS BigSur version 10.15.5
  • Liquibase 4.10.0


事前要求

參考「Liquibase sql-format H2建立資料表」安裝liquibase及在H2建立EMPLOYEE資料表。


Changelogs

changelog.sql最下面加上兩筆changeset來新增資料。

--changeset employee:2
INSERT INTO employee (name, age) VALUES ('John', '33');

--changeset employee:3
INSERT INTO employee (name, age) VALUES ('Mary', '28');

修改後的changelog.sql內容如下:

changelog.sql

--liquibase formatted sql

--changeset employee:1
CREATE TABLE employee (
    id int PRIMARY KEY auto_increment NOT NULL,
    name varchar(50) UNIQUE NOT NULL,
    age int,
    create_time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
)

--changeset employee:2
INSERT INTO employee (name, age) VALUES ('John', '33');

--changeset employee:3
INSERT INTO employee (name, age) VALUES ('Mary', '28');


新增資料

liquibase.properties所在目錄命令列輸入liquibase updateliquibase.properties參數執行changelog的script來更新資料庫狀態。

在H2 Shell輸入SELECT * FROM employee可看到新增的兩筆資料。

sql> SELECT * FROM employee;
ID | NAME | AGE | CREATE_TIME
1  | John | 33  | 2022-05-24 16:51:02.361003
2  | Mary | 28  | 2022-05-24 16:51:02.401038
(2 rows, 3 ms)

查看DATABASECHANGELOG資料表可看到多了兩筆紀錄。

sql> SELECT * FROM databasechangelog;
ID | AUTHOR   | FILENAME      | DATEEXECUTED               | ORDEREXECUTED | EXECTYPE | MD5SUM                             | DESCRIPTION | COMMENTS | TAG  | LIQUIBASE | CONTEXTS | LABELS | DEPLOYMENT_ID
1  | employee | changelog.sql | 2022-05-24 16:43:01.221643 | 1             | EXECUTED | 8:28bb5eee313e523c6f750d4473e7bb0b | sql         |          | null | 4.10.0    | null     | null   | 3381780263
2  | employee | changelog.sql | 2022-05-24 16:51:02.397504 | 2             | EXECUTED | 8:b1bb25282c1a42ee4b411b34e605247f | sql         |          | null | 4.10.0    | null     | null   | 3382261332
3  | employee | changelog.sql | 2022-05-24 16:51:02.403449 | 3             | EXECUTED | 8:a0a2e29df3bb3d319f3f3c7d8dd907bd | sql         |          | null | 4.10.0    | null     | null   | 3382261332
(3 rows, 5 ms)


沒有留言:

AdSense