Skip to content

Commit

Permalink
Better makefile (#140)
Browse files Browse the repository at this point in the history
* proper alignment in `Makefile`

* use diesel cli arguments in `Makefile`

* fix mistake with diesel cli

* better organization

* user/password consistency in `Makefile`

* add `.PHONY` target

* move `clippy` target

* remove unnecessary environment variables

* fix problem from 4d22be3

* maybe this is better for users/passwords

* fix fuck up again

* better organization and wait for MySQL

* format `README.md`

* format `README.md` Rust code

* update `README.md`

* another probably better approach

* better output

* bold echoes

* update `make doc`

* use variables in `Makefile`

* use `--migration-dir` for Diesel

* add variables for migrations

* add `fang/mysql_migrations/diesel.toml` and use variables for Diesel config files

* add default goal

* add `.env` recipe

* add variables for URLs of DBs

* format `Makefile`

* fix fuck-up in last commit

* setup `Makefile` for parallel execution

* fix workflow

* fix workflow var

* move `make` variables into `.env` file

This commit makes the repository dependent on `.env` being both a valid `.env` file for `dotenvy` to use and being valid to include in the `Makefile`.

* add quotation marks for `Makefile` substitution

* solve race condition in `clean_sqlite`

* fix `README.md`

* stupid problems require stupid solutions

* remove unnecessary clean

* add `sleep`s

* add docker image versions as a `.env` and `Makefile` variable

* latest databases but without latest

* small clarification in `README.md`

---------

Co-authored-by: Dopplerian <[email protected]>
  • Loading branch information
pxp9 and Dopplerian authored Aug 31, 2023
1 parent 2a5aae6 commit 1143285
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 100 deletions.
28 changes: 27 additions & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
DATABASE_URL=postgres://postgres:postgres@localhost/fang
POSTGRES_CONTAINER=postgres
POSTGRES_VERSION=15.4
POSTGRES_DB=fang
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_DIESEL_DIR=fang/postgres_migrations
POSTGRES_MIGRATIONS=${POSTGRES_DIESEL_DIR}/migrations
POSTGRES_CONFIG=${POSTGRES_DIESEL_DIR}/diesel.toml

MYSQL_CONTAINER=mysql
MYSQL_VERSION=8.1
MYSQL_DB=fang
MYSQL_USER=root
MYSQL_PASSWORD=mysql
MYSQL_DIESEL_DIR=fang/mysql_migrations
MYSQL_MIGRATIONS=${MYSQL_DIESEL_DIR}/migrations
MYSQL_CONFIG=${MYSQL_DIESEL_DIR}/diesel.toml

SQLITE_FILE=fang.db
SQLITE_DIESEL_DIR=fang/sqlite_migrations
SQLITE_MIGRATIONS=${SQLITE_DIESEL_DIR}/migrations
SQLITE_CONFIG=${SQLITE_DIESEL_DIR}/diesel.toml

HOST=127.0.0.1
POSTGRES_URL=postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${HOST}/${POSTGRES_DB}
MYSQL_URL=mysql://${MYSQL_USER}:${MYSQL_PASSWORD}@${HOST}/${MYSQL_DB}
DATABASE_URL=${POSTGRES_URL}
3 changes: 3 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ on:
# Check if it works with current dependencies (weekly on Wednesday 2:32 UTC)
- cron: '32 2 * * 3'

env :
DATABASE_URL : postgres://postgres:postgres@localhost/fang

jobs:
test:
name: Test
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
**/target
Cargo.lock
src/schema.rs
docs/content/docs/CHANGELOG.md
docs/content/docs/README.md
fang.db
129 changes: 103 additions & 26 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,46 +1,123 @@
include .env

BOLD='\033[1m'
END_BOLD='\033[0m'

DB_TARGETS=db_postgres db_mysql db_sqlite
WAIT_TARGETS=wait_for_postgres wait_for_mysql wait_for_sqlite
DIESEL_TARGETS=diesel_postgres diesel_mysql diesel_sqlite
CLEAN_TARGETS=clean_postgres clean_mysql clean_sqlite
STOP_TARGETS=stop_postgres stop_mysql stop_sqlite

.DEFAULT_GOAL=default

default: db tests ignored stop

.PHONY: db $(DB_TARGETS) \
$(WAIT_TARGETS) \
diesel $(DIESEL_TARGETS) \
clean $(CLEAN_TARGETS)
stop $(STOP_TARGETS) \
default clippy tests ignored doc .FORCE

.SILENT: $(DB_TARGETS) $(WAIT_TARGETS) $(DIESEL_TARGETS) $(CLEAN_TARGETS) $(STOP_TARGETS)

.NOTPARALLEL: default

db: $(DB_TARGETS)

db_postgres:
docker run --rm -d --name postgres -p 5432:5432 \
-e POSTGRES_DB=fang \
-e POSTGRES_USER=postgres \
-e POSTGRES_PASSWORD=postgres \
postgres:latest
@echo -e $(BOLD)Setting up Postgres database...$(END_BOLD)
docker run --rm -d --name "$(POSTGRES_CONTAINER)" -p 5432:5432 \
-e POSTGRES_DB="$(POSTGRES_DB)" \
-e POSTGRES_USER="$(POSTGRES_USER)" \
-e POSTGRES_PASSWORD="$(POSTGRES_PASSWORD)" \
postgres:"$(POSTGRES_VERSION)"
$(MAKE) diesel_postgres

# login is root fang
db_mysql:
docker run --rm -d --name mysql -p 3306:3306 \
-e MYSQL_DATABASE=fang \
-e MYSQL_ROOT_PASSWORD=fang \
-e TZ=UTC \
mysql:latest
@echo -e $(BOLD)Setting up MySQL database...$(END_BOLD)
docker run --rm -d --name "$(MYSQL_CONTAINER)" -p 3306:3306 \
-e MYSQL_DATABASE="$(MYSQL_DB)" \
-e MYSQL_ROOT_PASSWORD="$(MYSQL_PASSWORD)" \
-e TZ=UTC \
mysql:"$(MYSQL_VERSION)"
$(MAKE) diesel_mysql

db_sqlite:
sqlite3 fang.db "VACUUM;"

clippy:
cargo clippy --verbose --all-targets --all-features -- -D warnings
@echo -e $(BOLD)Setting up SQLite database...$(END_BOLD)
sqlite3 "$(SQLITE_FILE)" "VACUUM;"
$(MAKE) diesel_sqlite

diesel_sqlite:
cd fang/sqlite_migrations && DATABASE_URL=sqlite://../../fang.db diesel migration run
diesel: $(DIESEL_TARGETS)

diesel_postgres:
cd fang/postgres_migrations && DATABASE_URL=postgres://postgres:postgres@localhost/fang diesel migration run
@echo -e $(BOLD)Running Diesel migrations on Postgres database...$(END_BOLD)
while ! diesel migration run --database-url "$(POSTGRES_URL)" --migration-dir "$(POSTGRES_MIGRATIONS)" --config-file "$(POSTGRES_CONFIG)" 2> /dev/null; \
do \
sleep 1; \
done

diesel_mysql:
cd fang/mysql_migrations && DATABASE_URL=mysql://root:[email protected]/fang diesel migration run
@echo -e $(BOLD)Running Diesel migrations on MySQL database...$(END_BOLD)
while ! diesel migration run --database-url "$(MYSQL_URL)" --migration-dir "$(MYSQL_MIGRATIONS)" --config-file "$(MYSQL_CONFIG)" 2> /dev/null; \
do \
sleep 1; \
done

diesel_sqlite:
@echo -e $(BOLD)Running Diesel migrations on SQLite database...$(END_BOLD)
while ! diesel migration run --database-url sqlite://"$(SQLITE_FILE)" --migration-dir "$(SQLITE_MIGRATIONS)" --config-file "$(SQLITE_CONFIG)" 2> /dev/null; \
do \
sleep 1; \
done

clean: $(CLEAN_TARGETS)

clean_postgres:
@echo -e $(BOLD)Cleaning Postgres database...$(END_BOLD)
docker exec "$(POSTGRES_CONTAINER)" dropdb -U "$(POSTGRES_USER)" "$(POSTGRES_DB)"
docker exec "$(POSTGRES_CONTAINER)" psql -U "$(POSTGRES_USER)" --command="CREATE DATABASE $(POSTGRES_DB);"
$(MAKE) diesel_postgres

clean_mysql:
@echo -e $(BOLD)Cleaning MySQL database...$(END_BOLD)
docker exec "$(MYSQL_CONTAINER)" mysql \
--user="$(MYSQL_USER)" \
--password="$(MYSQL_PASSWORD)" \
--execute="DROP DATABASE $(MYSQL_DB); CREATE DATABASE $(MYSQL_DB);"
$(MAKE) diesel_mysql

stop_mysql:
docker kill mysql
clean_sqlite:
@echo -e $(BOLD)Cleaning SQLite database...$(END_BOLD)
$(MAKE) stop_sqlite
$(MAKE) db_sqlite

stop: $(STOP_TARGETS)

stop_postgres:
docker kill postgres
@echo -e $(BOLD)Stopping Postgres database...$(END_BOLD)
docker kill "$(POSTGRES_CONTAINER)"

stop_mysql:
@echo -e $(BOLD)Stopping MySQL database...$(END_BOLD)
docker kill "$(MYSQL_CONTAINER)"

stop_sqlite:
rm fang.db
@echo -e $(BOLD)Stopping SQLite database...$(END_BOLD)
rm "$(SQLITE_FILE)"

clippy:
cargo clippy --verbose --all-targets --all-features -- -D warnings

tests:
DATABASE_URL=postgres://postgres:postgres@localhost/fang cargo test --all-features -- --color always --nocapture
@echo -e $(BOLD)Running tests...$(END_BOLD)
cargo test --all-features -- --color always --nocapture

ignored:
DATABASE_URL=postgres://postgres:postgres@localhost/fang cargo test --all-features -- --color always --nocapture --ignored
@echo -e $(BOLD)Running ignored tests...$(END_BOLD)
cargo test --all-features -- --color always --nocapture --ignored
$(MAKE) clean

doc:
cargo doc --open
cargo doc --package fang --open
Loading

0 comments on commit 1143285

Please sign in to comment.