-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlx_codegen_database.sh
More file actions
executable file
·64 lines (51 loc) · 2.11 KB
/
Copy pathsqlx_codegen_database.sh
File metadata and controls
executable file
·64 lines (51 loc) · 2.11 KB
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
#!/bin/bash
#
# Regenerates the workspace-root `.sqlx/` offline query cache for
# `mysql_queries`, checked against the local development MySQL database.
# Uses DATABASE_URL from the environment or the repository-root `.env`.
#
# Requirements:
# - sqlx-cli matching the workspace sqlx version, with the MySQL driver:
# cargo install sqlx-cli --version 0.7.4 --no-default-features \
# --features mysql,rustls --locked
# - A running local MySQL with the migrated dev database.
#
# The new cache is staged in a temp directory and only replaces the old
# `.sqlx/*.json` files after preparation succeeds, so a failure part-way
# never leaves the repo without a query cache.
set -euo pipefail
root_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
mysql_package_path="${root_dir}/crates/schema/database/mysql_queries"
query_cache_dir="${root_dir}/.sqlx"
staging_dir="$(mktemp -d /tmp/sqlx_codegen.XXXXXX)"
# The dev MySQL database. Falls back to the DATABASE_URL in the repo root
# .env so the script works from any directory.
mysql_database_url="${DATABASE_URL:-$(grep -m1 '^DATABASE_URL=mysql' "${root_dir}/.env" | cut -d= -f2-)}"
prepare_mysql() {
echo "Preparing the MySQL query cache..."
pushd "${mysql_package_path}"
cargo sqlx prepare \
--database-url "${mysql_database_url}"
popd
mv "${mysql_package_path}/.sqlx/"*.json "${staging_dir}/"
}
replace_query_cache() {
echo "Replacing the query cache..."
mkdir -p "${query_cache_dir}"
rm -f "${query_cache_dir}/"*.json
mv "${staging_dir}/"*.json "${query_cache_dir}/"
rmdir "${staging_dir}"
# Remove the (now empty) per-package cache directory so sqlx never resolves
# offline queries against a stale crate-local cache instead of the
# workspace root one.
rmdir "${mysql_package_path}/.sqlx" 2>/dev/null || true
}
# The sqlx macros only emit query metadata when the crates actually
# recompile; a fresh (cached) build would yield an EMPTY cache. Force the
# query crate to rebuild.
cargo clean -p mysql_queries
# Prepare must expand the macros against the live databases.
export SQLX_OFFLINE=false
prepare_mysql
replace_query_cache
echo 'done'