Skip to content

Commit c53ecd4

Browse files
update to include code analysis and drift report artifact production
1 parent 0bef5b4 commit c53ecd4

5 files changed

Lines changed: 903 additions & 2 deletions

File tree

Gitlab-Templatized/.gitlab/ci/flyway.yml

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@
4242
FLYWAY_BASELINE_ON_MIGRATE: "true"
4343
FLYWAY_MIXED: "false"
4444
FLYWAY_OUT_OF_ORDER: "false"
45+
FLYWAY_CLEAN_DISABLED: "false"
46+
FLYWAY_ENV_NATIVE_SQLFLUFF: "true"
47+
# Templates repo — used to fetch rules for code analysis
48+
TEMPLATE_PROJECT: "root/templatized-with-parser"
49+
TEMPLATE_REF: "main"
50+
CHECK_RULES_LOCATION: "/tmp/templates/rules"
51+
REPORT_DIR: "reports"
4552
before_script:
4653
- "echo =========================================="
4754
- "echo Flyway Database Migration"
@@ -51,6 +58,9 @@
5158
- "echo Target user: ${FLYWAY_USER}"
5259
- "echo SQL locations: ${FLYWAY_LOCATIONS}"
5360
- "echo =========================================="
61+
- "apk add --no-cache git >/dev/null 2>&1 || apt-get update -qq && apt-get install -y -qq git >/dev/null 2>&1 || true"
62+
- "git clone --depth 1 --branch ${TEMPLATE_REF} http://gitlab-ci-token:${CI_JOB_TOKEN}@172.30.0.2/${TEMPLATE_PROJECT}.git /tmp/templates 2>/dev/null || true"
63+
- "mkdir -p ${REPORT_DIR}"
5464

5565
# Job template: Validate SQL migrations
5666
.flyway_validate:
@@ -65,13 +75,90 @@
6575
script:
6676
- flyway -email="${FLYWAY_EMAIL}" -token="${FLYWAY_TOKEN}" info
6777

68-
# Job template: Run migrations
78+
# Job template: Run migrations (with code analysis report + snapshot)
6979
.flyway_migrate:
7080
extends: .flyway_base
7181
script:
7282
- flyway -email="${FLYWAY_EMAIL}" -token="${FLYWAY_TOKEN}" info
73-
- flyway -email="${FLYWAY_EMAIL}" -token="${FLYWAY_TOKEN}" migrate
83+
- >
84+
flyway -email="${FLYWAY_EMAIL}" -token="${FLYWAY_TOKEN}"
85+
info check -code
86+
-check.rulesLocation="${CHECK_RULES_LOCATION}"
87+
-reportFilename="${REPORT_DIR}/${CI_JOB_NAME}-${CI_PIPELINE_ID}-code-analysis.html"
88+
|| true
89+
- flyway -email="${FLYWAY_EMAIL}" -token="${FLYWAY_TOKEN}" migrate -saveSnapshot=true
7490
- flyway -email="${FLYWAY_EMAIL}" -token="${FLYWAY_TOKEN}" info
91+
artifacts:
92+
expose_as: "Flyway Reports"
93+
paths:
94+
- reports/
95+
when: always
96+
expire_in: 30 days
97+
98+
# Job template: Drift detection (compare snapshot against live DB)
99+
.flyway_drift:
100+
extends: .flyway_base
101+
script:
102+
- "echo '--- Drift Detection ---'"
103+
- |
104+
if [ -f flyway-snapshot.json ]; then
105+
flyway -email="${FLYWAY_EMAIL}" -token="${FLYWAY_TOKEN}" diff \
106+
-diff.source=snapshot:flyway-snapshot.json \
107+
-diff.target=env:target
108+
else
109+
echo 'No snapshot file found — skipping drift detection'
110+
fi
111+
allow_failure: true
112+
113+
# Job template: Code analysis (SQLFluff rules via Flyway check)
114+
.flyway_check:
115+
extends: .flyway_base
116+
script:
117+
- "echo '--- Code Analysis Report ---'"
118+
- >
119+
flyway -email="${FLYWAY_EMAIL}" -token="${FLYWAY_TOKEN}"
120+
info check -code
121+
-check.rulesLocation="${CHECK_RULES_LOCATION}"
122+
-reportFilename="${REPORT_DIR}/${CI_JOB_NAME}-${CI_PIPELINE_ID}-code-analysis.html"
123+
artifacts:
124+
expose_as: "Flyway Reports"
125+
paths:
126+
- reports/
127+
when: always
128+
expire_in: 30 days
129+
allow_failure: true
130+
131+
# Job template: Migrate + drift detection + code analysis (all-in-one)
132+
.flyway_migrate_full:
133+
extends: .flyway_base
134+
script:
135+
- "echo '--- Code Analysis Report ---'"
136+
- >
137+
flyway -email="${FLYWAY_EMAIL}" -token="${FLYWAY_TOKEN}"
138+
info check -code
139+
-check.rulesLocation="${CHECK_RULES_LOCATION}"
140+
-reportFilename="${REPORT_DIR}/${CI_JOB_NAME}-${CI_PIPELINE_ID}-code-analysis.html"
141+
- "echo '--- Pre-Migration Info ---'"
142+
- flyway -email="${FLYWAY_EMAIL}" -token="${FLYWAY_TOKEN}" info
143+
- "echo '--- Migrate with Snapshot ---'"
144+
- flyway -email="${FLYWAY_EMAIL}" -token="${FLYWAY_TOKEN}" migrate -saveSnapshot=true
145+
- "echo '--- Post-Migration Info ---'"
146+
- flyway -email="${FLYWAY_EMAIL}" -token="${FLYWAY_TOKEN}" info
147+
- "echo '--- Drift Detection ---'"
148+
- |
149+
if [ -f flyway-snapshot.json ]; then
150+
flyway -email="${FLYWAY_EMAIL}" -token="${FLYWAY_TOKEN}" diff \
151+
-diff.source=snapshot:flyway-snapshot.json \
152+
-diff.target=env:target || true
153+
else
154+
echo 'No snapshot file found — skipping drift detection'
155+
fi
156+
artifacts:
157+
expose_as: "Flyway Reports"
158+
paths:
159+
- reports/
160+
when: always
161+
expire_in: 30 days
75162

76163
# Job template: Repair migration history
77164
.flyway_repair:

0 commit comments

Comments
 (0)