forked from 4GeeksAcademy/flask-rest-hello
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMakefile
72 lines (61 loc) · 2.17 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Makefile
ALEMBIC=alembic -c migrations/alembic.ini
# Include environment variables if .env file exists
ifneq (,$(wildcard $(ENV)))
include $(ENV)
export $(shell sed 's/=.*//' $(ENV))
endif
install:
pipenv install --deploy --ignore-pipfile
# start dev server on production
web:
pipenv run uvicorn main:app --chdir ./src/
# Apply all migrations
.PHONY: upgrade
upgrade:
$(ALEMBIC) upgrade head
# Create a new migration (revision message is required)
.PHONY: migrate
migrate:
@read -p "Enter migration message: " msg; \
$(ALEMBIC) revision --autogenerate -m "$$msg"
# Downgrade the last migration
.PHONY: downgrade
downgrade:
$(ALEMBIC) downgrade -1
# Remove an restart de database
.PHONY: init
init:
$(ALEMBIC) init migrations
@cp .devcontainer/migrations/alembic.ini migrations/alembic.ini
@cp .devcontainer/migrations/env.py migrations/env.py
@echo "Copied custom alembic.ini and env.py to migrations directory."
@echo "Database and migrations initialized successfully."
# Remove an restart de database
.PHONY: reset_db
reset_db:
@echo "Restarting database and initializing migrations..."
@if [ -d "migrations" ]; then \
rm -rf migrations; \
echo "Removed existing migrations directory."; \
fi
@$(ALEMBIC) init migrations
@echo "Reinitialized alembic in migrations directory."
@if [ ! -f ".devcontainer/migrations/alembic.ini" ] || [ ! -f ".devcontainer/migrations/env.py" ]; then \
echo "Error: .devcontainer/migrations/alembic.ini or .devcontainer/migrations/env.py not found."; \
exit 1; \
fi
@cp .devcontainer/migrations/alembic.ini migrations/alembic.ini
@cp .devcontainer/migrations/env.py migrations/env.py
@echo "Copied custom alembic.ini and env.py to migrations directory."
@echo "Database and migrations initialized successfully."
@if psql -lqt -h localhost -U gitpod | cut -d \| -f 1 | grep -qw example; then \
dropdb -h localhost -U gitpod example; \
echo "Dropped existing database 'example'."; \
else \
echo "Database 'example' does not exist."; \
fi
createdb -h localhost -U gitpod example || true && \
psql -h localhost example -U gitpod -c 'CREATE EXTENSION unaccent;' || true && \
$(ALEMBIC) revision --autogenerate && \
$(ALEMBIC) upgrade head