Commit 79ba75a7 authored by Felix Delattre's avatar Felix Delattre
Browse files

Added checks for linters and database migrations

parent 6ce25fa3
Pipeline #29030 passed with stage
in 2 minutes and 9 seconds
image: debian:bullseye-slim
image: debian:bookworm-slim
before_script:
- apt update -y
- apt install -y git make shellcheck golang-go postgresql -y -qq
- export GOPATH="$HOME/go"
- export PATH="$PATH:$GOPATH/bin"
- go get -u github.com/mvdan/sh/cmd/shfmt
- go get -u mvdan.cc/sh/v3/cmd/shfmt
linters:
script:
......
SOURCES=./run-migrations.sh
check: $(SOURCES)
shellcheck --external-sources $^
shfmt -i 2 -d $^
format: $(SOURCES)
shfmt -i 2 -w $^
#!/bin/bash
#
# Deletes current `gde_tiles` database and sets it up anew by running
# all SQL scripts in the migrations folder in ascending order.
# Run: script_name [db_user]
# May provide the database user as an argument (default: postgres).
# Copyright (C) 2021:
# Helmholtz-Zentrum Potsdam Deutsches GeoForschungsZentrum GFZ
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
# General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.
sql_scripts_dir='migrations'
db_name='gde_tiles'
db_user='postgres'
# Test for argument from the command line (now only one argument: database user)
if [[ -z ${1+x} ]]; then
# First parameter is the database user
printf 'Running %s with default database user %s\n' "$(basename "$0")" "${db_user}"
else
db_user=${1}
printf 'Running %s with database user %s\n' "$(basename "$0")" "${db_user}"
fi
# Delete current `gde_tiles` database if exists and create anew
if psql -lqt | cut -d \| -f 1 | grep -qw ${db_name}; then
# Database exists
printf 'Database %s exists, deleting.\n' ${db_name}
dropdb ${db_name}
printf 'Creating %s\n' ${db_name}
createdb ${db_name}
else
createdb ${db_name}
fi
# Run the SQL scripts in the migrations folder in ascending order
for file in "${sql_scripts_dir}"/*.sql; do
f_base=$(basename "${file}" .sql)
printf 'Running script %s\n' "${f_base}"
psql --username "${db_user}" \
--dbname "${db_name}" \
--echo-all --file "${file}"
done
printf 'Done.\n'
exit 0
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment