Skip to content

Commit dbd597c

Browse files
Liquibase
1 parent cb8fd25 commit dbd597c

File tree

9 files changed

+391
-11
lines changed

9 files changed

+391
-11
lines changed

LIQUIBASE.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Liquibase
2+
3+
<div>
4+
<img src="https://github.com/devicons/devicon/blob/master/icons/liquibase/liquibase-original-wordmark.svg" width="40" height="40"/>&nbsp;
5+
</div>
6+
7+
Liquibase - это инструмент для управления и отслеживания изменений в структуре базы данных. Он позволяет разработчикам автоматизировать процесс развертывания и управления изменениями в базе данных.
8+
9+
С помощью Liquibase разработчики могут создавать и обновлять схему базы данных, добавлять новые таблицы, столбцы, индексы, ключи, а также выполнять другие операции.
10+
11+
Liquibase использует файлы XML, YAML, JSON или SQL для описания изменений в базе данных. Эти файлы содержат информацию о том, какие изменения нужно применить, порядок их применения, условия выполнения и отката изменений.
12+
13+
Одним из основных преимуществ Liquibase является возможность отслеживания и контроля версий базы данных. Он позволяет разработчикам создавать файлы изменений, хранить их в репозитории версий и автоматически применять их к базе данных при необходимости.
14+
15+
16+
[Документация Liquibase для PostgreSQL](https://docs.liquibase.com/start/tutorials/postgresql/postgresql.html)
17+
18+
19+
### Установка Liquibase в Ubuntu
20+
21+
1. Установка Java:
22+
23+
`java -version` Проверка наличия на сервере Java. Если нет, о следующий шаг - установка Java
24+
25+
`sudo apt install default-jre`
26+
27+
28+
2. Импорт ключа Liquibase GPG и добавление репозитория Liquibase в apt:
29+
30+
`sudo wget -O- https://repo.liquibase.com/liquibase.asc | gpg --dearmor > liquibase-keyring.gpg && \
31+
cat liquibase-keyring.gpg | sudo tee /usr/share/keyrings/liquibase-keyring.gpg > /dev/null && \
32+
echo 'deb [arch=amd64 signed-by=/usr/share/keyrings/liquibase-keyring.gpg] https://repo.liquibase.com stable main' | sudo tee /etc/apt/sources.list.d/liquibase.list`
33+
34+
35+
3. Установка Liquibase:
36+
37+
`sudo apt-get install liquibase`
38+
39+
`sudo apt-get install liquibase=4.29.2` Установка определенной версии
40+
41+
4. Проверка версии:
42+
43+
`liquibase --version`
44+
45+
46+
<b>Обновление Liquibase</b>
47+
48+
`sudo apt-get upgrade liquibase`
49+
50+
51+
<b>Удаление Liquibase</b>
52+
53+
`sudo apt-get remove liquibase`
54+
55+
56+
### Команды Liquibase
57+
58+
`liquibase init project` Инициализация проекта (команда init доступна только в версиях 4.7.0 и выше)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--liquibase formatted sql
2+
3+
--changeset your.name:1 labels:example-label context:example-context
4+
--comment: example comment
5+
create table person (
6+
id int primary key auto_increment not null,
7+
name varchar(50) not null,
8+
address1 varchar(50),
9+
address2 varchar(50),
10+
city varchar(30)
11+
)
12+
--rollback DROP TABLE person;
13+
14+
--changeset your.name:2 labels:example-label context:example-context
15+
--comment: example comment
16+
create table company (
17+
id int primary key auto_increment not null,
18+
name varchar(50) not null,
19+
address1 varchar(50),
20+
address2 varchar(50),
21+
city varchar(30)
22+
)
23+
--rollback DROP TABLE company;
24+
25+
--changeset other.dev:3 labels:example-label context:example-context
26+
--comment: example comment
27+
alter table person add column country varchar(2)
28+
--rollback ALTER TABLE person DROP COLUMN country;
29+
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
########## LIQUIBASE FLOW FILE ##########
2+
########## learn more http://docs.liquibase.com/flow ##########
3+
4+
## NOTE: This is an advanced example flowfile, compared to the other sample at examples/liquibase.flowfile.yaml
5+
#### HOW TO USE THIS FILE:
6+
#### example for CLI: liquibase flow --flow-file=liquibase.advanced.flowfile.yaml
7+
#### example for ENV Var: LIQUIBASE_FLOW_FLOW_FILE=liquibase.advanced.flowfile.yaml
8+
9+
## Advanced options show in this file include:
10+
#### non-default name of 'liquibase.advanced.flowfile.yaml' (use by setting flowfile property to this name)
11+
#### use of 'include' to inject namespaced yaml files of key: val variables
12+
#### use of globalVariables and stageVariables
13+
#### use of globalArgs and cmdArgs
14+
#### use of property substitution
15+
#### use of a nested flowfile (in this case in the endStage, but could be elsewhere)
16+
#### use of if: conditional which allows a -type: shell or -type: liquibase command to run
17+
###### In the example below, we set an environment variable LIQUIBASE_CURRENT_TARGET, such as 'export LIQUIBASE_CURRENT_TARGET=dev'
18+
###### This could be determined dynamically, of course, from the build tools, bu tthis is simpler for this example "if:" conditional
19+
#### use of shell commands in a -type: shell block.
20+
###### command: bash -c "the shell command || and its chained commands && go in the quotes"
21+
########
22+
#### POTENTIAL use of environment variables:
23+
###### DATETIME STAMP
24+
######## In this file, you could replace ${FLOWVARS.THISDATE} with an env var, such as ${LIQUIBASE_THISDATE} set via .bash_profile
25+
######## for example 'export LIQUIBASE_THISDATE=$( date +'%Y-%m-%dT%H-%M-%S' )'
26+
27+
28+
## Bring in and namespace an external file with yaml 'key: val' pairs for use in this file
29+
## The variables will be used as ${namespace.variablename}, seen in this example as ${FLOWVARS.PROJNAME}
30+
include:
31+
FLOWVARS: liquibase.flowvariables.yaml
32+
33+
34+
35+
## Set up some global variables for property substitution in ANY stage
36+
globalVariables:
37+
DIRNAME: "./${FLOWVARS.PROJNAME}_${FLOWVARS.THISDATE}"
38+
STATUSFILE: "status.txt"
39+
UPDATELOG: "update.log"
40+
HISTORYFILE: "history.txt"
41+
42+
43+
## Start of the stages.
44+
stages:
45+
46+
## A prep stage. There can be more than one stage if desired.
47+
stage-prep:
48+
49+
actions:
50+
51+
- type: shell
52+
command: bash -c "mkdir -p ${DIRNAME}"
53+
54+
55+
## Another stage.
56+
stage-dowork:
57+
58+
## set up vars for property substitution in THIS stage only
59+
stageVariables:
60+
61+
VERBOSESTATE: TRUE
62+
63+
64+
actions:
65+
#
66+
# Do a validate command
67+
#
68+
- type: liquibase
69+
command: validate
70+
71+
#
72+
# Tell me what is pending a deployment
73+
#
74+
- type: shell
75+
command: bash -c "liquibase --show-banner false --outputfile ./${DIRNAME}/${STATUSFILE} status --verbose ${VERBOSESTATE}"
76+
77+
# This is the structured way to setup a liquibase command, if you dont want to run it as one 'bash -c' command
78+
#- type: liquibase
79+
# command: status
80+
# globalArgs:
81+
# outputfile: "${DIRNAME}/${STATUSFILE}"
82+
# showbanner: false
83+
# cmdArgs: {verbose: "${VERBOSESTATE}"
84+
85+
86+
87+
88+
#
89+
# And then save a version in detail, if env var LIQUIBASE_FILE_OUTPUT == 1
90+
#
91+
- type: shell
92+
command: bash -c "echo 'LIQUIBASE_ env vars ' && env | grep 'LIQUIBASE_' "
93+
94+
- type: liquibase
95+
## if this var LIQUIBASE_CURRENT_TARGET is "dev", then the updatesql will run
96+
if: "${LIQUIBASE_CURRENT_TARGET} == dev"
97+
command: updatesql
98+
globalArgs: {outputfile: "${DIRNAME}/${UPDATELOG}"}
99+
100+
- type: shell
101+
## if this var LIQUIBASE_CURRENT_TARGET is not "dev", then the message will be displayed
102+
if: "${LIQUIBASE_CURRENT_TARGET} != dev"
103+
command: echo "No output files created. Set env var LIQUIBASE_CURRENT_TARGET to dev to trigger file creation."
104+
105+
106+
107+
#
108+
# Policy Checks for changelog
109+
#
110+
- type: liquibase
111+
command: checks run
112+
cmdArgs: {checks-scope: changelog}
113+
114+
#
115+
# Run update
116+
#
117+
- type: liquibase
118+
command: update
119+
120+
121+
#
122+
# Policy Checks for database
123+
#
124+
- type: liquibase
125+
command: checks run
126+
cmdArgs: {checks-scope: database}
127+
128+
129+
#
130+
# Create a history file
131+
#
132+
- type: liquibase
133+
command: history
134+
globalArgs: {outputfile: "${DIRNAME}/${HISTORYFILE}"}
135+
136+
137+
138+
## The endStage ALWAYS RUNS.
139+
## So put actions here which you desire to perform whether previous stages' actions succeed or fail.
140+
## If you do not want any actions to ALWAYS RUN, simply delete the endStage from your flow file.
141+
142+
endStage:
143+
actions:
144+
- type: liquibase
145+
## Notice this is a flow command in a flow file, and it called a 'nested' flowfile, which in this case lives in the same dir, but could be elsewhere
146+
command: flow
147+
cmdArgs: {flowfile: liquibase.endstage.flow}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## This is a Liquibase checks packages formatted yaml file.
2+
## It contains just one or more 'checksPackages' objects
3+
## with one or more named checks-settings-files.
4+
## These checks-settings-files listed in a package are processed sequentially
5+
## during 'liquibase checks run' or 'liquibase checks show' commands
6+
## which specify this file as the checks-settings-file property.
7+
## Learn more at https://docs.liquibase.com/policy-checks
8+
9+
## Uncomment and change names with your checks-settings-files
10+
#checksPackages:
11+
# - name: "my-checks-files.pkg"
12+
# files:
13+
# - "./liquibase.checks-settings.conf"
14+
# - "./liquibase.more.checks-settings.yaml"
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
########## LIQUIBASE FLOW FILE ##########
2+
########## learn more http://docs.liquibase.com/flow ##########
3+
4+
## NOTE: This example flowfile is called from the examples/liquibase.advanced.flowfile.yaml file
5+
## While it could be run on its own, this file is designed to show that flow-files can be decomposed
6+
## into separate files as makes sense for your use cases.
7+
8+
stages:
9+
10+
cleanuptheDB:
11+
12+
actions:
13+
14+
#
15+
# Clear out the database
16+
#
17+
- type: liquibase
18+
command: dropAll
19+
20+
#
21+
# Check that database is empty by seeing what is ready to be deployed
22+
#
23+
- type: liquibase
24+
command: status
25+
cmdArgs: {verbose: TRUE}
26+
27+
28+
## The endStage ALWAYS RUNS.
29+
## So put actions here which you desire to perform whether previous stages' actions succeed or fail.
30+
## If you do not want any actions to ALWAYS RUN, simply delete the endStage from your flow file,
31+
## as it has been deleted here in this liquibase.endStage.flow file.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
########## LIQUIBASE FLOWFILE ##########
2+
########## learn more http://docs.liquibase.com/flow ##########
3+
4+
## Note: Any command which fails in any stage below result in the command stopping, and endStage being run.
5+
## A flow file can have one or more stages, each with multiple "actions",
6+
## or your flow file can have multiple stages with fewer actions in each stage.
7+
stages:
8+
9+
10+
## The first stage of actions.
11+
Default:
12+
13+
actions:
14+
#
15+
# Policy Checks for changelog
16+
#
17+
- type: liquibase
18+
command: checks run
19+
cmdArgs: {checks-scope: changelog}
20+
#
21+
# Run the update
22+
#
23+
- type: liquibase
24+
command: update
25+
26+
#
27+
# Policy Checks for database
28+
#
29+
- type: liquibase
30+
command: checks run
31+
cmdArgs: {checks-scope: database}
32+
33+
34+
## The endStage ALWAYS RUNS.
35+
## So put actions here which you desire to perform whether previous stages' actions succeed or fail.
36+
## If you do not want any actions to ALWAYS RUN, simply delete the endStage from your flow file.
37+
38+
endStage:
39+
actions:
40+
- type: liquibase
41+
command: history
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
########## LIQUIBASE FLOWFILE ##########
2+
########## learn more http://docs.liquibase.com/flow ##########
3+
4+
## NOTE: This example yaml file of key:value variables is injected into examples/liquibase.advanced.flow file
5+
## using the "include" ability. It will be given the namespace "DATES" but could be given any namespace.
6+
7+
PROJNAME: "MyFlowProj"
8+
THISDATE: "2022-11-28T15-00-20"
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#### _ _ _ _
2+
## | | (_) (_) |
3+
## | | _ __ _ _ _ _| |__ __ _ ___ ___
4+
## | | | |/ _` | | | | | '_ \ / _` / __|/ _ \
5+
## | |___| | (_| | |_| | | |_) | (_| \__ \ __/
6+
## \_____/_|\__, |\__,_|_|_.__/ \__,_|___/\___|
7+
## | |
8+
## |_|
9+
##
10+
## The liquibase.properties file stores properties which do not change often,
11+
## such as database connection information. Properties stored here save time
12+
## and reduce risk of mistyped command line arguments.
13+
## Learn more: https://docs.liquibase.com/concepts/connections/creating-config-properties.html
14+
####
15+
####
16+
## Note about relative and absolute paths:
17+
## The liquibase.properties file requires paths for some properties.
18+
## The classpath is the path/to/resources (ex. src/main/resources).
19+
## The changeLogFile path is relative to the classpath.
20+
## The url H2 example below is relative to 'pwd' resource.
21+
####
22+
# Enter the path for your changelog file.
23+
changeLogFile=example-changelog.sql
24+
25+
#### Enter the Target database 'url' information ####
26+
liquibase.command.url=jdbc:h2:tcp://localhost:9090/mem:dev
27+
28+
# Enter the username for your Target database.
29+
liquibase.command.username: dbuser
30+
31+
# Enter the password for your Target database.
32+
liquibase.command.password: letmein
33+
34+
#### Enter the Source Database 'referenceUrl' information ####
35+
## The source database is the baseline or reference against which your target database is compared for diff/diffchangelog commands.
36+
37+
# Enter URL for the source database
38+
liquibase.command.referenceUrl: jdbc:h2:tcp://localhost:9090/mem:integration
39+
40+
# Enter the username for your source database
41+
liquibase.command.referenceUsername: dbuser
42+
43+
# Enter the password for your source database
44+
liquibase.command.referencePassword: letmein
45+
46+
# Logging Configuration
47+
# logLevel controls the amount of logging information generated. If not set, the default logLevel is INFO.
48+
# Valid values, from least amount of logging to most, are:
49+
# OFF, ERROR, WARN, INFO, DEBUG, TRACE, ALL
50+
# If you are having problems, setting the logLevel to DEBUG and re-running the command can be helpful.
51+
# logLevel: DEBUG
52+
53+
# The logFile property controls where logging messages are sent. If this is not set, then logging messages are
54+
# displayed on the console. If this is set, then messages will be sent to a file with the given name.
55+
# logFile: liquibase.log
56+
57+
#### Liquibase Pro Key Information ####
58+
# Learn more, contact support, or get or renew a Pro Key at https://www.liquibase.com/trial
59+
# liquibase.licenseKey:
60+
61+
## Get documentation at docs.liquibase.com ##
62+
## Get certified courses at learn.liquibase.com ##
63+
## Get support at liquibase.com/support ##

0 commit comments

Comments
 (0)