Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

postgresql-upgrade-v2 #63

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added bin/database_console
Empty file.
16 changes: 10 additions & 6 deletions bin/dump_database
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

source $(dirname $0)/_basics

dump=dumps/$(date +%F-%H-%M).sql.gz
dump="tmp/$(date +%F-%H-%M-%S).sql.gz"

docker compose exec db bash -e -c '
export MYSQL_PWD="$MYSQL_ROOT_PASSWORD"
mysqldump -u root $MYSQL_DATABASE
' | gzip -c > $dump
docker-compose exec postgres bash -e -c "
pg_dump -U \$POSTGRES_USER -d \$POSTGRES_DB | gzip > \"$dump\""

du -h $dump
mkdir -p "dumps"

container_name=$(docker-compose ps -q postgres)

docker cp $container_name:$dump dumps/

dump="dumps/$(date +%F-%H-%M-%S).sql.gz"
16 changes: 10 additions & 6 deletions bin/dump_database_schema
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

source $(dirname $0)/_basics

dump=dumps/$(date +%F-%H-%M)-only-schema.sql
dump="tmp/$(date +%F-%H-%M-%S)-only-schema.sql.gz"

docker compose exec db bash -e -c '
export MYSQL_PWD="$MYSQL_ROOT_PASSWORD"
mysqldump --no-data -u root $MYSQL_DATABASE
' > $dump
docker-compose exec postgres bash -e -c "
pg_dump --schema-only -U \$POSTGRES_USER -d \$POSTGRES_DB | gzip > \"$dump\""

du -h $dump
mkdir -p "dumps"

container_name=$(docker-compose ps -q postgres)

docker cp $container_name:$dump dumps/

dump="dumps/$(date +%F-%H-%M-%S)-only-schema.sql.gz"
2 changes: 1 addition & 1 deletion bin/fresh-restart
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
nameb=$(basename $(pwd))
docker compose kill
docker compose rm -vf
docker volume rm ${nameb}_db ${nameb}_seed ${nameb}_sphinx_indexes
docker volume rm ${nameb}_postgres ${nameb}_seed ${nameb}_sphinx_indexes
docker compose up -d
6 changes: 3 additions & 3 deletions bin/hit/db/console
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env bash
#!/usr/bin/bash

docker compose exec db bash -c '
mysql -uroot -phitobito -D hitobito_development
docker compose exec postgres bash -c '
psql -U hitobito -d hitobito_development
'
19 changes: 16 additions & 3 deletions bin/hit/test/env
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,43 @@ source $HIT_SCRIPT_DIR/test/test_db_setup
source $HIT_SCRIPT_DIR/test/select_test_env

# make sure db is up and running
docker compose up db -d
docker compose up postgres -d

# ask user for environment
HIT_TEST_ENV=$(select_test_env)
TEST_DB_NAME="hitobito_test_$HIT_TEST_ENV"

echo "Starting test env for: $HIT_TEST_ENV"

apply_core_db_migrations() {
docker compose run --rm \
--workdir=/usr/src/app/hitobito \
-e RAILS_ENV=test \
-e RAILS_TEST_DB_NAME=$TEST_DB_NAME \
-e RAILS_DB_NAME=$TEST_DB_NAME \
-e SKIP_INIT=1 \
rails_test \
bundle exec "rake db:migrate"
}

# reset/create mysql db
setup_test_db $TEST_DB_NAME

if [[ "$HIT_TEST_ENV" == "core" ]]; then
PREPARE_RAILS_SPEC_CMD="rails db:schema:load"
PREPARE_RAILS_SPEC_CMD="rake db:migrate"
WORKDIR=/usr/src/app/hitobito
else
PREPARE_RAILS_SPEC_CMD="cp ../hitobito/Gemfile.lock . && rake app:db:schema:load app:wagon:migrate"
PREPARE_RAILS_SPEC_CMD="cp ../hitobito/Gemfile.lock . && rake app:wagon:migrate"
WORKDIR="/usr/src/app/hitobito_$HIT_TEST_ENV"
apply_core_db_migrations
fi

# run test env
docker compose run --rm \
--workdir=$WORKDIR \
-e RAILS_ENV=test \
-e RAILS_TEST_DB_NAME=$TEST_DB_NAME \
-e RAILS_DB_NAME=$TEST_DB_NAME \
-e SKIP_INIT=1 \
rails_test \
bundle exec "$PREPARE_RAILS_SPEC_CMD && bash"
12 changes: 6 additions & 6 deletions bin/hit/test/test_db_setup
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/usr/bin/env bash

mysql_sql_cmd() {
CMD="mysql -uroot -phitobito -e \"$1\""
docker compose exec db bash -c "$CMD"
psql_sql_cmd() {
CMD="psql -U hitobito -d postgres -c \"$1\""
docker compose exec postgres bash -c "$CMD"
}

setup_test_db() {
mysql_sql_cmd "DROP DATABASE IF EXISTS $1;"
mysql_sql_cmd "CREATE DATABASE $1;"
}
psql_sql_cmd "DROP DATABASE IF EXISTS $1 WITH (FORCE);"
psql_sql_cmd "CREATE DATABASE $1;"
}
4 changes: 2 additions & 2 deletions bin/load_database
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ if [ ! -f $dump ]; then
exit 2
fi

