diff options
| author | Julie Qiu <julie@golang.org> | 2020-05-21 14:47:12 -0400 |
|---|---|---|
| committer | Julie Qiu <julieqiu@google.com> | 2020-05-21 19:10:19 +0000 |
| commit | 947bb6d235a20ae00d89243aab2b4a2c97352801 (patch) | |
| tree | bf2f38e1473f7097ce4e4aba04ee038eaee2b628 /devtools | |
| parent | daf67561f61f05eb4bc8a5c31061198ec507c3e0 (diff) | |
| download | go-x-pkgsite-947bb6d235a20ae00d89243aab2b4a2c97352801.tar.xz | |
devtools: renamed from scripts
Change-Id: I1bf0f8a3297b2a83bb6515db25cb04e1b67220d0
Reviewed-on: https://team-review.git.corp.google.com/c/golang/discovery/+/752611
CI-Result: Cloud Build <devtools-proctor-result-processor@system.gserviceaccount.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
Diffstat (limited to 'devtools')
| -rwxr-xr-x | devtools/create_local_db.sh | 15 | ||||
| -rwxr-xr-x | devtools/create_migration.sh | 19 | ||||
| -rwxr-xr-x | devtools/drop_test_dbs.sh | 17 | ||||
| -rw-r--r-- | devtools/lib.sh | 43 | ||||
| -rwxr-xr-x | devtools/migrate_db.sh | 28 |
5 files changed, 122 insertions, 0 deletions
diff --git a/devtools/create_local_db.sh b/devtools/create_local_db.sh new file mode 100755 index 00000000..d64e09d5 --- /dev/null +++ b/devtools/create_local_db.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env -S bash -e + +# Copyright 2019 The Go Authors. All rights reserved. +# Use of this source code is governed by a BSD-style +# license that can be found in the LICENSE file. + +# Script for creating a new database locally. + +source scripts/lib.sh || { echo "Are you at repo root?"; exit 1; } + +echo "CREATE DATABASE \"discovery-db\" \ + OWNER = postgres \ + TEMPLATE=template0 \ + LC_COLLATE = 'C' \ + LC_CTYPE = 'C';" | psql 'host=127.0.0.1 sslmode=disable user=postgres' diff --git a/devtools/create_migration.sh b/devtools/create_migration.sh new file mode 100755 index 00000000..bf2e57be --- /dev/null +++ b/devtools/create_migration.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env -S bash -e + +# Copyright 2019 The Go Authors. All rights reserved. +# Use of this source code is governed by a BSD-style +# license that can be found in the LICENSE file. + +# Script for creating a new migration file. + +migrate create -ext sql -dir migrations -seq $1 +HEADER="-- Copyright 2020 The Go Authors. All rights reserved. +-- Use of this source code is governed by a BSD-style +-- license that can be found in the LICENSE file. + +BEGIN; + +-- Write your migration here. + +END;" +for f in $(ls migrations | tail -n 2); do echo "$HEADER" >> "migrations/$f"; done diff --git a/devtools/drop_test_dbs.sh b/devtools/drop_test_dbs.sh new file mode 100755 index 00000000..84696b95 --- /dev/null +++ b/devtools/drop_test_dbs.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env -S bash -e + +# Copyright 2019 The Go Authors. All rights reserved. +# Use of this source code is governed by a BSD-style +# license that can be found in the LICENSE file. + +# Drop all test databases, when migrations are beyond repair. + +run_pg() { + PGPASSWORD="${GO_DISCOVERY_DATABASE_TEST_PASSWORD}" \ + psql -U postgres -h localhost -c "$1" +} + +run_pg "DROP DATABASE discovery_frontend_test;" +run_pg "DROP DATABASE discovery_integration_test;" +run_pg "DROP DATABASE discovery_postgres_test;" +run_pg "DROP DATABASE discovery_worker_test;" diff --git a/devtools/lib.sh b/devtools/lib.sh new file mode 100644 index 00000000..3fee0582 --- /dev/null +++ b/devtools/lib.sh @@ -0,0 +1,43 @@ +# Copyright 2019 The Go Authors. All rights reserved. +# Use of this source code is governed by a BSD-style +# license that can be found in the LICENSE file. + +# Library of useful bash functions and variables. + +if [ -t 1 ] && which tput >/dev/null 2>&1; then + RED="$(tput setaf 1)" + GREEN="$(tput setaf 2)" + YELLOW="$(tput setaf 3)" + NORMAL="$(tput sgr0)" +else + RED="" + GREEN="" + YELLOW="" + NORMAL="" +fi + +EXIT_CODE=0 + +info() { echo -e "${GREEN}$@${NORMAL}" 1>&2; } +warn() { echo -e "${YELLOW}$@${NORMAL}" 1>&2; } +err() { echo -e "${RED}$@${NORMAL}" 1>&2; EXIT_CODE=1; } + +die() { + err $@ + exit 1 +} + +# runcmd prints an info log describing the command that is about to be run, and +# then runs it. It sets EXIT_CODE to non-zero if the command fails, but does not exit +# the script. +runcmd() { + msg="$@" + # Truncate command logging for narrow terminals. + # Account for the 2 characters of '$ '. + maxwidth=$(( $(tput cols) - 2 )) + if [[ ${#msg} -gt $maxwidth ]]; then + msg="${msg::$(( maxwidth - 3 ))}..." + fi + info "\$ $msg" + $@ || err "command failed" +} diff --git a/devtools/migrate_db.sh b/devtools/migrate_db.sh new file mode 100755 index 00000000..03a5b5bc --- /dev/null +++ b/devtools/migrate_db.sh @@ -0,0 +1,28 @@ +#!/usr/bin/env -S bash -e + +# Copyright 2019 The Go Authors. All rights reserved. +# Use of this source code is governed by a BSD-style +# license that can be found in the LICENSE file. + +source scripts/lib.sh || { echo "Are you at repo root?"; exit 1; } + +usage() { + cat <<EOUSAGE +Usage: $0 [up|down|force|version] {#}" +EOUSAGE +} + +# Redirect stderr to stdout because migrate outputs to stderr, and we want +# to be able to use ordinary output redirection. +case "$1" in + up|down|force|version) + migrate \ + -source file:migrations \ + -database "postgres://postgres@localhost:5432/discovery-db?sslmode=disable" \ + "$@" 2>&1 + ;; + *) + usage + exit 1 + ;; +esac |
