Skip to content

Commit fca2efa

Browse files
authored
move parts of our db initialization into db service (#15549)
* move parts of our db initialization into db service * simplify dev experience further by complicating Makefile * remove orphans on purge * sush encoding warning on make tests * give more time in bin/tests for db-initialization * pause web and workers during resetdb
1 parent 223de96 commit fca2efa

File tree

6 files changed

+93
-24
lines changed

6 files changed

+93
-24
lines changed

Makefile

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ build:
5555
docker system prune -f --filter "label=com.docker.compose.project=warehouse"
5656

5757
serve: .state/docker-build
58+
$(MAKE) .state/db-populated
59+
$(MAKE) .state/search-indexed
5860
docker compose up --remove-orphans
5961

6062
debug: .state/docker-build-base
@@ -97,17 +99,30 @@ translations: .state/docker-build-base
9799
requirements/%.txt: requirements/%.in
98100
docker compose run --rm base bin/pip-compile --generate-hashes --output-file=$@ $<
99101

100-
initdb: .state/docker-build-base
101-
docker compose run --rm web psql -h db -d postgres -U postgres -c "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname ='warehouse';"
102-
docker compose run --rm web psql -h db -d postgres -U postgres -c "DROP DATABASE IF EXISTS warehouse"
103-
docker compose run --rm web psql -h db -d postgres -U postgres -c "CREATE DATABASE warehouse ENCODING 'UTF8'"
104-
docker compose run --rm web psql -h db -d postgres -U postgres -c "DROP DATABASE IF EXISTS rstuf"
105-
docker compose run --rm web psql -h db -d postgres -U postgres -c "CREATE DATABASE rstuf ENCODING 'UTF8'"
106-
docker compose run --rm web bash -c "xz -d -f -k dev/$(DB).sql.xz --stdout | psql -h db -d warehouse -U postgres -v ON_ERROR_STOP=1 -1 -f -"
107-
docker compose run --rm web psql -h db -d warehouse -U postgres -c "UPDATE users SET name='Ee Durbin' WHERE username='ewdurbin'"
108-
$(MAKE) runmigrations
102+
resetdb: .state/docker-build-base
103+
docker compose pause web worker
104+
docker compose up -d db
105+
docker compose exec --user postgres db /docker-entrypoint-initdb.d/init-dbs.sh
106+
rm -f .state/db-populated .state/db-migrated
107+
$(MAKE) initdb
108+
docker compose unpause web worker
109+
110+
.state/search-indexed: .state/db-populated
111+
$(MAKE) reindex
112+
mkdir -p .state && touch .state/search-indexed
113+
114+
.state/db-populated: .state/db-migrated
109115
docker compose run --rm web python -m warehouse sponsors populate-db
110116
docker compose run --rm web python -m warehouse classifiers sync
117+
mkdir -p .state && touch .state/db-populated
118+
119+
.state/db-migrated: .state/docker-build-base
120+
docker compose up -d db
121+
docker compose exec db /usr/local/bin/wait-for-db
122+
$(MAKE) runmigrations
123+
mkdir -p .state && touch .state/db-migrated
124+
125+
initdb: .state/docker-build-base .state/db-populated
111126
$(MAKE) reindex
112127

113128
runmigrations: .state/docker-build-base
@@ -126,11 +141,11 @@ clean:
126141
rm -rf dev/*.sql
127142

128143
purge: stop clean
129-
rm -rf .state dev/.coverage* dev/.mypy_cache dev/.pip-cache dev/.pip-tools-cache dev/.pytest_cache
130-
docker compose down -v
144+
rm -rf .state dev/.coverage* dev/.mypy_cache dev/.pip-cache dev/.pip-tools-cache dev/.pytest_cache .state/db-populated .state/db-migrated
145+
docker compose down -v --remove-orphans
131146
docker compose rm --force
132147

133148
stop:
134149
docker compose stop
135150

136-
.PHONY: default build serve initdb shell dbshell tests dev-docs user-docs deps clean purge debug stop compile-pot runmigrations
151+
.PHONY: default build serve resetdb initdb shell dbshell tests dev-docs user-docs deps clean purge debug stop compile-pot runmigrations

bin/tests

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ done
1919

2020
# Test the postgres connection
2121
ATTEMPTS=0
22-
until [ $ATTEMPTS -eq 5 ] || pg_isready -t 10 -h $POSTGRES_HOST; do
22+
until [ $ATTEMPTS -eq 12 ] || pg_isready -t 10 -h $POSTGRES_HOST; do
2323
>&2 echo "Postgres is unavailable, sleeping"
2424
sleep $(( ATTEMPTS++ ))
2525
done
2626

27-
if [ $ATTEMPTS -eq 5 ]; then
27+
if [ $ATTEMPTS -eq 12 ]; then
2828
>&2 echo "Postgres is unavailable, exiting"
2929
exit 1
3030
fi

bin/wait-for-db

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# Click requires us to ensure we have a well configured environment to run
5+
# our click commands. So we'll set our environment to ensure our locale is
6+
# correct.
7+
export LC_ALL="${ENCODING:-en_US.UTF-8}"
8+
export LANG="${ENCODING:-en_US.UTF-8}"
9+
10+
echo -n 'waiting for db to be prepared.'
11+
12+
QUERY="select name from users where username='ewdurbin';"
13+
14+
ATTEMPTS=0
15+
until [ $ATTEMPTS -eq 60 ] || [ "$(psql -U postgres -d warehouse -A -t -c "$QUERY" 2>&1)" == "Ee Durbin" ]; do
16+
>&2 >/dev/null
17+
ATTEMPTS=$((ATTEMPTS+1));
18+
echo -n "."
19+
sleep 1
20+
done
21+
22+
if [ $ATTEMPTS -eq 60 ]; then
23+
>&2 echo ""
24+
echo "db failed to prepare, exiting"
25+
exit 1
26+
fi
27+
28+
echo ""
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
set -e
3+
4+
psql -d "$POSTGRES_DB" -U "$POSTGRES_USER" <<-EOF
5+
SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname ='warehouse';
6+
DROP DATABASE IF EXISTS warehouse;
7+
CREATE DATABASE warehouse ENCODING 'UTF8';
8+
SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname ='rstuf';
9+
DROP DATABASE IF EXISTS rstuf;
10+
CREATE DATABASE rstuf ENCODING 'UTF8';
11+
EOF
12+
13+
xz -d -f -k /example.sql.xz --stdout | psql -d warehouse -U "$POSTGRES_USER" -v ON_ERROR_STOP=1 -1 -f -
14+
15+
psql -d warehouse -U "$POSTGRES_USER" <<-EOF
16+
UPDATE users SET name='Ee Durbin' WHERE username='ewdurbin'
17+
EOF

docker-compose.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ services:
2121
test: ["CMD", "pg_isready", "-U", "postgres", "-d", "postgres"]
2222
interval: 1s
2323
start_period: 10s
24+
volumes:
25+
- ./dev/db/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d:z
26+
- ./dev/example.sql.xz:/example.sql.xz:z
27+
- ./bin/wait-for-db:/usr/local/bin/wait-for-db:z
2428

2529
stripe:
2630
image: stripe/stripe-mock:v0.162.0
@@ -117,6 +121,8 @@ services:
117121
tests:
118122
image: warehouse:docker-compose
119123
pull_policy: never
124+
environment:
125+
ENCODING: "C"
120126
volumes: *base_volumes
121127
depends_on:
122128
db:

docs/dev/development/getting-started.rst

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -209,23 +209,16 @@ Once ``make build`` has finished, run the command:
209209

210210
.. code-block:: console
211211
212-
make initdb
212+
make serve
213213
214214
This command will:
215215

216-
* create a new Postgres database,
217-
* install example data to the Postgres database,
216+
* ensure the db is prepared,
218217
* run migrations,
219218
* load some example data from `Test PyPI`_, and
220219
* index all the data for the search database.
220+
* start up the containers needed to run Warehouse
221221

222-
Once the ``make initdb`` command has finished, you are ready to continue:
223-
224-
.. code-block:: console
225-
226-
make serve
227-
228-
This command starts the containers that run Warehouse on your local machine.
229222
After the initial build process, you will only need this command each time you
230223
want to startup Warehouse locally.
231224

@@ -250,6 +243,16 @@ or that the ``static`` container has finished compiling the static assets:
250243
or maybe something else.
251244

252245

246+
Resetting the development database
247+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
248+
249+
.. code-block:: console
250+
251+
make resetdb
252+
253+
This command will fully reset the development database.
254+
255+
253256
Viewing Warehouse in a browser
254257
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
255258

0 commit comments

Comments
 (0)