zcat $dump | docker compose exec -T db bash -c '
mysql -uroot -p$MYSQL_ROOT_PASSWORD -D $MYSQL_DATABASE
zcat "$dump" | docker-compose exec -T postgres bash -c '
psql -U $POSTGRES_USER -d $POSTGRES_DB
'

echo "Done."
2 changes: 1 addition & 1 deletion bin/reset_database
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ project=$(basename $(pwd))

bin/stop

volume=${project}_db
volume=${project}_postgres
echo "Removing docker volume $volume"
docker volume rm $volume

Expand Down
38 changes: 13 additions & 25 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ services:
tty: true
stdin_open: true
depends_on:
- db
- postgres
- mailcatcher
- cache
- webpack
Expand Down Expand Up @@ -39,7 +39,7 @@ services:
SKIP_SEEDS: 1
SKIP_BUNDLE_INSTALL: 1
depends_on:
- db
- postgres

worker:
<<: *rails
Expand All @@ -51,7 +51,7 @@ services:
SKIP_BUNDLE_INSTALL: 1
depends_on:
- rails
- db
- postgres
- mailcatcher
- cache

Expand All @@ -72,36 +72,24 @@ services:
image: macbre/sphinxsearch:3.1.1
command: sphinx-start
depends_on:
- db
- postgres
volumes:
- ./docker/sphinx.conf:/opt/sphinx/conf/sphinx.conf:ro
- ./docker/sphinx-start:/usr/local/bin/sphinx-start:ro
- ./docker/sphinx-indexer:/usr/local/bin/sphinx-indexer:ro
- sphinx_indexes:/opt/sphinx/indexes
profiles: [sphinx]

db:
image: mysql:5.7
platform: linux/amd64
command:
- --sort_buffer_size=2M
- --character-set-server=utf8mb4
- --collation-server=utf8mb4_general_ci
- --innodb-lock-wait-timeout=2
- --max_allowed_packet=3250585600
env_file: ./docker/mysql.env
postgres:
image: postgres:16
env_file: ./docker/postgres.env
ports:
- "33066:3306"
ulimits:
nproc: 65535
nofile:
soft: 20000
hard: 40000
- "5432:5432"
volumes:
- ./docker/mysql-setup.sql:/docker-entrypoint-initdb.d/mysql-setup.sql:ro
- ./docker/test-setup.sql:/docker-entrypoint-initdb.d/test-setup.sql:ro
- db:/var/lib/mysql

- ./docker/postgresql-setup.sql:/docker-entrypoint-initdb.d/postgresql-setup.sql:ro
- ./docker/test-setup-postgresql.sql:/docker-entrypoint-initdb.d/test-setup-postgresql.sql:ro
- postgres:/var/lib/postgresql/data
webpack:
build:
context: ./docker
Expand All @@ -122,7 +110,7 @@ services:
- hitobito_yarn_cache:/home/developer/.cache/yarn

volumes:
db:
postgres:
seed:
hitobito_bundle:
external: true
Expand Down
3 changes: 3 additions & 0 deletions docker/postgres.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
POSTGRES_DB=hitobito_development
POSTGRES_USER=hitobito
POSTGRES_PASSWORD=hitobito
7 changes: 7 additions & 0 deletions docker/postgresql-setup.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE DATABASE hitobito_test
WITH TEMPLATE = template0
ENCODING = 'UTF8'
LC_COLLATE = 'en_US.UTF-8'
LC_CTYPE = 'en_US.UTF-8';

GRANT ALL PRIVILEGES ON DATABASE hitobito_test TO hitobito;
2 changes: 1 addition & 1 deletion docker/rails.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ FROM ruby:${RUBY_VERSION} AS build
USER root

ENV RAILS_ENV=development
ENV RAILS_DB_ADAPTER=mysql2
ENV RAILS_DB_ADAPTER=postgresql
ENV BUNDLE_PATH=/opt/bundle

WORKDIR /usr/src/app/hitobito
Expand Down
8 changes: 4 additions & 4 deletions docker/rails.env
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
RAILS_DB_ADAPTER=mysql2
RAILS_DB_HOST=db
RAILS_DB_PORT=3306
RAILS_DB_USERNAME=root
RAILS_DB_ADAPTER=postgresql
RAILS_DB_HOST=postgres
RAILS_DB_PORT=5432
RAILS_DB_USERNAME=hitobito
RAILS_DB_PASSWORD=hitobito
RAILS_DB_NAME=hitobito_development
MEMCACHE_SERVERS=cache
Expand Down
5 changes: 5 additions & 0 deletions docker/test-setup-postgresql.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE DATABASE hitobito_test_core
WITH TEMPLATE = template0
ENCODING = 'UTF8'
LC_COLLATE = 'en_US.UTF-8'
LC_CTYPE = 'en_US.UTF-8';