diff --git a/.sqlx/query-03919ba8286d18980ecaa0038df76f43f5388db0d3838181fa8f15f4cbb932af.json b/.sqlx/query-03919ba8286d18980ecaa0038df76f43f5388db0d3838181fa8f15f4cbb932af.json index f52725d30b..9881114dde 100644 --- a/.sqlx/query-03919ba8286d18980ecaa0038df76f43f5388db0d3838181fa8f15f4cbb932af.json +++ b/.sqlx/query-03919ba8286d18980ecaa0038df76f43f5388db0d3838181fa8f15f4cbb932af.json @@ -9,4 +9,4 @@ "nullable": [] }, "hash": "03919ba8286d18980ecaa0038df76f43f5388db0d3838181fa8f15f4cbb932af" -} \ No newline at end of file +} diff --git a/.sqlx/query-258ac40d3fab39f110e1e82d50e73542423bfe9a21f8127e6cd83214c1726e25.json b/.sqlx/query-258ac40d3fab39f110e1e82d50e73542423bfe9a21f8127e6cd83214c1726e25.json index 5138d316ea..f35bafdaa1 100644 --- a/.sqlx/query-258ac40d3fab39f110e1e82d50e73542423bfe9a21f8127e6cd83214c1726e25.json +++ b/.sqlx/query-258ac40d3fab39f110e1e82d50e73542423bfe9a21f8127e6cd83214c1726e25.json @@ -9,4 +9,4 @@ "nullable": [] }, "hash": "258ac40d3fab39f110e1e82d50e73542423bfe9a21f8127e6cd83214c1726e25" -} \ No newline at end of file +} diff --git a/.sqlx/query-62b3e2e058331a6819acd98c634b3bf2d10aa38d43b49ab69fb8637497beec2f.json b/.sqlx/query-62b3e2e058331a6819acd98c634b3bf2d10aa38d43b49ab69fb8637497beec2f.json index 716e0076f4..47e099f1be 100644 --- a/.sqlx/query-62b3e2e058331a6819acd98c634b3bf2d10aa38d43b49ab69fb8637497beec2f.json +++ b/.sqlx/query-62b3e2e058331a6819acd98c634b3bf2d10aa38d43b49ab69fb8637497beec2f.json @@ -110,4 +110,4 @@ ] }, "hash": "62b3e2e058331a6819acd98c634b3bf2d10aa38d43b49ab69fb8637497beec2f" -} \ No newline at end of file +} diff --git a/.sqlx/query-6ca0fcdf495624cf347f612586868f6dea93f52d9f942a92dcd2b3e6aba7c63c.json b/.sqlx/query-6ca0fcdf495624cf347f612586868f6dea93f52d9f942a92dcd2b3e6aba7c63c.json index 73bb24ee8a..16e8e8d27d 100644 --- a/.sqlx/query-6ca0fcdf495624cf347f612586868f6dea93f52d9f942a92dcd2b3e6aba7c63c.json +++ b/.sqlx/query-6ca0fcdf495624cf347f612586868f6dea93f52d9f942a92dcd2b3e6aba7c63c.json @@ -33,4 +33,4 @@ ] }, "hash": "6ca0fcdf495624cf347f612586868f6dea93f52d9f942a92dcd2b3e6aba7c63c" -} \ No newline at end of file +} diff --git a/.sqlx/query-96599b4854248d84188b092db84edb50dc5402bb7771fd8ea96a96ad43e732c0.json b/.sqlx/query-96599b4854248d84188b092db84edb50dc5402bb7771fd8ea96a96ad43e732c0.json index 8799953fff..a62bd529a5 100644 --- a/.sqlx/query-96599b4854248d84188b092db84edb50dc5402bb7771fd8ea96a96ad43e732c0.json +++ b/.sqlx/query-96599b4854248d84188b092db84edb50dc5402bb7771fd8ea96a96ad43e732c0.json @@ -9,4 +9,4 @@ "nullable": [] }, "hash": "96599b4854248d84188b092db84edb50dc5402bb7771fd8ea96a96ad43e732c0" -} \ No newline at end of file +} diff --git a/.sqlx/query-cd04f4f7b48ae9da53b6a210111f8e3edff94dee30263e22b45d5e29dbe9245a.json b/.sqlx/query-cd04f4f7b48ae9da53b6a210111f8e3edff94dee30263e22b45d5e29dbe9245a.json index 9573a50612..f7299377b9 100644 --- a/.sqlx/query-cd04f4f7b48ae9da53b6a210111f8e3edff94dee30263e22b45d5e29dbe9245a.json +++ b/.sqlx/query-cd04f4f7b48ae9da53b6a210111f8e3edff94dee30263e22b45d5e29dbe9245a.json @@ -176,4 +176,4 @@ ] }, "hash": "cd04f4f7b48ae9da53b6a210111f8e3edff94dee30263e22b45d5e29dbe9245a" -} \ No newline at end of file +} diff --git a/.sqlx/query-cf2794c87868e77dfaa45bdbe630ae276dbb8c2d041442ba465af0b7a6d61af6.json b/.sqlx/query-cf2794c87868e77dfaa45bdbe630ae276dbb8c2d041442ba465af0b7a6d61af6.json index 225b7e1638..82cbe54786 100644 --- a/.sqlx/query-cf2794c87868e77dfaa45bdbe630ae276dbb8c2d041442ba465af0b7a6d61af6.json +++ b/.sqlx/query-cf2794c87868e77dfaa45bdbe630ae276dbb8c2d041442ba465af0b7a6d61af6.json @@ -99,4 +99,4 @@ ] }, "hash": "cf2794c87868e77dfaa45bdbe630ae276dbb8c2d041442ba465af0b7a6d61af6" -} \ No newline at end of file +} diff --git a/.sqlx/query-d88098343686ded52a71fc45a1b96d28ea3003975d6e7d20eee2022aa31d8189.json b/.sqlx/query-d88098343686ded52a71fc45a1b96d28ea3003975d6e7d20eee2022aa31d8189.json index d0ee81fca5..c749c6df5b 100644 --- a/.sqlx/query-d88098343686ded52a71fc45a1b96d28ea3003975d6e7d20eee2022aa31d8189.json +++ b/.sqlx/query-d88098343686ded52a71fc45a1b96d28ea3003975d6e7d20eee2022aa31d8189.json @@ -9,4 +9,4 @@ "nullable": [] }, "hash": "d88098343686ded52a71fc45a1b96d28ea3003975d6e7d20eee2022aa31d8189" -} \ No newline at end of file +} diff --git a/.sqlx/query-f1d41ad17cb01f5aef0ce2eea042ccb3e5e649365a0befaf0f46492b75523442.json b/.sqlx/query-f1d41ad17cb01f5aef0ce2eea042ccb3e5e649365a0befaf0f46492b75523442.json index 0e58a37d2c..48c595e58a 100644 --- a/.sqlx/query-f1d41ad17cb01f5aef0ce2eea042ccb3e5e649365a0befaf0f46492b75523442.json +++ b/.sqlx/query-f1d41ad17cb01f5aef0ce2eea042ccb3e5e649365a0befaf0f46492b75523442.json @@ -121,4 +121,4 @@ ] }, "hash": "f1d41ad17cb01f5aef0ce2eea042ccb3e5e649365a0befaf0f46492b75523442" -} \ No newline at end of file +} diff --git a/script/rust/sqlx_codegen_database.sh b/script/rust/sqlx_codegen_database.sh index 64d158792f..515c7f6b86 100755 --- a/script/rust/sqlx_codegen_database.sh +++ b/script/rust/sqlx_codegen_database.sh @@ -1,106 +1,90 @@ #!/bin/bash - -# NB: This format can be used without changing directory: -# SQLX_OFFLINE=true cargo sqlx prepare -- --bin storyteller-web --manifest-path crates/service/storyteller_web/Cargo.toml +# +# Regenerates the workspace-root `.sqlx/` offline query cache for BOTH query +# crates: +# +# 1. `sqlite_tasks` — desktop app "tasks" queries, checked against a scratch +# SQLite database migrated from `_database/sql/artcraft_migrations`. +# 2. `mysql_queries` — backend queries, checked against the local dev MySQL +# (the `DATABASE_URL` in the repo root `.env`, overridable via env). +# +# Requirements: +# - sqlx-cli matching the workspace sqlx version, with both drivers: +# cargo install sqlx-cli --version 0.7.4 --no-default-features \ +# --features mysql,sqlite,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 BOTH prepares succeed, so a failure part-way +# never leaves the repo without a query cache. set -euxo pipefail -# TODO: Fix up root dir determination -root_dir=$(pwd) -sqlite_db_file="/tmp/tasks.sqlite" +root_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" +sqlite_db_file="/tmp/tasks.sqlite" sqlite_package_path="${root_dir}/crates/schema/database/sqlite_tasks" mysql_package_path="${root_dir}/crates/schema/database/mysql_queries" -query_cache_dir="${root_dir}/.sqlx/" +query_cache_dir="${root_dir}/.sqlx" +staging_dir="$(mktemp -d /tmp/sqlx_codegen.XXXXXX)" -remove_old_query_cache() { - echo "Removing old query cache files..." - pushd "${query_cache_dir}" - rm -f *.json - popd -} +# 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_sqlite_tasks() { - echo 'Create Tauri SQLite tasks database file...' + echo "Creating a fresh Tauri SQLite tasks database..." + rm -f "${sqlite_db_file}" touch "${sqlite_db_file}" - echo "Migrate Tauri SQLite tasks database file..." + echo "Migrating the SQLite tasks database..." cargo sqlx migrate run \ --database-url "sqlite:${sqlite_db_file}" \ --source "${root_dir}/_database/sql/artcraft_migrations" - echo "Prepare Tauri SQLite tasks query cache..." + echo "Preparing the SQLite tasks query cache..." pushd "${sqlite_package_path}" cargo sqlx prepare \ --database-url "sqlite:${sqlite_db_file}" popd - echo "Move query cache files..." - pushd "${sqlite_package_path}/.sqlx" - mv *.json "${query_cache_dir}" - popd + mv "${sqlite_package_path}/.sqlx/"*.json "${staging_dir}/" } prepare_mysql() { - echo "Prepare MySQL database query cache..." + echo "Preparing the MySQL query cache..." pushd "${mysql_package_path}" - cargo sqlx prepare + cargo sqlx prepare \ + --database-url "${mysql_database_url}" popd - echo "Move query cache files..." - pushd "${mysql_package_path}/.sqlx" - mv *.json "${query_cache_dir}" - popd + mv "${mysql_package_path}/.sqlx/"*.json "${staging_dir}/" } -remove_old_query_cache +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 dirs so sqlx never resolves + # offline queries against a stale crate-local cache instead of the + # workspace root one. + rmdir "${sqlite_package_path}/.sqlx" "${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 both +# query crates to rebuild. +cargo clean -p sqlite_tasks -p mysql_queries + +# Prepare must expand the macros against the live databases. +export SQLX_OFFLINE=false + prepare_sqlite_tasks prepare_mysql - -# NB(bt): Below are several generational revisions of sqlx query caching. -# The most recent was for a workspace with only one sqlx query package. -# There are older remnants from a multi-package configuration, but I believe -# the tooling has improved since then. Keeping this here just in case. -# -# #export DATABASE_URL="sqlite:${sqlite_db_file}" -# -# # NB: Database used by desktop app for AiChatBot sidecar. -# # Make sure it has been migrated (use aichatbot-sidecar app directory as root). -# # SQLITE_DATABASE_PATH=./runtime_data/database.db -# -# # NB: OpenSSL is broken on Ubuntu 22.04 (this is documented elsewhere) -# # See https://askubuntu.com/a/1403961 -# # For some reason, the choice of "rustls" or the vendored openssl is not persistent between calls. -# #cargo install sqlx-cli --features openssl-vendored,mysql -# -# build_shared_mysql_database_library() { -# -# # NB: For now, this is our monolithic DB that serves all of our microservices (gross) -# -# # It's a single package so that queries can be shared (again, gross) -# #pushd crates/lib/mysql_queries -# #SQLX_OFFLINE=true cargo sqlx prepare -# #popd -# cargo sqlx prepare --workspace -# } -# -# #build_shared_sqlite_database_library() { -# # # NB(1): These are *SQLite* queries for the desktop AiChatBot app. -# # # NB(2): Make sure to run migrations first. -# -# # # NB(3): SqLite has trouble with relative paths, so we copy the file to / -# # cp $SQLITE_DATABASE_PATH /tmp -# # pushd crates/lib/sqlite_queries -# # DATABASE_URL=sqlite:///tmp/database.db cargo sqlx prepare --merged -# # popd -# #} -# -# -# echo 'mysql prepare (if this takes a while, it may need to rebuild all the rust code)' -# build_shared_mysql_database_library -# -# #echo 'sqlite prepare' -# #build_shared_sqlite_database_library +replace_query_cache echo 'done'