Compare commits
51 Commits
house-trac
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| b77c3f8cf5 | |||
| 1754ea9316 | |||
| 6a127de950 | |||
| 9d08e6af23 | |||
| 941a9f16e1 | |||
| c8205e474e | |||
| 81ee02f11a | |||
| eb80a830f5 | |||
| 988c6c31a9 | |||
| 2af279e8a9 | |||
| faeb76a7e7 | |||
| a022c45499 | |||
| 6f82d1350e | |||
| 7a039b23f4 | |||
| 8445eef3cf | |||
| 1d4cfd58c5 | |||
| 763d7881fb | |||
| 2cada618bf | |||
| 7914b0140b | |||
| 178fa8ddce | |||
| 9c8f20a618 | |||
| 9c46e281c2 | |||
| 8b2dfed239 | |||
| 2da375451a | |||
| e4de2dcec3 | |||
| 00c1e8b1e7 | |||
| 9a1b21ec39 | |||
| b1ce715ec7 | |||
| 064d41595f | |||
| fa2456b99e | |||
| 8aa689af64 | |||
| 8275f1fb22 | |||
| 0cf5d44062 | |||
| 88837af624 | |||
| 45f2bbb198 | |||
| 36af5675e9 | |||
| 7e69d1e888 | |||
| 2915135ed8 | |||
| 4cfe6bad6c | |||
| 64fb8df262 | |||
| b8b366b542 | |||
| 6ed508702e | |||
| 46ab4a0adb | |||
| 08ab1f2716 | |||
| e039f69afd | |||
| 1ff6bb859e | |||
| 997c54133f | |||
| bdc8ac24ac | |||
| c688b874c8 | |||
| 74f64c7457 | |||
| 245137c65d |
14
.env.example
14
.env.example
@@ -1,5 +1,9 @@
|
||||
# PostgreSQL connection (server only)
|
||||
DB_URL=postgresql://postgres:postgres@localhost:5432/personal_dev
|
||||
# PostgreSQL connections (server only). Never commit real passwords.
|
||||
# DB_URL: the app role (personal_app: DML only, no DDL).
|
||||
DB_URL=postgresql://personal_app:CHANGE_ME@localhost:5432/personal_dev
|
||||
# DB_MIGRATE_URL: the schema owner (personal), used only by drizzle-kit,
|
||||
# scripts/db-migrate.ts, the seed scripts and deploy backups.
|
||||
DB_MIGRATE_URL=postgresql://personal:CHANGE_ME@localhost:5432/personal_dev
|
||||
|
||||
# Use the exact public origin, including scheme and port. Set the same value for both.
|
||||
ORIGIN=http://localhost:5173
|
||||
@@ -35,3 +39,9 @@ HOUSE_WORKER_TOKEN=
|
||||
HOUSE_DISCORD_WEBHOOK_URL=
|
||||
HOUSE_NTFY_URL=
|
||||
HOUSE_NTFY_TOKEN=
|
||||
|
||||
# Money tracker bank sync (scripts/bank-link.ts, scripts/bank-sync.ts; not read by the web app).
|
||||
# Keep these in ~/.config/money-sync.env (mode 600), never in git.
|
||||
ENABLEBANKING_APP_ID=
|
||||
ENABLEBANKING_KEY_PATH=
|
||||
ENABLEBANKING_REDIRECT_URL=https://stanrunge.dev/projects/money-tracker/bank-callback
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
name: Test and deploy staging
|
||||
on:
|
||||
push:
|
||||
branches: [staging]
|
||||
|
||||
jobs:
|
||||
staging:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
# Build once. The same SHA-tagged image stays on the deployment host for
|
||||
# one-click promotion to production; the production workflow never rebuilds it.
|
||||
- uses: docker/build-push-action@v5
|
||||
with:
|
||||
context: .
|
||||
push: false
|
||||
load: true
|
||||
tags: git.stanrunge.dev/stan/personal-website:${{ github.sha }}
|
||||
build-args: |
|
||||
GIT_SHA=${{ github.sha }}
|
||||
|
||||
- name: Configure deployment SSH key
|
||||
env:
|
||||
DEPLOY_KEY: ${{ secrets.DEPLOY_KEY }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
install -d -m 700 ~/.ssh
|
||||
printf '%s\n' "$DEPLOY_KEY" > ~/.ssh/id_ed25519
|
||||
chmod 600 ~/.ssh/id_ed25519
|
||||
ssh-keyscan -H 172.17.0.1 >> ~/.ssh/known_hosts
|
||||
|
||||
- name: Transfer tested image
|
||||
run: |
|
||||
set -euo pipefail
|
||||
image="git.stanrunge.dev/stan/personal-website:${{ github.sha }}"
|
||||
docker save "$image" | ssh -o BatchMode=yes deploy@172.17.0.1 'sudo docker load'
|
||||
|
||||
- name: Deploy staging
|
||||
run: |
|
||||
set -euo pipefail
|
||||
ssh -o BatchMode=yes deploy@172.17.0.1 \
|
||||
"bash -s -- '${{ github.sha }}' '/opt/personal-website-staging' 'https://staging.stanrunge.dev'" \
|
||||
< scripts/deploy-remote.sh
|
||||
@@ -1,33 +0,0 @@
|
||||
name: Deploy production
|
||||
on:
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
promote:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
# Always test the staging branch, regardless of which branch the button
|
||||
# appears on in Gitea's Actions UI.
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
ref: staging
|
||||
|
||||
- name: Configure deployment SSH key
|
||||
env:
|
||||
DEPLOY_KEY: ${{ secrets.DEPLOY_KEY }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
install -d -m 700 ~/.ssh
|
||||
printf '%s\n' "$DEPLOY_KEY" > ~/.ssh/id_ed25519
|
||||
chmod 600 ~/.ssh/id_ed25519
|
||||
ssh-keyscan -H 172.17.0.1 >> ~/.ssh/known_hosts
|
||||
|
||||
- name: Promote the exact staging image
|
||||
run: |
|
||||
set -euo pipefail
|
||||
commit=$(git rev-parse HEAD)
|
||||
ssh -o BatchMode=yes deploy@172.17.0.1 "bash -s -- '$commit'" \
|
||||
< scripts/verify-staging-image.sh
|
||||
ssh -o BatchMode=yes deploy@172.17.0.1 \
|
||||
"bash -s -- '$commit' '/opt/personal-website' 'https://stanrunge.dev'" \
|
||||
< scripts/deploy-remote.sh
|
||||
70
.gitea/workflows/deploy.yml
Normal file
70
.gitea/workflows/deploy.yml
Normal file
@@ -0,0 +1,70 @@
|
||||
name: Deploy production
|
||||
# Two environments only: local dev (see README "Development") and prod.
|
||||
# A push/merge to main builds and tests the image, backs up prod (pg_dump via
|
||||
# postgres:17-alpine), runs Drizzle migrations, then switches the container.
|
||||
# Pushes to any other branch (e.g. db-schemas) do not match and run nothing.
|
||||
on:
|
||||
push:
|
||||
branches: [main]
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
rollback_tag:
|
||||
description: 'Roll back: redeploy this earlier image SHA (skips migrations/seeds)'
|
||||
required: true
|
||||
|
||||
concurrency:
|
||||
group: deploy-production
|
||||
cancel-in-progress: false
|
||||
|
||||
jobs:
|
||||
deploy:
|
||||
# Belt and braces: a manual dispatch from another branch does nothing either.
|
||||
if: github.ref == 'refs/heads/main'
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Configure deployment SSH key
|
||||
env:
|
||||
DEPLOY_KEY: ${{ secrets.DEPLOY_KEY }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
install -d -m 700 ~/.ssh
|
||||
printf '%s\n' "$DEPLOY_KEY" > ~/.ssh/id_ed25519
|
||||
chmod 600 ~/.ssh/id_ed25519
|
||||
ssh-keyscan -H 172.17.0.1 >> ~/.ssh/known_hosts
|
||||
|
||||
# The Dockerfile runs bun install --frozen-lockfile, unit tests and the build,
|
||||
# so a failing test never produces a deployable image.
|
||||
- if: github.event_name == 'push'
|
||||
uses: docker/build-push-action@v5
|
||||
with:
|
||||
context: .
|
||||
push: false
|
||||
load: true
|
||||
tags: git.stanrunge.dev/stan/website:${{ github.sha }}
|
||||
build-args: |
|
||||
GIT_SHA=${{ github.sha }}
|
||||
|
||||
- name: Transfer image
|
||||
if: github.event_name == 'push'
|
||||
run: |
|
||||
set -euo pipefail
|
||||
image="git.stanrunge.dev/stan/website:${{ github.sha }}"
|
||||
docker save "$image" | ssh -o BatchMode=yes deploy@172.17.0.1 'sudo docker load'
|
||||
|
||||
- name: Back up, migrate and deploy
|
||||
if: github.event_name == 'push'
|
||||
run: |
|
||||
set -euo pipefail
|
||||
ssh -o BatchMode=yes deploy@172.17.0.1 \
|
||||
"bash -s -- '${{ github.sha }}' deploy" < scripts/deploy-remote.sh
|
||||
|
||||
- name: Roll back to an earlier image
|
||||
if: github.event_name == 'workflow_dispatch'
|
||||
env:
|
||||
TAG: ${{ inputs.rollback_tag }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
case "$TAG" in *[!0-9a-f]*|'') echo 'Invalid image tag' >&2; exit 2 ;; esac
|
||||
ssh -o BatchMode=yes deploy@172.17.0.1 "bash -s -- '$TAG' rollback" < scripts/deploy-remote.sh
|
||||
@@ -19,3 +19,9 @@ data/cod-challenges.json
|
||||
data/game-images.json
|
||||
data/cod-achievement-art.json
|
||||
src/lib/house-tracker/fixtures/
|
||||
|
||||
# Vendored third-party code
|
||||
static/vendor/
|
||||
|
||||
# Generated drizzle snapshots
|
||||
drizzle/*/snapshot.json
|
||||
|
||||
@@ -13,6 +13,7 @@ ENV APP_VERSION=$GIT_SHA
|
||||
COPY --from=builder /app/node_modules ./node_modules
|
||||
COPY --from=builder /app/build ./build
|
||||
COPY --from=builder /app/scripts ./scripts
|
||||
COPY --from=builder /app/drizzle ./drizzle
|
||||
COPY --from=builder /app/data ./data
|
||||
COPY --from=builder /app/src/lib/server/db/schema ./src/lib/server/db/schema
|
||||
COPY --from=builder /app/src/lib/cs-prices ./src/lib/cs-prices
|
||||
|
||||
207
README.md
207
README.md
@@ -227,29 +227,42 @@ progress and warns about them.
|
||||
|
||||
### Database
|
||||
|
||||
For an existing database using the current domain schemas:
|
||||
The schema is managed by Drizzle (`src/lib/server/db/schema/`, migrations in
|
||||
`drizzle/`). Postgres schemas: `auth`, `money`, `house`, `ref`, `games`, `cs`,
|
||||
`animaldex`, `site`, `meta` (Drizzle-managed) and `lists` (one real table per list,
|
||||
created at runtime through the `meta.create_list` family of SQL functions, so it is
|
||||
excluded from Drizzle via `schemaFilter`). See [Development](#development) for the
|
||||
local database and migrations, and `db/reorg/README.md` for the one-time move of the
|
||||
old flat schema to this layout. Catalog seeds (`npm run db:seed`, `db:seed:cod`, …)
|
||||
upsert reference data and run on every production deploy.
|
||||
|
||||
```sh
|
||||
npm run db:migrate:auth
|
||||
npm run db:migrate:foundation
|
||||
npm run db:migrate:money-tracker
|
||||
npm run db:migrate:game-tracker
|
||||
npm run db:seed
|
||||
npm run db:seed:cod
|
||||
```
|
||||
## Book tracker
|
||||
|
||||
For a fresh database, run `npm run db:push` followed by both seeds.
|
||||
The migrations are additive and repeatable; they do not reset tables. The
|
||||
game-tracker migration adds the `evolve`/`breed` availability values, the entry
|
||||
`source` column (marking pre-existing hand-edited notes as `manual`), and the
|
||||
location, encounter, evolution and `tracker_*` tables. Unknown legacy OAuth
|
||||
providers cause the auth migration to fail for explicit review instead of
|
||||
guessing an issuer. Back up a production database before any schema migration.
|
||||
The scripts use `DB_URL` from `.env` via Bun.
|
||||
`/trackers/books` is the public catalogue (covers, status, progress, stats). `/trackers/books/<id>`
|
||||
shows a book; the owner also sees files, notes, highlights and reading sessions there and can
|
||||
edit status, rating and progress. `/trackers/books/<id>/read` is the owner-only reader:
|
||||
EPUB, MOBI/AZW3 and FB2 open in [foliate-js](https://github.com/johnfactotum/foliate-js)
|
||||
(vendored in `static/vendor/foliate-js`, no conversion), PDFs in pdf.js. The reader remembers the
|
||||
position per file, autosaves progress and sessions, and stores highlights with notes.
|
||||
|
||||
## Money tracker
|
||||
Files are never public. They are streamed (with Range support) by
|
||||
`/trackers/books/files/<id>` to the owner only; everyone else gets a 404. The reader, progress
|
||||
and notes endpoints are owner-only too (`src/lib/server/owner.ts`, backstopped in
|
||||
`hooks.server.ts`).
|
||||
|
||||
`/projects/money-tracker` is restricted to administrator accounts. Its tables
|
||||
Files are read from `BOOKS_ROOT` (default `/books` when it exists, otherwise `~/books`):
|
||||
|
||||
- `BOOKS_ROOT/library/<books.files.path>` for book files (written by `scripts/books-ingest.py`)
|
||||
- `BOOKS_ROOT/covers/<books.works.cover_path>` for downloaded covers
|
||||
|
||||
In production, `scripts/deploy-remote.sh` writes `~deploy/compose.books.yml` (the deploy account may only sudo-tee `/opt/personal-website/.env`), which
|
||||
bind-mounts the host's `BOOKS_HOST_DIR` (default `/home/stan/books`) read-only at `/books` and sets
|
||||
`BOOKS_ROOT=/books`. Without a `library` directory there the deploy still succeeds, but files can't
|
||||
be opened.
|
||||
|
||||
## Money
|
||||
|
||||
`/trackers/money` (formerly `/projects/money-tracker`, which now redirects) is restricted to administrator accounts. Its tables
|
||||
are created by `npm run db:migrate:money-tracker`; apply that migration to each
|
||||
environment before deploying the new route. The deployment preflight checks for
|
||||
all nine tracker tables and the projection columns.
|
||||
@@ -317,112 +330,87 @@ registered callback URL. The integration follows the installed Better Auth APIs;
|
||||
see [SvelteKit integration](https://better-auth.com/docs/integrations/svelte-kit)
|
||||
and [Generic OAuth](https://better-auth.com/docs/plugins/generic-oauth).
|
||||
|
||||
## Staging and production rollout
|
||||
## Development
|
||||
|
||||
Use `staging` as the working branch and set it as the repository's default branch
|
||||
in Gitea after the first push. A direct push to `staging` runs unit tests, builds
|
||||
the site and a SHA-tagged Docker image, and deploys that image to
|
||||
`https://staging.stanrunge.dev`. After checking staging, use Gitea Actions →
|
||||
**Deploy production** → **Run workflow**. That action retests the current staging
|
||||
commit, verifies that staging runs its exact image, and starts the **same image**
|
||||
at `https://stanrunge.dev`. It does not rebuild the image or need a PR. Pushes to
|
||||
`main` no longer deploy production; once `staging` is the default branch, its
|
||||
current manual workflow is also the one shown in Gitea's Actions UI.
|
||||
There are two environments: local dev and production. Prod is the source of truth
|
||||
for data; the dev database is a disposable copy of it.
|
||||
|
||||
The staging Docker build runs `bun install --frozen-lockfile`, unit tests, and
|
||||
the production build, so a failed test never produces a deployable image. The
|
||||
production button verifies the exact tested staging image before switching
|
||||
production. Browser/database tests need a
|
||||
dedicated test database and are not part of this gate yet. `npm run check` and
|
||||
full lint are also not gates while the repository has existing unrelated
|
||||
diagnostics; add them after those are fixed. A failed test, build, or preflight
|
||||
prevents the switch; a failed post-deploy health check attempts to restore the
|
||||
previous image tag. Deployment never migrates or seeds either database. The
|
||||
Gitea runner needs Docker CLI/socket access and the existing `DEPLOY_KEY` secret.
|
||||
```sh
|
||||
cp .env.example .env # DB_URL (app role) + DB_MIGRATE_URL (owner role) -> local personal_dev
|
||||
scripts/db-refresh-dev.sh # fresh prod dump -> personal_dev (sessions cleared)
|
||||
bun run db:migrate # apply this branch's pending Drizzle migrations
|
||||
bun run dev
|
||||
```
|
||||
|
||||
Before either push, configure the existing private environment file on that
|
||||
host. Staging uses `/etc/personal-website-staging/staging.env`; production uses
|
||||
`/etc/personal-website/prod.env`. Each file needs `DB_URL`, a unique random
|
||||
`BETTER_AUTH_SECRET` of at least 32 characters, and both `ORIGIN` and
|
||||
`BETTER_AUTH_URL` set exactly to its public HTTPS origin. Add the Google and
|
||||
osu! client ID/secret pairs for whichever sign-in buttons should be enabled.
|
||||
List and Animaldex picture uploads reuse the existing R2 bucket; set the five
|
||||
`R2_*` values from `.env.example`. The app server uploads images to R2, so the
|
||||
bucket does not need browser CORS rules for these forms. Each image is limited
|
||||
to 5 MB; the container sets `BODY_SIZE_LIMIT=6M` for multipart requests.
|
||||
Their provider console callback URLs must use that environment's origin:
|
||||
`db-refresh-dev.sh` reads `PROD_DB_URL` (a role that can read everything, e.g.
|
||||
`personal`) and `DEV_ADMIN_URL` (superuser on the dev cluster) from the environment
|
||||
or `.env.dev-refresh` (git-ignored, never commit it). It refuses to overwrite the
|
||||
source database or any database named `personal`. `PG_DOCKER=1` uses the
|
||||
`postgres:17-alpine` image when local `pg_dump` 17 isn't installed.
|
||||
|
||||
Schema changes: edit `src/lib/server/db/schema/`, run `bun run db:generate --name
|
||||
<change>`, review the SQL in `drizzle/`, then `bun run db:migrate`. Never edit an
|
||||
applied migration; never use `db:push` against prod.
|
||||
|
||||
`scripts/preview.sh <branch> [port]` runs a branch against a throwaway copy of the
|
||||
dev database (migrated with that branch's migrations) and cleans everything up on
|
||||
Ctrl-C.
|
||||
|
||||
### Database roles
|
||||
|
||||
| env var | role | used for |
|
||||
| ---------------- | -------------- | --------------------------------------------------------------- |
|
||||
| `DB_URL` | `personal_app` | the app at runtime: DML only, calls the `meta.*` list functions |
|
||||
| `DB_MIGRATE_URL` | `personal` | Drizzle migrations, seeds, backups (`pg_dump`) |
|
||||
|
||||
`personal_ddl` owns the `lists` schema (only reachable through the SECURITY DEFINER
|
||||
`meta.*` functions); `personal_ro` is read-only. The deploy preflight fails if
|
||||
`DB_URL` can create objects, i.e. if it isn't the app role.
|
||||
|
||||
## Production
|
||||
|
||||
Push or merge to `main` → Gitea Actions (`.gitea/workflows/deploy.yml`) builds a
|
||||
SHA-tagged image (unit tests + production build run inside the Docker build),
|
||||
transfers it, and runs `scripts/deploy-remote.sh <sha> deploy` on the host:
|
||||
|
||||
1. checks the image and that `DB_MIGRATE_URL` is set in `/etc/personal-website/prod.env`;
|
||||
2. `pg_dump` of prod (postgres:17-alpine) into the backup directory;
|
||||
3. `bun scripts/db-migrate.ts` (Drizzle migrations) and the catalog seeds;
|
||||
4. preflight (tables, column types, app-role privileges), switch the container, health checks.
|
||||
|
||||
A failed health check restores the previous image tag automatically. Manual
|
||||
rollback: Gitea Actions → **Deploy production** → **Run workflow** with
|
||||
`rollback_tag=<previous sha>`; this redeploys that image without migrating or
|
||||
seeding. Database changes are not rolled back with the image; restore the
|
||||
pre-deploy dump if a migration has to be undone. Only `main` deploys; other
|
||||
branches never trigger the workflow. The runner needs Docker CLI/socket access
|
||||
and the `DEPLOY_KEY` secret.
|
||||
|
||||
`/etc/personal-website/prod.env` needs `DB_URL`, `DB_MIGRATE_URL`, a unique random
|
||||
`BETTER_AUTH_SECRET` (32+ characters), and `ORIGIN`/`BETTER_AUTH_URL` set to
|
||||
`https://stanrunge.dev`, plus the Google and osu! client ID/secret pairs and the five
|
||||
`R2_*` values from `.env.example`. Picture uploads (lists, Animaldex) go through the
|
||||
app server to R2 (5 MB per image; the container sets `BODY_SIZE_LIMIT=6M`). OAuth
|
||||
callback URLs:
|
||||
|
||||
```text
|
||||
https://staging.stanrunge.dev/api/auth/callback/google
|
||||
https://staging.stanrunge.dev/api/auth/callback/osu
|
||||
https://stanrunge.dev/api/auth/callback/google
|
||||
https://stanrunge.dev/api/auth/callback/osu
|
||||
```
|
||||
|
||||
For this first rollout, review the SQL and back up each database. The first push
|
||||
to `staging` may pass **Transfer tested image** but fail **Deploy staging** at
|
||||
the database preflight. That is expected if the database has not been migrated:
|
||||
the image is then available on the deployment host for you to run the migrations
|
||||
yourself. If `animals`, `sightings`, and `photos` still have UUID IDs, first apply
|
||||
`scripts/animaldex-uuid-to-serial.sql` with `psql` once. If the database has the
|
||||
legacy singular tables `game`, `pokemon_game_entry`, `user_entry`, and `user_game`,
|
||||
then apply `scripts/legacy-schema-migration.sql` with `psql` once. That migration
|
||||
renames those tables without discarding their rows and creates the empty base
|
||||
`lists` and `list_items` tables. Run both reviewed SQL files before the container
|
||||
commands below. On the deployment host, use the SHA from the staging Actions run:
|
||||
After registering, promote your account (ID shown on `/account`):
|
||||
|
||||
```sh
|
||||
release_image=git.stanrunge.dev/stan/personal-website:<staging-sha>
|
||||
sudo docker run --rm --network backend --env-file /etc/personal-website-staging/staging.env "$release_image" bun scripts/migrate-auth-catalog.ts
|
||||
sudo docker run --rm --network backend --env-file /etc/personal-website-staging/staging.env "$release_image" bun scripts/migrate-foundation.ts
|
||||
sudo docker run --rm --network backend --env-file /etc/personal-website-staging/staging.env "$release_image" bun scripts/migrate-money-tracker.ts
|
||||
sudo docker run --rm --network backend --env-file /etc/personal-website-staging/staging.env "$release_image" bun scripts/migrate-game-tracker.ts
|
||||
sudo docker run --rm --network backend --env-file /etc/personal-website-staging/staging.env "$release_image" bun scripts/migrate-tracker-journal.ts
|
||||
sudo docker run --rm --network backend --env-file /etc/personal-website-staging/staging.env "$release_image" bun scripts/seed-catalog.ts
|
||||
sudo docker run --rm --network backend --env-file /etc/personal-website-staging/staging.env "$release_image" bun scripts/seed-cod-catalog.ts
|
||||
sudo docker run --rm --network backend --env-file /etc/personal-website-staging/staging.env "$release_image" bun scripts/seed-pokemon-research.ts
|
||||
sudo docker run --rm --network backend --env-file /etc/personal-website-staging/staging.env "$release_image" bun scripts/seed-cod-challenges.ts
|
||||
sudo docker run --rm --network backend --env-file /etc/personal-website-staging/staging.env "$release_image" bun scripts/seed-cod-art.ts
|
||||
sudo docker run --rm --network backend --env-file /etc/personal-website/prod.env \
|
||||
git.stanrunge.dev/stan/website:<deployed-sha> bun scripts/set-admin.ts <account-id> admin
|
||||
```
|
||||
|
||||
These catalog seeds use the reviewed files in `data/` and upsert catalog rows;
|
||||
they preserve user accounts, catches, ranks, and saved challenge progress. Do not
|
||||
copy or restore the entire development database over staging or production.
|
||||
Repeat the same migration and seed commands for staging first, replacing the
|
||||
environment file with `/etc/personal-website/prod.env` only after staging has
|
||||
passed its preflight and been reviewed. Back up production before applying its
|
||||
migrations. Rerun the same staging Actions run after the staging migration and
|
||||
seed complete. Click the production button only after production migration and
|
||||
seed complete. Future releases without database changes need only a staging push
|
||||
and the production button.
|
||||
|
||||
The foundation migration gives lists slugs, progress, pictures, and blog tags;
|
||||
gives site-owned join/settings rows numeric IDs; and adds `created_at`/
|
||||
`updated_at` with a database update trigger. Blog posts already had numeric IDs
|
||||
and timestamps. Better Auth's account/session IDs stay as text because they are
|
||||
provider-managed identities. The deploy script verifies the migrations and
|
||||
catalog before switching the image, then checks `/login` and Catchemall. A
|
||||
failed app health check attempts to restore the previous image tag. Database
|
||||
changes are not rolled back with the app image.
|
||||
|
||||
The release image contains `scripts/`, `data/`, and the schema files needed by
|
||||
the migration tools. After registration in each environment, promote your own
|
||||
account using the host's matching environment file, for example:
|
||||
|
||||
```sh
|
||||
sudo docker run --rm --network backend \
|
||||
--env-file /etc/personal-website-staging/staging.env \
|
||||
git.stanrunge.dev/stan/personal-website:<deployed-sha> \
|
||||
bun scripts/set-admin.ts <account-id> admin
|
||||
```
|
||||
|
||||
Use `/etc/personal-website/prod.env` and its deployed image SHA for production.
|
||||
The account ID is shown on `/account`. No public signup grants admin privileges.
|
||||
No public signup grants admin privileges.
|
||||
|
||||
# CS prices
|
||||
|
||||
`/projects/cs-prices` reads only from Postgres. Each Steam market hash name is an
|
||||
individual listing. The separate `cs_item_dimensions` table makes it possible to
|
||||
individual listing. The separate `cs.item_dimensions` table makes it possible to
|
||||
compare or group by weapon type, weapon name, weapon plus skin, skin, wear, StatTrak, Souvenir, and
|
||||
additional imported dimensions such as collection or rarity. Group chart points
|
||||
average the available per-listing prices equally within each time bucket. Raw
|
||||
@@ -431,10 +419,9 @@ volume. Chart responses use up to roughly 240 time buckets per series and allow
|
||||
up to 1,000 selected series; actual response time still depends on database size
|
||||
and hardware.
|
||||
|
||||
With `DB_URL` pointed at a running Postgres database:
|
||||
With `DB_URL` pointed at a migrated database:
|
||||
|
||||
```sh
|
||||
npm run db:migrate:cs-prices
|
||||
npm run cs-prices:ingest -- catalog
|
||||
STEAM_SESSIONID=... STEAM_LOGIN_SECURE=... npm run cs-prices:ingest -- history
|
||||
```
|
||||
|
||||
28
bun.lock
28
bun.lock
@@ -14,9 +14,11 @@
|
||||
"drizzle-orm": "^1.0.0-rc.4",
|
||||
"echarts": "^6.1.0",
|
||||
"highlight.js": "^11.12.0",
|
||||
"jose": "^6.2.10",
|
||||
"marked": "^18.0.11",
|
||||
"marked-highlight": "^2.2.4",
|
||||
"mode-watcher": "^1.1.0",
|
||||
"pdfjs-dist": "^6.4.299",
|
||||
"postgres": "^3.4.9",
|
||||
"topojson-client": "^3.1.0",
|
||||
"world-atlas": "^2.0.2",
|
||||
@@ -325,6 +327,30 @@
|
||||
|
||||
"@melt-ui/svelte": ["@melt-ui/svelte@0.76.2", "", { "dependencies": { "@floating-ui/core": "^1.3.1", "@floating-ui/dom": "^1.4.5", "@internationalized/date": "^3.5.0", "dequal": "^2.0.3", "focus-trap": "^7.5.2", "nanoid": "^5.0.4" }, "peerDependencies": { "svelte": ">=3 <5" } }, "sha512-7SbOa11tXUS95T3fReL+dwDs5FyJtCEqrqG3inRziDws346SYLsxOQ6HmX+4BkIsQh1R8U3XNa+EMmdMt38lMA=="],
|
||||
|
||||
"@napi-rs/canvas": ["@napi-rs/canvas@1.0.13", "", { "optionalDependencies": { "@napi-rs/canvas-android-arm64": "1.0.13", "@napi-rs/canvas-darwin-arm64": "1.0.13", "@napi-rs/canvas-darwin-x64": "1.0.13", "@napi-rs/canvas-linux-arm-gnueabihf": "1.0.13", "@napi-rs/canvas-linux-arm64-gnu": "1.0.13", "@napi-rs/canvas-linux-arm64-musl": "1.0.13", "@napi-rs/canvas-linux-riscv64-gnu": "1.0.13", "@napi-rs/canvas-linux-x64-gnu": "1.0.13", "@napi-rs/canvas-linux-x64-musl": "1.0.13", "@napi-rs/canvas-win32-arm64-msvc": "1.0.13", "@napi-rs/canvas-win32-x64-msvc": "1.0.13" } }, "sha512-b87ugZpQ5ViwqVBkjervxpwa0GmJPsKHLBNoZN6GpjWeHzTbDUGryriq+2DYW2XbB+xCqSIINQysC0/I87nPbw=="],
|
||||
|
||||
"@napi-rs/canvas-android-arm64": ["@napi-rs/canvas-android-arm64@1.0.13", "", { "os": "android", "cpu": "arm64" }, "sha512-trn7AaDzQ+yeeA0ezhkC7cB1ZDPtp4UYk0EvB5F8qtYL9jEVWtVrU30WOpI52DszoTxaTsCgf5PP4ZVaOlOIHQ=="],
|
||||
|
||||
"@napi-rs/canvas-darwin-arm64": ["@napi-rs/canvas-darwin-arm64@1.0.13", "", { "os": "darwin", "cpu": "arm64" }, "sha512-6Y2djEetL8copxfNK6hg7oig/xqTwOchv19hBOm+zBNdKc/02Z3kjZN5wrLzv/l5O04/bAQS6I/AlQpwBlXgPg=="],
|
||||
|
||||
"@napi-rs/canvas-darwin-x64": ["@napi-rs/canvas-darwin-x64@1.0.13", "", { "os": "darwin", "cpu": "x64" }, "sha512-OZoPwwu4TMMg8GTCFBrg8UFExwQTU/55i3uTC/izDat6PHE+ro9dimyQoOboFf91+NY7Pnn70ScR8f8I8+eZXw=="],
|
||||
|
||||
"@napi-rs/canvas-linux-arm-gnueabihf": ["@napi-rs/canvas-linux-arm-gnueabihf@1.0.13", "", { "os": "linux", "cpu": "arm" }, "sha512-dZSYaKROuswU2eUWG5aBKndNVe68elo1YX93xtRax7q658tj8rYE+HwduovWJgs6qwv1r1/CLR60nDeBbyjvyg=="],
|
||||
|
||||
"@napi-rs/canvas-linux-arm64-gnu": ["@napi-rs/canvas-linux-arm64-gnu@1.0.13", "", { "os": "linux", "cpu": "arm64" }, "sha512-W6FzYE8ADHI93In1vi80KBL6btDtAMuj50Ch5ptkWS8xuntyzbR4h5JDUmXoakxZn2yMInksudTEqVSWoFqFGg=="],
|
||||
|
||||
"@napi-rs/canvas-linux-arm64-musl": ["@napi-rs/canvas-linux-arm64-musl@1.0.13", "", { "os": "linux", "cpu": "arm64" }, "sha512-+6fQ+rR49hkAa4voh2stLIwQ+4t+gmoseM8Bc53/DlMXb73IKymDPWK9A/NtWeqTW4ijWppYuyuxS2hPI8tzNA=="],
|
||||
|
||||
"@napi-rs/canvas-linux-riscv64-gnu": ["@napi-rs/canvas-linux-riscv64-gnu@1.0.13", "", { "os": "linux", "cpu": "none" }, "sha512-17XEMO3t9RW/TtHtSFf2AkaSWuCWdzbPeidz7zrOFawHxoLSJCnpTI1hN5aUvrZNnkNNmt/vZVJCh0ryUaik3A=="],
|
||||
|
||||
"@napi-rs/canvas-linux-x64-gnu": ["@napi-rs/canvas-linux-x64-gnu@1.0.13", "", { "os": "linux", "cpu": "x64" }, "sha512-lc48IwknVHBsNzXn9Xs5OkUsRX38Ml3Z1aOybQ51mMEzZckm8+4A0qROlyYHEoBPzr1kL3IU8ZUAqDb7wtdX8w=="],
|
||||
|
||||
"@napi-rs/canvas-linux-x64-musl": ["@napi-rs/canvas-linux-x64-musl@1.0.13", "", { "os": "linux", "cpu": "x64" }, "sha512-bGY1r26S2bs8UNFJcPWcCSNdCFbKep5PATsvtgtC9A5u6SRgr2/jJE/RlhHL5m9ZpixNDhVkLY/5h16Kx5V9YA=="],
|
||||
|
||||
"@napi-rs/canvas-win32-arm64-msvc": ["@napi-rs/canvas-win32-arm64-msvc@1.0.13", "", { "os": "win32", "cpu": "arm64" }, "sha512-2vRPBZoaIvJrUkDhZTE2Hn0YrnpJRCWVM3EJACoBKjuWI6WgPxGtUoRMsxRviCsyks8bKTSFdyw14CellnG2qA=="],
|
||||
|
||||
"@napi-rs/canvas-win32-x64-msvc": ["@napi-rs/canvas-win32-x64-msvc@1.0.13", "", { "os": "win32", "cpu": "x64" }, "sha512-DUlW3lZPz63kYsf8mxgyyZuevhlH0T3/BGP9l+NazhB0F1+gL6D4hqZdnhZpw24DlWmTpATDcyYSZlJOi+DdfQ=="],
|
||||
|
||||
"@noble/ciphers": ["@noble/ciphers@2.4.0", "", {}, "sha512-AnjFn0Jv92laAkvMrghlFZq4qQCIN/4DxFV/eooqtC2YTjB7kBeLMS2T9KJX4Dn+ZVXLOwK0lSgqDtx9gvxtiw=="],
|
||||
|
||||
"@noble/hashes": ["@noble/hashes@2.4.0", "", {}, "sha512-X5XaVWZIBCT7HHZGm5I7ZQXDwLG+bGXuSrMQAW+7Zvl87h1kmc1ZB1VSRJcpUfoUrGQp4Fkoxm5kZ+Ms+aW+eA=="],
|
||||
@@ -837,6 +863,8 @@
|
||||
|
||||
"pbf": ["pbf@4.0.2", "", { "dependencies": { "resolve-protobuf-schema": "^2.1.0" }, "bin": "bin/pbf" }, "sha512-J0ajxARhZfpUEebxYs1vhMGMuLSXtBe1e+fFPDrf2uA2hgo+UshKfNUWOz92HJNz6/NFEXseQPddnHkTreWRqg=="],
|
||||
|
||||
"pdfjs-dist": ["pdfjs-dist@6.4.299", "", { "optionalDependencies": { "@napi-rs/canvas": "^1.0.10" } }, "sha512-AVl138zALtfaAPvADulE0PZThbYzCBS79nL4pOSL/6Sm/4AH5A21BD9VHt97OlCuzJuCpmeZtAtkinisF4Vb1g=="],
|
||||
|
||||
"pg-int8": ["pg-int8@1.0.1", "", {}, "sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw=="],
|
||||
|
||||
"pg-protocol": ["pg-protocol@1.16.0", "", {}, "sha512-sILXutLVjCLjcDuOmvhX5e2Z4cS5qG/6Bu3VkpFwdf/633ElGLpEh9bgmuI5I4sqKqkifQiGyiCcx1HdtrK7tg=="],
|
||||
|
||||
643
data/tasks/daisy-import.json
Normal file
643
data/tasks/daisy-import.json
Normal file
@@ -0,0 +1,643 @@
|
||||
{
|
||||
"source": "daisy-import",
|
||||
"areas": [
|
||||
{
|
||||
"slug": "deadlines",
|
||||
"name": "Deadlines and dates",
|
||||
"sortOrder": 10
|
||||
},
|
||||
{
|
||||
"slug": "house",
|
||||
"name": "House and moving",
|
||||
"description": "Jan–Feb 2027 with Juhee",
|
||||
"sortOrder": 20
|
||||
},
|
||||
{
|
||||
"slug": "kibi",
|
||||
"name": "Kibi",
|
||||
"sortOrder": 30
|
||||
},
|
||||
{
|
||||
"slug": "admin",
|
||||
"name": "Phone, subscriptions and admin",
|
||||
"sortOrder": 40
|
||||
},
|
||||
{
|
||||
"slug": "clients",
|
||||
"name": "Clients",
|
||||
"sortOrder": 50
|
||||
},
|
||||
{
|
||||
"slug": "website",
|
||||
"name": "Website (stanrunge.dev)",
|
||||
"sortOrder": 60
|
||||
},
|
||||
{
|
||||
"slug": "setup",
|
||||
"name": "Setup and security",
|
||||
"description": "Perma evolvable setup, before 2027",
|
||||
"sortOrder": 70
|
||||
},
|
||||
{
|
||||
"slug": "home",
|
||||
"name": "Home",
|
||||
"sortOrder": 80
|
||||
},
|
||||
{
|
||||
"slug": "health",
|
||||
"name": "Health and self-care",
|
||||
"sortOrder": 90
|
||||
},
|
||||
{
|
||||
"slug": "reading",
|
||||
"name": "Reading",
|
||||
"sortOrder": 100
|
||||
},
|
||||
{
|
||||
"slug": "planning",
|
||||
"name": "Planning and goals",
|
||||
"sortOrder": 110
|
||||
}
|
||||
],
|
||||
"goals": [
|
||||
{
|
||||
"title": "€10k net/month into own Revolut by 2030",
|
||||
"area": "planning",
|
||||
"targetDate": "2030-01-01",
|
||||
"metric": "Net income per month",
|
||||
"metricUnit": "EUR",
|
||||
"metricTarget": 10000
|
||||
},
|
||||
{
|
||||
"title": "Own a really nice Ford Mustang GT"
|
||||
},
|
||||
{
|
||||
"title": "PC overhaul",
|
||||
"notes": "Top AMD X3D CPU, RTX 5090.",
|
||||
"metric": "Budget",
|
||||
"metricUnit": "EUR",
|
||||
"metricTarget": 10000
|
||||
},
|
||||
{
|
||||
"title": "Organise goals into a plan",
|
||||
"area": "planning",
|
||||
"notes": "Social media, Grok Bot, IT company."
|
||||
},
|
||||
{
|
||||
"title": "Perma evolvable setup foundation ready before 2027",
|
||||
"area": "setup",
|
||||
"targetDate": "2026-12-31"
|
||||
},
|
||||
{
|
||||
"title": "Get in shape long term",
|
||||
"area": "health"
|
||||
}
|
||||
],
|
||||
"tasks": [
|
||||
{
|
||||
"area": "deadlines",
|
||||
"title": "Jan Loman printer visit",
|
||||
"status": "done",
|
||||
"notes": "Settled.",
|
||||
"due": "2026-10-24",
|
||||
"dueTime": "14:00"
|
||||
},
|
||||
{
|
||||
"area": "deadlines",
|
||||
"title": "Call BackHome about Kibi's chip",
|
||||
"status": "todo",
|
||||
"due": "2026-10-26",
|
||||
"dueTime": "10:00",
|
||||
"tags": ["kibi", "call"]
|
||||
},
|
||||
{
|
||||
"area": "deadlines",
|
||||
"title": "Cargill collective health insurance (OHRA, code 114)",
|
||||
"status": "todo",
|
||||
"notes": "Needs current insurer + premium to compare.",
|
||||
"due": "2026-12-31",
|
||||
"tags": ["insurance"]
|
||||
},
|
||||
{
|
||||
"area": "deadlines",
|
||||
"title": "Reply to mom about insurance info",
|
||||
"status": "todo",
|
||||
"notes": "Waiting since Oct 9.",
|
||||
"tags": ["insurance"]
|
||||
},
|
||||
{
|
||||
"area": "deadlines",
|
||||
"title": "Stop zorgtoeslag from Jan 2027",
|
||||
"status": "todo",
|
||||
"due": "2027-01-01",
|
||||
"reminderAt": "2026-12-07T09:00:00+01:00",
|
||||
"tags": ["admin"]
|
||||
},
|
||||
{
|
||||
"area": "deadlines",
|
||||
"title": "Supermarket product list",
|
||||
"status": "todo",
|
||||
"reminderAt": "2026-10-24T11:50:00+02:00"
|
||||
},
|
||||
{
|
||||
"area": "deadlines",
|
||||
"title": "Book haircut",
|
||||
"status": "todo",
|
||||
"notes": "Around Nov 14.",
|
||||
"due": "2026-11-14"
|
||||
},
|
||||
{
|
||||
"area": "house",
|
||||
"title": "House overview",
|
||||
"status": "in_progress",
|
||||
"flagged": true,
|
||||
"notes": "Applied only to Graaf Hendrikstraat 16 so far."
|
||||
},
|
||||
{
|
||||
"area": "house",
|
||||
"title": "React to house options",
|
||||
"status": "todo",
|
||||
"notes": "Raam €1,475; Burg. van Dijkesingel 214-B; Groenhovenweg 161; Spoorstraat €795; Noothoven van Goorstraat 1-206."
|
||||
},
|
||||
{
|
||||
"area": "house",
|
||||
"title": "Tell Daisy budget, move date, must-haves, Funda/Pararius applications",
|
||||
"status": "todo"
|
||||
},
|
||||
{
|
||||
"area": "house",
|
||||
"title": "Mijndak Midden-Holland sign-up",
|
||||
"status": "todo",
|
||||
"flagged": true,
|
||||
"notes": "€23 option."
|
||||
},
|
||||
{
|
||||
"area": "house",
|
||||
"title": "Sign up: socialehuurwoning.nl, Vesteda Goudshof, VBT, Viramah, agency Jesse's mom uses",
|
||||
"status": "todo"
|
||||
},
|
||||
{
|
||||
"area": "house",
|
||||
"title": "House tracker/scraper on the site",
|
||||
"status": "todo",
|
||||
"tags": ["website"]
|
||||
},
|
||||
{
|
||||
"area": "house",
|
||||
"title": "Update address everywhere",
|
||||
"status": "todo",
|
||||
"notes": "Checklist ready."
|
||||
},
|
||||
{
|
||||
"area": "house",
|
||||
"title": "UniFi network for the new place",
|
||||
"status": "todo",
|
||||
"tags": ["network"]
|
||||
},
|
||||
{
|
||||
"area": "kibi",
|
||||
"title": "Food ordered",
|
||||
"status": "done"
|
||||
},
|
||||
{
|
||||
"area": "kibi",
|
||||
"title": "Chip scanner ordered",
|
||||
"status": "done"
|
||||
},
|
||||
{
|
||||
"area": "kibi",
|
||||
"title": "Chip number found",
|
||||
"status": "done"
|
||||
},
|
||||
{
|
||||
"area": "kibi",
|
||||
"title": "Chip owner change at BackHome",
|
||||
"status": "todo",
|
||||
"notes": "Still 'Kwiebus' under E Zwart."
|
||||
},
|
||||
{
|
||||
"area": "kibi",
|
||||
"title": "Call vet about Kibi's patient card",
|
||||
"status": "todo",
|
||||
"tags": ["call"]
|
||||
},
|
||||
{
|
||||
"area": "kibi",
|
||||
"title": "GPS tracker",
|
||||
"status": "todo",
|
||||
"notes": "Dad may have done it already."
|
||||
},
|
||||
{
|
||||
"area": "kibi",
|
||||
"title": "Dash KIBI",
|
||||
"status": "todo",
|
||||
"flagged": true,
|
||||
"notes": "Meaning unclear, ask Stan."
|
||||
},
|
||||
{
|
||||
"area": "admin",
|
||||
"title": "Basic-Fit cancelled",
|
||||
"status": "done"
|
||||
},
|
||||
{
|
||||
"area": "admin",
|
||||
"title": "Ohra inboedelverzekering cancelled",
|
||||
"status": "done"
|
||||
},
|
||||
{
|
||||
"area": "admin",
|
||||
"title": "Zorgtoeslag income updated for 2026",
|
||||
"status": "done"
|
||||
},
|
||||
{
|
||||
"area": "admin",
|
||||
"title": "Move number to Revolut eSIM, then iPad eSIM, cancel Odido",
|
||||
"status": "in_progress"
|
||||
},
|
||||
{
|
||||
"area": "admin",
|
||||
"title": "Move SIM + WhatsApp from iPhone to Pixel 11 Pro XL",
|
||||
"status": "todo"
|
||||
},
|
||||
{
|
||||
"area": "admin",
|
||||
"title": "Check Oasen and Delta Energie fully cancelled",
|
||||
"status": "todo"
|
||||
},
|
||||
{
|
||||
"area": "admin",
|
||||
"title": "Cancel other unused subscriptions",
|
||||
"status": "todo"
|
||||
},
|
||||
{
|
||||
"area": "admin",
|
||||
"title": "Merge banks into Revolut",
|
||||
"status": "todo"
|
||||
},
|
||||
{
|
||||
"area": "clients",
|
||||
"title": "Quote dack €450 for vet clinic site fix",
|
||||
"status": "done",
|
||||
"tags": ["dack"]
|
||||
},
|
||||
{
|
||||
"area": "clients",
|
||||
"title": "Reply to dack",
|
||||
"status": "done",
|
||||
"tags": ["dack"]
|
||||
},
|
||||
{
|
||||
"area": "clients",
|
||||
"title": "Cloudflare DNS/SSL for dack",
|
||||
"status": "in_progress",
|
||||
"tags": ["dack"],
|
||||
"notes": "Grind-week goal."
|
||||
},
|
||||
{
|
||||
"area": "clients",
|
||||
"title": "Send dack's invoice via Moneybird",
|
||||
"status": "todo",
|
||||
"tags": ["dack"]
|
||||
},
|
||||
{
|
||||
"area": "clients",
|
||||
"title": "Cheap shared WordPress hosting for dack's clients on own VMs",
|
||||
"status": "todo",
|
||||
"tags": ["dack"]
|
||||
},
|
||||
{
|
||||
"area": "clients",
|
||||
"title": "Karolina's Wix site, Master Purpose (NL/PL)",
|
||||
"status": "todo",
|
||||
"tags": ["karolina"]
|
||||
},
|
||||
{
|
||||
"area": "clients",
|
||||
"title": "Wix Studio + Velo/CLI so an AI can edit code",
|
||||
"status": "todo",
|
||||
"tags": ["karolina"]
|
||||
},
|
||||
{
|
||||
"area": "website",
|
||||
"title": "DB reorganised into schemas, staging retired",
|
||||
"status": "done"
|
||||
},
|
||||
{
|
||||
"area": "website",
|
||||
"title": "Energy tracker (NED data) live",
|
||||
"status": "done"
|
||||
},
|
||||
{
|
||||
"area": "website",
|
||||
"title": "Health tracker (private) + 15 RIVM vaccinations",
|
||||
"status": "done"
|
||||
},
|
||||
{
|
||||
"area": "website",
|
||||
"title": "Book tracker, reader, 362 books live",
|
||||
"status": "done"
|
||||
},
|
||||
{
|
||||
"area": "website",
|
||||
"title": "Reader zoom, 2-page view, scroll/paging",
|
||||
"status": "in_progress"
|
||||
},
|
||||
{
|
||||
"area": "website",
|
||||
"title": "Money tracker with MoneyMan",
|
||||
"status": "in_progress"
|
||||
},
|
||||
{
|
||||
"area": "website",
|
||||
"title": "NED backfill",
|
||||
"status": "in_progress"
|
||||
},
|
||||
{
|
||||
"area": "website",
|
||||
"title": "Music listening tracker + now-playing",
|
||||
"status": "todo"
|
||||
},
|
||||
{
|
||||
"area": "website",
|
||||
"title": "Task/goal tracker to replace TickTick",
|
||||
"status": "in_progress"
|
||||
},
|
||||
{
|
||||
"area": "website",
|
||||
"title": "Hardware DB (current PC parts first)",
|
||||
"status": "todo"
|
||||
},
|
||||
{
|
||||
"area": "website",
|
||||
"title": "List of every organisation holding my data and what they know",
|
||||
"status": "todo"
|
||||
},
|
||||
{
|
||||
"area": "website",
|
||||
"title": "Move to SvelteKit 3",
|
||||
"status": "todo"
|
||||
},
|
||||
{
|
||||
"area": "website",
|
||||
"title": "Juhee website data",
|
||||
"status": "todo"
|
||||
},
|
||||
{
|
||||
"area": "setup",
|
||||
"title": "AWS cleanup ($0/month)",
|
||||
"status": "done",
|
||||
"goal": "Perma evolvable setup foundation ready before 2027"
|
||||
},
|
||||
{
|
||||
"area": "setup",
|
||||
"title": "Google Cloud cleanup",
|
||||
"status": "done",
|
||||
"goal": "Perma evolvable setup foundation ready before 2027"
|
||||
},
|
||||
{
|
||||
"area": "setup",
|
||||
"title": "stan-vm hardened (ufw, Tailscale-only SSH)",
|
||||
"status": "done",
|
||||
"goal": "Perma evolvable setup foundation ready before 2027"
|
||||
},
|
||||
{
|
||||
"area": "setup",
|
||||
"title": "Nightly restic backups to B2 + Storage Box",
|
||||
"status": "done",
|
||||
"goal": "Perma evolvable setup foundation ready before 2027"
|
||||
},
|
||||
{
|
||||
"area": "setup",
|
||||
"title": "WhatsApp bridge on stan-vm",
|
||||
"status": "done",
|
||||
"goal": "Perma evolvable setup foundation ready before 2027"
|
||||
},
|
||||
{
|
||||
"area": "setup",
|
||||
"title": "gcloud + moneybird-cli on stan-vm",
|
||||
"status": "done",
|
||||
"goal": "Perma evolvable setup foundation ready before 2027"
|
||||
},
|
||||
{
|
||||
"area": "setup",
|
||||
"title": "Domains Squarespace to Cloudflare",
|
||||
"status": "in_progress",
|
||||
"goal": "Perma evolvable setup foundation ready before 2027",
|
||||
"notes": "Both transfers pending.",
|
||||
"due": "2026-10-29"
|
||||
},
|
||||
{
|
||||
"area": "setup",
|
||||
"title": "Syncthing: Introducer + Auto Accept on Arch and Windows",
|
||||
"status": "in_progress",
|
||||
"goal": "Perma evolvable setup foundation ready before 2027"
|
||||
},
|
||||
{
|
||||
"area": "setup",
|
||||
"title": "Backup follow-up: restic passwords into 1Password",
|
||||
"status": "todo",
|
||||
"goal": "Perma evolvable setup foundation ready before 2027",
|
||||
"due": "2026-10-24",
|
||||
"dueTime": "11:00"
|
||||
},
|
||||
{
|
||||
"area": "setup",
|
||||
"title": "Delete empty B2 bucket",
|
||||
"status": "todo",
|
||||
"goal": "Perma evolvable setup foundation ready before 2027",
|
||||
"due": "2026-10-25"
|
||||
},
|
||||
{
|
||||
"area": "setup",
|
||||
"title": "Rotate weak admin secret + DB password",
|
||||
"status": "todo",
|
||||
"goal": "Perma evolvable setup foundation ready before 2027",
|
||||
"tags": ["security"]
|
||||
},
|
||||
{
|
||||
"area": "setup",
|
||||
"title": "1Password overhaul",
|
||||
"status": "todo",
|
||||
"goal": "Perma evolvable setup foundation ready before 2027",
|
||||
"notes": "Imports, 2FA, passkeys, dedupe, delete unused accounts.",
|
||||
"tags": ["security"]
|
||||
},
|
||||
{
|
||||
"area": "setup",
|
||||
"title": "Moneybird API token, then clean up Moneybird",
|
||||
"status": "todo",
|
||||
"goal": "Perma evolvable setup foundation ready before 2027"
|
||||
},
|
||||
{
|
||||
"area": "setup",
|
||||
"title": "Authentik SSO on stan-vm",
|
||||
"status": "todo",
|
||||
"goal": "Perma evolvable setup foundation ready before 2027"
|
||||
},
|
||||
{
|
||||
"area": "setup",
|
||||
"title": "vash-prod memory warnings + finish security audit",
|
||||
"status": "todo",
|
||||
"goal": "Perma evolvable setup foundation ready before 2027",
|
||||
"tags": ["security"]
|
||||
},
|
||||
{
|
||||
"area": "setup",
|
||||
"title": "Remove old grok-bot devices from Tailscale",
|
||||
"status": "todo",
|
||||
"goal": "Perma evolvable setup foundation ready before 2027"
|
||||
},
|
||||
{
|
||||
"area": "setup",
|
||||
"title": "Close empty GCP billing account; decide on AWS 'Vash Software'",
|
||||
"status": "todo",
|
||||
"goal": "Perma evolvable setup foundation ready before 2027",
|
||||
"notes": "AWS part optional."
|
||||
},
|
||||
{
|
||||
"area": "setup",
|
||||
"title": "Raise WSL RAM",
|
||||
"status": "todo",
|
||||
"goal": "Perma evolvable setup foundation ready before 2027",
|
||||
"notes": "Sees 8 of 16 GB."
|
||||
},
|
||||
{
|
||||
"area": "setup",
|
||||
"title": "Home server rebuild with ZFS",
|
||||
"status": "todo",
|
||||
"goal": "Perma evolvable setup foundation ready before 2027"
|
||||
},
|
||||
{
|
||||
"area": "setup",
|
||||
"title": "Analytics, monitoring, alerts across devices",
|
||||
"status": "todo",
|
||||
"goal": "Perma evolvable setup foundation ready before 2027"
|
||||
},
|
||||
{
|
||||
"area": "setup",
|
||||
"title": "Mouse sensitivity cm/360 across devices",
|
||||
"status": "todo",
|
||||
"goal": "Perma evolvable setup foundation ready before 2027"
|
||||
},
|
||||
{
|
||||
"area": "setup",
|
||||
"title": "Buy Logitech G840 XL mousepad",
|
||||
"status": "todo",
|
||||
"goal": "Perma evolvable setup foundation ready before 2027",
|
||||
"tags": ["buy"]
|
||||
},
|
||||
{
|
||||
"area": "setup",
|
||||
"title": "Router and networking setup",
|
||||
"status": "todo",
|
||||
"goal": "Perma evolvable setup foundation ready before 2027",
|
||||
"tags": ["network"]
|
||||
},
|
||||
{
|
||||
"area": "setup",
|
||||
"title": "J5 Hexagon research, then move wachtboden",
|
||||
"status": "todo",
|
||||
"goal": "Perma evolvable setup foundation ready before 2027"
|
||||
},
|
||||
{
|
||||
"area": "setup",
|
||||
"title": "Agent-deck + AI/Linux dev setup",
|
||||
"status": "todo",
|
||||
"goal": "Perma evolvable setup foundation ready before 2027"
|
||||
},
|
||||
{
|
||||
"area": "setup",
|
||||
"title": "MITM/certificate lab",
|
||||
"status": "todo",
|
||||
"goal": "Perma evolvable setup foundation ready before 2027",
|
||||
"notes": "Idea.",
|
||||
"tags": ["idea"]
|
||||
},
|
||||
{
|
||||
"area": "home",
|
||||
"title": "Unpack boxes; organise batteries and tools; buy thermometer",
|
||||
"status": "todo"
|
||||
},
|
||||
{
|
||||
"area": "home",
|
||||
"title": "Clean desk, laundry, clothes",
|
||||
"status": "todo"
|
||||
},
|
||||
{
|
||||
"area": "home",
|
||||
"title": "Connect vinyl player, printer, monitor, switch, osu! pad, tablet, fridge, microwave",
|
||||
"status": "todo"
|
||||
},
|
||||
{
|
||||
"area": "home",
|
||||
"title": "Hue lights + Home Assistant + Apple Home",
|
||||
"status": "todo"
|
||||
},
|
||||
{
|
||||
"area": "home",
|
||||
"title": "Fan controller",
|
||||
"status": "todo"
|
||||
},
|
||||
{
|
||||
"area": "home",
|
||||
"title": "Opa's stuff (WhatsApp, sensor install)",
|
||||
"status": "todo"
|
||||
},
|
||||
{
|
||||
"area": "health",
|
||||
"title": "Getting in shape with Doc",
|
||||
"status": "in_progress",
|
||||
"notes": "Lesson 1 done; ~2,300–2,400 kcal and 160–190 g protein a day.",
|
||||
"goal": "Get in shape long term"
|
||||
},
|
||||
{
|
||||
"area": "health",
|
||||
"title": "Join the gym my parents use",
|
||||
"status": "todo",
|
||||
"goal": "Get in shape long term"
|
||||
},
|
||||
{
|
||||
"area": "health",
|
||||
"title": "Brush teeth twice a day, shower, hair care",
|
||||
"status": "todo",
|
||||
"tags": ["habit"]
|
||||
},
|
||||
{
|
||||
"area": "health",
|
||||
"title": "Dentist checkup",
|
||||
"status": "todo"
|
||||
},
|
||||
{
|
||||
"area": "health",
|
||||
"title": "Pick hardware for Withings/Garmin syncs",
|
||||
"status": "todo"
|
||||
},
|
||||
{
|
||||
"area": "health",
|
||||
"title": "Curaçao vaccines",
|
||||
"status": "todo",
|
||||
"notes": "Via Doc."
|
||||
},
|
||||
{
|
||||
"area": "reading",
|
||||
"title": "Read 'De meeste mensen deugen' (Bregman)",
|
||||
"status": "todo"
|
||||
},
|
||||
{
|
||||
"area": "reading",
|
||||
"title": "Re-download 'Haves and Have-Nots'",
|
||||
"status": "todo"
|
||||
},
|
||||
{
|
||||
"area": "reading",
|
||||
"title": "Book questions: duplicates, 6 unknown files, Nick's repo 404",
|
||||
"status": "todo"
|
||||
},
|
||||
{
|
||||
"area": "planning",
|
||||
"title": "Schedule sync (paper agenda, TickTick, Daisy)",
|
||||
"status": "todo",
|
||||
"goal": "Organise goals into a plan"
|
||||
}
|
||||
]
|
||||
}
|
||||
7
data/tasks/ticktick-merges.json
Normal file
7
data/tasks/ticktick-merges.json
Normal file
@@ -0,0 +1,7 @@
|
||||
[
|
||||
{ "ticktickId": "6ad52191ebcd7400000000ce", "title": "Jan Loman printer visit" },
|
||||
{ "ticktickId": "690d2fd98c824268f297a676", "title": "Router and networking setup" },
|
||||
{ "ticktickId": "6844769df471d103461fa9f1", "title": "Home server rebuild with ZFS" },
|
||||
{ "ticktickId": "6ab6f32759b6090811e91a00", "title": "Dentist checkup" },
|
||||
{ "ticktickId": "68225a8dbfe81104eac49a7a", "title": "Merge banks into Revolut" }
|
||||
]
|
||||
17
db/reorg/01_roles.sql
Normal file
17
db/reorg/01_roles.sql
Normal file
@@ -0,0 +1,17 @@
|
||||
-- 01_roles.sql: cluster-level roles. Run as a superuser (postgres). Idempotent.
|
||||
-- In prod: needs `sudo docker exec -i <pg-container> psql -U postgres`; set passwords separately (\password), never in this file.
|
||||
DO $$
|
||||
BEGIN
|
||||
IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'personal_app') THEN
|
||||
CREATE ROLE personal_app LOGIN; -- app, ned-collector, house worker
|
||||
END IF;
|
||||
IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'personal_ddl') THEN
|
||||
CREATE ROLE personal_ddl NOLOGIN; -- owns schema lists; only reachable via SECURITY DEFINER fns
|
||||
END IF;
|
||||
IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'personal_ro') THEN
|
||||
CREATE ROLE personal_ro LOGIN; -- read-only for audits/agents/pgAdmin
|
||||
END IF;
|
||||
END $$;
|
||||
ALTER ROLE personal_ro SET default_transaction_read_only = on;
|
||||
ALTER ROLE personal_app SET search_path = '$user'; -- force schema-qualified names in app SQL
|
||||
ALTER ROLE personal_ro SET search_path = '$user';
|
||||
142
db/reorg/02_schemas_move.sql
Normal file
142
db/reorg/02_schemas_move.sql
Normal file
@@ -0,0 +1,142 @@
|
||||
-- 02_schemas_move.sql: create schemas, snapshot counts, move + rename all 58 public tables.
|
||||
SET ROLE personal;
|
||||
|
||||
CREATE SCHEMA auth; CREATE SCHEMA money; CREATE SCHEMA house; CREATE SCHEMA ref;
|
||||
CREATE SCHEMA games; CREATE SCHEMA cs; CREATE SCHEMA animaldex; CREATE SCHEMA site;
|
||||
CREATE SCHEMA meta; CREATE SCHEMA hardware; CREATE SCHEMA energy; CREATE SCHEMA legacy;
|
||||
COMMENT ON SCHEMA legacy IS 'Pre-reorg list tables + reorg bookkeeping. Drop 30 days after cutover.';
|
||||
COMMENT ON SCHEMA ref IS 'Seeded third-party catalog data (Pokemon, CoD tracker definitions). Read-only in the GUI; seeders own it.';
|
||||
COMMENT ON SCHEMA games IS 'Stan''s own game progress.';
|
||||
|
||||
-- exact row counts of every public table before anything moves
|
||||
CREATE TABLE legacy.reorg_counts_before (table_name text PRIMARY KEY, n bigint NOT NULL);
|
||||
DO $$
|
||||
DECLARE t text; c bigint;
|
||||
BEGIN
|
||||
FOR t IN SELECT tablename FROM pg_tables WHERE schemaname = 'public' LOOP
|
||||
EXECUTE format('SELECT count(*) FROM public.%I', t) INTO c;
|
||||
INSERT INTO legacy.reorg_counts_before VALUES (t, c);
|
||||
END LOOP;
|
||||
END $$;
|
||||
|
||||
CREATE TABLE legacy.reorg_table_map (
|
||||
old_name text PRIMARY KEY,
|
||||
old_prefix text NOT NULL, -- prefix used by its index/constraint/sequence names
|
||||
new_schema text NOT NULL,
|
||||
new_name text NOT NULL,
|
||||
UNIQUE (new_schema, new_name)
|
||||
);
|
||||
INSERT INTO legacy.reorg_table_map (old_name, old_prefix, new_schema, new_name) VALUES
|
||||
('user','user','auth','user'), ('session','session','auth','session'),
|
||||
('account','account','auth','account'), ('verification','verification','auth','verification'),
|
||||
('money_accounts','money_accounts','money','accounts'), ('money_transactions','money_transactions','money','transactions'),
|
||||
('money_recurring','money_recurring','money','recurring'), ('money_forecasts','money_forecasts','money','forecasts'),
|
||||
('money_insurances','money_insurances','money','insurances'), ('money_wishlist','money_wishlist','money','wishlist'),
|
||||
('money_projection_settings','money_projection_settings','money','projection_settings'),
|
||||
('money_utility_settings','money_utility_settings','money','utility_settings'),
|
||||
('money_utility_readings','money_utility_readings','money','utility_readings'),
|
||||
('house_listings','house_listings','house','listings'), ('house_price_history','house_price_history','house','price_history'),
|
||||
('house_searches','house_searches','house','searches'), ('house_worker_runs','house_worker_runs','house','worker_runs'),
|
||||
('house_alert_rules','house_alert_rules','house','alert_rules'),
|
||||
('pokemon','pokemon','ref','pokemon'), ('games','game','ref','pokemon_games'),
|
||||
('pokemon_game_entries','pokemon_game_entry','ref','pokemon_game_entries'),
|
||||
('pokemon_locations','pokemon_locations','ref','pokemon_locations'), ('pokemon_encounters','pokemon_encounters','ref','pokemon_encounters'),
|
||||
('pokemon_evolutions','pokemon_evolutions','ref','pokemon_evolutions'), ('pokemon_dex_numbers','pokemon_dex_numbers','ref','pokemon_dex_numbers'),
|
||||
('pokemon_moves','pokemon_moves','ref','pokemon_moves'), ('pokemon_learnsets','pokemon_learnsets','ref','pokemon_learnsets'),
|
||||
('pokemon_research','pokemon_research','ref','pokemon_research'), ('pokemon_game_research','pokemon_game_research','ref','pokemon_game_research'),
|
||||
('tracker_games','tracker_games','ref','tracker_games'), ('tracker_items','tracker_items','ref','tracker_items'),
|
||||
('tracker_challenges','tracker_challenges','ref','tracker_challenges'),
|
||||
('user_games','user_game','games','catch_em_all_games'), ('user_entries','user_entry','games','catch_em_all_entries'),
|
||||
('catch_em_all_settings','catch_em_all_settings','games','catch_em_all_settings'),
|
||||
('pokemon_journeys','pokemon_journeys','games','pokemon_journeys'), ('pokemon_seen','pokemon_seen','games','pokemon_seen'),
|
||||
('pokemon_catches','pokemon_catches','games','pokemon_catches'), ('pokemon_milestones','pokemon_milestones','games','pokemon_milestones'),
|
||||
('tracker_progress','tracker_progress','games','tracker_progress'), ('tracker_ranks','tracker_ranks','games','tracker_ranks'),
|
||||
('tracker_weapon_ranks','tracker_weapon_ranks','games','tracker_weapon_ranks'),
|
||||
('tracker_challenge_progress','tracker_challenge_progress','games','tracker_challenge_progress'),
|
||||
('osu_entries','osu_entries','games','osu_entries'),
|
||||
('cs_market_items','cs_market_items','cs','items'), ('cs_price_observations','cs_price_observations','cs','price_observations'),
|
||||
('cs_item_dimensions','cs_item_dimensions','cs','item_dimensions'), ('cs_price_streams','cs_price_streams','cs','price_streams'),
|
||||
('animals','animals','animaldex','animals'), ('sightings','sightings','animaldex','sightings'),
|
||||
('photos','photos','animaldex','sighting_photos'),
|
||||
('blog_articles','blog_articles','site','blog_articles'), ('tasks','tasks','site','tasks'),
|
||||
('topics','topics','site','topics'), ('tasks_to_topics','tasks_to_topics','site','task_topics'),
|
||||
('lists','lists','legacy','lists'), ('list_items','list_items','legacy','list_items'),
|
||||
('list_item_completions','list_item_completions','legacy','list_item_completions');
|
||||
|
||||
DO $$
|
||||
DECLARE
|
||||
missing text;
|
||||
BEGIN
|
||||
SELECT string_agg(tablename, ', ') INTO missing FROM pg_tables
|
||||
WHERE schemaname = 'public' AND tablename NOT IN (SELECT old_name FROM legacy.reorg_table_map);
|
||||
IF missing IS NOT NULL THEN RAISE EXCEPTION 'unmapped public tables: %', missing; END IF;
|
||||
END $$;
|
||||
|
||||
-- types + shared trigger function
|
||||
ALTER TYPE public.availability SET SCHEMA ref;
|
||||
ALTER TYPE public.blog_article_status SET SCHEMA site;
|
||||
ALTER FUNCTION public.site_touch_updated_at() SET SCHEMA meta;
|
||||
ALTER FUNCTION meta.site_touch_updated_at() RENAME TO touch_updated_at;
|
||||
|
||||
-- Duplicate indexes (hand SQL + Drizzle both created one). Keep Drizzle's `_index` / constraint-backed `_key`.
|
||||
DROP INDEX public.games_source_id_unique; -- dup of constraint games_source_id_key
|
||||
DROP INDEX public.pokemon_number_unique; -- dup of constraint pokemon_number_key
|
||||
DROP INDEX public.catch_em_all_settings_user_id_unique;-- dup of constraint ..._user_id_key
|
||||
DROP INDEX public.money_accounts_user_id_idx;
|
||||
DROP INDEX public.money_forecasts_user_date_idx;
|
||||
DROP INDEX public.money_insurances_user_id_idx;
|
||||
DROP INDEX public.money_recurring_user_id_idx;
|
||||
DROP INDEX public.money_transactions_account_id_idx;
|
||||
DROP INDEX public.money_transactions_user_date_idx;
|
||||
DROP INDEX public.money_wishlist_user_id_idx;
|
||||
DROP INDEX public.lists_slug_unique; -- dup of lists_slug_key (legacy table, tidy anyway)
|
||||
|
||||
-- Move + rename tables; rename their constraints, indexes and owned sequences to the new prefix.
|
||||
CREATE FUNCTION pg_temp.reprefix(name text, old_name text, old_prefix text, new_name text) RETURNS text
|
||||
LANGUAGE sql IMMUTABLE AS $$
|
||||
SELECT CASE
|
||||
WHEN name LIKE old_name || '\_%' THEN new_name || substr(name, length(old_name) + 1)
|
||||
WHEN name LIKE old_prefix || '\_%' THEN new_name || substr(name, length(old_prefix) + 1)
|
||||
ELSE NULL END
|
||||
$$;
|
||||
|
||||
DO $$
|
||||
DECLARE
|
||||
m record; r record; tbl regclass; newn text;
|
||||
BEGIN
|
||||
FOR m IN SELECT * FROM legacy.reorg_table_map ORDER BY old_name LOOP
|
||||
EXECUTE format('ALTER TABLE public.%I SET SCHEMA %I', m.old_name, m.new_schema);
|
||||
IF m.old_name <> m.new_name THEN
|
||||
EXECUTE format('ALTER TABLE %I.%I RENAME TO %I', m.new_schema, m.old_name, m.new_name);
|
||||
END IF;
|
||||
tbl := format('%I.%I', m.new_schema, m.new_name)::regclass;
|
||||
-- constraints (renaming a pk/unique constraint renames its index too)
|
||||
FOR r IN SELECT conname AS n FROM pg_constraint WHERE conrelid = tbl LOOP
|
||||
newn := pg_temp.reprefix(r.n, m.old_name, m.old_prefix, m.new_name);
|
||||
IF newn IS NOT NULL AND newn <> r.n THEN
|
||||
EXECUTE format('ALTER TABLE %s RENAME CONSTRAINT %I TO %I', tbl, r.n, newn);
|
||||
END IF;
|
||||
END LOOP;
|
||||
-- remaining plain indexes
|
||||
FOR r IN SELECT i.relname AS n FROM pg_index x JOIN pg_class i ON i.oid = x.indexrelid
|
||||
WHERE x.indrelid = tbl
|
||||
AND NOT EXISTS (SELECT 1 FROM pg_constraint c WHERE c.conindid = x.indexrelid AND c.conrelid = tbl) LOOP
|
||||
newn := pg_temp.reprefix(r.n, m.old_name, m.old_prefix, m.new_name);
|
||||
IF newn IS NOT NULL AND newn <> r.n THEN
|
||||
EXECUTE format('ALTER INDEX %I.%I RENAME TO %I', m.new_schema, r.n, newn);
|
||||
END IF;
|
||||
END LOOP;
|
||||
-- owned sequences
|
||||
FOR r IN SELECT s.relname AS n FROM pg_class s JOIN pg_depend d ON d.objid = s.oid AND d.deptype IN ('a','i')
|
||||
WHERE s.relkind = 'S' AND d.refobjid = tbl LOOP
|
||||
newn := pg_temp.reprefix(r.n, m.old_name, m.old_prefix, m.new_name);
|
||||
IF newn IS NOT NULL AND newn <> r.n THEN
|
||||
EXECUTE format('ALTER SEQUENCE %I.%I RENAME TO %I', m.new_schema, r.n, newn);
|
||||
END IF;
|
||||
END LOOP;
|
||||
END LOOP;
|
||||
IF EXISTS (SELECT 1 FROM pg_tables WHERE schemaname = 'public') THEN
|
||||
RAISE EXCEPTION 'public still has tables';
|
||||
END IF;
|
||||
END $$;
|
||||
RESET ROLE;
|
||||
32
db/reorg/03_fixes.sql
Normal file
32
db/reorg/03_fixes.sql
Normal file
@@ -0,0 +1,32 @@
|
||||
-- 03_fixes.sql: missing PKs, type fixes, CHECKs, updated_at columns.
|
||||
SET ROLE personal;
|
||||
|
||||
-- missing primary keys (promote the existing unique indexes)
|
||||
ALTER TABLE cs.item_dimensions ADD CONSTRAINT item_dimensions_pkey PRIMARY KEY USING INDEX item_dimensions_unique_idx;
|
||||
ALTER TABLE cs.price_streams ADD CONSTRAINT price_streams_pkey PRIMARY KEY USING INDEX price_streams_unique_idx;
|
||||
ALTER TABLE house.price_history ADD CONSTRAINT price_history_pkey PRIMARY KEY (listing_id, observed_at);
|
||||
DROP INDEX house.price_history_listing_idx; -- now covered by the PK
|
||||
|
||||
-- include_recurring was an int used as a boolean (app code: charts.ts `!== 0`)
|
||||
ALTER TABLE money.projection_settings ALTER COLUMN include_recurring DROP DEFAULT;
|
||||
ALTER TABLE money.projection_settings ALTER COLUMN include_recurring TYPE boolean USING include_recurring <> 0;
|
||||
ALTER TABLE money.projection_settings ALTER COLUMN include_recurring SET DEFAULT true;
|
||||
|
||||
-- osu numbers stored as text (0 rows in prod)
|
||||
ALTER TABLE games.osu_entries ALTER COLUMN pp TYPE numeric USING nullif(pp, '')::numeric;
|
||||
ALTER TABLE games.osu_entries ALTER COLUMN accuracy TYPE numeric USING nullif(replace(accuracy, '%', ''), '')::numeric;
|
||||
ALTER TABLE games.osu_entries ALTER COLUMN rate DROP DEFAULT;
|
||||
ALTER TABLE games.osu_entries ALTER COLUMN rate TYPE numeric(4,2) USING nullif(rate, '')::numeric;
|
||||
ALTER TABLE games.osu_entries ALTER COLUMN rate SET DEFAULT 1.00;
|
||||
|
||||
-- CHECKs matching the values the app already enforces (src/lib/server/money-tracker.ts, money page)
|
||||
ALTER TABLE money.recurring ADD CONSTRAINT recurring_cadence_check CHECK (cadence IN ('weekly','monthly','yearly'));
|
||||
ALTER TABLE money.insurances ADD CONSTRAINT insurances_cadence_check CHECK (cadence IN ('weekly','monthly','yearly'));
|
||||
ALTER TABLE money.accounts ADD CONSTRAINT accounts_kind_check CHECK (kind IN ('spendable','investment'));
|
||||
ALTER TABLE money.utility_settings ADD CONSTRAINT utility_settings_kind_check CHECK (kind IN ('electricity','gas','water'));
|
||||
ALTER TABLE money.utility_readings ADD CONSTRAINT utility_readings_kind_check CHECK (kind IN ('electricity','gas','water'));
|
||||
ALTER TABLE money.projection_settings ADD CONSTRAINT projection_settings_contribution_mode_check CHECK (contribution_mode IN ('excess','fixed'));
|
||||
|
||||
-- user-editable tables without updated_at
|
||||
ALTER TABLE house.alert_rules ADD COLUMN updated_at timestamptz NOT NULL DEFAULT now();
|
||||
RESET ROLE;
|
||||
224
db/reorg/04_meta.sql
Normal file
224
db/reorg/04_meta.sql
Normal file
@@ -0,0 +1,224 @@
|
||||
-- 04_meta.sql: GUI registry (meta.tables / meta.columns), runtime-DDL functions, schema `lists`.
|
||||
SET ROLE personal;
|
||||
|
||||
CREATE TABLE meta.tables (
|
||||
id integer GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
|
||||
schema_name text NOT NULL,
|
||||
table_name text NOT NULL,
|
||||
slug text NOT NULL UNIQUE CHECK (slug ~ '^[a-z0-9][a-z0-9-]{0,62}$'),
|
||||
display_name text NOT NULL,
|
||||
description text,
|
||||
icon text,
|
||||
picture_url text,
|
||||
kind text NOT NULL CHECK (kind IN ('list','core','ref')),
|
||||
editable boolean NOT NULL DEFAULT true,
|
||||
has_done boolean NOT NULL DEFAULT false,
|
||||
default_sort text,
|
||||
sort_order integer NOT NULL DEFAULT 0,
|
||||
archived_at timestamptz,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
updated_at timestamptz NOT NULL DEFAULT now(),
|
||||
UNIQUE (schema_name, table_name)
|
||||
);
|
||||
COMMENT ON TABLE meta.tables IS 'One row per GUI-visible table (runtime lists + core + ref). Structure truth stays in pg_catalog; this is presentation.';
|
||||
|
||||
CREATE TABLE meta.columns (
|
||||
id integer GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
|
||||
table_id integer NOT NULL REFERENCES meta.tables(id) ON DELETE CASCADE,
|
||||
column_name text NOT NULL,
|
||||
label text,
|
||||
ui_type text NOT NULL CHECK (ui_type IN ('text','longtext','url','image','number','cents','date','datetime','bool','select','multiselect','fk','json')),
|
||||
options jsonb NOT NULL DEFAULT '{}'::jsonb,
|
||||
position integer NOT NULL DEFAULT 0,
|
||||
hidden boolean NOT NULL DEFAULT false,
|
||||
help_text text,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
updated_at timestamptz NOT NULL DEFAULT now(),
|
||||
UNIQUE (table_id, column_name)
|
||||
);
|
||||
CREATE INDEX columns_table_id_index ON meta.columns (table_id);
|
||||
|
||||
CREATE TRIGGER touch_updated_at BEFORE UPDATE ON meta.tables FOR EACH ROW EXECUTE FUNCTION meta.touch_updated_at();
|
||||
CREATE TRIGGER touch_updated_at BEFORE UPDATE ON meta.columns FOR EACH ROW EXECUTE FUNCTION meta.touch_updated_at();
|
||||
|
||||
RESET ROLE;
|
||||
-- schema for runtime lists, owned by personal_ddl (not by the migration owner)
|
||||
CREATE SCHEMA lists AUTHORIZATION personal_ddl;
|
||||
COMMENT ON SCHEMA lists IS 'Runtime-created user lists. Never touched by Drizzle migrations (excluded via schemaFilter). Create/alter only via meta.* functions.';
|
||||
GRANT USAGE, CREATE ON SCHEMA meta TO personal_ddl;
|
||||
GRANT SELECT, INSERT, UPDATE, DELETE ON meta.tables, meta.columns TO personal_ddl;
|
||||
SET ROLE personal_ddl;
|
||||
|
||||
-- guess a ui_type from a column's catalog type (format_type output) and name
|
||||
CREATE FUNCTION meta.guess_ui_type(p_type text, p_col text, p_is_enum boolean DEFAULT false) RETURNS text
|
||||
LANGUAGE sql IMMUTABLE SET search_path = pg_catalog AS $$
|
||||
SELECT CASE
|
||||
WHEN p_is_enum THEN 'select'
|
||||
WHEN p_type LIKE '%[]' THEN 'multiselect'
|
||||
WHEN p_col IN ('picture_url','image_url','icon_url','image') THEN 'image'
|
||||
WHEN p_col LIKE '%\_url' OR p_col = 'url' THEN 'url'
|
||||
WHEN p_col LIKE '%\_cents' THEN 'cents'
|
||||
WHEN p_col IN ('notes','description','content','note') THEN 'longtext'
|
||||
WHEN p_type IN ('smallint','integer','bigint','real','double precision') OR p_type LIKE 'numeric%' THEN 'number'
|
||||
WHEN p_type = 'boolean' THEN 'bool'
|
||||
WHEN p_type = 'date' THEN 'date'
|
||||
WHEN p_type LIKE 'timestamp%' THEN 'datetime'
|
||||
WHEN p_type IN ('json','jsonb') THEN 'json'
|
||||
ELSE 'text' END
|
||||
$$;
|
||||
|
||||
-- reconcile meta.columns with pg_catalog for one table (or all registered tables).
|
||||
-- Uses pg_attribute (not information_schema, which hides tables the caller has no grants on).
|
||||
CREATE FUNCTION meta.sync(p_table_id integer DEFAULT NULL) RETURNS integer
|
||||
LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, meta AS $$
|
||||
DECLARE k integer;
|
||||
BEGIN
|
||||
IF to_regclass('pg_temp._meta_cols') IS NULL THEN CREATE TEMP TABLE _meta_cols ON COMMIT DROP AS SELECT 1::integer AS table_id, ''::text AS col, ''::text AS typ, false AS is_enum, 0::integer AS pos, NULL::jsonb AS vals LIMIT 0; END IF;
|
||||
TRUNCATE _meta_cols;
|
||||
INSERT INTO _meta_cols
|
||||
SELECT t.id, a.attname, format_type(a.atttypid, a.atttypmod), ty.typtype = 'e', a.attnum,
|
||||
CASE WHEN ty.typtype = 'e' THEN (SELECT jsonb_agg(e.enumlabel ORDER BY e.enumsortorder) FROM pg_enum e WHERE e.enumtypid = ty.oid) END
|
||||
FROM meta.tables t
|
||||
JOIN pg_class c ON c.relname = t.table_name
|
||||
JOIN pg_namespace n ON n.oid = c.relnamespace AND n.nspname = t.schema_name
|
||||
JOIN pg_attribute a ON a.attrelid = c.oid AND a.attnum > 0 AND NOT a.attisdropped
|
||||
JOIN pg_type ty ON ty.oid = a.atttypid
|
||||
WHERE p_table_id IS NULL OR t.id = p_table_id;
|
||||
DELETE FROM meta.columns mc
|
||||
WHERE (p_table_id IS NULL OR mc.table_id = p_table_id)
|
||||
AND NOT EXISTS (SELECT 1 FROM _meta_cols x WHERE x.table_id = mc.table_id AND x.col = mc.column_name);
|
||||
INSERT INTO meta.columns (table_id, column_name, label, ui_type, options, position, hidden)
|
||||
SELECT x.table_id, x.col, initcap(replace(x.col, '_', ' ')), meta.guess_ui_type(x.typ, x.col, x.is_enum),
|
||||
CASE WHEN x.vals IS NOT NULL THEN jsonb_build_object('values', x.vals) ELSE '{}'::jsonb END,
|
||||
x.pos, x.col IN ('id','created_at','updated_at','user_id','sort_order')
|
||||
FROM _meta_cols x
|
||||
ON CONFLICT (table_id, column_name) DO NOTHING;
|
||||
GET DIAGNOSTICS k = ROW_COUNT;
|
||||
RETURN k;
|
||||
END $$;
|
||||
|
||||
-- allowed column types for runtime lists -> SQL type
|
||||
CREATE FUNCTION meta.sql_type(p_ui_type text) RETURNS text
|
||||
LANGUAGE sql IMMUTABLE SET search_path = pg_catalog AS $$
|
||||
SELECT CASE p_ui_type
|
||||
WHEN 'text' THEN 'text' WHEN 'longtext' THEN 'text' WHEN 'url' THEN 'text' WHEN 'image' THEN 'text'
|
||||
WHEN 'select' THEN 'text' WHEN 'number' THEN 'numeric' WHEN 'cents' THEN 'integer'
|
||||
WHEN 'date' THEN 'date' WHEN 'datetime' THEN 'timestamptz' WHEN 'bool' THEN 'boolean'
|
||||
WHEN 'multiselect' THEN 'text[]' WHEN 'json' THEN 'jsonb' END
|
||||
$$;
|
||||
|
||||
CREATE FUNCTION meta.assert_ident(p text) RETURNS void
|
||||
LANGUAGE plpgsql IMMUTABLE SET search_path = pg_catalog AS $$
|
||||
BEGIN
|
||||
IF p IS NULL OR p !~ '^[a-z][a-z0-9_]{0,62}$' THEN
|
||||
RAISE EXCEPTION 'invalid identifier: %', p USING ERRCODE = '22023';
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
-- add one column to a runtime list. p_col: {"name","ui_type","label"?,"options"?,"default"?}
|
||||
CREATE FUNCTION meta.add_column(p_slug text, p_col jsonb) RETURNS void
|
||||
LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, meta AS $$
|
||||
DECLARE
|
||||
t meta.tables; c text := p_col->>'name'; u text := coalesce(p_col->>'ui_type', 'text');
|
||||
st text := meta.sql_type(u); vals jsonb := p_col->'options'->'values'; ddl text;
|
||||
BEGIN
|
||||
SELECT * INTO t FROM meta.tables WHERE slug = p_slug AND kind = 'list';
|
||||
IF NOT FOUND THEN RAISE EXCEPTION 'no runtime list %', p_slug; END IF;
|
||||
PERFORM meta.assert_ident(c);
|
||||
IF c IN ('id','name','notes','picture_url','done_at','sort_order','created_at','updated_at') THEN
|
||||
RAISE EXCEPTION 'column % is a standard column', c; END IF;
|
||||
IF st IS NULL THEN RAISE EXCEPTION 'unsupported ui_type %', u; END IF;
|
||||
ddl := format('ALTER TABLE lists.%I ADD COLUMN %I %s', t.table_name, c, st);
|
||||
IF p_col ? 'default' THEN ddl := ddl || format(' DEFAULT %L', p_col->>'default'); END IF;
|
||||
IF u = 'select' AND jsonb_typeof(vals) = 'array' THEN
|
||||
ddl := ddl || format(' CONSTRAINT %I CHECK (%I IN (%s))', t.table_name || '_' || c || '_check', c,
|
||||
(SELECT string_agg(quote_literal(v), ',') FROM jsonb_array_elements_text(vals) v));
|
||||
END IF;
|
||||
EXECUTE ddl;
|
||||
INSERT INTO meta.columns (table_id, column_name, label, ui_type, options, position)
|
||||
VALUES (t.id, c, coalesce(p_col->>'label', initcap(replace(c, '_', ' '))), u,
|
||||
coalesce(p_col->'options', '{}'::jsonb),
|
||||
(SELECT coalesce(max(position), 0) + 1 FROM meta.columns WHERE table_id = t.id));
|
||||
END $$;
|
||||
|
||||
-- create a runtime list table with the standard columns + extras
|
||||
CREATE FUNCTION meta.create_list(
|
||||
p_slug text, p_table text, p_display_name text,
|
||||
p_has_done boolean DEFAULT true, p_columns jsonb DEFAULT '[]'::jsonb,
|
||||
p_picture_url text DEFAULT NULL, p_description text DEFAULT NULL, p_icon text DEFAULT NULL,
|
||||
p_sort_order integer DEFAULT 0
|
||||
) RETURNS integer
|
||||
LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, meta AS $$
|
||||
DECLARE tid integer; col jsonb;
|
||||
BEGIN
|
||||
PERFORM meta.assert_ident(p_table);
|
||||
IF EXISTS (SELECT 1 FROM pg_tables WHERE schemaname = 'lists' AND tablename = p_table) THEN
|
||||
RAISE EXCEPTION 'lists.% already exists', p_table; END IF;
|
||||
EXECUTE format($f$CREATE TABLE lists.%1$I (
|
||||
id integer GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
|
||||
name text NOT NULL CHECK (btrim(name) <> ''),
|
||||
notes text,
|
||||
picture_url text,
|
||||
%2$s
|
||||
sort_order integer NOT NULL DEFAULT 0,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
updated_at timestamptz NOT NULL DEFAULT now())$f$,
|
||||
p_table, CASE WHEN p_has_done THEN 'done_at timestamptz,' ELSE '' END);
|
||||
EXECUTE format('CREATE TRIGGER touch_updated_at BEFORE UPDATE ON lists.%I FOR EACH ROW EXECUTE FUNCTION meta.touch_updated_at()', p_table);
|
||||
INSERT INTO meta.tables (schema_name, table_name, slug, display_name, description, icon, picture_url, kind, editable, has_done, default_sort, sort_order)
|
||||
VALUES ('lists', p_table, p_slug, p_display_name, p_description, p_icon, p_picture_url, 'list', true, p_has_done,
|
||||
CASE WHEN p_has_done THEN 'done_at nulls first, sort_order, name' ELSE 'sort_order, name' END, p_sort_order)
|
||||
RETURNING id INTO tid;
|
||||
PERFORM meta.sync(tid);
|
||||
UPDATE meta.columns SET label = 'Done', ui_type = 'datetime' WHERE table_id = tid AND column_name = 'done_at';
|
||||
FOR col IN SELECT * FROM jsonb_array_elements(p_columns) LOOP
|
||||
PERFORM meta.add_column(p_slug, col);
|
||||
END LOOP;
|
||||
RETURN tid;
|
||||
END $$;
|
||||
|
||||
CREATE FUNCTION meta.rename_column(p_slug text, p_old text, p_new text) RETURNS void
|
||||
LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, meta AS $$
|
||||
DECLARE t meta.tables;
|
||||
BEGIN
|
||||
SELECT * INTO t FROM meta.tables WHERE slug = p_slug AND kind = 'list';
|
||||
IF NOT FOUND THEN RAISE EXCEPTION 'no runtime list %', p_slug; END IF;
|
||||
PERFORM meta.assert_ident(p_new);
|
||||
IF p_old IN ('id','name','done_at','created_at','updated_at') THEN RAISE EXCEPTION 'standard column'; END IF;
|
||||
EXECUTE format('ALTER TABLE lists.%I RENAME COLUMN %I TO %I', t.table_name, p_old, p_new);
|
||||
UPDATE meta.columns SET column_name = p_new WHERE table_id = t.id AND column_name = p_old;
|
||||
END $$;
|
||||
|
||||
CREATE FUNCTION meta.drop_column(p_slug text, p_col text) RETURNS void
|
||||
LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, meta AS $$
|
||||
DECLARE t meta.tables;
|
||||
BEGIN
|
||||
SELECT * INTO t FROM meta.tables WHERE slug = p_slug AND kind = 'list';
|
||||
IF NOT FOUND THEN RAISE EXCEPTION 'no runtime list %', p_slug; END IF;
|
||||
IF p_col IN ('id','name','notes','picture_url','done_at','sort_order','created_at','updated_at') THEN
|
||||
RAISE EXCEPTION 'standard column'; END IF;
|
||||
EXECUTE format('ALTER TABLE lists.%I DROP COLUMN %I', t.table_name, p_col);
|
||||
DELETE FROM meta.columns WHERE table_id = t.id AND column_name = p_col;
|
||||
END $$;
|
||||
|
||||
CREATE FUNCTION meta.archive_list(p_slug text) RETURNS void
|
||||
LANGUAGE sql SECURITY DEFINER SET search_path = pg_catalog, meta AS $$
|
||||
UPDATE meta.tables SET archived_at = now() WHERE slug = p_slug AND kind = 'list' AND archived_at IS NULL
|
||||
$$;
|
||||
|
||||
-- real DROP only after 7 days in the archive (backups cover the rest)
|
||||
CREATE FUNCTION meta.drop_list(p_slug text) RETURNS void
|
||||
LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, meta AS $$
|
||||
DECLARE t meta.tables;
|
||||
BEGIN
|
||||
SELECT * INTO t FROM meta.tables WHERE slug = p_slug AND kind = 'list';
|
||||
IF NOT FOUND THEN RAISE EXCEPTION 'no runtime list %', p_slug; END IF;
|
||||
IF t.archived_at IS NULL OR t.archived_at > now() - interval '7 days' THEN
|
||||
RAISE EXCEPTION 'archive % first and wait 7 days', p_slug; END IF;
|
||||
EXECUTE format('DROP TABLE lists.%I', t.table_name);
|
||||
DELETE FROM meta.tables WHERE id = t.id;
|
||||
END $$;
|
||||
|
||||
RESET ROLE;
|
||||
REVOKE EXECUTE ON ALL FUNCTIONS IN SCHEMA meta FROM PUBLIC;
|
||||
GRANT EXECUTE ON FUNCTION meta.touch_updated_at() TO PUBLIC; -- trigger fn, must stay callable
|
||||
176
db/reorg/05_lists.sql
Normal file
176
db/reorg/05_lists.sql
Normal file
@@ -0,0 +1,176 @@
|
||||
-- 05_lists.sql: the 22 legacy lists become real tables in schema `lists`.
|
||||
-- legacy.* stays untouched (rollback reference); typo fixes + dedupes are applied to a temp working copy.
|
||||
-- Runs as superuser (inserts into personal_ddl-owned tables); tables are created via meta.create_list.
|
||||
|
||||
CREATE TEMP TABLE src ON COMMIT DROP AS
|
||||
SELECT i.id, i.list_id, btrim(i.name) AS name, nullif(btrim(i.description), '') AS notes, i.picture_url,
|
||||
c.created_at AS done_at, i.created_at, i.updated_at
|
||||
FROM legacy.list_items i
|
||||
LEFT JOIN legacy.list_item_completions c ON c.item_id = i.id;
|
||||
|
||||
CREATE TEMP TABLE fix_log (item_id integer, list_id integer, action text, before text, after text) ON COMMIT DROP;
|
||||
|
||||
-- ---- dedupes: keep the lower id, carry over done_at / picture / notes, delete the other ----
|
||||
CREATE TEMP TABLE dedupe (keep_id integer, drop_id integer, new_name text) ON COMMIT DROP;
|
||||
INSERT INTO dedupe VALUES
|
||||
(165, 323, NULL), -- places: New York x2
|
||||
(168, 331, NULL), -- places: France x2
|
||||
(156, 271, 'Advanced Programming in the UNIX Environment'), -- books: "...UNIX® Environment" dup
|
||||
(139, 287, 'Building Knowledge Graphs: A Practitioner''s Guide'); -- books: short + full title
|
||||
UPDATE src k SET done_at = coalesce(k.done_at, d.done_at), picture_url = coalesce(k.picture_url, d.picture_url),
|
||||
notes = coalesce(k.notes, d.notes), name = coalesce(x.new_name, k.name)
|
||||
FROM dedupe x JOIN src d ON d.id = x.drop_id WHERE k.id = x.keep_id;
|
||||
INSERT INTO fix_log SELECT s.id, s.list_id, 'dedupe-drop', s.name, 'merged into ' || x.keep_id FROM src s JOIN dedupe x ON x.drop_id = s.id;
|
||||
DELETE FROM src WHERE id IN (SELECT drop_id FROM dedupe);
|
||||
|
||||
-- ---- typo / naming fixes ----
|
||||
CREATE TEMP TABLE renames (id integer PRIMARY KEY, new_name text NOT NULL) ON COMMIT DROP;
|
||||
INSERT INTO renames VALUES
|
||||
-- vinyls (kept as "Artist - Album" here; split below)
|
||||
(343, 'Pearl Jam - Ten'), (346, 'Amy Winehouse - Back to Black'),
|
||||
(27, 'Slipknot - Vol. 3: (The Subliminal Verses)'), (6, 'Them Crooked Vultures - Them Crooked Vultures'),
|
||||
(39, 'Various Artists - Katamari Damacy (OST)'),
|
||||
(40, 'Gojira - The Way of All Flesh'), (370, 'Gojira - The Link'),
|
||||
(80, 'Deftones - Private Music'), (308, 'Kendrick Lamar - DAMN.'),
|
||||
-- movies/shows
|
||||
(98, 'Kim''s Convenience'), (394, 'Azuki-chan'), (111, 'A Minecraft Movie'),
|
||||
-- books
|
||||
(150, 'Domain-Driven Design: Tackling Complexity in the Heart of Software'),
|
||||
(285, 'Molecular Biology of the Cell'), (288, 'Building Microservices'),
|
||||
(147, 'Scrum: The Art of Doing Twice the Work in Half the Time'),
|
||||
-- places
|
||||
(325, 'Curaçao'),
|
||||
-- cars
|
||||
(322, 'Mazda MX-5 Miata'),
|
||||
-- leetcode
|
||||
(217, 'Two Sum'),
|
||||
-- foods
|
||||
(221, 'Sotteok Sotteok'), (230, 'Bibim Naengmyeon'), (240, 'Doenjang Jjigae'), (241, 'Kimchi Jjigae'),
|
||||
-- papers
|
||||
(190, 'Order Matters: Sequence to Sequence for Sets'),
|
||||
(200, 'GPipe: Easy Scaling with Micro-Batch Pipeline Parallelism'),
|
||||
(203, 'Attention Is All You Need'),
|
||||
(201, 'Deep Speech 2: End-to-End Speech Recognition in English and Mandarin'),
|
||||
-- games
|
||||
(385, 'Chrono Trigger');
|
||||
DO $$ BEGIN
|
||||
IF (SELECT count(*) FROM renames r JOIN src s ON s.id = r.id) <> (SELECT count(*) FROM renames) THEN
|
||||
RAISE EXCEPTION 'rename list references missing item ids'; END IF;
|
||||
END $$;
|
||||
INSERT INTO fix_log SELECT s.id, s.list_id, 'rename', s.name, r.new_name FROM src s JOIN renames r ON r.id = s.id WHERE s.name <> r.new_name;
|
||||
UPDATE src s SET name = r.new_name FROM renames r WHERE r.id = s.id;
|
||||
|
||||
-- helper: copy standard columns for one list into lists.<t> and return the row count
|
||||
CREATE FUNCTION pg_temp.copy_std(p_list integer, p_table text, p_has_done boolean) RETURNS bigint LANGUAGE plpgsql AS $$
|
||||
DECLARE n bigint;
|
||||
BEGIN
|
||||
EXECUTE format('INSERT INTO lists.%I (id, name, notes, picture_url, %s sort_order, created_at, updated_at)
|
||||
SELECT id, name, notes, picture_url, %s row_number() OVER (ORDER BY id), created_at, updated_at
|
||||
FROM src WHERE list_id = %s', p_table,
|
||||
CASE WHEN p_has_done THEN 'done_at,' ELSE '' END,
|
||||
CASE WHEN p_has_done THEN 'done_at,' ELSE '' END, p_list);
|
||||
GET DIAGNOSTICS n = ROW_COUNT;
|
||||
EXECUTE format('SELECT setval(pg_get_serial_sequence(%L, ''id''), (SELECT max(id) FROM lists.%I))', 'lists.' || p_table, p_table);
|
||||
RETURN n;
|
||||
END $$;
|
||||
|
||||
CREATE TEMP TABLE list_map (list_id integer PRIMARY KEY, tbl text NOT NULL, has_done boolean NOT NULL, cols jsonb NOT NULL) ON COMMIT DROP;
|
||||
INSERT INTO list_map VALUES
|
||||
(1, 'vinyls', true, '[{"name":"artist","ui_type":"text"}]'),
|
||||
(2, 'watchlist', true, '[{"name":"kind","ui_type":"select","options":{"values":["movie","show","anime","documentary"]}}]'),
|
||||
(3, 'books', true, '[{"name":"author","ui_type":"text"}]'),
|
||||
(4, 'side_projects', false, '[{"name":"status","ui_type":"select","default":"idea","options":{"values":["idea","active","paused","done","dropped"]}}]'),
|
||||
(5, 'artists_to_see', true, '[]'),
|
||||
(6, 'osu_maps', true, '[{"name":"mods","ui_type":"text"},{"name":"goal","ui_type":"select","options":{"values":["pass","fc"]}}]'),
|
||||
(7, 'animals_to_spot', true, '[]'),
|
||||
(8, 'vehicles_to_spot', true, '[{"name":"kind","ui_type":"select","default":"car","options":{"values":["car","train","plane"]}}]'),
|
||||
(9, 'leetcode', true, '[{"name":"kind","ui_type":"select","default":"problem","options":{"values":["problem","topic"]}},{"name":"url","ui_type":"url"}]'),
|
||||
(10, 'restaurants', true, '[{"name":"city","ui_type":"text"}]'),
|
||||
(11, 'recipes', true, '[{"name":"cuisine","ui_type":"text"},{"name":"recipe_url","ui_type":"url"}]'),
|
||||
(12, 'places', true, '[{"name":"kind","ui_type":"select","options":{"values":["country","region","city","island"]}}]'),
|
||||
(13, 'papers', false, '[{"name":"authors","ui_type":"text"},{"name":"year","ui_type":"number"},{"name":"url","ui_type":"url"}]'),
|
||||
(14, 'piano_songs', true, '[{"name":"artist","ui_type":"text"},{"name":"reference_url","ui_type":"url"}]'),
|
||||
(15, 'games_to_complete', true, '[{"name":"platform","ui_type":"text"}]'),
|
||||
(22, 'things_to_learn', true, '[]'),
|
||||
(23, 'build_from_scratch', true, '[]'),
|
||||
(24, 'tools_to_try', true, '[{"name":"url","ui_type":"url"}]'),
|
||||
(25, 'setup_upgrades', true, '[]'),
|
||||
(26, 'bucket_list', true, '[]'),
|
||||
(27, 'docs_to_read', true, '[{"name":"rfc_number","ui_type":"number"},{"name":"topic","ui_type":"text"},{"name":"starter_pack","ui_type":"bool","default":"false"},{"name":"url","ui_type":"url"}]'),
|
||||
(28, 'banger_songs', false, '[{"name":"artist","ui_type":"text"},{"name":"kind","ui_type":"select","default":"song","options":{"values":["song","playlist"]}},{"name":"url","ui_type":"url"}]');
|
||||
|
||||
DO $$ BEGIN
|
||||
IF EXISTS (SELECT 1 FROM legacy.lists l WHERE NOT EXISTS (SELECT 1 FROM list_map m WHERE m.list_id = l.id))
|
||||
OR EXISTS (SELECT 1 FROM list_map m JOIN legacy.lists l ON l.id = m.list_id WHERE l.is_progress <> m.has_done AND m.list_id NOT IN (4)) THEN
|
||||
RAISE EXCEPTION 'list_map does not cover legacy.lists / has_done mismatch';
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
-- create tables (as personal_ddl via SECURITY DEFINER) and copy standard columns
|
||||
DO $$
|
||||
DECLARE m record; l record; n bigint;
|
||||
BEGIN
|
||||
FOR m IN SELECT * FROM list_map ORDER BY list_id LOOP
|
||||
SELECT * INTO l FROM legacy.lists WHERE id = m.list_id;
|
||||
PERFORM meta.create_list(l.slug, m.tbl, l.name, m.has_done, m.cols, l.picture_url, NULL, NULL, m.list_id);
|
||||
UPDATE meta.tables SET created_at = l.created_at WHERE slug = l.slug;
|
||||
n := pg_temp.copy_std(m.list_id, m.tbl, m.has_done);
|
||||
RAISE NOTICE 'lists.%: % rows', m.tbl, n;
|
||||
END LOOP;
|
||||
END $$;
|
||||
|
||||
-- parsing must not bump updated_at: suppress user triggers for these UPDATEs (superuser only)
|
||||
SET LOCAL session_replication_role = replica;
|
||||
-- ---- per-list parsing into the extra columns (only where it parses cleanly) ----
|
||||
-- vinyls: "Artist - Album"
|
||||
UPDATE lists.vinyls SET artist = split_part(name, ' - ', 1), name = substr(name, strpos(name, ' - ') + 3)
|
||||
WHERE strpos(name, ' - ') > 0;
|
||||
-- books: "Author - Title" only for the 3 personal-name ones (others contain ':' subtitles, never ' - ')
|
||||
UPDATE lists.books SET author = split_part(name, ' - ', 1), name = substr(name, strpos(name, ' - ') + 3)
|
||||
WHERE id IN (28, 29, 306);
|
||||
-- side projects: everything starts as an idea
|
||||
UPDATE lists.side_projects SET status = 'idea';
|
||||
-- osu: trailing mods
|
||||
UPDATE lists.osu_maps SET name = 'Lonely Go', mods = 'HR' WHERE id = 310;
|
||||
UPDATE lists.osu_maps SET name = 'Imagination', mods = 'HR' WHERE id = 317;
|
||||
-- vehicles: all current entries are cars
|
||||
UPDATE lists.vehicles_to_spot SET kind = 'car';
|
||||
-- leetcode
|
||||
UPDATE lists.leetcode SET kind = CASE WHEN name IN ('Two Sum','3Sum') THEN 'problem' ELSE 'topic' END;
|
||||
UPDATE lists.leetcode SET url = 'https://leetcode.com/problems/two-sum/' WHERE name = 'Two Sum';
|
||||
UPDATE lists.leetcode SET url = 'https://leetcode.com/problems/3sum/' WHERE name = '3Sum';
|
||||
-- restaurants: "Name (City)"
|
||||
UPDATE lists.restaurants SET city = substring(name from '\(([^)]+)\)\s*$'), name = btrim(regexp_replace(name, '\s*\([^)]+\)\s*$', ''))
|
||||
WHERE name ~ '\([^)]+\)\s*$';
|
||||
-- recipes
|
||||
UPDATE lists.recipes SET cuisine = CASE WHEN name = 'Yogurt, honey & cruesli' THEN NULL ELSE 'korean' END;
|
||||
-- places
|
||||
UPDATE lists.places SET kind = CASE
|
||||
WHEN name IN ('London','Seattle','Miami','Dubai','New York') THEN 'city'
|
||||
WHEN name IN ('California','Texas') THEN 'region'
|
||||
WHEN name IN ('Curaçao','Aruba','Bonaire') THEN 'island'
|
||||
ELSE 'country' END;
|
||||
-- piano: "artist - title" + URL in notes
|
||||
UPDATE lists.piano_songs SET artist = 'ryo (supercell)', name = 'odds & ends', reference_url = notes, notes = NULL
|
||||
WHERE name = 'ryo/supercell - odds & ends';
|
||||
-- tools: notes that are a bare URL become url
|
||||
UPDATE lists.tools_to_try SET url = notes, notes = NULL WHERE notes ~ '^https?://\S+$';
|
||||
-- docs/RFCs: "RFC n – Title", notes "Topic · starter pack"
|
||||
UPDATE lists.docs_to_read SET
|
||||
rfc_number = substring(name from '^RFC (\d+)')::integer,
|
||||
topic = btrim(split_part(notes, '·', 1)),
|
||||
starter_pack = notes LIKE '%starter pack%',
|
||||
url = 'https://www.rfc-editor.org/rfc/rfc' || substring(name from '^RFC (\d+)'),
|
||||
notes = NULL
|
||||
WHERE name ~ '^RFC \d+';
|
||||
-- banger songs: "Artist - Title" + URL notes; the 2 YT mixes are playlists
|
||||
UPDATE lists.banger_songs SET url = notes, notes = NULL WHERE notes ~ '^https?://\S+$';
|
||||
UPDATE lists.banger_songs SET artist = split_part(name, ' - ', 1), name = substr(name, strpos(name, ' - ') + 3), kind = 'song'
|
||||
WHERE strpos(name, ' - ') > 0;
|
||||
UPDATE lists.banger_songs SET kind = 'playlist' WHERE artist IS NULL;
|
||||
|
||||
SET LOCAL session_replication_role = origin;
|
||||
|
||||
-- persist the fix log for review (legacy schema, dropped with it)
|
||||
CREATE TABLE legacy.reorg_list_fixes AS SELECT * FROM fix_log;
|
||||
ALTER TABLE legacy.reorg_list_fixes OWNER TO personal; -- so `personal` can pg_dump everything
|
||||
148
db/reorg/06_hardware_energy_cs.sql
Normal file
148
db/reorg/06_hardware_energy_cs.sql
Normal file
@@ -0,0 +1,148 @@
|
||||
-- 06_hardware_energy_cs.sql: bring hardware + energy + CS prices over from dev.
|
||||
-- Expects schemas import_hardware / import_energy / import_cs, staged by run.sh from dumps/dev-import-staged.dump.
|
||||
SET ROLE personal;
|
||||
|
||||
-- ---------- energy (DDL identical to ned-collector/collect.py so its IF NOT EXISTS is a no-op) ----------
|
||||
CREATE TABLE energy.gas_storage_daily (
|
||||
gas_day date NOT NULL,
|
||||
site text NOT NULL,
|
||||
point_name text,
|
||||
volume_twh numeric(12,4),
|
||||
fill_pct numeric(7,3),
|
||||
source text NOT NULL DEFAULT 'ned-json',
|
||||
first_seen timestamptz NOT NULL DEFAULT now(),
|
||||
updated_at timestamptz NOT NULL DEFAULT now(),
|
||||
PRIMARY KEY (gas_day, site)
|
||||
);
|
||||
CREATE TABLE energy.ned_utilization (
|
||||
valid_from timestamptz NOT NULL, valid_to timestamptz NOT NULL,
|
||||
point text NOT NULL, type text NOT NULL, activity text NOT NULL, classification text NOT NULL, granularity text NOT NULL,
|
||||
capacity_kw numeric, volume_kwh numeric, percentage numeric, emission_kg numeric, emission_factor numeric,
|
||||
source_file text,
|
||||
first_seen timestamptz NOT NULL DEFAULT now(),
|
||||
updated_at timestamptz NOT NULL DEFAULT now(),
|
||||
PRIMARY KEY (valid_from, point, type, activity, classification, granularity)
|
||||
);
|
||||
CREATE INDEX ned_utilization_type_time ON energy.ned_utilization (type, granularity, valid_from DESC);
|
||||
CREATE TABLE energy.collector_runs (
|
||||
id bigserial PRIMARY KEY,
|
||||
started_at timestamptz NOT NULL DEFAULT now(), finished_at timestamptz,
|
||||
mode text NOT NULL, ok boolean, rows_upserted integer, errors text
|
||||
);
|
||||
CREATE VIEW energy.gas_storage_latest AS
|
||||
SELECT DISTINCT ON (site) site, gas_day, volume_twh, fill_pct, updated_at
|
||||
FROM energy.gas_storage_daily ORDER BY site, gas_day DESC;
|
||||
CREATE VIEW energy.electricity_hourly AS
|
||||
SELECT valid_from AT TIME ZONE 'Europe/Amsterdam' AS hour_local, type, classification,
|
||||
round(volume_kwh / 1000.0, 1) AS mwh, percentage, emission_factor AS kg_co2_per_kwh
|
||||
FROM energy.ned_utilization WHERE granularity = 'Hour';
|
||||
|
||||
INSERT INTO energy.gas_storage_daily SELECT * FROM import_energy.gas_storage_daily;
|
||||
INSERT INTO energy.ned_utilization SELECT * FROM import_energy.ned_utilization;
|
||||
INSERT INTO energy.collector_runs SELECT * FROM import_energy.collector_runs;
|
||||
SELECT setval('energy.collector_runs_id_seq', coalesce((SELECT max(id) FROM energy.collector_runs), 1));
|
||||
|
||||
-- ---------- hardware (prices -> integer cents) ----------
|
||||
CREATE TABLE hardware.devices (
|
||||
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
name text NOT NULL UNIQUE,
|
||||
kind text NOT NULL CHECK (kind IN ('desktop','laptop','phone','vm','monitor')),
|
||||
notes text,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
updated_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
CREATE TABLE hardware.components (
|
||||
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
device_id bigint NOT NULL REFERENCES hardware.devices(id) ON DELETE CASCADE,
|
||||
category text NOT NULL CHECK (category IN ('case','cpu','motherboard','ram','cpu_cooler','gpu','psu','storage','fan','monitor','lighting','accessory')),
|
||||
brand text,
|
||||
model text NOT NULL,
|
||||
quantity integer NOT NULL DEFAULT 1 CHECK (quantity > 0),
|
||||
specs jsonb NOT NULL DEFAULT '{}'::jsonb,
|
||||
unit_price_cents integer CHECK (unit_price_cents >= 0),
|
||||
total_price_cents integer CHECK (total_price_cents >= 0),
|
||||
shop text,
|
||||
order_number text,
|
||||
order_date date,
|
||||
warranty_until date,
|
||||
invoice_number text,
|
||||
status text NOT NULL DEFAULT 'in_use' CHECK (status IN ('in_use','retired','sold')),
|
||||
verified boolean NOT NULL DEFAULT true,
|
||||
notes text,
|
||||
source text,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
updated_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
CREATE INDEX components_category_idx ON hardware.components (category);
|
||||
CREATE INDEX components_device_idx ON hardware.components (device_id);
|
||||
CREATE UNIQUE INDEX components_dedupe_uq ON hardware.components (device_id, model, order_number) NULLS NOT DISTINCT;
|
||||
CREATE INDEX components_specs_gin ON hardware.components USING gin (specs);
|
||||
COMMENT ON SCHEMA hardware IS 'Stan''s hardware inventory: devices (PCs, laptops, phones, VMs, monitors) and their components/peripherals with purchase + warranty details.';
|
||||
COMMENT ON TABLE hardware.devices IS 'A physical or virtual machine Stan owns/uses (e.g. stan-pc). Components hang off a device.';
|
||||
COMMENT ON TABLE hardware.components IS 'One line item (part or peripheral) belonging to a device, with purchase, price and warranty info.';
|
||||
COMMENT ON COLUMN hardware.components.unit_price_cents IS 'Price per unit in EUR cents incl. VAT.';
|
||||
COMMENT ON COLUMN hardware.components.total_price_cents IS 'unit price * quantity in EUR cents incl. VAT (excl. shipping).';
|
||||
COMMENT ON COLUMN hardware.components.verified IS 'false = purchase is real but unconfirmed whether it is (still) part of this setup, or some details are guessed. See notes.';
|
||||
COMMENT ON COLUMN hardware.components.source IS 'Where the data came from: user_list, gmail, manual.';
|
||||
|
||||
INSERT INTO hardware.devices (id, name, kind, notes, created_at, updated_at) OVERRIDING SYSTEM VALUE
|
||||
SELECT id, name, kind, notes, created_at, created_at FROM import_hardware.devices;
|
||||
INSERT INTO hardware.components (id, device_id, category, brand, model, quantity, specs, unit_price_cents, total_price_cents,
|
||||
shop, order_number, order_date, warranty_until, invoice_number, status, verified, notes, source, created_at, updated_at)
|
||||
OVERRIDING SYSTEM VALUE
|
||||
SELECT id, device_id, category, brand, model, quantity, specs, round(unit_price_eur * 100)::integer, round(total_price_eur * 100)::integer,
|
||||
shop, order_number, order_date, warranty_until, invoice_number, status, verified, notes, source, created_at, updated_at
|
||||
FROM import_hardware.components;
|
||||
SELECT setval(pg_get_serial_sequence('hardware.devices', 'id'), (SELECT max(id) FROM hardware.devices));
|
||||
SELECT setval(pg_get_serial_sequence('hardware.components', 'id'), (SELECT max(id) FROM hardware.components));
|
||||
DO $$ BEGIN
|
||||
IF (SELECT sum(total_price_cents) FROM hardware.components) <> (SELECT round(sum(total_price_eur) * 100) FROM import_hardware.components) THEN
|
||||
RAISE EXCEPTION 'hardware cents conversion mismatch'; END IF;
|
||||
END $$;
|
||||
|
||||
CREATE VIEW hardware.setup_overview AS
|
||||
SELECT d.name AS device, d.kind AS device_kind, c.category, c.brand, c.model, c.quantity,
|
||||
c.total_price_cents, round(c.total_price_cents / 100.0, 2) AS total_price_eur,
|
||||
c.shop, c.order_date, c.warranty_until, (c.warranty_until >= CURRENT_DATE) AS under_warranty,
|
||||
c.status, c.verified, c.specs, c.notes, c.order_number, c.invoice_number, c.source, c.id AS component_id
|
||||
FROM hardware.components c JOIN hardware.devices d ON d.id = c.device_id
|
||||
ORDER BY d.name, array_position(ARRAY['case','cpu','motherboard','ram','cpu_cooler','gpu','psu','storage','fan','lighting','monitor','accessory'], c.category),
|
||||
c.order_date, c.model;
|
||||
COMMENT ON VIEW hardware.setup_overview IS 'Flat view of every component per device, build order, live under_warranty flag. Filter status = ''in_use'' for the current setup.';
|
||||
|
||||
-- ---------- CS prices (prod tables are empty; copy dev) ----------
|
||||
DO $$ BEGIN
|
||||
IF EXISTS (SELECT 1 FROM cs.items) OR EXISTS (SELECT 1 FROM cs.price_observations)
|
||||
OR EXISTS (SELECT 1 FROM cs.item_dimensions) OR EXISTS (SELECT 1 FROM cs.price_streams) THEN
|
||||
RAISE EXCEPTION 'cs.* not empty in target; refusing to merge dev data'; END IF;
|
||||
END $$;
|
||||
INSERT INTO cs.items (id, market_hash_name, name, icon_url, item_type, first_seen_at, last_seen_at)
|
||||
SELECT id, market_hash_name, name, icon_url, item_type, first_seen_at, last_seen_at FROM import_cs.cs_market_items;
|
||||
INSERT INTO cs.item_dimensions (item_id, key, value) SELECT item_id, key, value FROM import_cs.cs_item_dimensions;
|
||||
INSERT INTO cs.price_streams (source, currency, kind) SELECT source, currency, kind FROM import_cs.cs_price_streams;
|
||||
INSERT INTO cs.price_observations (id, item_id, source, currency, observed_at, price_cents, volume, kind, imported_at) OVERRIDING SYSTEM VALUE
|
||||
SELECT id, item_id, source, currency, observed_at, price_cents, volume, kind, imported_at FROM import_cs.cs_price_observations;
|
||||
SELECT setval(pg_get_serial_sequence('cs.items', 'id'), coalesce((SELECT max(id) FROM cs.items), 1));
|
||||
SELECT setval(pg_get_serial_sequence('cs.price_observations', 'id'), coalesce((SELECT max(id) FROM cs.price_observations), 1));
|
||||
|
||||
CREATE TABLE legacy.reorg_imports (dst text PRIMARY KEY, src text NOT NULL, n bigint NOT NULL);
|
||||
-- ---------- verify imports against the staged dev copies, then drop staging ----------
|
||||
RESET ROLE;
|
||||
DO $$
|
||||
DECLARE r record; a bigint; b bigint;
|
||||
BEGIN
|
||||
FOR r IN SELECT * FROM (VALUES
|
||||
('energy.gas_storage_daily','import_energy.gas_storage_daily'), ('energy.ned_utilization','import_energy.ned_utilization'),
|
||||
('energy.collector_runs','import_energy.collector_runs'), ('hardware.devices','import_hardware.devices'),
|
||||
('hardware.components','import_hardware.components'), ('cs.items','import_cs.cs_market_items'),
|
||||
('cs.item_dimensions','import_cs.cs_item_dimensions'), ('cs.price_streams','import_cs.cs_price_streams'),
|
||||
('cs.price_observations','import_cs.cs_price_observations')) v(dst, srct) LOOP
|
||||
EXECUTE 'SELECT count(*) FROM ' || r.dst INTO a;
|
||||
EXECUTE 'SELECT count(*) FROM ' || r.srct INTO b;
|
||||
IF a <> b THEN RAISE EXCEPTION '% has % rows, dev % has %', r.dst, a, r.srct, b; END IF;
|
||||
INSERT INTO legacy.reorg_imports VALUES (r.dst, r.srct, a);
|
||||
END LOOP;
|
||||
END $$;
|
||||
DROP SCHEMA import_energy CASCADE;
|
||||
DROP SCHEMA import_hardware CASCADE;
|
||||
DROP SCHEMA import_cs CASCADE;
|
||||
79
db/reorg/07_triggers_meta_grants.sql
Normal file
79
db/reorg/07_triggers_meta_grants.sql
Normal file
@@ -0,0 +1,79 @@
|
||||
-- 07_triggers_meta_grants.sql: updated_at triggers everywhere, register core tables in meta, grants.
|
||||
SET ROLE personal;
|
||||
|
||||
-- 1) updated_at trigger on every table that has the column but no trigger (27 pre-existing + new ones)
|
||||
DO $$
|
||||
DECLARE r record; n integer := 0;
|
||||
BEGIN
|
||||
FOR r IN
|
||||
SELECT n.nspname AS s, c.relname AS t
|
||||
FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace
|
||||
JOIN pg_attribute a ON a.attrelid = c.oid AND a.attname = 'updated_at' AND NOT a.attisdropped
|
||||
WHERE c.relkind = 'r'
|
||||
AND n.nspname IN ('auth','money','house','ref','games','cs','animaldex','site','hardware','energy')
|
||||
AND NOT EXISTS (SELECT 1 FROM pg_trigger tg WHERE tg.tgrelid = c.oid AND NOT tg.tgisinternal
|
||||
AND tg.tgfoid = 'meta.touch_updated_at()'::regprocedure)
|
||||
LOOP
|
||||
EXECUTE format('CREATE TRIGGER touch_updated_at BEFORE UPDATE ON %I.%I FOR EACH ROW EXECUTE FUNCTION meta.touch_updated_at()', r.s, r.t);
|
||||
n := n + 1;
|
||||
END LOOP;
|
||||
RAISE NOTICE 'added updated_at triggers: %', n;
|
||||
END $$;
|
||||
-- the 16 old triggers were all named site_touch_updated_at; normalise the name
|
||||
DO $$
|
||||
DECLARE r record;
|
||||
BEGIN
|
||||
FOR r IN SELECT tg.tgname, tg.tgrelid::regclass AS rel FROM pg_trigger tg WHERE tg.tgname = 'site_touch_updated_at' LOOP
|
||||
EXECUTE format('ALTER TRIGGER site_touch_updated_at ON %s RENAME TO touch_updated_at', r.rel);
|
||||
END LOOP;
|
||||
END $$;
|
||||
RESET ROLE;
|
||||
|
||||
-- 2) register core + ref tables in meta (GUI visibility); auth + bookkeeping are not exposed
|
||||
INSERT INTO meta.tables (schema_name, table_name, slug, display_name, kind, editable, has_done, sort_order)
|
||||
SELECT n.nspname, c.relname,
|
||||
replace(n.nspname || '-' || c.relname, '_', '-'),
|
||||
initcap(replace(c.relname, '_', ' ')),
|
||||
CASE WHEN n.nspname = 'ref' THEN 'ref' ELSE 'core' END,
|
||||
n.nspname <> 'ref', false, 1000
|
||||
FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace
|
||||
WHERE c.relkind IN ('r','v')
|
||||
AND n.nspname IN ('money','house','ref','games','cs','animaldex','site','hardware','energy')
|
||||
ON CONFLICT DO NOTHING;
|
||||
UPDATE meta.tables SET editable = false WHERE kind = 'core' AND (schema_name, table_name) IN
|
||||
(SELECT n.nspname, c.relname FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace WHERE c.relkind = 'v');
|
||||
SELECT meta.sync();
|
||||
|
||||
-- 3) grants
|
||||
GRANT USAGE ON SCHEMA auth, money, house, ref, games, cs, animaldex, site, meta, hardware, energy, lists TO personal_app, personal_ro;
|
||||
-- app: full DML on its own data, read-only on ref
|
||||
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA auth, money, house, games, cs, animaldex, site, hardware, energy, lists TO personal_app;
|
||||
GRANT SELECT ON ALL TABLES IN SCHEMA ref, meta TO personal_app;
|
||||
GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA auth, money, house, games, cs, animaldex, site, hardware, energy, lists TO personal_app;
|
||||
-- app may edit presentation fields of the registry directly
|
||||
GRANT UPDATE (slug, display_name, description, icon, picture_url, default_sort, sort_order) ON meta.tables TO personal_app; -- slug: URL only, table name stays
|
||||
GRANT UPDATE (label, ui_type, options, position, hidden, help_text) ON meta.columns TO personal_app;
|
||||
GRANT EXECUTE ON FUNCTION meta.create_list(text, text, text, boolean, jsonb, text, text, text, integer),
|
||||
meta.add_column(text, jsonb), meta.rename_column(text, text, text), meta.drop_column(text, text),
|
||||
meta.archive_list(text), meta.drop_list(text), meta.sync(integer) TO personal_app;
|
||||
-- ro: everything readable
|
||||
GRANT SELECT ON ALL TABLES IN SCHEMA auth, money, house, ref, games, cs, animaldex, site, meta, hardware, energy, lists TO personal_ro;
|
||||
-- future objects
|
||||
ALTER DEFAULT PRIVILEGES FOR ROLE personal IN SCHEMA auth, money, house, games, cs, animaldex, site, hardware, energy
|
||||
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO personal_app;
|
||||
ALTER DEFAULT PRIVILEGES FOR ROLE personal IN SCHEMA auth, money, house, games, cs, animaldex, site, hardware, energy
|
||||
GRANT USAGE, SELECT ON SEQUENCES TO personal_app;
|
||||
ALTER DEFAULT PRIVILEGES FOR ROLE personal IN SCHEMA ref, meta GRANT SELECT ON TABLES TO personal_app;
|
||||
ALTER DEFAULT PRIVILEGES FOR ROLE personal IN SCHEMA auth, money, house, ref, games, cs, animaldex, site, meta, hardware, energy
|
||||
GRANT SELECT ON TABLES TO personal_ro;
|
||||
ALTER DEFAULT PRIVILEGES FOR ROLE personal_ddl IN SCHEMA lists GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO personal_app;
|
||||
ALTER DEFAULT PRIVILEGES FOR ROLE personal_ddl IN SCHEMA lists GRANT USAGE, SELECT ON SEQUENCES TO personal_app;
|
||||
ALTER DEFAULT PRIVILEGES FOR ROLE personal_ddl IN SCHEMA lists GRANT SELECT ON TABLES TO personal_ro;
|
||||
-- migration owner can read the runtime lists, so deploy backups (pg_dump as `personal`) cover everything
|
||||
GRANT USAGE ON SCHEMA lists TO personal;
|
||||
GRANT SELECT ON ALL TABLES IN SCHEMA lists TO personal;
|
||||
GRANT SELECT ON ALL SEQUENCES IN SCHEMA lists TO personal;
|
||||
ALTER DEFAULT PRIVILEGES FOR ROLE personal_ddl IN SCHEMA lists GRANT SELECT ON TABLES TO personal;
|
||||
ALTER DEFAULT PRIVILEGES FOR ROLE personal_ddl IN SCHEMA lists GRANT SELECT ON SEQUENCES TO personal;
|
||||
-- nobody creates in public anymore
|
||||
REVOKE CREATE ON SCHEMA public FROM PUBLIC;
|
||||
68
db/reorg/08_verify.sql
Normal file
68
db/reorg/08_verify.sql
Normal file
@@ -0,0 +1,68 @@
|
||||
-- 08_verify.sql: hard assertions; any failure aborts the whole transaction.
|
||||
DO $$
|
||||
DECLARE r record; a bigint; bad text := '';
|
||||
BEGIN
|
||||
-- every moved table keeps its exact row count
|
||||
-- expected = prod count before + rows imported from dev (cs.* only; prod cs was empty)
|
||||
FOR r IN SELECT m.*, b.n + coalesce(i.n, 0) AS n FROM legacy.reorg_table_map m
|
||||
JOIN legacy.reorg_counts_before b ON b.table_name = m.old_name
|
||||
LEFT JOIN legacy.reorg_imports i ON i.dst = m.new_schema || '.' || m.new_name LOOP
|
||||
EXECUTE format('SELECT count(*) FROM %I.%I', r.new_schema, r.new_name) INTO a;
|
||||
IF a <> r.n THEN bad := bad || format('%s.%s %s<>%s; ', r.new_schema, r.new_name, a, r.n); END IF;
|
||||
END LOOP;
|
||||
IF bad <> '' THEN RAISE EXCEPTION 'row count mismatch: %', bad; END IF;
|
||||
IF (SELECT count(*) FROM legacy.reorg_counts_before) <> 58 THEN RAISE EXCEPTION 'expected 58 source tables'; END IF;
|
||||
IF EXISTS (SELECT 1 FROM pg_tables WHERE schemaname = 'public') THEN RAISE EXCEPTION 'public not empty'; END IF;
|
||||
END $$;
|
||||
|
||||
-- lists: every legacy item is in exactly one lists.* table, except the 4 merged duplicates; done count preserved
|
||||
DO $$
|
||||
DECLARE t record; n bigint; total bigint := 0; done bigint := 0; d bigint;
|
||||
dropped bigint := (SELECT count(*) FROM legacy.reorg_list_fixes WHERE action = 'dedupe-drop');
|
||||
BEGIN
|
||||
FOR t IN SELECT * FROM meta.tables WHERE kind = 'list' LOOP
|
||||
EXECUTE format('SELECT count(*) FROM lists.%I', t.table_name) INTO n;
|
||||
total := total + n;
|
||||
IF t.has_done THEN EXECUTE format('SELECT count(*) FROM lists.%I WHERE done_at IS NOT NULL', t.table_name) INTO d; done := done + d; END IF;
|
||||
END LOOP;
|
||||
IF total + dropped <> (SELECT count(*) FROM legacy.list_items) THEN
|
||||
RAISE EXCEPTION 'lists total % + % dropped <> legacy %', total, dropped, (SELECT count(*) FROM legacy.list_items); END IF;
|
||||
-- expected done = legacy completions minus dedupe pairs where BOTH copies were done (they collapse to one)
|
||||
d := (SELECT count(*) FROM legacy.list_item_completions)
|
||||
- (SELECT count(*) FROM dedupe x
|
||||
WHERE EXISTS (SELECT 1 FROM legacy.list_item_completions c WHERE c.item_id = x.keep_id)
|
||||
AND EXISTS (SELECT 1 FROM legacy.list_item_completions c WHERE c.item_id = x.drop_id));
|
||||
IF done <> d THEN RAISE EXCEPTION 'done count % <> expected %', done, d; END IF;
|
||||
IF (SELECT count(*) FROM meta.tables WHERE kind = 'list') <> (SELECT count(*) FROM legacy.lists) THEN
|
||||
RAISE EXCEPTION 'list count mismatch'; END IF;
|
||||
RAISE NOTICE 'lists OK: % rows (+% merged dupes), % done', total, dropped, done;
|
||||
END $$;
|
||||
|
||||
-- every table with updated_at has the trigger
|
||||
DO $$
|
||||
DECLARE missing text;
|
||||
BEGIN
|
||||
SELECT string_agg(n.nspname || '.' || c.relname, ', ') INTO missing
|
||||
FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace
|
||||
JOIN pg_attribute a ON a.attrelid = c.oid AND a.attname = 'updated_at' AND NOT a.attisdropped
|
||||
WHERE c.relkind = 'r' AND n.nspname NOT IN ('legacy','pg_catalog','information_schema') AND n.nspname !~ '^pg_'
|
||||
AND NOT EXISTS (SELECT 1 FROM pg_trigger tg WHERE tg.tgrelid = c.oid AND tg.tgfoid = 'meta.touch_updated_at()'::regprocedure);
|
||||
IF missing IS NOT NULL THEN RAISE EXCEPTION 'missing updated_at trigger: %', missing; END IF;
|
||||
-- every table has a PK
|
||||
SELECT string_agg(n.nspname || '.' || c.relname, ', ') INTO missing
|
||||
FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace
|
||||
WHERE c.relkind = 'r' AND n.nspname NOT IN ('legacy','pg_catalog','information_schema') AND n.nspname !~ '^pg_'
|
||||
AND NOT EXISTS (SELECT 1 FROM pg_constraint k WHERE k.conrelid = c.oid AND k.contype = 'p');
|
||||
IF missing IS NOT NULL THEN RAISE EXCEPTION 'tables without PK: %', missing; END IF;
|
||||
END $$;
|
||||
|
||||
-- list rows keep their original timestamps
|
||||
DO $$
|
||||
DECLARE t record; n bigint; bad bigint := 0;
|
||||
BEGIN
|
||||
FOR t IN SELECT table_name FROM meta.tables WHERE kind = 'list' LOOP
|
||||
EXECUTE format('SELECT count(*) FROM lists.%I x JOIN legacy.list_items i ON i.id = x.id WHERE x.updated_at <> i.updated_at OR x.created_at <> i.created_at', t.table_name) INTO n;
|
||||
bad := bad + n;
|
||||
END LOOP;
|
||||
IF bad > 0 THEN RAISE EXCEPTION '% list rows lost their original timestamps', bad; END IF;
|
||||
END $$;
|
||||
58
db/reorg/09_drizzle_names.sql
Normal file
58
db/reorg/09_drizzle_names.sql
Normal file
@@ -0,0 +1,58 @@
|
||||
-- 09_drizzle_names.sql: rename constraints/indexes to the names Drizzle generates, so future
|
||||
-- drizzle-kit migrations (which address objects by name) match the database. Pure renames, no data change.
|
||||
-- Generated from a diff of the rehearsed DB against the Drizzle baseline (2026-10-23).
|
||||
SET ROLE personal;
|
||||
ALTER TABLE cs.item_dimensions RENAME CONSTRAINT item_dimensions_item_id_cs_market_items_id_fkey TO item_dimensions_item_id_items_id_fkey;
|
||||
ALTER TABLE cs.price_observations RENAME CONSTRAINT price_observations_item_id_cs_market_items_id_fkey TO price_observations_item_id_items_id_fkey;
|
||||
ALTER TABLE games.catch_em_all_entries RENAME CONSTRAINT catch_em_all_entries_pokemon_game_entry_id_pokemon_game_entry_i TO "catch_em_all_entries_HeABXvGNiB6D_fkey";
|
||||
ALTER TABLE games.catch_em_all_games RENAME CONSTRAINT catch_em_all_games_game_id_game_id_fkey TO catch_em_all_games_game_id_pokemon_games_id_fkey;
|
||||
ALTER TABLE games.catch_em_all_settings RENAME CONSTRAINT catch_em_all_settings_user_id_fkey TO catch_em_all_settings_user_id_user_id_fkey;
|
||||
ALTER TABLE games.osu_entries RENAME CONSTRAINT osu_entries_user_id_fkey TO osu_entries_user_id_user_id_fkey;
|
||||
ALTER TABLE games.pokemon_catches RENAME CONSTRAINT pokemon_catches_game_id_fkey TO pokemon_catches_game_id_pokemon_games_id_fkey;
|
||||
ALTER TABLE games.pokemon_catches RENAME CONSTRAINT pokemon_catches_pokemon_id_fkey TO pokemon_catches_pokemon_id_pokemon_id_fkey;
|
||||
ALTER TABLE games.pokemon_catches RENAME CONSTRAINT pokemon_catches_user_id_fkey TO pokemon_catches_user_id_user_id_fkey;
|
||||
ALTER TABLE games.pokemon_catches RENAME CONSTRAINT pokemon_catches_user_id_game_id_party_slot_key TO pokemon_catches_user_id_game_id_party_slot_unique;
|
||||
ALTER TABLE games.pokemon_journeys RENAME CONSTRAINT pokemon_journeys_game_id_fkey TO pokemon_journeys_game_id_pokemon_games_id_fkey;
|
||||
ALTER TABLE games.pokemon_journeys RENAME CONSTRAINT pokemon_journeys_user_id_fkey TO pokemon_journeys_user_id_user_id_fkey;
|
||||
ALTER TABLE games.pokemon_journeys RENAME CONSTRAINT pokemon_journeys_user_id_game_id_key TO pokemon_journeys_user_id_game_id_unique;
|
||||
ALTER TABLE games.pokemon_milestones RENAME CONSTRAINT pokemon_milestones_game_id_fkey TO pokemon_milestones_game_id_pokemon_games_id_fkey;
|
||||
ALTER TABLE games.pokemon_milestones RENAME CONSTRAINT pokemon_milestones_user_id_fkey TO pokemon_milestones_user_id_user_id_fkey;
|
||||
ALTER TABLE games.pokemon_milestones RENAME CONSTRAINT pokemon_milestones_user_id_game_id_key_key TO pokemon_milestones_user_id_game_id_key_unique;
|
||||
ALTER TABLE games.pokemon_seen RENAME CONSTRAINT pokemon_seen_game_id_fkey TO pokemon_seen_game_id_pokemon_games_id_fkey;
|
||||
ALTER TABLE games.pokemon_seen RENAME CONSTRAINT pokemon_seen_pokemon_id_fkey TO pokemon_seen_pokemon_id_pokemon_id_fkey;
|
||||
ALTER TABLE games.pokemon_seen RENAME CONSTRAINT pokemon_seen_user_id_fkey TO pokemon_seen_user_id_user_id_fkey;
|
||||
ALTER TABLE games.pokemon_seen RENAME CONSTRAINT pokemon_seen_user_id_game_id_pokemon_id_key TO pokemon_seen_user_id_game_id_pokemon_id_unique;
|
||||
ALTER TABLE games.tracker_challenge_progress RENAME CONSTRAINT tracker_challenge_progress_challenge_id_fkey TO "tracker_challenge_progress_HtzyFtOyUAhk_fkey";
|
||||
ALTER TABLE games.tracker_challenge_progress RENAME CONSTRAINT tracker_challenge_progress_user_id_challenge_id_key TO tracker_challenge_progress_user_id_challenge_id_unique;
|
||||
ALTER TABLE games.tracker_challenge_progress RENAME CONSTRAINT tracker_challenge_progress_user_id_fkey TO tracker_challenge_progress_user_id_user_id_fkey;
|
||||
ALTER TABLE games.tracker_ranks RENAME CONSTRAINT tracker_ranks_game_id_fkey TO tracker_ranks_game_id_tracker_games_id_fkey;
|
||||
ALTER TABLE games.tracker_ranks RENAME CONSTRAINT tracker_ranks_user_id_fkey TO tracker_ranks_user_id_user_id_fkey;
|
||||
ALTER TABLE games.tracker_ranks RENAME CONSTRAINT tracker_ranks_user_id_game_id_key TO tracker_ranks_user_id_game_id_unique;
|
||||
ALTER TABLE games.tracker_weapon_ranks RENAME CONSTRAINT tracker_weapon_ranks_item_id_fkey TO tracker_weapon_ranks_item_id_tracker_items_id_fkey;
|
||||
ALTER TABLE games.tracker_weapon_ranks RENAME CONSTRAINT tracker_weapon_ranks_user_id_fkey TO tracker_weapon_ranks_user_id_user_id_fkey;
|
||||
ALTER TABLE games.tracker_weapon_ranks RENAME CONSTRAINT tracker_weapon_ranks_user_id_item_id_key TO tracker_weapon_ranks_user_id_item_id_unique;
|
||||
ALTER TABLE house.price_history RENAME CONSTRAINT price_history_listing_id_fkey TO price_history_listing_id_listings_id_fkey;
|
||||
ALTER TABLE meta.columns RENAME CONSTRAINT columns_table_id_fkey TO columns_table_id_tables_id_fkey;
|
||||
ALTER TABLE money.forecasts RENAME CONSTRAINT forecasts_account_id_money_accounts_id_fkey TO forecasts_account_id_accounts_id_fkey;
|
||||
ALTER TABLE money.projection_settings RENAME CONSTRAINT "projection_settings_fqvzThdinM8E_fkey" TO projection_settings_investment_account_id_accounts_id_fkey;
|
||||
ALTER TABLE money.recurring RENAME CONSTRAINT recurring_account_id_money_accounts_id_fkey TO recurring_account_id_accounts_id_fkey;
|
||||
ALTER TABLE money.transactions RENAME CONSTRAINT transactions_account_id_money_accounts_id_fkey TO transactions_account_id_accounts_id_fkey;
|
||||
ALTER TABLE ref.pokemon_dex_numbers RENAME CONSTRAINT pokemon_dex_numbers_game_id_fkey TO pokemon_dex_numbers_game_id_pokemon_games_id_fkey;
|
||||
ALTER TABLE ref.pokemon_dex_numbers RENAME CONSTRAINT pokemon_dex_numbers_pokemon_id_fkey TO pokemon_dex_numbers_pokemon_id_pokemon_id_fkey;
|
||||
ALTER TABLE ref.pokemon_dex_numbers RENAME CONSTRAINT pokemon_dex_numbers_pokemon_id_game_id_dex_key TO pokemon_dex_numbers_pokemon_id_game_id_dex_unique;
|
||||
ALTER TABLE ref.pokemon_encounters RENAME CONSTRAINT pokemon_encounters_game_id_games_id_fkey TO pokemon_encounters_game_id_pokemon_games_id_fkey;
|
||||
ALTER TABLE ref.pokemon_evolutions RENAME CONSTRAINT pokemon_evolutions_game_id_games_id_fkey TO pokemon_evolutions_game_id_pokemon_games_id_fkey;
|
||||
ALTER TABLE ref.pokemon_game_entries RENAME CONSTRAINT pokemon_game_entries_game_id_game_id_fkey TO pokemon_game_entries_game_id_pokemon_games_id_fkey;
|
||||
ALTER TABLE ref.pokemon_game_research RENAME CONSTRAINT pokemon_game_research_game_id_fkey TO pokemon_game_research_game_id_pokemon_games_id_fkey;
|
||||
ALTER TABLE ref.pokemon_learnsets RENAME CONSTRAINT pokemon_learnsets_pokemon_id_fkey TO pokemon_learnsets_pokemon_id_pokemon_id_fkey;
|
||||
ALTER TABLE ref.pokemon_learnsets RENAME CONSTRAINT pokemon_learnsets_pokemon_id_version_group_key TO pokemon_learnsets_pokemon_id_version_group_unique;
|
||||
ALTER TABLE ref.pokemon_research RENAME CONSTRAINT pokemon_research_pokemon_id_fkey TO pokemon_research_pokemon_id_pokemon_id_fkey;
|
||||
ALTER TABLE ref.tracker_challenges RENAME CONSTRAINT tracker_challenges_game_id_fkey TO tracker_challenges_game_id_tracker_games_id_fkey;
|
||||
ALTER TABLE ref.tracker_challenges RENAME CONSTRAINT tracker_challenges_game_id_key_key TO tracker_challenges_game_id_key_unique;
|
||||
ALTER TABLE site.blog_articles RENAME CONSTRAINT blog_articles_slug_unique TO blog_articles_slug_key;
|
||||
ALTER INDEX games.osu_entries_owner_idx RENAME TO osu_entries_user_id_kind_index;
|
||||
ALTER INDEX games.pokemon_catches_owner_idx RENAME TO pokemon_catches_user_id_game_id_pokemon_id_index;
|
||||
ALTER INDEX games.pokemon_seen_user_idx RENAME TO pokemon_seen_user_id_index;
|
||||
ALTER INDEX money.utility_readings_user_kind_date_idx RENAME TO utility_readings_user_id_kind_date_index;
|
||||
ALTER INDEX ref.pokemon_dex_game_idx RENAME TO pokemon_dex_numbers_game_id_index;
|
||||
RESET ROLE;
|
||||
21
db/reorg/README.md
Normal file
21
db/reorg/README.md
Normal file
@@ -0,0 +1,21 @@
|
||||
# One-time schema reorg (2026-10)
|
||||
|
||||
Hand-written, reviewed SQL that moved the old flat `public.*` tables into the
|
||||
domain schemas (`auth`, `money`, `house`, `ref`, `games`, `cs`, `animaldex`,
|
||||
`site`, `meta`, `lists`, `hardware`, `energy`, `legacy`), created the roles
|
||||
(`personal`, `personal_app`, `personal_ddl`, `personal_ro`), the `meta.*`
|
||||
registry/API and the 22 runtime lists. It is the state that the Drizzle baseline
|
||||
in `drizzle/` describes.
|
||||
|
||||
These files are kept for the record and for the cutover only. They were
|
||||
rehearsed on a copy of prod (PG17) and run in one transaction (`run.sh`,
|
||||
which refuses the real database names unless `ALLOW_REAL=yes`). After the
|
||||
cutover, record the baseline with `bun scripts/db-migrate.ts --init`; from then
|
||||
on every schema change is a normal Drizzle migration (`bun run db:generate`).
|
||||
|
||||
Passwords are never in these files; set them with `\password` in psql.
|
||||
|
||||
`09_drizzle_names.sql` runs just before `08_verify.sql` and only renames 48
|
||||
constraints and 5 indexes to Drizzle's generated names, so a catalog diff of the
|
||||
reorged DB against a DB built from the baseline is empty (columns, defaults,
|
||||
constraints, indexes, enums in the nine Drizzle schemas).
|
||||
24
db/reorg/run.sh
Executable file
24
db/reorg/run.sh
Executable file
@@ -0,0 +1,24 @@
|
||||
#!/usr/bin/env bash
|
||||
# run.sh <target-db> : stage the dev import, then apply 01..08 in ONE transaction as superuser.
|
||||
# Rehearsal: runs against the private PG17 cluster in ~/dev/setup/db/rehearsal (port 5441, unix socket only).
|
||||
# Prod: same files, but psql = `sudo docker exec -i <pg-container> psql -U postgres -d personal` (see PLAN.md §4c).
|
||||
set -euo pipefail
|
||||
DB=${1:?target db}
|
||||
case "$DB" in personal|personal_dev|personal_staging) [ "${ALLOW_REAL:-}" = yes ] || { echo "refusing $DB without ALLOW_REAL=yes"; exit 3; };; esac
|
||||
D=~/dev/setup/db
|
||||
export LD_LIBRARY_PATH=$D/tools/root/usr/lib/x86_64-linux-gnu
|
||||
BIN=$D/tools/root/usr/lib/postgresql/17/bin
|
||||
CONN=(-h "${PGSOCK:-$D/rehearsal/sock}" -p "${PGPORT:-5441}" -U postgres)
|
||||
STAGED=${STAGED:-$D/dumps/dev-import-staged.dump}
|
||||
|
||||
# 1) stage dev data as import_* schemas (owned by personal so 06 can read it)
|
||||
$BIN/psql "${CONN[@]}" -d "$DB" -v ON_ERROR_STOP=1 -qc "DROP SCHEMA IF EXISTS import_energy, import_hardware, import_cs CASCADE"
|
||||
$BIN/pg_restore "${CONN[@]}" -d "$DB" --no-owner --role=personal --single-transaction --exit-on-error "$STAGED"
|
||||
|
||||
# 2) the migration, all-or-nothing
|
||||
cd "$D/migration"
|
||||
$BIN/psql "${CONN[@]}" -d "$DB" -v ON_ERROR_STOP=1 --single-transaction \
|
||||
-c "SET lock_timeout = '5s'" -c "SET statement_timeout = '10min'" \
|
||||
-f 01_roles.sql -f 02_schemas_move.sql -f 03_fixes.sql -f 04_meta.sql -f 05_lists.sql \
|
||||
-f 06_hardware_energy_cs.sql -f 07_triggers_meta_grants.sql -f 09_drizzle_names.sql -f 08_verify.sql
|
||||
echo "migration committed on $DB"
|
||||
13
deploy/systemd/money-sync.service
Normal file
13
deploy/systemd/money-sync.service
Normal file
@@ -0,0 +1,13 @@
|
||||
[Unit]
|
||||
Description=Money tracker bank sync (Enable Banking -> money.transactions)
|
||||
Wants=network-online.target
|
||||
After=network-online.target
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
# A checkout of stan/website (main once merged). Not the live dev tree.
|
||||
WorkingDirectory=%h/dev/website-money
|
||||
# DB_URL (app role), ENABLEBANKING_APP_ID, ENABLEBANKING_KEY_PATH; mode 600, never committed.
|
||||
EnvironmentFile=%h/.config/money-sync.env
|
||||
ExecStart=/usr/bin/bun scripts/bank-sync.ts
|
||||
TimeoutStartSec=600
|
||||
10
deploy/systemd/money-sync.timer
Normal file
10
deploy/systemd/money-sync.timer
Normal file
@@ -0,0 +1,10 @@
|
||||
[Unit]
|
||||
Description=Money tracker bank sync, 4x per day (PSD2 allows ~4 unattended fetches/day)
|
||||
|
||||
[Timer]
|
||||
OnCalendar=*-*-* 06,11,16,21:20:00 Europe/Amsterdam
|
||||
RandomizedDelaySec=300
|
||||
Persistent=true
|
||||
|
||||
[Install]
|
||||
WantedBy=timers.target
|
||||
@@ -51,7 +51,7 @@ HOUSE_NTFY_TOKEN= # only for a protected ntfy topic
|
||||
PUBLIC_MAPTILER_API_KEY= # already set for Animaldex
|
||||
```
|
||||
|
||||
`scripts/migrate-house-tracker.ts` runs on every deploy (idempotent). Use
|
||||
The `house` schema is part of the Drizzle migrations that run on every deploy. Use
|
||||
"send test alert" on the page to check the alert channel.
|
||||
|
||||
If Cloudflare Bot Fight Mode / WAF challenges are on for stanrunge.dev, add a
|
||||
@@ -91,7 +91,7 @@ newer; older versions cannot read this repository's lockfile. Use `bun upgrade`
|
||||
if needed.
|
||||
|
||||
```sh
|
||||
git clone https://git.stanrunge.dev/stan/personal-website ~/house-worker
|
||||
git clone https://git.stanrunge.dev/stan/website ~/house-worker
|
||||
cd ~/house-worker && bun install --frozen-lockfile
|
||||
cat > house.env <<'EOF'
|
||||
HOUSE_API_URL=https://stanrunge.dev
|
||||
|
||||
@@ -1,12 +1,33 @@
|
||||
import { defineConfig } from 'drizzle-kit';
|
||||
|
||||
// Drizzle owns the core schemas only. Deliberately excluded:
|
||||
// - lists: runtime tables created through meta.create_list() (owned by personal_ddl)
|
||||
// - legacy: pre-reorg tables kept read-only for rollback
|
||||
// - energy: owned by ned-collector
|
||||
// - hardware: not modelled yet
|
||||
// Migrations run as the schema owner (`personal`), never as the app role.
|
||||
export default defineConfig({
|
||||
dialect: 'postgresql',
|
||||
schema: './src/lib/server/db/schema/index.ts',
|
||||
out: './drizzle',
|
||||
schemaFilter: [
|
||||
'auth',
|
||||
'money',
|
||||
'house',
|
||||
'ref',
|
||||
'games',
|
||||
'cs',
|
||||
'animaldex',
|
||||
'site',
|
||||
'meta',
|
||||
'health',
|
||||
'books',
|
||||
'tasks'
|
||||
],
|
||||
migrations: { schema: 'drizzle', table: '__drizzle_migrations' },
|
||||
verbose: true,
|
||||
strict: true,
|
||||
dbCredentials: {
|
||||
url: process.env.DB_URL!
|
||||
url: (process.env.DB_MIGRATE_URL || process.env.DB_URL)!
|
||||
}
|
||||
});
|
||||
|
||||
857
drizzle/20261023215112_baseline/migration.sql
Normal file
857
drizzle/20261023215112_baseline/migration.sql
Normal file
@@ -0,0 +1,857 @@
|
||||
CREATE SCHEMA "animaldex";
|
||||
--> statement-breakpoint
|
||||
CREATE SCHEMA "auth";
|
||||
--> statement-breakpoint
|
||||
CREATE SCHEMA "cs";
|
||||
--> statement-breakpoint
|
||||
CREATE SCHEMA "games";
|
||||
--> statement-breakpoint
|
||||
CREATE SCHEMA "house";
|
||||
--> statement-breakpoint
|
||||
CREATE SCHEMA "meta";
|
||||
--> statement-breakpoint
|
||||
CREATE SCHEMA "money";
|
||||
--> statement-breakpoint
|
||||
CREATE SCHEMA "ref";
|
||||
--> statement-breakpoint
|
||||
CREATE SCHEMA "site";
|
||||
--> statement-breakpoint
|
||||
CREATE TYPE "site"."blog_article_status" AS ENUM('draft', 'published');--> statement-breakpoint
|
||||
CREATE TYPE "ref"."availability" AS ENUM('catchable', 'trade_only', 'transfer_only', 'event_only', 'unknown', 'evolve', 'breed');--> statement-breakpoint
|
||||
CREATE TABLE "auth"."account" (
|
||||
"id" text PRIMARY KEY,
|
||||
"account_id" text NOT NULL,
|
||||
"issuer" text NOT NULL,
|
||||
"provider_id" text NOT NULL,
|
||||
"user_id" text NOT NULL,
|
||||
"access_token" text,
|
||||
"refresh_token" text,
|
||||
"id_token" text,
|
||||
"access_token_expires_at" timestamp with time zone,
|
||||
"refresh_token_expires_at" timestamp with time zone,
|
||||
"scope" text,
|
||||
"password" text,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "auth"."session" (
|
||||
"id" text PRIMARY KEY,
|
||||
"expires_at" timestamp with time zone NOT NULL,
|
||||
"token" text NOT NULL UNIQUE,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone NOT NULL,
|
||||
"ip_address" text,
|
||||
"user_agent" text,
|
||||
"user_id" text NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "auth"."user" (
|
||||
"id" text PRIMARY KEY,
|
||||
"name" text NOT NULL,
|
||||
"role" text DEFAULT 'user' NOT NULL,
|
||||
"email" text NOT NULL UNIQUE,
|
||||
"email_verified" boolean DEFAULT false NOT NULL,
|
||||
"image" text,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "auth"."verification" (
|
||||
"id" text PRIMARY KEY,
|
||||
"identifier" text NOT NULL,
|
||||
"value" text NOT NULL,
|
||||
"expires_at" timestamp with time zone NOT NULL,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "site"."blog_articles" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"title" text NOT NULL,
|
||||
"slug" text NOT NULL UNIQUE,
|
||||
"content" text DEFAULT '' NOT NULL,
|
||||
"tags" text[] DEFAULT '{}'::text[] NOT NULL,
|
||||
"status" "site"."blog_article_status" DEFAULT 'draft'::"site"."blog_article_status" NOT NULL,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "site"."tasks" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"name" text NOT NULL,
|
||||
"current_points" integer DEFAULT 0 NOT NULL,
|
||||
"total_points" integer DEFAULT 1 NOT NULL,
|
||||
"notes" text,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "site"."task_topics" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"task_id" integer NOT NULL,
|
||||
"topic_id" integer NOT NULL,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "task_topics_task_id_topic_id_unique" UNIQUE("task_id","topic_id")
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "site"."topics" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"name" text NOT NULL,
|
||||
"emoji" text,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "games"."catch_em_all_settings" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"user_id" text NOT NULL UNIQUE,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "ref"."pokemon_games" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"source_id" integer UNIQUE,
|
||||
"slug" text UNIQUE,
|
||||
"name" text NOT NULL,
|
||||
"generation" integer DEFAULT 1 NOT NULL,
|
||||
"sort_order" integer DEFAULT 0 NOT NULL,
|
||||
"breeding" boolean DEFAULT true NOT NULL,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "ref"."pokemon" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"number" integer NOT NULL UNIQUE,
|
||||
"generation" integer DEFAULT 1 NOT NULL,
|
||||
"name" text NOT NULL,
|
||||
"type1" text NOT NULL,
|
||||
"type2" text,
|
||||
"hp" integer DEFAULT 0 NOT NULL,
|
||||
"attack" integer DEFAULT 0 NOT NULL,
|
||||
"defense" integer DEFAULT 0 NOT NULL,
|
||||
"special_attack" integer DEFAULT 0 NOT NULL,
|
||||
"special_defense" integer DEFAULT 0 NOT NULL,
|
||||
"speed" integer DEFAULT 0 NOT NULL,
|
||||
"evolves_from" integer,
|
||||
"evolution_chain" integer,
|
||||
"is_baby" boolean DEFAULT false NOT NULL,
|
||||
"is_legendary" boolean DEFAULT false NOT NULL,
|
||||
"is_mythical" boolean DEFAULT false NOT NULL,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "ref"."pokemon_encounters" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"pokemon_id" integer NOT NULL,
|
||||
"game_id" integer NOT NULL,
|
||||
"location_id" integer NOT NULL,
|
||||
"area" text,
|
||||
"method" text NOT NULL,
|
||||
"min_level" integer,
|
||||
"max_level" integer,
|
||||
"rarity" integer,
|
||||
"form" text,
|
||||
"conditions" text[] DEFAULT '{}'::text[] NOT NULL,
|
||||
"note" text,
|
||||
"source" text DEFAULT 'pokeapi' NOT NULL,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "ref"."pokemon_evolutions" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"from_pokemon_id" integer NOT NULL,
|
||||
"to_pokemon_id" integer NOT NULL,
|
||||
"game_id" integer NOT NULL,
|
||||
"method" text NOT NULL,
|
||||
"source" text DEFAULT 'pokeapi' NOT NULL,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "pokemon_evolutions_from_pokemon_id_to_pokemon_id_game_id_method_unique" UNIQUE("from_pokemon_id","to_pokemon_id","game_id","method")
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "ref"."pokemon_game_entries" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"pokemon_id" integer NOT NULL,
|
||||
"game_id" integer NOT NULL,
|
||||
"availability" "ref"."availability" DEFAULT 'catchable'::"ref"."availability" NOT NULL,
|
||||
"note" text,
|
||||
"source" text DEFAULT 'pokeapi' NOT NULL,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "pokemon_game_entries_pokemon_id_game_id_unique" UNIQUE("pokemon_id","game_id")
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "ref"."pokemon_locations" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"source_id" integer UNIQUE,
|
||||
"slug" text NOT NULL,
|
||||
"name" text NOT NULL,
|
||||
"region" text,
|
||||
"description" text,
|
||||
"image_key" text,
|
||||
"image_credit" text,
|
||||
"source" text DEFAULT 'pokeapi' NOT NULL,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "games"."catch_em_all_entries" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"pokemon_game_entry_id" integer NOT NULL,
|
||||
"user_id" text NOT NULL,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "catch_em_all_entries_pokemon_game_entry_id_user_id_unique" UNIQUE("pokemon_game_entry_id","user_id")
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "games"."catch_em_all_games" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"user_id" text NOT NULL,
|
||||
"game_id" integer NOT NULL,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "catch_em_all_games_user_id_game_id_unique" UNIQUE("user_id","game_id")
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "ref"."tracker_games" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"slug" text NOT NULL UNIQUE,
|
||||
"franchise" text NOT NULL,
|
||||
"name" text NOT NULL,
|
||||
"short_name" text NOT NULL,
|
||||
"release_year" integer,
|
||||
"sort_order" integer DEFAULT 0 NOT NULL,
|
||||
"rules" jsonb NOT NULL,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "ref"."tracker_items" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"game_id" integer NOT NULL,
|
||||
"key" text NOT NULL,
|
||||
"kind" text NOT NULL,
|
||||
"category" text NOT NULL,
|
||||
"name" text NOT NULL,
|
||||
"description" text,
|
||||
"icon" text,
|
||||
"post_launch" boolean DEFAULT false NOT NULL,
|
||||
"sort_order" integer DEFAULT 0 NOT NULL,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "tracker_items_game_id_key_unique" UNIQUE("game_id","key")
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "games"."tracker_progress" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"user_id" text NOT NULL,
|
||||
"item_id" integer NOT NULL,
|
||||
"milestone" text DEFAULT 'done' NOT NULL,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "tracker_progress_user_id_item_id_milestone_unique" UNIQUE("user_id","item_id","milestone")
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "games"."osu_entries" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"user_id" text NOT NULL,
|
||||
"kind" text NOT NULL,
|
||||
"title" text NOT NULL,
|
||||
"mode" text DEFAULT 'osu' NOT NULL,
|
||||
"beatmap_url" text,
|
||||
"score_url" text,
|
||||
"goal" text DEFAULT 'pass' NOT NULL,
|
||||
"skill" text DEFAULT 'stamina' NOT NULL,
|
||||
"bpm" integer,
|
||||
"duration" integer,
|
||||
"accuracy" numeric,
|
||||
"combo" integer,
|
||||
"misses" integer,
|
||||
"pp" numeric,
|
||||
"mods" text DEFAULT 'NM' NOT NULL,
|
||||
"rate" numeric(4,2) DEFAULT 1.00 NOT NULL,
|
||||
"achieved" boolean DEFAULT false NOT NULL,
|
||||
"played_on" date NOT NULL,
|
||||
"notes" text DEFAULT '' NOT NULL,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "osu_entries_kind_check" CHECK ("kind" IN ('score','goal','training'))
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "games"."pokemon_catches" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"user_id" text NOT NULL,
|
||||
"game_id" integer NOT NULL,
|
||||
"pokemon_id" integer NOT NULL,
|
||||
"nickname" text DEFAULT '' NOT NULL,
|
||||
"level" integer DEFAULT 1 NOT NULL,
|
||||
"moves" text[] DEFAULT '{}'::text[] NOT NULL,
|
||||
"party_slot" integer,
|
||||
"shiny" boolean DEFAULT false NOT NULL,
|
||||
"nature" text,
|
||||
"ability" text,
|
||||
"ball" text,
|
||||
"location" text,
|
||||
"obtained" text DEFAULT 'caught' NOT NULL,
|
||||
"stage" text DEFAULT 'story' NOT NULL,
|
||||
"notes" text DEFAULT '' NOT NULL,
|
||||
"caught_on" date,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "pokemon_catches_user_id_game_id_party_slot_unique" UNIQUE("user_id","game_id","party_slot"),
|
||||
CONSTRAINT "pokemon_catches_level_check" CHECK ("level" between 1 and 100),
|
||||
CONSTRAINT "pokemon_catches_party_slot_check" CHECK ("party_slot" between 1 and 6),
|
||||
CONSTRAINT "pokemon_catches_moves_check" CHECK (cardinality("moves") <= 4),
|
||||
CONSTRAINT "pokemon_catches_stage_check" CHECK ("stage" IN ('story','postgame','collection'))
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "ref"."pokemon_dex_numbers" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"pokemon_id" integer NOT NULL,
|
||||
"game_id" integer NOT NULL,
|
||||
"dex" text NOT NULL,
|
||||
"dex_name" text NOT NULL,
|
||||
"number" integer NOT NULL,
|
||||
CONSTRAINT "pokemon_dex_numbers_pokemon_id_game_id_dex_unique" UNIQUE("pokemon_id","game_id","dex")
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "ref"."pokemon_game_research" (
|
||||
"game_id" integer PRIMARY KEY,
|
||||
"version_group" integer NOT NULL,
|
||||
"regions" text[] NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "games"."pokemon_journeys" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"user_id" text NOT NULL,
|
||||
"game_id" integer NOT NULL,
|
||||
"stage" text DEFAULT 'story' NOT NULL,
|
||||
"location" text,
|
||||
"completed" boolean DEFAULT false NOT NULL,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "pokemon_journeys_user_id_game_id_unique" UNIQUE("user_id","game_id"),
|
||||
CONSTRAINT "pokemon_journeys_stage_check" CHECK ("stage" IN ('story','postgame','collection'))
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "ref"."pokemon_learnsets" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"pokemon_id" integer NOT NULL,
|
||||
"version_group" integer NOT NULL,
|
||||
"moves" jsonb NOT NULL,
|
||||
CONSTRAINT "pokemon_learnsets_pokemon_id_version_group_unique" UNIQUE("pokemon_id","version_group")
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "games"."pokemon_milestones" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"user_id" text NOT NULL,
|
||||
"game_id" integer NOT NULL,
|
||||
"key" text NOT NULL,
|
||||
"title" text NOT NULL,
|
||||
"category" text NOT NULL,
|
||||
"stage" text DEFAULT 'story' NOT NULL,
|
||||
"done" boolean DEFAULT false NOT NULL,
|
||||
"notes" text DEFAULT '' NOT NULL,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "pokemon_milestones_user_id_game_id_key_unique" UNIQUE("user_id","game_id","key")
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "ref"."pokemon_moves" (
|
||||
"id" integer PRIMARY KEY,
|
||||
"name" text NOT NULL,
|
||||
"type" text NOT NULL,
|
||||
"category" text NOT NULL,
|
||||
"power" integer,
|
||||
"accuracy" integer,
|
||||
"pp" integer,
|
||||
"priority" integer NOT NULL,
|
||||
"target" text NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "ref"."pokemon_research" (
|
||||
"pokemon_id" integer PRIMARY KEY,
|
||||
"facts" jsonb NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "games"."pokemon_seen" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"user_id" text NOT NULL,
|
||||
"game_id" integer NOT NULL,
|
||||
"pokemon_id" integer NOT NULL,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "pokemon_seen_user_id_game_id_pokemon_id_unique" UNIQUE("user_id","game_id","pokemon_id")
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "games"."tracker_challenge_progress" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"user_id" text NOT NULL,
|
||||
"challenge_id" integer NOT NULL,
|
||||
"current" integer DEFAULT 0 NOT NULL,
|
||||
"done" boolean DEFAULT false NOT NULL,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "tracker_challenge_progress_user_id_challenge_id_unique" UNIQUE("user_id","challenge_id"),
|
||||
CONSTRAINT "tracker_challenge_progress_current_check" CHECK ("current" >= 0)
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "ref"."tracker_challenges" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"game_id" integer NOT NULL,
|
||||
"key" text NOT NULL,
|
||||
"name" text NOT NULL,
|
||||
"category" text NOT NULL,
|
||||
"requirement" text NOT NULL,
|
||||
"reward" text,
|
||||
"icon" text,
|
||||
"target" integer,
|
||||
"source_url" text NOT NULL,
|
||||
CONSTRAINT "tracker_challenges_game_id_key_unique" UNIQUE("game_id","key")
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "games"."tracker_ranks" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"user_id" text NOT NULL,
|
||||
"game_id" integer NOT NULL,
|
||||
"level" integer DEFAULT 1 NOT NULL,
|
||||
"prestige" integer DEFAULT 0 NOT NULL,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "tracker_ranks_user_id_game_id_unique" UNIQUE("user_id","game_id"),
|
||||
CONSTRAINT "tracker_ranks_level_check" CHECK ("level" between 1 and 10000),
|
||||
CONSTRAINT "tracker_ranks_prestige_check" CHECK ("prestige" between 0 and 1000)
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "games"."tracker_weapon_ranks" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"user_id" text NOT NULL,
|
||||
"item_id" integer NOT NULL,
|
||||
"level" integer DEFAULT 1 NOT NULL,
|
||||
"prestige" integer DEFAULT 0 NOT NULL,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "tracker_weapon_ranks_user_id_item_id_unique" UNIQUE("user_id","item_id"),
|
||||
CONSTRAINT "tracker_weapon_ranks_level_check" CHECK ("level" between 1 and 10000),
|
||||
CONSTRAINT "tracker_weapon_ranks_prestige_check" CHECK ("prestige" between 0 and 1000)
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "animaldex"."animals" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"species" text NOT NULL,
|
||||
"breed" text,
|
||||
"animal_name" text,
|
||||
"description" text,
|
||||
"ai_breed_suggestion" text,
|
||||
"ai_breed_confidence" double precision,
|
||||
"submitted_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"accepted_at" timestamp with time zone,
|
||||
"denied_at" timestamp with time zone,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "animaldex"."sighting_photos" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"sighting_id" integer NOT NULL,
|
||||
"r2_key" text NOT NULL,
|
||||
"url" text NOT NULL,
|
||||
"sort_order" integer DEFAULT 0 NOT NULL,
|
||||
"uploaded_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "animaldex"."sightings" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"animal_id" integer NOT NULL,
|
||||
"reporter_name" text,
|
||||
"seen_at" timestamp with time zone NOT NULL,
|
||||
"lat" double precision NOT NULL,
|
||||
"lng" double precision NOT NULL,
|
||||
"submitted_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"accepted_at" timestamp with time zone,
|
||||
"denied_at" timestamp with time zone,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "meta"."columns" (
|
||||
"id" integer PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY (sequence name "meta"."columns_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START WITH 1 CACHE 1),
|
||||
"table_id" integer NOT NULL,
|
||||
"column_name" text NOT NULL,
|
||||
"label" text,
|
||||
"ui_type" text NOT NULL,
|
||||
"options" jsonb DEFAULT '{}' NOT NULL,
|
||||
"position" integer DEFAULT 0 NOT NULL,
|
||||
"hidden" boolean DEFAULT false NOT NULL,
|
||||
"help_text" text,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "columns_table_id_column_name_key" UNIQUE("table_id","column_name"),
|
||||
CONSTRAINT "columns_ui_type_check" CHECK (ui_type IN ('text','longtext','url','image','number','cents','date','datetime','bool','select','multiselect','fk','json'))
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "meta"."tables" (
|
||||
"id" integer PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY (sequence name "meta"."tables_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START WITH 1 CACHE 1),
|
||||
"schema_name" text NOT NULL,
|
||||
"table_name" text NOT NULL,
|
||||
"slug" text NOT NULL CONSTRAINT "tables_slug_key" UNIQUE,
|
||||
"display_name" text NOT NULL,
|
||||
"description" text,
|
||||
"icon" text,
|
||||
"picture_url" text,
|
||||
"kind" text NOT NULL,
|
||||
"editable" boolean DEFAULT true NOT NULL,
|
||||
"has_done" boolean DEFAULT false NOT NULL,
|
||||
"default_sort" text,
|
||||
"sort_order" integer DEFAULT 0 NOT NULL,
|
||||
"archived_at" timestamp with time zone,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "tables_schema_name_table_name_key" UNIQUE("schema_name","table_name"),
|
||||
CONSTRAINT "tables_slug_check" CHECK (slug ~ '^[a-z0-9][a-z0-9-]{0,62}$'),
|
||||
CONSTRAINT "tables_kind_check" CHECK (kind IN ('list','core','ref'))
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "money"."accounts" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"user_id" text NOT NULL,
|
||||
"name" text NOT NULL,
|
||||
"opening_balance_cents" integer DEFAULT 0 NOT NULL,
|
||||
"kind" text DEFAULT 'spendable' NOT NULL,
|
||||
"color" text,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "accounts_user_id_name_unique" UNIQUE("user_id","name"),
|
||||
CONSTRAINT "accounts_kind_check" CHECK (kind IN ('spendable','investment'))
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "money"."forecasts" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"user_id" text NOT NULL,
|
||||
"date" date NOT NULL,
|
||||
"description" text NOT NULL,
|
||||
"amount_cents" integer NOT NULL,
|
||||
"account_id" integer,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "money"."insurances" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"user_id" text NOT NULL,
|
||||
"name" text NOT NULL,
|
||||
"provider" text,
|
||||
"coverage" text,
|
||||
"premium_cents" integer,
|
||||
"cadence" text DEFAULT 'monthly' NOT NULL,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "insurances_cadence_check" CHECK (cadence IN ('weekly','monthly','yearly'))
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "money"."projection_settings" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"user_id" text NOT NULL UNIQUE,
|
||||
"investment_account_id" integer,
|
||||
"reserve_cents" integer DEFAULT 100000 NOT NULL,
|
||||
"annual_return_percent" numeric(5,2) DEFAULT '5.00' NOT NULL,
|
||||
"contribution_mode" text DEFAULT 'excess' NOT NULL,
|
||||
"include_recurring" boolean DEFAULT true NOT NULL,
|
||||
"fixed_monthly_cents" integer DEFAULT 0 NOT NULL,
|
||||
"end_date" date,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "projection_settings_contribution_mode_check" CHECK (contribution_mode IN ('excess','fixed'))
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "money"."recurring" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"user_id" text NOT NULL,
|
||||
"name" text NOT NULL,
|
||||
"amount_cents" integer NOT NULL,
|
||||
"cadence" text NOT NULL,
|
||||
"starts_on" date,
|
||||
"ends_on" date,
|
||||
"account_id" integer,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "recurring_cadence_check" CHECK (cadence IN ('weekly','monthly','yearly'))
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "money"."transactions" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"user_id" text NOT NULL,
|
||||
"account_id" integer NOT NULL,
|
||||
"date" date NOT NULL,
|
||||
"description" text NOT NULL,
|
||||
"amount_cents" integer NOT NULL,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "money"."utility_readings" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"user_id" text NOT NULL,
|
||||
"kind" text NOT NULL,
|
||||
"date" date NOT NULL,
|
||||
"reading" numeric(12,3) NOT NULL,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "utility_readings_kind_check" CHECK (kind IN ('electricity','gas','water'))
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "money"."utility_settings" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"user_id" text NOT NULL,
|
||||
"kind" text NOT NULL,
|
||||
"rate_per_unit" numeric(12,4) DEFAULT '0' NOT NULL,
|
||||
"monthly_advance_cents" integer DEFAULT 0 NOT NULL,
|
||||
"billing_start" date,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "utility_settings_user_id_kind_unique" UNIQUE("user_id","kind"),
|
||||
CONSTRAINT "utility_settings_kind_check" CHECK (kind IN ('electricity','gas','water'))
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "money"."wishlist" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"user_id" text NOT NULL,
|
||||
"name" text NOT NULL,
|
||||
"estimated_cents" integer,
|
||||
"priority" integer DEFAULT 3 NOT NULL,
|
||||
"notes" text,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "cs"."item_dimensions" (
|
||||
"item_id" integer,
|
||||
"key" text,
|
||||
"value" text NOT NULL,
|
||||
CONSTRAINT "item_dimensions_pkey" PRIMARY KEY("item_id","key")
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "cs"."items" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"market_hash_name" text NOT NULL,
|
||||
"name" text NOT NULL,
|
||||
"icon_url" text,
|
||||
"item_type" text,
|
||||
"first_seen_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"last_seen_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "cs"."price_observations" (
|
||||
"id" bigint PRIMARY KEY GENERATED ALWAYS AS IDENTITY (sequence name "cs"."price_observations_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854775807 START WITH 1 CACHE 1),
|
||||
"item_id" integer NOT NULL,
|
||||
"source" text NOT NULL,
|
||||
"currency" text NOT NULL,
|
||||
"observed_at" timestamp with time zone NOT NULL,
|
||||
"price_cents" integer NOT NULL,
|
||||
"volume" integer,
|
||||
"kind" text NOT NULL,
|
||||
"imported_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "cs"."price_streams" (
|
||||
"source" text,
|
||||
"currency" text,
|
||||
"kind" text,
|
||||
CONSTRAINT "price_streams_pkey" PRIMARY KEY("source","currency","kind")
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "house"."alert_rules" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"name" text NOT NULL,
|
||||
"enabled" boolean DEFAULT true NOT NULL,
|
||||
"offer" text DEFAULT 'rent' NOT NULL,
|
||||
"min_price" integer,
|
||||
"max_price" integer,
|
||||
"min_area" integer,
|
||||
"min_bedrooms" integer,
|
||||
"cities" text[] DEFAULT '{}'::text[] NOT NULL,
|
||||
"property_types" text[] DEFAULT '{}'::text[] NOT NULL,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "alert_rules_offer_check" CHECK (offer IN ('rent', 'buy'))
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "house"."listings" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"source" text NOT NULL,
|
||||
"source_id" text NOT NULL,
|
||||
"external_id" text,
|
||||
"url" text NOT NULL,
|
||||
"offer" text NOT NULL,
|
||||
"property_type" text NOT NULL,
|
||||
"title" text,
|
||||
"street" text,
|
||||
"postal_code" text,
|
||||
"city" text,
|
||||
"city_slug" text,
|
||||
"neighbourhood" text,
|
||||
"price_eur" integer,
|
||||
"area_m2" integer,
|
||||
"rooms" integer,
|
||||
"bedrooms" integer,
|
||||
"bathrooms" integer,
|
||||
"construction_year" integer,
|
||||
"energy_label" text,
|
||||
"interior" text,
|
||||
"deposit_eur" integer,
|
||||
"available_from" text,
|
||||
"contract_type" text,
|
||||
"agent" text,
|
||||
"image_url" text,
|
||||
"description" text,
|
||||
"features" jsonb DEFAULT '{}' NOT NULL,
|
||||
"lat" double precision,
|
||||
"lng" double precision,
|
||||
"offered_at" timestamp with time zone,
|
||||
"first_seen_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"first_seen_lane" text DEFAULT 'slow' NOT NULL,
|
||||
"last_seen_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"detail_fetched_at" timestamp with time zone,
|
||||
"removed_at" timestamp with time zone,
|
||||
"alerted_at" timestamp with time zone,
|
||||
"status" text,
|
||||
"notes" text,
|
||||
CONSTRAINT "listings_offer_check" CHECK (offer IN ('rent', 'buy')),
|
||||
CONSTRAINT "listings_price_eur_check" CHECK (price_eur IS NULL OR price_eur >= 0)
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "house"."price_history" (
|
||||
"listing_id" integer,
|
||||
"observed_at" timestamp with time zone DEFAULT now(),
|
||||
"price_eur" integer NOT NULL,
|
||||
CONSTRAINT "price_history_pkey" PRIMARY KEY("listing_id","observed_at")
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "house"."searches" (
|
||||
"search_url" text PRIMARY KEY,
|
||||
"source" text NOT NULL,
|
||||
"lane" text NOT NULL,
|
||||
"first_synced_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"last_synced_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"last_found" integer DEFAULT 0 NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "house"."worker_runs" (
|
||||
"id" bigint PRIMARY KEY GENERATED ALWAYS AS IDENTITY (sequence name "house"."worker_runs_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854775807 START WITH 1 CACHE 1),
|
||||
"source" text NOT NULL,
|
||||
"lane" text NOT NULL,
|
||||
"url" text NOT NULL,
|
||||
"status" text NOT NULL,
|
||||
"http_status" integer,
|
||||
"found" integer,
|
||||
"duration_ms" integer,
|
||||
"message" text,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE INDEX "account_userId_idx" ON "auth"."account" ("user_id");--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX "account_issuer_account_id_unique" ON "auth"."account" ("issuer","account_id");--> statement-breakpoint
|
||||
CREATE INDEX "session_userId_idx" ON "auth"."session" ("user_id");--> statement-breakpoint
|
||||
CREATE INDEX "verification_identifier_idx" ON "auth"."verification" ("identifier");--> statement-breakpoint
|
||||
CREATE INDEX "pokemon_encounters_pokemon_id_game_id_index" ON "ref"."pokemon_encounters" ("pokemon_id","game_id");--> statement-breakpoint
|
||||
CREATE INDEX "pokemon_encounters_location_id_index" ON "ref"."pokemon_encounters" ("location_id");--> statement-breakpoint
|
||||
CREATE INDEX "pokemon_evolutions_game_id_index" ON "ref"."pokemon_evolutions" ("game_id");--> statement-breakpoint
|
||||
CREATE INDEX "pokemon_game_entries_game_id_index" ON "ref"."pokemon_game_entries" ("game_id");--> statement-breakpoint
|
||||
CREATE INDEX "catch_em_all_entries_user_id_index" ON "games"."catch_em_all_entries" ("user_id");--> statement-breakpoint
|
||||
CREATE INDEX "catch_em_all_games_user_id_index" ON "games"."catch_em_all_games" ("user_id");--> statement-breakpoint
|
||||
CREATE INDEX "tracker_items_game_id_index" ON "ref"."tracker_items" ("game_id");--> statement-breakpoint
|
||||
CREATE INDEX "tracker_progress_user_id_index" ON "games"."tracker_progress" ("user_id");--> statement-breakpoint
|
||||
CREATE INDEX "osu_entries_user_id_kind_index" ON "games"."osu_entries" ("user_id","kind");--> statement-breakpoint
|
||||
CREATE INDEX "pokemon_catches_user_id_game_id_pokemon_id_index" ON "games"."pokemon_catches" ("user_id","game_id","pokemon_id");--> statement-breakpoint
|
||||
CREATE INDEX "pokemon_dex_numbers_game_id_index" ON "ref"."pokemon_dex_numbers" ("game_id");--> statement-breakpoint
|
||||
CREATE INDEX "pokemon_seen_user_id_index" ON "games"."pokemon_seen" ("user_id");--> statement-breakpoint
|
||||
CREATE INDEX "columns_table_id_index" ON "meta"."columns" ("table_id");--> statement-breakpoint
|
||||
CREATE INDEX "accounts_user_id_index" ON "money"."accounts" ("user_id");--> statement-breakpoint
|
||||
CREATE INDEX "forecasts_user_id_date_index" ON "money"."forecasts" ("user_id","date");--> statement-breakpoint
|
||||
CREATE INDEX "insurances_user_id_index" ON "money"."insurances" ("user_id");--> statement-breakpoint
|
||||
CREATE INDEX "recurring_user_id_index" ON "money"."recurring" ("user_id");--> statement-breakpoint
|
||||
CREATE INDEX "transactions_user_id_date_id_index" ON "money"."transactions" ("user_id","date","id");--> statement-breakpoint
|
||||
CREATE INDEX "transactions_account_id_index" ON "money"."transactions" ("account_id");--> statement-breakpoint
|
||||
CREATE INDEX "utility_readings_user_id_kind_date_index" ON "money"."utility_readings" ("user_id","kind","date");--> statement-breakpoint
|
||||
CREATE INDEX "wishlist_user_id_index" ON "money"."wishlist" ("user_id");--> statement-breakpoint
|
||||
CREATE INDEX "item_dimensions_filter_idx" ON "cs"."item_dimensions" ("key","value","item_id");--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX "items_hash_idx" ON "cs"."items" ("market_hash_name");--> statement-breakpoint
|
||||
CREATE INDEX "items_name_idx" ON "cs"."items" ("name");--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX "price_observations_unique_idx" ON "cs"."price_observations" ("item_id","source","currency","observed_at","kind");--> statement-breakpoint
|
||||
CREATE INDEX "price_observations_lookup_idx" ON "cs"."price_observations" ("item_id","source","currency","observed_at");--> statement-breakpoint
|
||||
CREATE INDEX "price_observations_time_idx" ON "cs"."price_observations" ("observed_at");--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX "listings_source_idx" ON "house"."listings" ("source","source_id");--> statement-breakpoint
|
||||
CREATE INDEX "listings_active_idx" ON "house"."listings" ("offer","first_seen_at" DESC) WHERE removed_at IS NULL;--> statement-breakpoint
|
||||
CREATE INDEX "listings_city_idx" ON "house"."listings" ("source","offer","city_slug");--> statement-breakpoint
|
||||
CREATE INDEX "listings_details_idx" ON "house"."listings" ("first_seen_at" DESC) WHERE detail_fetched_at IS NULL;--> statement-breakpoint
|
||||
CREATE INDEX "worker_runs_created_idx" ON "house"."worker_runs" ("created_at" DESC);--> statement-breakpoint
|
||||
ALTER TABLE "auth"."account" ADD CONSTRAINT "account_user_id_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth"."user"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "auth"."session" ADD CONSTRAINT "session_user_id_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth"."user"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "site"."task_topics" ADD CONSTRAINT "task_topics_task_id_tasks_id_fkey" FOREIGN KEY ("task_id") REFERENCES "site"."tasks"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "site"."task_topics" ADD CONSTRAINT "task_topics_topic_id_topics_id_fkey" FOREIGN KEY ("topic_id") REFERENCES "site"."topics"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "games"."catch_em_all_settings" ADD CONSTRAINT "catch_em_all_settings_user_id_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth"."user"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "ref"."pokemon_encounters" ADD CONSTRAINT "pokemon_encounters_pokemon_id_pokemon_id_fkey" FOREIGN KEY ("pokemon_id") REFERENCES "ref"."pokemon"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "ref"."pokemon_encounters" ADD CONSTRAINT "pokemon_encounters_game_id_pokemon_games_id_fkey" FOREIGN KEY ("game_id") REFERENCES "ref"."pokemon_games"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "ref"."pokemon_encounters" ADD CONSTRAINT "pokemon_encounters_location_id_pokemon_locations_id_fkey" FOREIGN KEY ("location_id") REFERENCES "ref"."pokemon_locations"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "ref"."pokemon_evolutions" ADD CONSTRAINT "pokemon_evolutions_from_pokemon_id_pokemon_id_fkey" FOREIGN KEY ("from_pokemon_id") REFERENCES "ref"."pokemon"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "ref"."pokemon_evolutions" ADD CONSTRAINT "pokemon_evolutions_to_pokemon_id_pokemon_id_fkey" FOREIGN KEY ("to_pokemon_id") REFERENCES "ref"."pokemon"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "ref"."pokemon_evolutions" ADD CONSTRAINT "pokemon_evolutions_game_id_pokemon_games_id_fkey" FOREIGN KEY ("game_id") REFERENCES "ref"."pokemon_games"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "ref"."pokemon_game_entries" ADD CONSTRAINT "pokemon_game_entries_pokemon_id_pokemon_id_fkey" FOREIGN KEY ("pokemon_id") REFERENCES "ref"."pokemon"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "ref"."pokemon_game_entries" ADD CONSTRAINT "pokemon_game_entries_game_id_pokemon_games_id_fkey" FOREIGN KEY ("game_id") REFERENCES "ref"."pokemon_games"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "games"."catch_em_all_entries" ADD CONSTRAINT "catch_em_all_entries_HeABXvGNiB6D_fkey" FOREIGN KEY ("pokemon_game_entry_id") REFERENCES "ref"."pokemon_game_entries"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "games"."catch_em_all_entries" ADD CONSTRAINT "catch_em_all_entries_user_id_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth"."user"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "games"."catch_em_all_games" ADD CONSTRAINT "catch_em_all_games_user_id_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth"."user"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "games"."catch_em_all_games" ADD CONSTRAINT "catch_em_all_games_game_id_pokemon_games_id_fkey" FOREIGN KEY ("game_id") REFERENCES "ref"."pokemon_games"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "ref"."tracker_items" ADD CONSTRAINT "tracker_items_game_id_tracker_games_id_fkey" FOREIGN KEY ("game_id") REFERENCES "ref"."tracker_games"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "games"."tracker_progress" ADD CONSTRAINT "tracker_progress_user_id_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth"."user"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "games"."tracker_progress" ADD CONSTRAINT "tracker_progress_item_id_tracker_items_id_fkey" FOREIGN KEY ("item_id") REFERENCES "ref"."tracker_items"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "games"."osu_entries" ADD CONSTRAINT "osu_entries_user_id_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth"."user"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "games"."pokemon_catches" ADD CONSTRAINT "pokemon_catches_user_id_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth"."user"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "games"."pokemon_catches" ADD CONSTRAINT "pokemon_catches_game_id_pokemon_games_id_fkey" FOREIGN KEY ("game_id") REFERENCES "ref"."pokemon_games"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "games"."pokemon_catches" ADD CONSTRAINT "pokemon_catches_pokemon_id_pokemon_id_fkey" FOREIGN KEY ("pokemon_id") REFERENCES "ref"."pokemon"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "ref"."pokemon_dex_numbers" ADD CONSTRAINT "pokemon_dex_numbers_pokemon_id_pokemon_id_fkey" FOREIGN KEY ("pokemon_id") REFERENCES "ref"."pokemon"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "ref"."pokemon_dex_numbers" ADD CONSTRAINT "pokemon_dex_numbers_game_id_pokemon_games_id_fkey" FOREIGN KEY ("game_id") REFERENCES "ref"."pokemon_games"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "ref"."pokemon_game_research" ADD CONSTRAINT "pokemon_game_research_game_id_pokemon_games_id_fkey" FOREIGN KEY ("game_id") REFERENCES "ref"."pokemon_games"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "games"."pokemon_journeys" ADD CONSTRAINT "pokemon_journeys_user_id_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth"."user"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "games"."pokemon_journeys" ADD CONSTRAINT "pokemon_journeys_game_id_pokemon_games_id_fkey" FOREIGN KEY ("game_id") REFERENCES "ref"."pokemon_games"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "ref"."pokemon_learnsets" ADD CONSTRAINT "pokemon_learnsets_pokemon_id_pokemon_id_fkey" FOREIGN KEY ("pokemon_id") REFERENCES "ref"."pokemon"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "games"."pokemon_milestones" ADD CONSTRAINT "pokemon_milestones_user_id_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth"."user"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "games"."pokemon_milestones" ADD CONSTRAINT "pokemon_milestones_game_id_pokemon_games_id_fkey" FOREIGN KEY ("game_id") REFERENCES "ref"."pokemon_games"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "ref"."pokemon_research" ADD CONSTRAINT "pokemon_research_pokemon_id_pokemon_id_fkey" FOREIGN KEY ("pokemon_id") REFERENCES "ref"."pokemon"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "games"."pokemon_seen" ADD CONSTRAINT "pokemon_seen_user_id_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth"."user"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "games"."pokemon_seen" ADD CONSTRAINT "pokemon_seen_game_id_pokemon_games_id_fkey" FOREIGN KEY ("game_id") REFERENCES "ref"."pokemon_games"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "games"."pokemon_seen" ADD CONSTRAINT "pokemon_seen_pokemon_id_pokemon_id_fkey" FOREIGN KEY ("pokemon_id") REFERENCES "ref"."pokemon"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "games"."tracker_challenge_progress" ADD CONSTRAINT "tracker_challenge_progress_user_id_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth"."user"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "games"."tracker_challenge_progress" ADD CONSTRAINT "tracker_challenge_progress_HtzyFtOyUAhk_fkey" FOREIGN KEY ("challenge_id") REFERENCES "ref"."tracker_challenges"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "ref"."tracker_challenges" ADD CONSTRAINT "tracker_challenges_game_id_tracker_games_id_fkey" FOREIGN KEY ("game_id") REFERENCES "ref"."tracker_games"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "games"."tracker_ranks" ADD CONSTRAINT "tracker_ranks_user_id_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth"."user"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "games"."tracker_ranks" ADD CONSTRAINT "tracker_ranks_game_id_tracker_games_id_fkey" FOREIGN KEY ("game_id") REFERENCES "ref"."tracker_games"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "games"."tracker_weapon_ranks" ADD CONSTRAINT "tracker_weapon_ranks_user_id_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth"."user"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "games"."tracker_weapon_ranks" ADD CONSTRAINT "tracker_weapon_ranks_item_id_tracker_items_id_fkey" FOREIGN KEY ("item_id") REFERENCES "ref"."tracker_items"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "animaldex"."sighting_photos" ADD CONSTRAINT "sighting_photos_sighting_id_sightings_id_fkey" FOREIGN KEY ("sighting_id") REFERENCES "animaldex"."sightings"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "animaldex"."sightings" ADD CONSTRAINT "sightings_animal_id_animals_id_fkey" FOREIGN KEY ("animal_id") REFERENCES "animaldex"."animals"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "meta"."columns" ADD CONSTRAINT "columns_table_id_tables_id_fkey" FOREIGN KEY ("table_id") REFERENCES "meta"."tables"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "money"."accounts" ADD CONSTRAINT "accounts_user_id_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth"."user"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "money"."forecasts" ADD CONSTRAINT "forecasts_user_id_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth"."user"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "money"."forecasts" ADD CONSTRAINT "forecasts_account_id_accounts_id_fkey" FOREIGN KEY ("account_id") REFERENCES "money"."accounts"("id") ON DELETE SET NULL;--> statement-breakpoint
|
||||
ALTER TABLE "money"."insurances" ADD CONSTRAINT "insurances_user_id_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth"."user"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "money"."projection_settings" ADD CONSTRAINT "projection_settings_user_id_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth"."user"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "money"."projection_settings" ADD CONSTRAINT "projection_settings_investment_account_id_accounts_id_fkey" FOREIGN KEY ("investment_account_id") REFERENCES "money"."accounts"("id") ON DELETE SET NULL;--> statement-breakpoint
|
||||
ALTER TABLE "money"."recurring" ADD CONSTRAINT "recurring_user_id_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth"."user"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "money"."recurring" ADD CONSTRAINT "recurring_account_id_accounts_id_fkey" FOREIGN KEY ("account_id") REFERENCES "money"."accounts"("id") ON DELETE SET NULL;--> statement-breakpoint
|
||||
ALTER TABLE "money"."transactions" ADD CONSTRAINT "transactions_user_id_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth"."user"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "money"."transactions" ADD CONSTRAINT "transactions_account_id_accounts_id_fkey" FOREIGN KEY ("account_id") REFERENCES "money"."accounts"("id");--> statement-breakpoint
|
||||
ALTER TABLE "money"."utility_readings" ADD CONSTRAINT "utility_readings_user_id_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth"."user"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "money"."utility_settings" ADD CONSTRAINT "utility_settings_user_id_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth"."user"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "money"."wishlist" ADD CONSTRAINT "wishlist_user_id_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth"."user"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "cs"."item_dimensions" ADD CONSTRAINT "item_dimensions_item_id_items_id_fkey" FOREIGN KEY ("item_id") REFERENCES "cs"."items"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "cs"."price_observations" ADD CONSTRAINT "price_observations_item_id_items_id_fkey" FOREIGN KEY ("item_id") REFERENCES "cs"."items"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "house"."price_history" ADD CONSTRAINT "price_history_listing_id_listings_id_fkey" FOREIGN KEY ("listing_id") REFERENCES "house"."listings"("id") ON DELETE CASCADE;
|
||||
10307
drizzle/20261023215112_baseline/snapshot.json
Normal file
10307
drizzle/20261023215112_baseline/snapshot.json
Normal file
File diff suppressed because it is too large
Load Diff
148
drizzle/20261024002120_health/migration.sql
Normal file
148
drizzle/20261024002120_health/migration.sql
Normal file
@@ -0,0 +1,148 @@
|
||||
CREATE SCHEMA "health";
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "health"."appointments" (
|
||||
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
"date" date NOT NULL,
|
||||
"type" text DEFAULT 'appointment' NOT NULL,
|
||||
"provider" text,
|
||||
"notes" text,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "appointments_type_check" CHECK ("type" IN ('appointment','checkup','condition','test','procedure','other')),
|
||||
CONSTRAINT "appointments_date_check" CHECK ("date" >= DATE '1900-01-01')
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "health"."body_measurements" (
|
||||
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
"date" date NOT NULL,
|
||||
"weight_kg" numeric(5,2),
|
||||
"waist_cm" numeric(5,1),
|
||||
"body_fat_pct" numeric(4,1),
|
||||
"notes" text,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "body_measurements_any_check" CHECK ("weight_kg" IS NOT NULL OR "waist_cm" IS NOT NULL OR "body_fat_pct" IS NOT NULL),
|
||||
CONSTRAINT "body_measurements_weight_check" CHECK ("weight_kg" IS NULL OR "weight_kg" BETWEEN 20 AND 400),
|
||||
CONSTRAINT "body_measurements_waist_check" CHECK ("waist_cm" IS NULL OR "waist_cm" BETWEEN 30 AND 250),
|
||||
CONSTRAINT "body_measurements_fat_check" CHECK ("body_fat_pct" IS NULL OR "body_fat_pct" BETWEEN 1 AND 75),
|
||||
CONSTRAINT "body_measurements_date_check" CHECK ("date" >= DATE '1900-01-01')
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "health"."medications" (
|
||||
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
"name" text NOT NULL,
|
||||
"kind" text DEFAULT 'medication' NOT NULL,
|
||||
"dose" text,
|
||||
"frequency" text,
|
||||
"start_date" date,
|
||||
"stop_date" date,
|
||||
"notes" text,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "medications_name_check" CHECK (length(btrim("name")) > 0),
|
||||
CONSTRAINT "medications_kind_check" CHECK ("kind" IN ('medication','supplement')),
|
||||
CONSTRAINT "medications_dates_check" CHECK ("stop_date" IS NULL OR "start_date" IS NULL OR "stop_date" >= "start_date")
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "health"."nutrition_daily" (
|
||||
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
"date" date NOT NULL CONSTRAINT "nutrition_daily_date_key" UNIQUE,
|
||||
"kcal" integer,
|
||||
"protein_g" numeric(5,1),
|
||||
"notes" text,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "nutrition_daily_kcal_check" CHECK ("kcal" IS NULL OR "kcal" BETWEEN 0 AND 20000),
|
||||
CONSTRAINT "nutrition_daily_protein_check" CHECK ("protein_g" IS NULL OR "protein_g" BETWEEN 0 AND 1000),
|
||||
CONSTRAINT "nutrition_daily_date_check" CHECK ("date" >= DATE '1900-01-01')
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "health"."sleep" (
|
||||
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
"date" date NOT NULL CONSTRAINT "sleep_date_key" UNIQUE,
|
||||
"bedtime" timestamp with time zone NOT NULL,
|
||||
"wake_time" timestamp with time zone NOT NULL,
|
||||
"hours" numeric(4,2) GENERATED ALWAYS AS (round((extract(epoch from (wake_time - bedtime)) / 3600)::numeric, 2)) STORED,
|
||||
"notes" text,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "sleep_order_check" CHECK ("wake_time" > "bedtime"),
|
||||
CONSTRAINT "sleep_length_check" CHECK ("wake_time" - "bedtime" <= interval '24 hours'),
|
||||
CONSTRAINT "sleep_date_check" CHECK ("date" >= DATE '1900-01-01')
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "health"."vaccinations" (
|
||||
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
"vaccine" text NOT NULL,
|
||||
"target" text,
|
||||
"dose_no" integer,
|
||||
"date" date NOT NULL,
|
||||
"batch_lot" text,
|
||||
"provider" text,
|
||||
"country" text,
|
||||
"notes" text,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "vaccinations_vaccine_check" CHECK (length(btrim("vaccine")) > 0),
|
||||
CONSTRAINT "vaccinations_dose_no_check" CHECK ("dose_no" IS NULL OR "dose_no" BETWEEN 1 AND 20),
|
||||
CONSTRAINT "vaccinations_country_check" CHECK ("country" IS NULL OR "country" ~ '^[A-Z]{2}$'),
|
||||
CONSTRAINT "vaccinations_date_check" CHECK ("date" >= DATE '1900-01-01')
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "health"."workouts" (
|
||||
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
"date" date NOT NULL,
|
||||
"session_id" uuid,
|
||||
"exercise" text NOT NULL,
|
||||
"sets" integer,
|
||||
"reps" integer,
|
||||
"load_kg" numeric(6,2),
|
||||
"rpe" numeric(3,1),
|
||||
"notes" text,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "workouts_exercise_check" CHECK (length(btrim("exercise")) > 0),
|
||||
CONSTRAINT "workouts_sets_check" CHECK ("sets" IS NULL OR "sets" BETWEEN 1 AND 100),
|
||||
CONSTRAINT "workouts_reps_check" CHECK ("reps" IS NULL OR "reps" BETWEEN 0 AND 1000),
|
||||
CONSTRAINT "workouts_load_check" CHECK ("load_kg" IS NULL OR "load_kg" BETWEEN 0 AND 1000),
|
||||
CONSTRAINT "workouts_rpe_check" CHECK ("rpe" IS NULL OR "rpe" BETWEEN 1 AND 10),
|
||||
CONSTRAINT "workouts_date_check" CHECK ("date" >= DATE '1900-01-01')
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE INDEX "appointments_date_idx" ON "health"."appointments" ("date");--> statement-breakpoint
|
||||
CREATE INDEX "body_measurements_date_idx" ON "health"."body_measurements" ("date");--> statement-breakpoint
|
||||
CREATE INDEX "medications_start_date_idx" ON "health"."medications" ("start_date");--> statement-breakpoint
|
||||
CREATE INDEX "medications_stop_date_idx" ON "health"."medications" ("stop_date");--> statement-breakpoint
|
||||
CREATE INDEX "vaccinations_date_idx" ON "health"."vaccinations" ("date");--> statement-breakpoint
|
||||
CREATE INDEX "workouts_date_idx" ON "health"."workouts" ("date");--> statement-breakpoint
|
||||
CREATE INDEX "workouts_session_idx" ON "health"."workouts" ("session_id");
|
||||
-- Hand-written below this line (not generated by drizzle-kit).
|
||||
-- updated_at triggers, same as every other core table (07_triggers_meta_grants.sql).
|
||||
DO $$
|
||||
DECLARE t text;
|
||||
BEGIN
|
||||
IF to_regprocedure('meta.touch_updated_at()') IS NOT NULL THEN
|
||||
FOREACH t IN ARRAY ARRAY['vaccinations','body_measurements','workouts','nutrition_daily','sleep','medications','appointments'] LOOP
|
||||
EXECUTE format('CREATE TRIGGER touch_updated_at BEFORE UPDATE ON health.%I FOR EACH ROW EXECUTE FUNCTION meta.touch_updated_at()', t);
|
||||
END LOOP;
|
||||
END IF;
|
||||
END $$;
|
||||
--> statement-breakpoint
|
||||
-- Privacy: health data is readable/writable by the app role only. Unlike the other core
|
||||
-- schemas it is deliberately NOT granted to personal_ro, and it is not registered in
|
||||
-- meta.tables (so the generic list/table GUI never shows it).
|
||||
-- Grants only run on prod (migrations run as the schema owner `personal`); the dev DB
|
||||
-- (owned by personal_dev) carries no prod grants, matching db-refresh-dev.sh.
|
||||
REVOKE ALL ON SCHEMA health FROM PUBLIC;
|
||||
--> statement-breakpoint
|
||||
DO $$
|
||||
BEGIN
|
||||
IF current_user = 'personal' AND EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'personal_app') THEN
|
||||
GRANT USAGE ON SCHEMA health TO personal_app;
|
||||
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA health TO personal_app;
|
||||
ALTER DEFAULT PRIVILEGES FOR ROLE personal IN SCHEMA health
|
||||
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO personal_app;
|
||||
ALTER DEFAULT PRIVILEGES FOR ROLE personal IN SCHEMA health
|
||||
GRANT USAGE, SELECT ON SEQUENCES TO personal_app;
|
||||
END IF;
|
||||
END $$;
|
||||
11583
drizzle/20261024002120_health/snapshot.json
Normal file
11583
drizzle/20261024002120_health/snapshot.json
Normal file
File diff suppressed because it is too large
Load Diff
65
drizzle/20261024003809_money_bank_sync/migration.sql
Normal file
65
drizzle/20261024003809_money_bank_sync/migration.sql
Normal file
@@ -0,0 +1,65 @@
|
||||
CREATE TABLE "money"."bank_sessions" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"user_id" text NOT NULL,
|
||||
"provider" text DEFAULT 'enablebanking' NOT NULL,
|
||||
"session_id" text NOT NULL CONSTRAINT "bank_sessions_session_id_key" UNIQUE,
|
||||
"aspsp_name" text NOT NULL,
|
||||
"aspsp_country" text NOT NULL,
|
||||
"psu_type" text,
|
||||
"status" text DEFAULT 'authorized' NOT NULL,
|
||||
"valid_until" timestamp with time zone,
|
||||
"accounts" jsonb DEFAULT '[]' NOT NULL,
|
||||
"last_synced_at" timestamp with time zone,
|
||||
"last_error" text,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "bank_sessions_status_check" CHECK (status IN ('authorized','expired','revoked','error'))
|
||||
);
|
||||
--> statement-breakpoint
|
||||
ALTER TABLE "money"."accounts" ADD COLUMN "scope" text DEFAULT 'personal' NOT NULL;--> statement-breakpoint
|
||||
ALTER TABLE "money"."accounts" ADD COLUMN "owner" text;--> statement-breakpoint
|
||||
ALTER TABLE "money"."accounts" ADD COLUMN "iban" text;--> statement-breakpoint
|
||||
ALTER TABLE "money"."accounts" ADD COLUMN "external_uid" text;--> statement-breakpoint
|
||||
ALTER TABLE "money"."accounts" ADD COLUMN "provider" text DEFAULT 'manual' NOT NULL;--> statement-breakpoint
|
||||
ALTER TABLE "money"."transactions" ADD COLUMN "external_id" text;--> statement-breakpoint
|
||||
ALTER TABLE "money"."transactions" ADD COLUMN "source" text DEFAULT 'manual' NOT NULL;--> statement-breakpoint
|
||||
ALTER TABLE "money"."transactions" ADD COLUMN "booking_status" text;--> statement-breakpoint
|
||||
ALTER TABLE "money"."transactions" ADD COLUMN "counterparty" text;--> statement-breakpoint
|
||||
ALTER TABLE "money"."transactions" ADD COLUMN "category" text;--> statement-breakpoint
|
||||
ALTER TABLE "money"."transactions" ADD COLUMN "raw" jsonb;--> statement-breakpoint
|
||||
ALTER TABLE "money"."transactions" ADD COLUMN "value_date" date;--> statement-breakpoint
|
||||
ALTER TABLE "money"."accounts" ADD CONSTRAINT "accounts_external_uid_key" UNIQUE("external_uid");--> statement-breakpoint
|
||||
ALTER TABLE "money"."transactions" ADD CONSTRAINT "transactions_account_external_id_key" UNIQUE("account_id","external_id");--> statement-breakpoint
|
||||
CREATE INDEX "bank_sessions_user_id_index" ON "money"."bank_sessions" ("user_id");--> statement-breakpoint
|
||||
ALTER TABLE "money"."bank_sessions" ADD CONSTRAINT "bank_sessions_user_id_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth"."user"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "money"."accounts" ADD CONSTRAINT "accounts_scope_check" CHECK (scope IN ('personal','joint'));--> statement-breakpoint
|
||||
ALTER TABLE "money"."accounts" ADD CONSTRAINT "accounts_provider_check" CHECK (provider IN ('manual','enablebanking'));--> statement-breakpoint
|
||||
ALTER TABLE "money"."transactions" ADD CONSTRAINT "transactions_source_check" CHECK (source IN ('manual','csv','enablebanking'));--> statement-breakpoint
|
||||
-- Hand-written below this line (not generated by drizzle-kit).
|
||||
-- Backfill: the shared account with Juhee is the only joint one; everything else stays personal.
|
||||
UPDATE "money"."accounts" SET "scope" = 'joint', "owner" = 'Stan & Juhee' WHERE lower("name") = 'joint';
|
||||
--> statement-breakpoint
|
||||
UPDATE "money"."accounts" SET "owner" = 'Stan' WHERE "scope" = 'personal' AND "owner" IS NULL;
|
||||
--> statement-breakpoint
|
||||
-- updated_at trigger, same as every other core table (07_triggers_meta_grants.sql).
|
||||
DO $$
|
||||
BEGIN
|
||||
IF to_regprocedure('meta.touch_updated_at()') IS NOT NULL THEN
|
||||
CREATE TRIGGER touch_updated_at BEFORE UPDATE ON money.bank_sessions
|
||||
FOR EACH ROW EXECUTE FUNCTION meta.touch_updated_at();
|
||||
END IF;
|
||||
END $$;
|
||||
--> statement-breakpoint
|
||||
-- Grants only run on prod (migrations run as the schema owner `personal`); the dev DB
|
||||
-- (owned by personal_dev) carries no prod grants, matching db-refresh-dev.sh. Default
|
||||
-- privileges on `money` already cover new tables; this is explicit for clarity.
|
||||
DO $$
|
||||
BEGIN
|
||||
IF current_user = 'personal' AND EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'personal_app') THEN
|
||||
GRANT SELECT, INSERT, UPDATE, DELETE ON money.bank_sessions TO personal_app;
|
||||
GRANT USAGE, SELECT ON SEQUENCE money.bank_sessions_id_seq TO personal_app;
|
||||
END IF;
|
||||
IF current_user = 'personal' AND EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'personal_ro') THEN
|
||||
GRANT SELECT ON money.bank_sessions TO personal_ro;
|
||||
END IF;
|
||||
END $$;
|
||||
11543
drizzle/20261024003809_money_bank_sync/snapshot.json
Normal file
11543
drizzle/20261024003809_money_bank_sync/snapshot.json
Normal file
File diff suppressed because it is too large
Load Diff
3
drizzle/20261024011919_money_bank_balance/migration.sql
Normal file
3
drizzle/20261024011919_money_bank_balance/migration.sql
Normal file
@@ -0,0 +1,3 @@
|
||||
ALTER TABLE "money"."accounts" ADD COLUMN "bank_balance_cents" integer;--> statement-breakpoint
|
||||
ALTER TABLE "money"."accounts" ADD COLUMN "bank_balance_type" text;--> statement-breakpoint
|
||||
ALTER TABLE "money"."accounts" ADD COLUMN "bank_balance_at" timestamp with time zone;
|
||||
12076
drizzle/20261024011919_money_bank_balance/snapshot.json
Normal file
12076
drizzle/20261024011919_money_bank_balance/snapshot.json
Normal file
File diff suppressed because it is too large
Load Diff
194
drizzle/20261024013208_books/migration.sql
Normal file
194
drizzle/20261024013208_books/migration.sql
Normal file
@@ -0,0 +1,194 @@
|
||||
CREATE SCHEMA "books";
|
||||
--> statement-breakpoint
|
||||
CREATE TYPE "books"."metadata_status" AS ENUM('pending', 'matched', 'low_confidence', 'not_found', 'manual');--> statement-breakpoint
|
||||
CREATE TYPE "books"."reading_status" AS ENUM('want_to_read', 'reading', 'finished', 'abandoned');--> statement-breakpoint
|
||||
CREATE TABLE "books"."files" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"work_id" integer NOT NULL,
|
||||
"path" text NOT NULL,
|
||||
"format" text NOT NULL,
|
||||
"size_bytes" bigint NOT NULL,
|
||||
"sha256" text NOT NULL,
|
||||
"md5" text,
|
||||
"source" text,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "books"."notes" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"work_id" integer NOT NULL,
|
||||
"file_id" integer,
|
||||
"locator" text,
|
||||
"page" integer,
|
||||
"highlight" text,
|
||||
"note" text,
|
||||
"color" text,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "books"."progress" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"work_id" integer NOT NULL,
|
||||
"file_id" integer,
|
||||
"locator" text,
|
||||
"page" integer,
|
||||
"percent" real DEFAULT 0 NOT NULL,
|
||||
"started_at" timestamp with time zone,
|
||||
"finished_at" timestamp with time zone,
|
||||
"status" "books"."reading_status" DEFAULT 'want_to_read'::"books"."reading_status" NOT NULL,
|
||||
"rating" smallint,
|
||||
"last_read_at" timestamp with time zone,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "progress_percent_check" CHECK ("percent" between 0 and 100),
|
||||
CONSTRAINT "progress_rating_check" CHECK ("rating" is null or "rating" between 1 and 5)
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "books"."sessions" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"work_id" integer NOT NULL,
|
||||
"file_id" integer,
|
||||
"started_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"ended_at" timestamp with time zone,
|
||||
"start_percent" real,
|
||||
"end_percent" real,
|
||||
"start_page" integer,
|
||||
"end_page" integer,
|
||||
"pages_read" integer,
|
||||
"source" text DEFAULT 'reader' NOT NULL,
|
||||
"note" text,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "sessions_percent_check" CHECK (("start_percent" is null or "start_percent" between 0 and 100) and ("end_percent" is null or "end_percent" between 0 and 100)),
|
||||
CONSTRAINT "sessions_end_check" CHECK ("ended_at" is null or "ended_at" >= "started_at")
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "books"."shelf_works" (
|
||||
"shelf_id" integer,
|
||||
"work_id" integer,
|
||||
"sort_order" integer DEFAULT 0 NOT NULL,
|
||||
"added_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "shelf_works_pkey" PRIMARY KEY("shelf_id","work_id")
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "books"."shelves" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"slug" text NOT NULL,
|
||||
"name" text NOT NULL,
|
||||
"sort_order" integer DEFAULT 0 NOT NULL,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "books"."works" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"title" text NOT NULL,
|
||||
"subtitle" text,
|
||||
"authors" text[] DEFAULT '{}'::text[] NOT NULL,
|
||||
"isbn10" text,
|
||||
"isbn13" text,
|
||||
"publisher" text,
|
||||
"year" integer,
|
||||
"language" text,
|
||||
"page_count" integer,
|
||||
"description" text,
|
||||
"tags" text[] DEFAULT '{}'::text[] NOT NULL,
|
||||
"cover_url" text,
|
||||
"cover_path" text,
|
||||
"openlibrary_work_id" text,
|
||||
"openlibrary_edition_id" text,
|
||||
"google_books_id" text,
|
||||
"annas_md5" text[] DEFAULT '{}'::text[] NOT NULL,
|
||||
"source_refs" jsonb DEFAULT '{}' NOT NULL,
|
||||
"metadata_status" "books"."metadata_status" DEFAULT 'pending'::"books"."metadata_status" NOT NULL,
|
||||
"metadata_source" text,
|
||||
"metadata_fetched_at" timestamp with time zone,
|
||||
"notes" text,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "works_year_check" CHECK ("year" is null or "year" between 0 and 2100)
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX "files_path_key" ON "books"."files" ("path");--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX "files_sha256_key" ON "books"."files" ("sha256");--> statement-breakpoint
|
||||
CREATE INDEX "files_work_id_idx" ON "books"."files" ("work_id");--> statement-breakpoint
|
||||
CREATE INDEX "notes_work_id_idx" ON "books"."notes" ("work_id");--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX "progress_work_id_key" ON "books"."progress" ("work_id");--> statement-breakpoint
|
||||
CREATE INDEX "progress_status_idx" ON "books"."progress" ("status");--> statement-breakpoint
|
||||
CREATE INDEX "sessions_work_id_idx" ON "books"."sessions" ("work_id");--> statement-breakpoint
|
||||
CREATE INDEX "sessions_started_at_idx" ON "books"."sessions" ("started_at");--> statement-breakpoint
|
||||
CREATE INDEX "shelf_works_work_id_idx" ON "books"."shelf_works" ("work_id");--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX "shelves_slug_key" ON "books"."shelves" ("slug");--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX "works_isbn13_key" ON "books"."works" ("isbn13") WHERE "isbn13" is not null;--> statement-breakpoint
|
||||
CREATE INDEX "works_title_idx" ON "books"."works" (lower("title"));--> statement-breakpoint
|
||||
ALTER TABLE "books"."files" ADD CONSTRAINT "files_work_id_works_id_fkey" FOREIGN KEY ("work_id") REFERENCES "books"."works"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "books"."notes" ADD CONSTRAINT "notes_work_id_works_id_fkey" FOREIGN KEY ("work_id") REFERENCES "books"."works"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "books"."notes" ADD CONSTRAINT "notes_file_id_files_id_fkey" FOREIGN KEY ("file_id") REFERENCES "books"."files"("id") ON DELETE SET NULL;--> statement-breakpoint
|
||||
ALTER TABLE "books"."progress" ADD CONSTRAINT "progress_work_id_works_id_fkey" FOREIGN KEY ("work_id") REFERENCES "books"."works"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "books"."progress" ADD CONSTRAINT "progress_file_id_files_id_fkey" FOREIGN KEY ("file_id") REFERENCES "books"."files"("id") ON DELETE SET NULL;--> statement-breakpoint
|
||||
ALTER TABLE "books"."sessions" ADD CONSTRAINT "sessions_work_id_works_id_fkey" FOREIGN KEY ("work_id") REFERENCES "books"."works"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "books"."sessions" ADD CONSTRAINT "sessions_file_id_files_id_fkey" FOREIGN KEY ("file_id") REFERENCES "books"."files"("id") ON DELETE SET NULL;--> statement-breakpoint
|
||||
ALTER TABLE "books"."shelf_works" ADD CONSTRAINT "shelf_works_shelf_id_shelves_id_fkey" FOREIGN KEY ("shelf_id") REFERENCES "books"."shelves"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "books"."shelf_works" ADD CONSTRAINT "shelf_works_work_id_works_id_fkey" FOREIGN KEY ("work_id") REFERENCES "books"."works"("id") ON DELETE CASCADE;
|
||||
--> statement-breakpoint
|
||||
-- Hand-written: updated_at triggers, grants, default shelves, GUI registration.
|
||||
DO $$
|
||||
DECLARE t text;
|
||||
BEGIN
|
||||
FOREACH t IN ARRAY ARRAY['works','files','shelves','progress','notes'] LOOP
|
||||
EXECUTE format('CREATE TRIGGER touch_updated_at BEFORE UPDATE ON books.%I FOR EACH ROW EXECUTE FUNCTION meta.touch_updated_at()', t);
|
||||
END LOOP;
|
||||
END $$;
|
||||
--> statement-breakpoint
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'personal_app') THEN
|
||||
GRANT USAGE ON SCHEMA books TO personal_app;
|
||||
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA books TO personal_app;
|
||||
GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA books TO personal_app;
|
||||
ALTER DEFAULT PRIVILEGES IN SCHEMA books GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO personal_app;
|
||||
ALTER DEFAULT PRIVILEGES IN SCHEMA books GRANT USAGE, SELECT ON SEQUENCES TO personal_app;
|
||||
END IF;
|
||||
IF EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'personal_ro') THEN
|
||||
GRANT USAGE ON SCHEMA books TO personal_ro;
|
||||
GRANT SELECT ON ALL TABLES IN SCHEMA books TO personal_ro;
|
||||
ALTER DEFAULT PRIVILEGES IN SCHEMA books GRANT SELECT ON TABLES TO personal_ro;
|
||||
END IF;
|
||||
END $$;
|
||||
--> statement-breakpoint
|
||||
INSERT INTO books.shelves (slug, name, sort_order) VALUES
|
||||
('to-read', 'To read', 10), ('reading', 'Reading', 20), ('done', 'Done', 30), ('reference', 'Reference', 40)
|
||||
ON CONFLICT DO NOTHING;
|
||||
--> statement-breakpoint
|
||||
DO $$
|
||||
BEGIN
|
||||
IF to_regclass('meta.tables') IS NOT NULL THEN
|
||||
INSERT INTO meta.tables (schema_name, table_name, slug, display_name, kind, editable, has_done, sort_order)
|
||||
VALUES ('books','works','books-works','Books','core',true,false,1000),
|
||||
('books','shelves','books-shelves','Book shelves','core',true,false,1000),
|
||||
('books','files','books-files','Book files','core',false,false,1000)
|
||||
ON CONFLICT DO NOTHING;
|
||||
-- meta.sync() is executable by personal_app/personal_ddl, not by the migration role
|
||||
-- (personal) in prod; deploy-remote.sh runs it as the app role after migrating.
|
||||
IF has_function_privilege('meta.sync(integer)', 'EXECUTE') THEN
|
||||
PERFORM meta.sync();
|
||||
END IF;
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
--> statement-breakpoint
|
||||
-- Hand-written: updated_at trigger and explicit grants (default privileges from the books migration cover new tables too).
|
||||
CREATE TRIGGER touch_updated_at BEFORE UPDATE ON books.sessions FOR EACH ROW EXECUTE FUNCTION meta.touch_updated_at();
|
||||
--> statement-breakpoint
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'personal_app') THEN
|
||||
GRANT SELECT, INSERT, UPDATE, DELETE ON books.sessions TO personal_app;
|
||||
GRANT USAGE, SELECT ON SEQUENCE books.sessions_id_seq TO personal_app;
|
||||
END IF;
|
||||
IF EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'personal_ro') THEN
|
||||
GRANT SELECT ON books.sessions TO personal_ro;
|
||||
END IF;
|
||||
END $$;
|
||||
13722
drizzle/20261024013208_books/snapshot.json
Normal file
13722
drizzle/20261024013208_books/snapshot.json
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1 @@
|
||||
ALTER TABLE "money"."accounts" ADD COLUMN "include_in_total" boolean DEFAULT true NOT NULL;
|
||||
13735
drizzle/20261024021157_money_include_in_total/snapshot.json
Normal file
13735
drizzle/20261024021157_money_include_in_total/snapshot.json
Normal file
File diff suppressed because it is too large
Load Diff
142
drizzle/20261024022836_tasks/migration.sql
Normal file
142
drizzle/20261024022836_tasks/migration.sql
Normal file
@@ -0,0 +1,142 @@
|
||||
CREATE SCHEMA "tasks";
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "tasks"."activity" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"task_id" integer,
|
||||
"goal_id" integer,
|
||||
"kind" text NOT NULL,
|
||||
"from_status" text,
|
||||
"to_status" text,
|
||||
"note" text,
|
||||
"value" numeric(14,2),
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "activity_target_check" CHECK ("task_id" IS NOT NULL OR "goal_id" IS NOT NULL),
|
||||
CONSTRAINT "activity_kind_check" CHECK ("kind" IN ('created','status','edit','note','progress'))
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "tasks"."areas" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"slug" text NOT NULL,
|
||||
"name" text NOT NULL,
|
||||
"description" text,
|
||||
"color" text,
|
||||
"sort_order" integer DEFAULT 0 NOT NULL,
|
||||
"archived_at" timestamp with time zone,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "areas_slug_check" CHECK ("slug" ~ '^[a-z0-9][a-z0-9-]{0,62}$'),
|
||||
CONSTRAINT "areas_name_check" CHECK (length(btrim("name")) > 0)
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "tasks"."goals" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"title" text NOT NULL,
|
||||
"notes" text,
|
||||
"status" text DEFAULT 'active' NOT NULL,
|
||||
"area_id" integer,
|
||||
"target_date" date,
|
||||
"metric" text,
|
||||
"metric_unit" text,
|
||||
"metric_target" numeric(14,2),
|
||||
"metric_current" numeric(14,2),
|
||||
"flagged" boolean DEFAULT false NOT NULL,
|
||||
"sort_order" integer DEFAULT 0 NOT NULL,
|
||||
"source" text,
|
||||
"achieved_at" timestamp with time zone,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "goals_title_check" CHECK (length(btrim("title")) > 0),
|
||||
CONSTRAINT "goals_status_check" CHECK ("status" IN ('active','achieved','dropped'))
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "tasks"."tasks" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"title" text NOT NULL,
|
||||
"notes" text,
|
||||
"status" text DEFAULT 'todo' NOT NULL,
|
||||
"priority" smallint DEFAULT 0 NOT NULL,
|
||||
"flagged" boolean DEFAULT false NOT NULL,
|
||||
"due_date" date,
|
||||
"due_time" time,
|
||||
"reminder_at" timestamp with time zone,
|
||||
"recurrence" text,
|
||||
"area_id" integer,
|
||||
"goal_id" integer,
|
||||
"parent_id" integer,
|
||||
"tags" text[] DEFAULT '{}'::text[] NOT NULL,
|
||||
"sort_order" integer DEFAULT 0 NOT NULL,
|
||||
"source" text,
|
||||
"external_id" text,
|
||||
"started_at" timestamp with time zone,
|
||||
"completed_at" timestamp with time zone,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "tasks_title_check" CHECK (length(btrim("title")) > 0),
|
||||
CONSTRAINT "tasks_status_check" CHECK ("status" IN ('todo','in_progress','done','dropped')),
|
||||
CONSTRAINT "tasks_priority_check" CHECK ("priority" BETWEEN 0 AND 3),
|
||||
CONSTRAINT "tasks_parent_check" CHECK ("parent_id" IS NULL OR "parent_id" <> "id"),
|
||||
CONSTRAINT "tasks_time_check" CHECK ("due_time" IS NULL OR "due_date" IS NOT NULL)
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE INDEX "activity_task_idx" ON "tasks"."activity" ("task_id","created_at");--> statement-breakpoint
|
||||
CREATE INDEX "activity_goal_idx" ON "tasks"."activity" ("goal_id","created_at");--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX "areas_slug_key" ON "tasks"."areas" ("slug");--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX "goals_title_key" ON "tasks"."goals" (lower("title"));--> statement-breakpoint
|
||||
CREATE INDEX "tasks_area_idx" ON "tasks"."tasks" ("area_id");--> statement-breakpoint
|
||||
CREATE INDEX "tasks_goal_idx" ON "tasks"."tasks" ("goal_id");--> statement-breakpoint
|
||||
CREATE INDEX "tasks_parent_idx" ON "tasks"."tasks" ("parent_id");--> statement-breakpoint
|
||||
CREATE INDEX "tasks_status_due_idx" ON "tasks"."tasks" ("status","due_date");--> statement-breakpoint
|
||||
CREATE INDEX "tasks_tags_idx" ON "tasks"."tasks" USING gin ("tags");--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX "tasks_source_external_key" ON "tasks"."tasks" ("source","external_id") WHERE "external_id" IS NOT NULL;--> statement-breakpoint
|
||||
ALTER TABLE "tasks"."activity" ADD CONSTRAINT "activity_task_id_tasks_id_fkey" FOREIGN KEY ("task_id") REFERENCES "tasks"."tasks"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "tasks"."activity" ADD CONSTRAINT "activity_goal_id_goals_id_fkey" FOREIGN KEY ("goal_id") REFERENCES "tasks"."goals"("id") ON DELETE CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "tasks"."goals" ADD CONSTRAINT "goals_area_id_areas_id_fkey" FOREIGN KEY ("area_id") REFERENCES "tasks"."areas"("id") ON DELETE SET NULL;--> statement-breakpoint
|
||||
ALTER TABLE "tasks"."tasks" ADD CONSTRAINT "tasks_area_id_areas_id_fkey" FOREIGN KEY ("area_id") REFERENCES "tasks"."areas"("id") ON DELETE SET NULL;--> statement-breakpoint
|
||||
ALTER TABLE "tasks"."tasks" ADD CONSTRAINT "tasks_goal_id_goals_id_fkey" FOREIGN KEY ("goal_id") REFERENCES "tasks"."goals"("id") ON DELETE SET NULL;--> statement-breakpoint
|
||||
ALTER TABLE "tasks"."tasks" ADD CONSTRAINT "tasks_parent_id_tasks_id_fkey" FOREIGN KEY ("parent_id") REFERENCES "tasks"."tasks"("id") ON DELETE CASCADE;
|
||||
--> statement-breakpoint
|
||||
-- Hand-written below this line (not generated by drizzle-kit).
|
||||
-- updated_at triggers, same as every other core table.
|
||||
DO $$
|
||||
DECLARE t text;
|
||||
BEGIN
|
||||
IF to_regprocedure('meta.touch_updated_at()') IS NOT NULL THEN
|
||||
FOREACH t IN ARRAY ARRAY['areas','goals','tasks'] LOOP
|
||||
EXECUTE format('CREATE TRIGGER touch_updated_at BEFORE UPDATE ON tasks.%I FOR EACH ROW EXECUTE FUNCTION meta.touch_updated_at()', t);
|
||||
END LOOP;
|
||||
END IF;
|
||||
END $$;
|
||||
--> statement-breakpoint
|
||||
-- Privacy: like health, the task tracker is readable/writable by the app role only
|
||||
-- (no personal_ro grant). Grants only run on prod, where migrations run as `personal`.
|
||||
REVOKE ALL ON SCHEMA tasks FROM PUBLIC;
|
||||
--> statement-breakpoint
|
||||
DO $$
|
||||
BEGIN
|
||||
IF current_user = 'personal' AND EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'personal_app') THEN
|
||||
GRANT USAGE ON SCHEMA tasks TO personal_app;
|
||||
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA tasks TO personal_app;
|
||||
GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA tasks TO personal_app;
|
||||
ALTER DEFAULT PRIVILEGES FOR ROLE personal IN SCHEMA tasks
|
||||
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO personal_app;
|
||||
ALTER DEFAULT PRIVILEGES FOR ROLE personal IN SCHEMA tasks
|
||||
GRANT USAGE, SELECT ON SEQUENCES TO personal_app;
|
||||
END IF;
|
||||
END $$;
|
||||
--> statement-breakpoint
|
||||
-- Registered in meta.tables (structure only; the lists GUI only shows kind = 'list').
|
||||
DO $$
|
||||
BEGIN
|
||||
IF to_regclass('meta.tables') IS NOT NULL THEN
|
||||
INSERT INTO meta.tables (schema_name, table_name, slug, display_name, kind, editable, has_done, sort_order)
|
||||
VALUES ('tasks','tasks','tasks-tasks','Tasks','core',true,true,1000),
|
||||
('tasks','goals','tasks-goals','Goals','core',true,false,1000),
|
||||
('tasks','areas','tasks-areas','Task areas','core',true,false,1000),
|
||||
('tasks','activity','tasks-activity','Task activity','core',false,false,1000)
|
||||
ON CONFLICT DO NOTHING;
|
||||
-- deploy-remote.sh runs meta.sync() as the app role after migrating.
|
||||
IF has_function_privilege('meta.sync(integer)', 'EXECUTE') THEN
|
||||
PERFORM meta.sync();
|
||||
END IF;
|
||||
END IF;
|
||||
END $$;
|
||||
14935
drizzle/20261024022836_tasks/snapshot.json
Normal file
14935
drizzle/20261024022836_tasks/snapshot.json
Normal file
File diff suppressed because it is too large
Load Diff
29
drizzle/20261024024249_meta_edit_log/migration.sql
Normal file
29
drizzle/20261024024249_meta_edit_log/migration.sql
Normal file
@@ -0,0 +1,29 @@
|
||||
CREATE TABLE "meta"."edit_log" (
|
||||
"id" bigint PRIMARY KEY GENERATED ALWAYS AS IDENTITY (sequence name "meta"."edit_log_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854775807 START WITH 1 CACHE 1),
|
||||
"at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"user_id" text,
|
||||
"user_email" text,
|
||||
"schema_name" text NOT NULL,
|
||||
"table_name" text NOT NULL,
|
||||
"action" text NOT NULL,
|
||||
"pk" jsonb,
|
||||
"before" jsonb,
|
||||
"after" jsonb,
|
||||
CONSTRAINT "edit_log_action_check" CHECK (action IN ('insert','update','delete','import','add_column'))
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE INDEX "edit_log_table_at_index" ON "meta"."edit_log" ("schema_name","table_name","at");--> statement-breakpoint
|
||||
-- Hand-written below this line (not generated by drizzle-kit).
|
||||
-- The app role appends to and reads the log; nobody updates or deletes through the app.
|
||||
-- personal_ro gets no access: before/after values can hold private (health) data.
|
||||
-- Grants only run on prod (as `personal`); the dev DB is owned by personal_dev.
|
||||
DO $$
|
||||
BEGIN
|
||||
IF current_user = 'personal' AND EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'personal_app') THEN
|
||||
GRANT SELECT, INSERT ON meta.edit_log TO personal_app;
|
||||
GRANT USAGE ON SEQUENCE meta.edit_log_id_seq TO personal_app;
|
||||
END IF;
|
||||
IF EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'personal_ro') THEN
|
||||
REVOKE ALL ON meta.edit_log FROM personal_ro;
|
||||
END IF;
|
||||
END $$;
|
||||
15132
drizzle/20261024024249_meta_edit_log/snapshot.json
Normal file
15132
drizzle/20261024024249_meta_edit_log/snapshot.json
Normal file
File diff suppressed because it is too large
Load Diff
2
drizzle/20261024030132_tasks_ticktick/migration.sql
Normal file
2
drizzle/20261024030132_tasks_ticktick/migration.sql
Normal file
@@ -0,0 +1,2 @@
|
||||
ALTER TABLE "tasks"."tasks" ADD COLUMN "start_at" timestamp with time zone;--> statement-breakpoint
|
||||
ALTER TABLE "tasks"."tasks" ADD COLUMN "time_zone" text;
|
||||
15158
drizzle/20261024030132_tasks_ticktick/snapshot.json
Normal file
15158
drizzle/20261024030132_tasks_ticktick/snapshot.json
Normal file
File diff suppressed because it is too large
Load Diff
@@ -34,6 +34,6 @@ export default [
|
||||
}
|
||||
},
|
||||
{
|
||||
ignores: ['build/', '.svelte-kit/', 'dist/']
|
||||
ignores: ['build/', '.svelte-kit/', 'dist/', 'static/vendor/']
|
||||
}
|
||||
];
|
||||
|
||||
13
package.json
13
package.json
@@ -15,23 +15,18 @@
|
||||
"test:unit": "vitest",
|
||||
"db:push": "drizzle-kit push",
|
||||
"db:generate": "drizzle-kit generate",
|
||||
"db:migrate": "drizzle-kit migrate",
|
||||
"db:migrate": "bun scripts/db-migrate.ts",
|
||||
"db:refresh-dev": "bash scripts/db-refresh-dev.sh",
|
||||
"preview:branch": "bash scripts/preview.sh",
|
||||
"db:studio": "drizzle-kit studio",
|
||||
"db:seed": "bun scripts/seed-catalog.ts",
|
||||
"db:seed:cod": "bun scripts/seed-cod-catalog.ts",
|
||||
"db:migrate:auth": "bun scripts/migrate-auth-catalog.ts",
|
||||
"db:migrate:foundation": "bun scripts/migrate-foundation.ts",
|
||||
"db:migrate:money-tracker": "bun scripts/migrate-money-tracker.ts",
|
||||
"db:migrate:game-tracker": "bun scripts/migrate-game-tracker.ts",
|
||||
"db:migrate:cs-prices": "bun scripts/migrate-cs-prices.ts",
|
||||
"cs-prices:ingest": "bun scripts/ingest-cs-prices.ts",
|
||||
"db:migrate:house-tracker": "bun scripts/migrate-house-tracker.ts",
|
||||
"house:worker": "bun scripts/house-worker.ts",
|
||||
"auth:role": "bun scripts/set-admin.ts",
|
||||
"catalog:build": "python3 scripts/build-pokemon-catalog.py",
|
||||
"catalog:cod": "python3 scripts/build-cod-catalog.py && prettier --write data/call-of-duty.json",
|
||||
"test:auth": "node --env-file=.env node_modules/@playwright/test/cli.js test --config playwright.auth.config.ts",
|
||||
"db:migrate:tracker-journal": "bun scripts/migrate-tracker-journal.ts",
|
||||
"catalog:research": "python3 scripts/build-pokemon-research.py",
|
||||
"db:seed:research": "bun scripts/seed-pokemon-research.ts",
|
||||
"db:seed:challenges": "bun scripts/seed-cod-challenges.ts",
|
||||
@@ -97,9 +92,11 @@
|
||||
"drizzle-orm": "^1.0.0-rc.4",
|
||||
"echarts": "^6.1.0",
|
||||
"highlight.js": "^11.12.0",
|
||||
"jose": "^6.2.10",
|
||||
"marked": "^18.0.11",
|
||||
"marked-highlight": "^2.2.4",
|
||||
"mode-watcher": "^1.1.0",
|
||||
"pdfjs-dist": "^6.4.299",
|
||||
"postgres": "^3.4.9",
|
||||
"topojson-client": "^3.1.0",
|
||||
"world-atlas": "^2.0.2",
|
||||
|
||||
@@ -1,84 +0,0 @@
|
||||
-- One-off: convert animaldex ids from uuid to serial, keeping existing rows.
|
||||
-- Postgres can't cast uuid -> integer, so db:push can't do this itself.
|
||||
--
|
||||
-- Run BEFORE re-running db:push, once per database:
|
||||
-- psql "$DB_URL" -v ON_ERROR_STOP=1 -f scripts/animaldex-uuid-to-serial.sql
|
||||
--
|
||||
-- Safe to run on an empty database too. Everything is in one transaction, so a
|
||||
-- failure leaves the database untouched.
|
||||
|
||||
BEGIN;
|
||||
|
||||
-- the failed push created this before erroring out
|
||||
DROP SEQUENCE IF EXISTS animals_id_seq;
|
||||
|
||||
-- 1. new integer ids alongside the uuid ones
|
||||
ALTER TABLE animals ADD COLUMN new_id serial;
|
||||
ALTER TABLE sightings ADD COLUMN new_id serial;
|
||||
ALTER TABLE photos ADD COLUMN new_id serial;
|
||||
|
||||
-- 2. translate the foreign keys through the uuid -> integer mapping
|
||||
ALTER TABLE sightings ADD COLUMN new_animal_id integer;
|
||||
UPDATE sightings s SET new_animal_id = a.new_id FROM animals a WHERE a.id = s.animal_id;
|
||||
|
||||
ALTER TABLE photos ADD COLUMN new_sighting_id integer;
|
||||
UPDATE photos p SET new_sighting_id = s.new_id FROM sightings s WHERE s.id = p.sighting_id;
|
||||
|
||||
-- 3. drop the old foreign keys (names vary by how they were created)
|
||||
DO $$
|
||||
DECLARE r record;
|
||||
BEGIN
|
||||
FOR r IN
|
||||
SELECT conrelid::regclass AS tbl, conname
|
||||
FROM pg_constraint
|
||||
WHERE contype = 'f' AND conrelid IN ('sightings'::regclass, 'photos'::regclass)
|
||||
LOOP
|
||||
EXECUTE format('ALTER TABLE %s DROP CONSTRAINT %I', r.tbl, r.conname);
|
||||
END LOOP;
|
||||
END $$;
|
||||
|
||||
-- 4. swap the foreign key columns
|
||||
ALTER TABLE photos DROP COLUMN sighting_id;
|
||||
ALTER TABLE photos RENAME COLUMN new_sighting_id TO sighting_id;
|
||||
ALTER TABLE photos ALTER COLUMN sighting_id SET NOT NULL;
|
||||
|
||||
ALTER TABLE sightings DROP COLUMN animal_id;
|
||||
ALTER TABLE sightings RENAME COLUMN new_animal_id TO animal_id;
|
||||
ALTER TABLE sightings ALTER COLUMN animal_id SET NOT NULL;
|
||||
|
||||
-- 5. swap the primary keys
|
||||
DO $$
|
||||
DECLARE r record;
|
||||
BEGIN
|
||||
FOR r IN
|
||||
SELECT conrelid::regclass AS tbl, conname
|
||||
FROM pg_constraint
|
||||
WHERE contype = 'p'
|
||||
AND conrelid IN ('animals'::regclass, 'sightings'::regclass, 'photos'::regclass)
|
||||
LOOP
|
||||
EXECUTE format('ALTER TABLE %s DROP CONSTRAINT %I', r.tbl, r.conname);
|
||||
END LOOP;
|
||||
END $$;
|
||||
|
||||
ALTER TABLE animals DROP COLUMN id;
|
||||
ALTER TABLE animals RENAME COLUMN new_id TO id;
|
||||
ALTER TABLE animals ADD PRIMARY KEY (id);
|
||||
ALTER SEQUENCE animals_new_id_seq RENAME TO animals_id_seq;
|
||||
|
||||
ALTER TABLE sightings DROP COLUMN id;
|
||||
ALTER TABLE sightings RENAME COLUMN new_id TO id;
|
||||
ALTER TABLE sightings ADD PRIMARY KEY (id);
|
||||
ALTER SEQUENCE sightings_new_id_seq RENAME TO sightings_id_seq;
|
||||
|
||||
ALTER TABLE photos DROP COLUMN id;
|
||||
ALTER TABLE photos RENAME COLUMN new_id TO id;
|
||||
ALTER TABLE photos ADD PRIMARY KEY (id);
|
||||
ALTER SEQUENCE photos_new_id_seq RENAME TO photos_id_seq;
|
||||
|
||||
-- 6. restore the foreign keys under the names drizzle expects
|
||||
ALTER TABLE sightings ADD CONSTRAINT sightings_animal_id_animals_id_fkey
|
||||
FOREIGN KEY (animal_id) REFERENCES animals(id) ON DELETE CASCADE;
|
||||
ALTER TABLE photos ADD CONSTRAINT photos_sighting_id_sightings_id_fkey
|
||||
FOREIGN KEY (sighting_id) REFERENCES sightings(id) ON DELETE CASCADE;
|
||||
|
||||
COMMIT;
|
||||
@@ -3,9 +3,10 @@ import postgres from 'postgres';
|
||||
// Read-only fingerprints: lets a deployment verify saved progress without logging it.
|
||||
if (!process.env.DB_URL) throw new Error('DB_URL is required');
|
||||
const client = postgres(process.env.DB_URL, { max: 1 });
|
||||
// All saved progress lives in schema `games` since the schema reorg.
|
||||
const tables = [
|
||||
'user_entries',
|
||||
'user_games',
|
||||
'catch_em_all_entries',
|
||||
'catch_em_all_games',
|
||||
'catch_em_all_settings',
|
||||
'pokemon_journeys',
|
||||
'pokemon_catches',
|
||||
@@ -20,11 +21,11 @@ const tables = [
|
||||
try {
|
||||
const result: Record<string, unknown> = {};
|
||||
for (const table of tables) {
|
||||
const [exists] = await client`select to_regclass(${'public.' + table}) is not null as present`;
|
||||
const [exists] = await client`select to_regclass(${'games.' + table}) is not null as present`;
|
||||
if (!exists.present) continue;
|
||||
const [row] = await client.unsafe(`select count(*)::int as rows,
|
||||
md5(coalesce(string_agg(record, E'\\n' order by record), '')) as fingerprint
|
||||
from (select row_to_json(t)::text as record from "${table}" t) s`);
|
||||
from (select row_to_json(t)::text as record from games."${table}" t) s`);
|
||||
result[table] = row;
|
||||
}
|
||||
console.log(JSON.stringify(result, null, 2));
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
-- Additive migration for the existing domain schema. No users or progress are removed.
|
||||
ALTER TABLE "user" ADD COLUMN IF NOT EXISTS role text NOT NULL DEFAULT 'user';
|
||||
ALTER TABLE account ADD COLUMN IF NOT EXISTS issuer text;
|
||||
-- Existing credentials use Better Auth's local namespace. Fail on unknown legacy
|
||||
-- OAuth providers rather than silently changing which remote identity they trust.
|
||||
UPDATE account SET issuer = 'local:credential' WHERE issuer IS NULL AND provider_id = 'credential';
|
||||
UPDATE account SET issuer = 'https://accounts.google.com' WHERE issuer IS NULL AND provider_id = 'google';
|
||||
UPDATE account SET issuer = 'local:oauth:github' WHERE issuer IS NULL AND provider_id = 'github';
|
||||
UPDATE account SET issuer = 'local:oauth:osu' WHERE issuer IS NULL AND provider_id = 'osu';
|
||||
ALTER TABLE account ALTER COLUMN issuer SET NOT NULL;
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS account_issuer_account_id_unique ON account (issuer, account_id);
|
||||
ALTER TABLE games ADD COLUMN IF NOT EXISTS source_id integer;
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS games_source_id_unique ON games (source_id);
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS pokemon_number_unique ON pokemon (number);
|
||||
ALTER TYPE availability ADD VALUE IF NOT EXISTS 'unknown';
|
||||
CREATE TABLE IF NOT EXISTS catch_em_all_settings (
|
||||
user_id text PRIMARY KEY REFERENCES "user"(id) ON DELETE CASCADE
|
||||
);
|
||||
161
scripts/bank-link.ts
Normal file
161
scripts/bank-link.ts
Normal file
@@ -0,0 +1,161 @@
|
||||
// Link a bank to the money tracker through Enable Banking. Each bank gets its own session;
|
||||
// linking one never touches another bank's session.
|
||||
//
|
||||
// bun --env-file=$HOME/.config/money-sync.env scripts/bank-link.ts Revolut (personal + joint)
|
||||
// ... scripts/bank-link.ts --bank abn ABN AMRO -> "ABN Amro"
|
||||
// ... scripts/bank-link.ts --bank knab Knab -> "Knab"
|
||||
// ... scripts/bank-link.ts --check verify key + app id only
|
||||
// ... scripts/bank-link.ts --code '<redirect URL or code>' [--yes] finish a started consent
|
||||
// ... scripts/bank-link.ts --map <uid|IBAN>=<Tracker account> [--map ...] override the mapping
|
||||
// ... scripts/bank-link.ts --aspsp '<ASPSP name>' --account <Tracker account> --country NL --user <id>
|
||||
//
|
||||
// Then sync within the hour: the first sync of an account asks for the longest history the
|
||||
// bank serves, and most banks only serve ~90 days once consent is older than an hour.
|
||||
//
|
||||
// Needs ENABLEBANKING_APP_ID, ENABLEBANKING_KEY_PATH, DB_URL (and optionally
|
||||
// ENABLEBANKING_REDIRECT_URL). Never prints the private key.
|
||||
import { createInterface } from 'node:readline/promises';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import postgres from 'postgres';
|
||||
import {
|
||||
configFromEnv,
|
||||
createEnableBankingClient,
|
||||
parseAuthCode
|
||||
} from '../src/lib/server/enable-banking';
|
||||
import { planAccountMapping, saveSession, trackerUserId } from '../src/lib/server/bank-sync';
|
||||
|
||||
const args = process.argv.slice(2);
|
||||
const flag = (name: string) => args.includes(name);
|
||||
const value = (name: string) => {
|
||||
const i = args.indexOf(name);
|
||||
return i >= 0 ? args[i + 1] : undefined;
|
||||
};
|
||||
const overrides: Record<string, string> = {};
|
||||
args.forEach((arg, i) => {
|
||||
if (arg !== '--map') return;
|
||||
const [key, ...rest] = (args[i + 1] ?? '').split('=');
|
||||
if (!key) return;
|
||||
overrides[key] = rest.join('=');
|
||||
overrides[key.replace(/\s/g, '').toUpperCase()] = rest.join('=');
|
||||
});
|
||||
|
||||
// Enable Banking ASPSP names (GET /aspsps?country=NL) and the tracker account they feed.
|
||||
const BANKS: Record<string, { aspsp: string; account: string; hint: string }> = {
|
||||
revolut: {
|
||||
aspsp: 'Revolut',
|
||||
account: 'Revolut',
|
||||
hint: 'Tick BOTH your personal account and the joint account with Juhee.'
|
||||
},
|
||||
abn: {
|
||||
aspsp: 'ABN AMRO',
|
||||
account: 'ABN Amro',
|
||||
hint: 'Log in with the ABN AMRO app or identifier and approve your current account.'
|
||||
},
|
||||
knab: {
|
||||
aspsp: 'Knab',
|
||||
account: 'Knab',
|
||||
hint: 'Approve your Knab current account (savings accounts are not needed).'
|
||||
}
|
||||
};
|
||||
const bankKey = (value('--bank') ?? 'revolut').toLowerCase().replace(/[\s-]|amro/g, '');
|
||||
const preset = value('--aspsp') && !value('--bank') ? undefined : BANKS[bankKey];
|
||||
if (!preset && !value('--aspsp'))
|
||||
throw new Error(`Unknown --bank ${value('--bank')}; use ${Object.keys(BANKS).join(', ')}`);
|
||||
const client = createEnableBankingClient(configFromEnv());
|
||||
const aspsp = {
|
||||
name: value('--aspsp') ?? preset!.aspsp,
|
||||
country: value('--country') ?? 'NL'
|
||||
};
|
||||
const defaultAccount = value('--account') ?? preset?.account ?? aspsp.name;
|
||||
const redirectUrl =
|
||||
process.env.ENABLEBANKING_REDIRECT_URL ??
|
||||
'https://stanrunge.dev/projects/money-tracker/bank-callback'; // registered with Enable Banking; 308s to /trackers/money/bank-callback with the query kept
|
||||
|
||||
const app = await client.application();
|
||||
console.log(
|
||||
`Enable Banking app "${app.name}" (${app.environment}, ${app.active ? 'active' : 'INACTIVE'}), services ${app.services.join(',')}`
|
||||
);
|
||||
const { aspsps } = await client.aspsps(aspsp.country);
|
||||
const bank = aspsps.find((a) => a.name.toLowerCase() === aspsp.name.toLowerCase());
|
||||
if (!bank) throw new Error(`${aspsp.name} (${aspsp.country}) is not available for this app`);
|
||||
console.log(
|
||||
`${bank.name} ${bank.country}${bank.bic ? ' ' + bank.bic : ''}: max consent ` +
|
||||
`${Math.floor(bank.maximum_consent_validity / 86400)} days${bank.beta ? ' (beta)' : ''}` +
|
||||
(bank.required_psu_headers?.length
|
||||
? `; requires PSU headers ${bank.required_psu_headers.join(', ')} (background syncs ` +
|
||||
'run as "PSU not present", which PSD2 limits to ~4 calls a day per account)'
|
||||
: '')
|
||||
);
|
||||
console.log(
|
||||
`Linked accounts go to tracker account "${defaultAccount}" unless --map says otherwise.`
|
||||
);
|
||||
if (flag('--check')) process.exit(0);
|
||||
|
||||
const rl = createInterface({ input: process.stdin, output: process.stdout });
|
||||
let pasted = value('--code');
|
||||
if (!pasted) {
|
||||
// Stay one hour under the bank's maximum so the API never rejects valid_until.
|
||||
const validUntil = new Date(Date.now() + (bank.maximum_consent_validity - 3600) * 1000);
|
||||
const auth = await client.startAuth({
|
||||
aspsp: { name: bank.name, country: bank.country },
|
||||
redirectUrl,
|
||||
validUntil,
|
||||
psuType: 'personal',
|
||||
state: randomUUID()
|
||||
});
|
||||
console.log(`\n1. Open this link and approve access at ${bank.name}.`);
|
||||
console.log(` ${preset?.hint ?? 'Approve the account(s) you want to track.'}\n`);
|
||||
console.log(' ' + auth.url + '\n');
|
||||
console.log(`2. You'll land on ${redirectUrl}?code=...`);
|
||||
console.log(
|
||||
' Copy the full address from the browser bar (or just the code) and paste it here.'
|
||||
);
|
||||
console.log(' The code expires quickly, so paste it within a few minutes.\n');
|
||||
pasted = await rl.question('Redirect URL or code: ');
|
||||
}
|
||||
const parsed = parseAuthCode(pasted ?? '');
|
||||
if (parsed.error) throw new Error(`Consent failed: ${parsed.error}`);
|
||||
if (!parsed.code) throw new Error('No authorisation code found in what was pasted');
|
||||
|
||||
const session = await client.createSession(parsed.code);
|
||||
console.log(
|
||||
`\nSession authorised until ${session.access.valid_until}; ${session.accounts.length} account(s):`
|
||||
);
|
||||
|
||||
const sql = postgres(process.env.DB_URL!, { max: 1, onnotice: () => {} });
|
||||
try {
|
||||
const userId = await trackerUserId(sql, value('--user'));
|
||||
const tracker = await sql<
|
||||
{ id: number; name: string; scope: string; iban: string | null; external_uid: string | null }[]
|
||||
>`select id, name, scope, iban, external_uid from money.accounts where user_id = ${userId} order by id`;
|
||||
const plan = planAccountMapping(session.accounts, tracker, overrides, {
|
||||
defaultAccount,
|
||||
joint: bank.name.toLowerCase() === 'revolut'
|
||||
});
|
||||
for (const { bank: account, tracker: target, reason } of plan) {
|
||||
const iban = account.account_id?.iban ?? '(no IBAN)';
|
||||
console.log(
|
||||
` ${account.uid} ${iban} ${account.currency ?? ''} ${account.name ?? ''}${account.details ? ' / ' + account.details : ''}` +
|
||||
`\n -> ${target ? target.name : 'not linked'} (${reason})`
|
||||
);
|
||||
}
|
||||
const answer = flag('--yes') ? 'y' : await rl.question('\nSave this session and mapping? [y/N] ');
|
||||
if (!/^y(es)?$/i.test(answer.trim())) {
|
||||
console.log('Nothing saved. Re-run with --code is not possible (codes are single-use);');
|
||||
console.log('start again and use --map <uid>=<Tracker account> to fix the mapping.');
|
||||
process.exit(1);
|
||||
}
|
||||
await saveSession(sql, userId, session, plan);
|
||||
const linked = plan.filter((p) => p.tracker).map((p) => p.tracker!.name);
|
||||
const only = linked.length === 1 ? ` --account '${linked[0]}'` : '';
|
||||
console.log('Saved. Within the next hour (full history is only served right after consent):');
|
||||
console.log(
|
||||
` bun --env-file=$HOME/.config/money-sync.env scripts/bank-sync.ts --reconcile --dry-run${only}`
|
||||
);
|
||||
console.log(
|
||||
` bun --env-file=$HOME/.config/money-sync.env scripts/bank-sync.ts --reconcile${only}`
|
||||
);
|
||||
} finally {
|
||||
rl.close();
|
||||
await sql.end();
|
||||
}
|
||||
53
scripts/bank-sync.ts
Normal file
53
scripts/bank-sync.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
// Pull booked bank transactions into money.transactions (Enable Banking).
|
||||
//
|
||||
// bun --env-file=$HOME/.config/money-sync.env scripts/bank-sync.ts [options]
|
||||
// --dry-run fetch and report, write nothing
|
||||
// --reconcile match against ALL hand-entered rows (account + amount + date ±1 day)
|
||||
// and attach the bank id to them instead of inserting duplicates;
|
||||
// run once after linking (with --dry-run first)
|
||||
// --from YYYY-MM-DD override the start date (default: last synced date - 7 days, or the
|
||||
// account's first transaction on the first run)
|
||||
// --full ask for the longest history the bank serves (automatic on an
|
||||
// account's first sync; only works within an hour of consent at most banks)
|
||||
// --account <name> only this tracker account
|
||||
//
|
||||
// Dedupe: unique (account_id, external_id); re-running is always safe.
|
||||
import postgres from 'postgres';
|
||||
import { configFromEnv, createEnableBankingClient } from '../src/lib/server/enable-banking';
|
||||
import { syncBankAccounts } from '../src/lib/server/bank-sync';
|
||||
|
||||
const args = process.argv.slice(2);
|
||||
const value = (name: string) => {
|
||||
const i = args.indexOf(name);
|
||||
return i >= 0 ? args[i + 1] : undefined;
|
||||
};
|
||||
const from = value('--from');
|
||||
if (from && !/^\d{4}-\d{2}-\d{2}$/.test(from)) throw new Error('--from must be YYYY-MM-DD');
|
||||
if (!process.env.DB_URL) throw new Error('DB_URL is not set');
|
||||
|
||||
const client = createEnableBankingClient(configFromEnv());
|
||||
const sql = postgres(process.env.DB_URL, { max: 2, connect_timeout: 10, onnotice: () => {} });
|
||||
const dryRun = args.includes('--dry-run');
|
||||
try {
|
||||
const results = await syncBankAccounts(sql, client, {
|
||||
dryRun,
|
||||
reconcile: args.includes('--reconcile'),
|
||||
from,
|
||||
full: args.includes('--full'),
|
||||
accountName: value('--account')
|
||||
});
|
||||
for (const r of results) {
|
||||
console.log(
|
||||
`${r.account}: fetched ${r.fetched}, booked ${r.booked}, new ${r.inserted}, ` +
|
||||
`matched to existing ${r.matched}, replaced sum entries ${r.absorbed}, ` +
|
||||
`already synced ${r.alreadyKnown}` +
|
||||
(r.historyFrom ? `, history from ${r.historyFrom}` : '') +
|
||||
(r.balance ? `, bank balance ${r.balance}` : '') +
|
||||
(r.error ? `\n ERROR ${r.error}` : '')
|
||||
);
|
||||
}
|
||||
if (dryRun) console.log('(dry run: nothing written)');
|
||||
if (results.some((r) => r.error)) process.exitCode = 1;
|
||||
} finally {
|
||||
await sql.end();
|
||||
}
|
||||
952
scripts/books-ingest.py
Normal file
952
scripts/books-ingest.py
Normal file
@@ -0,0 +1,952 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Books ingest + metadata enrichment.
|
||||
|
||||
Builds books.works / books.files from:
|
||||
* lists.books rows (the old GUI list) -> placeholders
|
||||
* Obsidian "Book links.md" md5s resolved via the libgen JSON API -> placeholders
|
||||
* ebook files dropped in $BOOKS_ROOT/inbox (sha256-deduped) -> works + files
|
||||
|
||||
Metadata: embedded (EPUB OPF / PDF info + ISBN scan) -> Open Library -> Google Books
|
||||
(-> arXiv for anything tagged paper). Covers go to $BOOKS_ROOT/covers/<sha1(cover_url)[:16]>.jpg (stable across dev/prod).
|
||||
Files are moved to $BOOKS_ROOT/library/<Author>/<Title>/.
|
||||
|
||||
Usage:
|
||||
DB_URL=postgresql://... BOOKS_ROOT=~/books \
|
||||
python scripts/books-ingest.py --obsidian-json resolved.json [--reset] [--dry-run]
|
||||
|
||||
Network calls are cached in $BOOKS_ROOT/.cache/http so reruns are cheap and polite.
|
||||
Refuses to run against the prod database name ('personal') unless --allow-prod.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse, difflib, hashlib, html, json, os, re, shutil, sys, time, unicodedata, urllib.parse, urllib.request, zipfile
|
||||
from dataclasses import dataclass, field
|
||||
from pathlib import Path
|
||||
from xml.etree import ElementTree as ET
|
||||
|
||||
def log(*a):
|
||||
print(*a, flush=True)
|
||||
|
||||
UA = "stan-books-ingest/0.1 (personal library catalogue; low volume)"
|
||||
LANG = {"eng": "en", "dut": "nl", "nld": "nl", "ger": "de", "deu": "de", "fre": "fr", "fra": "fr", "spa": "es", "ita": "it",
|
||||
"por": "pt", "rus": "ru", "jpn": "ja", "kor": "ko", "chi": "zh", "zho": "zh", "lat": "la", "gre": "el", "swe": "sv"}
|
||||
BOOK_EXT = {".pdf", ".epub", ".mobi", ".azw3", ".azw", ".lit", ".djvu", ".docx", ".fb2", ".cbz"}
|
||||
|
||||
# ---------------------------------------------------------------- helpers
|
||||
|
||||
def strip_accents(s: str) -> str:
|
||||
s = (s or "").replace("'", "").replace("’", "")
|
||||
return "".join(c for c in unicodedata.normalize("NFKD", s) if not unicodedata.combining(c))
|
||||
|
||||
def norm_title(t: str) -> str:
|
||||
t = strip_accents(html.unescape(t or "")).lower().replace("&", " and ")
|
||||
t = re.split(r"\s*[:(\[]\s*|\s+[-–—]\s+|_\s", t, maxsplit=1)[0] # main title only
|
||||
t = re.sub(r"[^a-z0-9]+", " ", t).strip()
|
||||
t = re.sub(r"^(the|a|an|de|het|een)\s+", "", t)
|
||||
t = re.sub(r"\s+(\d+(st|nd|rd|th)?\s+edition|ed)$", "", t)
|
||||
return t
|
||||
|
||||
def full_norm(t: str) -> str:
|
||||
return re.sub(r"[^a-z0-9]+", " ", strip_accents(t or "").lower()).strip()
|
||||
|
||||
def sim(a: str, b: str) -> float:
|
||||
na, nb = norm_title(a), norm_title(b)
|
||||
a, b = full_norm(a), full_norm(b)
|
||||
if not a or not b:
|
||||
return 0.0
|
||||
if a == b:
|
||||
return 1.0
|
||||
# volume / part / edition markers must agree ("Finance I" vs "Finance II", "Part 1" vs "Part 2")
|
||||
marks = lambda x: {t for t in x.split() if re.fullmatch(r"\d{1,2}|[ivx]{1,4}", t)}
|
||||
if marks(a) != marks(b) and marks(a) and marks(b):
|
||||
return 0.5
|
||||
r = difflib.SequenceMatcher(None, a, b).ratio()
|
||||
if na and na == nb and len(na) >= 15:
|
||||
r = max(r, 0.93)
|
||||
return r
|
||||
|
||||
def surnames(authors: list[str]) -> set[str]:
|
||||
out = set()
|
||||
for a in authors or []:
|
||||
a = strip_accents(a).lower()
|
||||
if "," in a: # "King, Michael"
|
||||
a = a.split(",")[0]
|
||||
parts = re.findall(r"[a-z]{3,}", a)
|
||||
if parts:
|
||||
out.add(parts[-1] if "," not in a else parts[0])
|
||||
out.update(parts)
|
||||
return out
|
||||
|
||||
JUNK_AUTHORS = {"admin", "administrator", "user", "owner", "unknown", "oreily", "oreilly", "o'reilly", "author", "john", "mr2"}
|
||||
|
||||
def clean_authors(names) -> list[str]:
|
||||
out = []
|
||||
for n in names or []:
|
||||
n = re.sub(r"\s+", " ", (n or "").strip(" ,;"))
|
||||
low = n.lower()
|
||||
if not n or low in JUNK_AUTHORS or re.search(r"\d|@|\.(com|org|net)\b", n) or " " not in n and "," not in n and "." not in n:
|
||||
continue
|
||||
if n.isupper():
|
||||
n = n.title()
|
||||
out.append(n)
|
||||
return out
|
||||
|
||||
def authors_compatible(a: list[str], b: list[str]) -> bool:
|
||||
if not a or not b:
|
||||
return True
|
||||
return bool(surnames(a) & surnames(b))
|
||||
|
||||
def isbn_ok(s: str) -> str | None:
|
||||
s = re.sub(r"[^0-9Xx]", "", s or "").upper()
|
||||
if len(s) == 13 and s.isdigit() and s[:3] in ("978", "979"):
|
||||
tot = sum(int(c) * (1 if i % 2 == 0 else 3) for i, c in enumerate(s[:12]))
|
||||
return s if (10 - tot % 10) % 10 == int(s[12]) else None
|
||||
if len(s) == 10 and s[:9].isdigit():
|
||||
tot = sum((10 - i) * int(c) for i, c in enumerate(s[:9]))
|
||||
chk = (11 - tot % 11) % 11
|
||||
return s if (("X" if chk == 10 else str(chk)) == s[9]) else None
|
||||
return None
|
||||
|
||||
def to13(s: str) -> str | None:
|
||||
s = isbn_ok(s)
|
||||
if not s:
|
||||
return None
|
||||
if len(s) == 13:
|
||||
return s
|
||||
core = "978" + s[:9]
|
||||
tot = sum(int(c) * (1 if i % 2 == 0 else 3) for i, c in enumerate(core))
|
||||
return core + str((10 - tot % 10) % 10)
|
||||
|
||||
def to10(s13: str) -> str | None:
|
||||
if not s13 or not s13.startswith("978"):
|
||||
return None
|
||||
core = s13[3:12]
|
||||
tot = sum((10 - i) * int(c) for i, c in enumerate(core))
|
||||
chk = (11 - tot % 11) % 11
|
||||
return core + ("X" if chk == 10 else str(chk))
|
||||
|
||||
ISBN_RE = re.compile(r"ISBN(?:-1[03])?[:\s]*((?:97[89][\s-]?)?(?:\d[\s-]?){9}[\dXx])", re.I)
|
||||
|
||||
def find_isbns(text: str) -> list[str]:
|
||||
out = []
|
||||
for m in ISBN_RE.finditer(text or ""):
|
||||
i = to13(m.group(1))
|
||||
if i and i not in out:
|
||||
out.append(i)
|
||||
return out
|
||||
|
||||
def safe_name(s: str, n: int = 80) -> str:
|
||||
s = strip_accents(s or "").replace("/", "-").replace("\\", "-")
|
||||
s = re.sub(r'[<>:"|?*\x00-\x1f]', "", s)
|
||||
s = re.sub(r"\s+", " ", s).strip(" .")
|
||||
return (s[:n].rstrip(" .") or "Untitled")
|
||||
|
||||
def year_of(v) -> int | None:
|
||||
m = re.search(r"(1[5-9]\d\d|20\d\d)", str(v or ""))
|
||||
return int(m.group(1)) if m else None
|
||||
|
||||
# ---------------------------------------------------------------- http with cache
|
||||
|
||||
class Http:
|
||||
def __init__(self, cache: Path, delay: float = 1.0):
|
||||
self.cache, self.delay, self.last = cache, delay, 0.0
|
||||
cache.mkdir(parents=True, exist_ok=True)
|
||||
self.calls = 0
|
||||
self.dead_hosts: set[str] = set()
|
||||
|
||||
def _get(self, url: str, binary=False):
|
||||
host = urllib.parse.urlparse(url).netloc
|
||||
if host in self.dead_hosts: # daily quota hit (Google Books): stop hammering it this run
|
||||
return 429, b""
|
||||
wait = self.delay - (time.time() - self.last)
|
||||
if wait > 0:
|
||||
time.sleep(wait)
|
||||
self.last = time.time()
|
||||
self.calls += 1
|
||||
req = urllib.request.Request(url, headers={"User-Agent": UA, "Accept": "*/*"})
|
||||
for attempt in range(3):
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=30) as r:
|
||||
return r.status, r.read()
|
||||
except urllib.error.HTTPError as e:
|
||||
if e.code == 429 and "googleapis" in host:
|
||||
self.dead_hosts.add(host)
|
||||
log(f" {host}: quota exhausted (429), skipping it for the rest of this run")
|
||||
return 429, b""
|
||||
if e.code in (429, 503) and attempt < 2:
|
||||
time.sleep(5 * (attempt + 1))
|
||||
continue
|
||||
return e.code, b""
|
||||
except Exception:
|
||||
if attempt < 2:
|
||||
time.sleep(3)
|
||||
continue
|
||||
return 0, b""
|
||||
return 0, b""
|
||||
|
||||
def json(self, url: str):
|
||||
key = self.cache / (hashlib.sha1(url.encode()).hexdigest() + ".json")
|
||||
if key.exists():
|
||||
return json.loads(key.read_text())
|
||||
status, body = self._get(url)
|
||||
data = None
|
||||
if status == 200:
|
||||
try:
|
||||
data = json.loads(body)
|
||||
except Exception:
|
||||
data = None
|
||||
if status in (200, 404): # don't cache transient errors
|
||||
key.write_text(json.dumps(data))
|
||||
return data
|
||||
|
||||
def text(self, url: str):
|
||||
key = self.cache / (hashlib.sha1(url.encode()).hexdigest() + ".txt")
|
||||
if key.exists():
|
||||
return key.read_text()
|
||||
status, body = self._get(url)
|
||||
if status == 200:
|
||||
key.write_text(body.decode("utf-8", "replace"))
|
||||
return body.decode("utf-8", "replace")
|
||||
return None
|
||||
|
||||
def download(self, url: str, dest: Path) -> bool:
|
||||
status, body = self._get(url, binary=True)
|
||||
if status == 200 and len(body) > 1500: # skip 1x1 placeholders
|
||||
dest.write_bytes(body)
|
||||
return True
|
||||
return False
|
||||
|
||||
# ---------------------------------------------------------------- model
|
||||
|
||||
@dataclass
|
||||
class Work:
|
||||
title: str
|
||||
authors: list[str] = field(default_factory=list)
|
||||
subtitle: str | None = None
|
||||
isbns: list[str] = field(default_factory=list) # isbn13s, best first
|
||||
publisher: str | None = None
|
||||
year: int | None = None
|
||||
language: str | None = None
|
||||
page_count: int | None = None
|
||||
description: str | None = None
|
||||
tags: list[str] = field(default_factory=list)
|
||||
cover_url: str | None = None
|
||||
cover_path: str | None = None
|
||||
ol_work: str | None = None
|
||||
ol_edition: str | None = None
|
||||
gb_id: str | None = None
|
||||
md5s: list[str] = field(default_factory=list)
|
||||
refs: dict = field(default_factory=dict)
|
||||
status: str = "pending"
|
||||
source: str | None = None
|
||||
notes: str | None = None
|
||||
files: list = field(default_factory=list)
|
||||
shelves: set = field(default_factory=set)
|
||||
origins: set = field(default_factory=set)
|
||||
db_id: int | None = None
|
||||
|
||||
def add_ref(self, k, v):
|
||||
self.refs.setdefault(k, [])
|
||||
if v not in self.refs[k]:
|
||||
self.refs[k].append(v)
|
||||
|
||||
@dataclass
|
||||
class FileRec:
|
||||
path: Path
|
||||
rel_inbox: str
|
||||
sha256: str
|
||||
md5: str
|
||||
size: int
|
||||
fmt: str
|
||||
title: str | None = None
|
||||
authors: list[str] = field(default_factory=list)
|
||||
isbns: list[str] = field(default_factory=list)
|
||||
year: int | None = None
|
||||
publisher: str | None = None
|
||||
language: str | None = None
|
||||
pages: int | None = None
|
||||
is_paper: bool = False
|
||||
broken: str | None = None
|
||||
dest_rel: str | None = None
|
||||
|
||||
# ---------------------------------------------------------------- embedded metadata
|
||||
|
||||
def epub_meta(p: Path) -> dict:
|
||||
out: dict = {}
|
||||
try:
|
||||
with zipfile.ZipFile(p) as z:
|
||||
cont = ET.fromstring(z.read("META-INF/container.xml"))
|
||||
opf_path = next(e.get("full-path") for e in cont.iter() if e.tag.endswith("rootfile"))
|
||||
opf = ET.fromstring(z.read(opf_path))
|
||||
md = next((e for e in opf.iter() if e.tag.endswith("metadata")), None)
|
||||
if md is not None:
|
||||
for e in md:
|
||||
tag = e.tag.split("}")[-1]
|
||||
txt = (e.text or "").strip()
|
||||
if not txt:
|
||||
continue
|
||||
if tag == "title" and "title" not in out:
|
||||
out["title"] = txt
|
||||
elif tag == "creator":
|
||||
out.setdefault("authors", []).append(txt)
|
||||
elif tag == "identifier":
|
||||
i = to13(txt.replace("urn:isbn:", ""))
|
||||
if i:
|
||||
out.setdefault("isbns", []).append(i)
|
||||
elif tag == "publisher":
|
||||
out.setdefault("publisher", txt)
|
||||
elif tag == "date":
|
||||
out.setdefault("year", year_of(txt))
|
||||
elif tag == "language":
|
||||
out.setdefault("language", txt[:2].lower())
|
||||
# scan the first few content docs (copyright page) for ISBNs
|
||||
names = [n for n in z.namelist() if n.lower().endswith((".xhtml", ".html", ".htm"))][:12]
|
||||
for n in names:
|
||||
for i in find_isbns(re.sub(r"<[^>]+>", " ", z.read(n).decode("utf-8", "ignore"))):
|
||||
out.setdefault("isbns", [])
|
||||
if i not in out["isbns"]:
|
||||
out["isbns"].append(i)
|
||||
except Exception as e: # noqa
|
||||
out["error"] = f"epub: {e.__class__.__name__}"
|
||||
return out
|
||||
|
||||
def pdf_meta(p: Path) -> dict:
|
||||
out: dict = {}
|
||||
try:
|
||||
from pypdf import PdfReader
|
||||
import logging
|
||||
logging.getLogger("pypdf").setLevel(logging.ERROR)
|
||||
r = PdfReader(str(p), strict=False)
|
||||
out["pages"] = len(r.pages)
|
||||
info = r.metadata or {}
|
||||
t = (info.get("/Title") or "").strip() if info else ""
|
||||
a = (info.get("/Author") or "").strip() if info else ""
|
||||
if t:
|
||||
out["title"] = t
|
||||
if a:
|
||||
out["authors"] = clean_authors(re.split(r";|\band\b|&", a))
|
||||
text = ""
|
||||
for i in range(min(len(r.pages), 10)):
|
||||
try:
|
||||
text += (r.pages[i].extract_text() or "") + "\n"
|
||||
except Exception:
|
||||
pass
|
||||
out["isbns"] = find_isbns(text)
|
||||
if re.search(r"arXiv:\d{4}\.\d{4,5}", text):
|
||||
out["arxiv"] = re.search(r"arXiv:(\d{4}\.\d{4,5})", text).group(1)
|
||||
except Exception as e: # noqa
|
||||
out["error"] = f"pdf: {e.__class__.__name__}"
|
||||
return out
|
||||
|
||||
ANNAS_RE = re.compile(r"^(?P<title>.+?) -- (?P<author>.+?) -- (?P<rest>.+?) -- (?P<md5>[0-9a-f]{32}) -- Anna’s Archive$")
|
||||
|
||||
def filename_meta(stem: str) -> dict:
|
||||
out: dict = {}
|
||||
m = ANNAS_RE.match(stem)
|
||||
if m:
|
||||
out["title"] = m["title"].replace("_ ", ": ").replace("_", " ")
|
||||
out["authors"] = [a.strip().replace("_", ".") for a in m["author"].split(",") if a.strip()]
|
||||
out["md5"] = m["md5"]
|
||||
for part in m["rest"].split(" -- "):
|
||||
i = to13(part)
|
||||
if i:
|
||||
out.setdefault("isbns", []).append(i)
|
||||
elif year_of(part) and "year" not in out:
|
||||
out["year"] = year_of(part)
|
||||
return out
|
||||
s = re.sub(r"\s*-\s*libgen\.(li|rs|is)$", "", stem)
|
||||
s = re.sub(r"\s*\((z-library|z-lib\.org|1lib\.\w+)\)", "", s, flags=re.I)
|
||||
s = re.sub(r"\s+(penguin (modern )?classics)$", "", s, flags=re.I)
|
||||
s = re.sub(r"\s+0$", "", s)
|
||||
mm = re.match(r"^([^-]+?) : ([^-]+?) - (.+)$", s) # "A : B - Title" (libgen multi-author)
|
||||
if mm:
|
||||
out["authors"] = [mm[1].strip(), mm[2].strip()]
|
||||
s = mm[3]
|
||||
pm = re.search(r"\s*\((\d{4}),\s*([^)]+)\)\s*$", s)
|
||||
if pm:
|
||||
out["year"], out["publisher"] = int(pm[1]), pm[2].strip()
|
||||
s = s[: pm.start()]
|
||||
# "Author - Title" (only when the left part looks like a person name)
|
||||
mm = re.match(r"^([A-Z][a-z]+(?: [A-Z][a-z]+){1,2}) - (.+)$", s)
|
||||
if mm:
|
||||
out["authors"] = [mm[1]]
|
||||
s = mm[2]
|
||||
mm = re.match(r"^(.+?) by ([A-Z][\w.]+(?: [A-Z][\w.]+){1,2})$", s)
|
||||
if mm:
|
||||
s, out["authors"] = mm[1], [mm[2]]
|
||||
if s.count("-") >= 4 and " " not in s: # The-Lean-Startup_-How-...-Eric-Ries-2011-Crown-Business-030788791X-
|
||||
for tok in s.split("-"):
|
||||
i = to13(tok)
|
||||
if i:
|
||||
out.setdefault("isbns", []).append(i)
|
||||
s = s.split("_")[0].replace("-", " ")
|
||||
s = s.replace("_ ", ": ").replace("_", " ")
|
||||
if s.count(".") >= 3 and s.count(" ") <= 1: # Sipser_Introduction.to.the.Theory.of.Computation.3E
|
||||
s = s.replace(".", " ")
|
||||
s = re.sub(r"\s+", " ", s).strip(" -")
|
||||
out["title"] = s
|
||||
return out
|
||||
|
||||
def good_embedded_title(t: str | None, fname_title: str) -> bool:
|
||||
if not t or len(t) < 4:
|
||||
return False
|
||||
bad = ("untitled", "microsoft word", ".doc", ".pdf", ".indd", "layout 1", "unknown", "title", "cover")
|
||||
tl = t.lower()
|
||||
if any(b in tl for b in bad) or re.fullmatch(r"[\w-]+\.\w+", t) or re.fullmatch(r"[0-9a-f\-_ ]+", tl):
|
||||
return False
|
||||
return sim(t, fname_title) > 0.55
|
||||
|
||||
# ---------------------------------------------------------------- enrichment
|
||||
|
||||
class Enricher:
|
||||
def __init__(self, http: Http):
|
||||
self.h = http
|
||||
|
||||
def ol_isbn(self, isbn: str):
|
||||
ed = self.h.json(f"https://openlibrary.org/isbn/{isbn}.json")
|
||||
if not ed or "title" not in ed:
|
||||
return None
|
||||
rec = {"title": ed.get("title"), "subtitle": ed.get("subtitle"), "publisher": (ed.get("publishers") or [None])[0],
|
||||
"year": year_of(ed.get("publish_date")), "pages": ed.get("number_of_pages"),
|
||||
"ol_edition": ed.get("key", "").split("/")[-1] or None, "isbn": isbn,
|
||||
"cover": f"https://covers.openlibrary.org/b/id/{ed['covers'][0]}-L.jpg" if ed.get("covers") and ed["covers"][0] > 0 else None,
|
||||
"lang": LANG.get(((ed.get("languages") or [{}])[0].get("key") or "").split("/")[-1])}
|
||||
wk = (ed.get("works") or [{}])[0].get("key")
|
||||
if wk:
|
||||
w = self.h.json(f"https://openlibrary.org{wk}.json") or {}
|
||||
rec["ol_work"] = wk.split("/")[-1]
|
||||
d = w.get("description")
|
||||
rec["description"] = d.get("value") if isinstance(d, dict) else d
|
||||
rec["subjects"] = (w.get("subjects") or [])[:8]
|
||||
names = []
|
||||
for a in (w.get("authors") or [])[:4]:
|
||||
k = (a.get("author") or {}).get("key")
|
||||
if k:
|
||||
aj = self.h.json(f"https://openlibrary.org{k}.json") or {}
|
||||
if aj.get("name"):
|
||||
names.append(aj["name"])
|
||||
rec["authors"] = names
|
||||
if not rec["cover"] and w.get("covers") and w["covers"][0] > 0:
|
||||
rec["cover"] = f"https://covers.openlibrary.org/b/id/{w['covers'][0]}-L.jpg"
|
||||
return rec
|
||||
|
||||
def ol_search(self, title: str, authors: list[str]):
|
||||
q = {"title": norm_title(title) if len(norm_title(title)) > 3 else title, "limit": 8,
|
||||
"fields": "key,title,subtitle,author_name,first_publish_year,isbn,cover_i,cover_edition_key,language,number_of_pages_median,publisher,subject"}
|
||||
if authors:
|
||||
q["author"] = re.sub(r"[^\w\s.-]", " ", authors[0])
|
||||
d = self.h.json("https://openlibrary.org/search.json?" + urllib.parse.urlencode(q))
|
||||
docs = (d or {}).get("docs") or []
|
||||
if not docs and authors:
|
||||
q.pop("author")
|
||||
d = self.h.json("https://openlibrary.org/search.json?" + urllib.parse.urlencode(q))
|
||||
docs = (d or {}).get("docs") or []
|
||||
best, bs = None, 0.0
|
||||
for doc in docs:
|
||||
t = doc.get("title", "")
|
||||
s = max(sim(title, t), sim(title, f"{t}: {doc.get('subtitle', '')}"))
|
||||
if norm_title(title) and norm_title(title) == norm_title(t) and (
|
||||
authors and doc.get("author_name") and authors_compatible(authors, doc["author_name"]) or len(norm_title(t)) >= 15):
|
||||
s = max(s, 0.92)
|
||||
if authors and doc.get("author_name"):
|
||||
s += 0.05 if authors_compatible(authors, doc["author_name"]) else -0.25
|
||||
if s > bs:
|
||||
best, bs = doc, s
|
||||
if not best:
|
||||
return None, 0.0
|
||||
isbns = [to13(i) for i in (best.get("isbn") or [])]
|
||||
isbns = [i for i in isbns if i]
|
||||
rec = {"title": best.get("title"), "subtitle": best.get("subtitle"), "authors": best.get("author_name") or [],
|
||||
"year": best.get("first_publish_year"), "ol_work": best.get("key", "").split("/")[-1] or None,
|
||||
"ol_edition": best.get("cover_edition_key"), "pages": best.get("number_of_pages_median"),
|
||||
"publisher": (best.get("publisher") or [None])[0],
|
||||
"cover": f"https://covers.openlibrary.org/b/id/{best['cover_i']}-L.jpg" if best.get("cover_i") else None,
|
||||
"lang": LANG.get((best.get("language") or [""])[0]), "subjects": (best.get("subject") or [])[:8],
|
||||
"isbn": next((i for i in isbns if i.startswith("978")), isbns[0] if isbns else None)}
|
||||
if rec["ol_work"]:
|
||||
w = self.h.json(f"https://openlibrary.org/works/{rec['ol_work']}.json") or {}
|
||||
dsc = w.get("description")
|
||||
rec["description"] = dsc.get("value") if isinstance(dsc, dict) else dsc
|
||||
return rec, min(bs, 1.0)
|
||||
|
||||
def gb(self, title: str | None, authors: list[str], isbn: str | None = None):
|
||||
if isbn:
|
||||
q = f"isbn:{isbn}"
|
||||
else:
|
||||
q = f'intitle:"{norm_title(title) or title}"' + (f" inauthor:{surnames(authors[:1]).pop()}" if authors and surnames(authors[:1]) else "")
|
||||
d = self.h.json("https://www.googleapis.com/books/v1/volumes?" + urllib.parse.urlencode({"q": q, "maxResults": 8, "printType": "books"}))
|
||||
items = (d or {}).get("items") or []
|
||||
best, bs = None, 0.0
|
||||
for it in items:
|
||||
v = it.get("volumeInfo", {})
|
||||
if isbn:
|
||||
s = 1.0
|
||||
else:
|
||||
s = max(sim(title, v.get("title", "")), sim(title, f"{v.get('title', '')}: {v.get('subtitle', '')}"))
|
||||
if norm_title(title) and norm_title(title) == norm_title(v.get("title", "")) and (
|
||||
authors and v.get("authors") and authors_compatible(authors, v["authors"]) or len(norm_title(title)) >= 15):
|
||||
s = max(s, 0.92)
|
||||
if authors and v.get("authors"):
|
||||
s += 0.05 if authors_compatible(authors, v["authors"]) else -0.25
|
||||
if s > bs:
|
||||
best, bs = it, s
|
||||
if isbn:
|
||||
break
|
||||
if not best:
|
||||
return None, 0.0
|
||||
v = best["volumeInfo"]
|
||||
ids = {x.get("type"): x.get("identifier") for x in v.get("industryIdentifiers", [])}
|
||||
img = (v.get("imageLinks") or {})
|
||||
cover = img.get("extraLarge") or img.get("large") or img.get("medium") or img.get("thumbnail")
|
||||
if cover:
|
||||
cover = cover.replace("http://", "https://").replace("&edge=curl", "")
|
||||
return {"title": v.get("title"), "subtitle": v.get("subtitle"), "authors": v.get("authors") or [],
|
||||
"publisher": v.get("publisher"), "year": year_of(v.get("publishedDate")), "pages": v.get("pageCount"),
|
||||
"description": re.sub(r"<[^>]+>", "", v.get("description") or "") or None,
|
||||
"lang": (v.get("language") or "")[:2] or None, "gb_id": best.get("id"), "cover": cover,
|
||||
"isbn": to13(ids.get("ISBN_13") or ids.get("ISBN_10") or ""), "subjects": v.get("categories") or []}, min(bs, 1.0)
|
||||
|
||||
def arxiv(self, title: str, arxiv_id: str | None = None):
|
||||
if arxiv_id:
|
||||
url = f"https://export.arxiv.org/api/query?id_list={arxiv_id}"
|
||||
else:
|
||||
t = re.sub(r"[^\w\s]", " ", title)
|
||||
url = "https://export.arxiv.org/api/query?" + urllib.parse.urlencode({"search_query": f'ti:"{t}"', "max_results": 5})
|
||||
x = self.h.text(url)
|
||||
if not x:
|
||||
return None, 0.0
|
||||
ns = {"a": "http://www.w3.org/2005/Atom"}
|
||||
try:
|
||||
root = ET.fromstring(x)
|
||||
except ET.ParseError:
|
||||
return None, 0.0
|
||||
best, bs = None, 0.0
|
||||
for e in root.findall("a:entry", ns):
|
||||
t = re.sub(r"\s+", " ", e.findtext("a:title", "", ns)).strip()
|
||||
if not t or t == "Error":
|
||||
continue
|
||||
s = 1.0 if arxiv_id else sim(title, t)
|
||||
if s > bs:
|
||||
best, bs = (e, t), s
|
||||
if not best:
|
||||
return None, 0.0
|
||||
e, t = best
|
||||
return {"title": t, "authors": [a.findtext("a:name", "", ns) for a in e.findall("a:author", ns)],
|
||||
"year": year_of(e.findtext("a:published", "", ns)),
|
||||
"description": re.sub(r"\s+", " ", e.findtext("a:summary", "", ns)).strip(),
|
||||
"arxiv": e.findtext("a:id", "", ns).split("/abs/")[-1]}, bs
|
||||
|
||||
# ---------------------------------------------------------------- main
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument("--obsidian-json", help="md5 -> libgen record map from the resolver")
|
||||
ap.add_argument("--extra", action="append", default=[], help='extra placeholder "Title|Author|isbn13|origin"')
|
||||
ap.add_argument("--reset", action="store_true", help="truncate books.works/files first (scratch DBs only)")
|
||||
ap.add_argument("--dry-run", action="store_true", help="no DB writes, no file moves; write plan json")
|
||||
ap.add_argument("--no-move", action="store_true", help="write DB rows (and covers) but leave files in the inbox; for a dev rehearsal before the prod run")
|
||||
ap.add_argument("--no-network", action="store_true")
|
||||
ap.add_argument("--allow-prod", action="store_true")
|
||||
ap.add_argument("--source", default="gdrive")
|
||||
args = ap.parse_args()
|
||||
|
||||
root = Path(os.path.expanduser(os.environ.get("BOOKS_ROOT", "/srv/books")))
|
||||
inbox, library, covers = root / "inbox", root / "library", root / "covers"
|
||||
for d in (inbox, library, covers):
|
||||
d.mkdir(parents=True, exist_ok=True)
|
||||
db_url = os.environ.get("DB_URL")
|
||||
if not db_url:
|
||||
sys.exit("DB_URL required")
|
||||
dbname = urllib.parse.urlparse(db_url).path.lstrip("/")
|
||||
if dbname == "personal" and not args.allow_prod:
|
||||
sys.exit("refusing to run against prod DB 'personal' (use --allow-prod)")
|
||||
|
||||
import psycopg
|
||||
conn = psycopg.connect(db_url)
|
||||
http = Http(root / ".cache" / "http", delay=0.0 if args.no_network else 1.0)
|
||||
en = Enricher(http)
|
||||
works: list[Work] = []
|
||||
|
||||
|
||||
# ---- 1. placeholders: lists.books
|
||||
with conn.cursor() as cur:
|
||||
cur.execute("select to_regclass('lists.books')")
|
||||
has_lists = cur.fetchone()[0] is not None
|
||||
rows = []
|
||||
if has_lists:
|
||||
cur.execute("select id, name, author, notes, done_at from lists.books order by sort_order nulls last, id")
|
||||
rows = cur.fetchall()
|
||||
for bid, name, author, notes, done_at in rows:
|
||||
w = Work(title=name.strip(), authors=[author.strip()] if author else [], notes=notes or None)
|
||||
w.add_ref("lists_books_id", bid)
|
||||
w.origins.add("lists")
|
||||
w.shelves.add("done" if done_at else "to-read")
|
||||
if done_at:
|
||||
w.refs["done_at"] = done_at.isoformat()
|
||||
works.append(w)
|
||||
n_lists = len(rows)
|
||||
|
||||
def find_work(title, authors, isbns, md5=None):
|
||||
for w in works:
|
||||
if md5 and md5 in w.md5s:
|
||||
return w
|
||||
if isbns and set(isbns) & set(w.isbns) and (not title or not w.title or sim(title, w.title) >= 0.85):
|
||||
return w
|
||||
best, bs = None, 0.0
|
||||
for w in works:
|
||||
s = sim(title, w.title)
|
||||
if s >= 0.9 and authors_compatible(authors, w.authors) and s > bs:
|
||||
best, bs = w, s
|
||||
return best
|
||||
|
||||
def merge(w: Work, title, authors, isbns, **kw):
|
||||
if authors and not w.authors:
|
||||
w.authors = authors
|
||||
for i in isbns or []:
|
||||
if i not in w.isbns:
|
||||
w.isbns.append(i)
|
||||
for k in ("year", "publisher", "language"):
|
||||
if kw.get(k) and not getattr(w, k):
|
||||
setattr(w, k, kw[k])
|
||||
# prefer the longer, properly-cased title when the list name is a lowercase shorthand
|
||||
if title and (w.title.islower() or len(norm_title(w.title)) < len(norm_title(title)) and sim(w.title, title) > 0.9 and w.title == w.title.lower()):
|
||||
w.title = title
|
||||
|
||||
# ---- 2. placeholders: Obsidian md5s
|
||||
obs_total = obs_resolved = obs_overlap = 0
|
||||
if args.obsidian_json:
|
||||
data = json.load(open(args.obsidian_json))
|
||||
for md5, rec in data.items():
|
||||
obs_total += 1
|
||||
eds = (rec or {}).get("editions") or []
|
||||
if not eds:
|
||||
continue
|
||||
obs_resolved += 1
|
||||
ed = next((e for e in eds if e.get("isbns")), eds[0])
|
||||
isbns = []
|
||||
for e in eds:
|
||||
for i in e.get("isbns") or []:
|
||||
i13 = to13(i)
|
||||
if i13 and i13 not in isbns:
|
||||
isbns.append(i13)
|
||||
title = (ed.get("title") or rec.get("locator") or md5).strip()
|
||||
authors = [a.strip() for a in re.split(r";|, (?=[A-Z][a-z]+ [A-Z])", ed.get("author") or "") if a.strip()]
|
||||
lang = LANG.get((ed.get("lang") or [""])[0], None) if isinstance(ed.get("lang"), list) else None
|
||||
w = find_work(title, authors, isbns, md5)
|
||||
if w:
|
||||
if "obsidian" not in w.origins:
|
||||
obs_overlap += 1
|
||||
merge(w, title, authors, isbns, year=year_of(ed.get("year")), publisher=ed.get("publisher") or None, language=lang)
|
||||
else:
|
||||
w = Work(title=title, authors=authors, isbns=isbns, year=year_of(ed.get("year")), publisher=ed.get("publisher") or None, language=lang)
|
||||
works.append(w)
|
||||
w.shelves.add("to-read")
|
||||
w.origins.add("obsidian")
|
||||
if md5 not in w.md5s:
|
||||
w.md5s.append(md5)
|
||||
w.add_ref("obsidian", {"md5": md5, "ext": rec.get("ext"), "filesize": rec.get("filesize")})
|
||||
for ex in args.extra:
|
||||
t, a, i, origin = (ex.split("|") + ["", "", "", ""])[:4]
|
||||
isbns = [to13(i)] if to13(i) else []
|
||||
authors = [a] if a else []
|
||||
w = find_work(t, authors, isbns)
|
||||
if w:
|
||||
merge(w, t, authors, isbns)
|
||||
else:
|
||||
w = Work(title=t, authors=authors, isbns=isbns)
|
||||
works.append(w)
|
||||
w.shelves.add("to-read")
|
||||
w.origins.add("obsidian")
|
||||
w.add_ref("obsidian_link", origin)
|
||||
n_placeholders = len(works)
|
||||
|
||||
# ---- 3. files in inbox
|
||||
files: list[FileRec] = []
|
||||
for p in sorted(inbox.rglob("*")):
|
||||
if not p.is_file() or p.name.startswith("."):
|
||||
continue
|
||||
h256, h5 = hashlib.sha256(), hashlib.md5()
|
||||
with open(p, "rb") as f:
|
||||
for chunk in iter(lambda: f.read(1 << 20), b""):
|
||||
h256.update(chunk)
|
||||
h5.update(chunk)
|
||||
rel = str(p.relative_to(inbox))
|
||||
ext = p.suffix.lower()
|
||||
with open(p, "rb") as hf:
|
||||
head = hf.read(8)
|
||||
fmt = ext.lstrip(".") or "bin"
|
||||
if head.startswith(b"%PDF"):
|
||||
fmt = "pdf"
|
||||
elif head.startswith(b"PK") and ext in (".epub", ""):
|
||||
fmt = "epub"
|
||||
fr = FileRec(path=p, rel_inbox=rel, sha256=h256.hexdigest(), md5=h5.hexdigest(), size=p.stat().st_size, fmt=fmt)
|
||||
fr.is_paper = rel.split("/")[0].lower() == "papers"
|
||||
if ext == ".crdownload":
|
||||
fr.broken = "partial Chrome download (.crdownload)"
|
||||
fr.fmt = "pdf" if head.startswith(b"%PDF") else "partial"
|
||||
files.append(fr)
|
||||
by_sha: dict[str, FileRec] = {}
|
||||
dups: list[tuple[FileRec, FileRec]] = []
|
||||
uniq: list[FileRec] = []
|
||||
for fr in files:
|
||||
if fr.sha256 in by_sha:
|
||||
dups.append((fr, by_sha[fr.sha256]))
|
||||
else:
|
||||
by_sha[fr.sha256] = fr
|
||||
uniq.append(fr)
|
||||
log(f"files: {len(files)} total, {len(uniq)} unique, {len(dups)} sha256 duplicates")
|
||||
|
||||
# already-ingested shas (rerun without --reset)
|
||||
with conn.cursor() as cur:
|
||||
cur.execute("select sha256 from books.files")
|
||||
known = {r[0] for r in cur.fetchall()} if not args.reset else set()
|
||||
|
||||
matched_existing = 0
|
||||
for fr in uniq:
|
||||
if fr.sha256 in known:
|
||||
continue
|
||||
stem = Path(fr.rel_inbox).name
|
||||
stem = stem[: -len(Path(stem).suffix)] if Path(stem).suffix.lower() in BOOK_EXT | {".crdownload"} else stem
|
||||
fm = filename_meta(stem)
|
||||
em = epub_meta(fr.path) if fr.fmt == "epub" else pdf_meta(fr.path) if fr.fmt == "pdf" and not fr.broken else {}
|
||||
fr.title = fm.get("title") or stem
|
||||
if fr.fmt == "epub" and good_embedded_title(em.get("title"), fr.title):
|
||||
fr.title = em["title"]
|
||||
fr.authors = fm.get("authors") or [a for a in em.get("authors", []) if len(a) > 2 and "unknown" not in a.lower()][:4]
|
||||
fr.isbns = list(dict.fromkeys((fm.get("isbns") or []) + (em.get("isbns") or [])))[:6]
|
||||
fr.year = fm.get("year") or em.get("year")
|
||||
fr.publisher = fm.get("publisher") or em.get("publisher")
|
||||
fr.language = em.get("language")
|
||||
fr.pages = em.get("pages")
|
||||
md5 = fm.get("md5") or fr.md5
|
||||
w = find_work(fr.title, fr.authors, fr.isbns[:2], md5) or (find_work(fr.title, fr.authors, [], fr.md5))
|
||||
if w and w.files == [] and w.origins & {"lists", "obsidian"} and "drive" not in w.origins:
|
||||
matched_existing += 1
|
||||
if w:
|
||||
merge(w, fr.title, fr.authors, fr.isbns[:2], year=fr.year, publisher=fr.publisher, language=fr.language)
|
||||
else:
|
||||
w = Work(title=fr.title, authors=fr.authors, isbns=fr.isbns[:2], year=fr.year, publisher=fr.publisher, language=fr.language, page_count=fr.pages)
|
||||
works.append(w)
|
||||
if fr.is_paper and "paper" not in w.tags:
|
||||
w.tags.append("paper")
|
||||
if fr.md5 not in w.md5s and fm.get("md5"):
|
||||
w.md5s.append(fm["md5"])
|
||||
w.origins.add("drive")
|
||||
w.add_ref("drive", fr.rel_inbox)
|
||||
if em.get("arxiv"):
|
||||
w.refs["arxiv"] = em["arxiv"]
|
||||
if fr.broken:
|
||||
w.notes = ((w.notes + "\n") if w.notes else "") + f"Drive copy is broken: {fr.broken}; needs a fresh copy."
|
||||
w.add_ref("broken_files", fr.rel_inbox)
|
||||
else:
|
||||
w.files.append(fr)
|
||||
for d, orig in dups:
|
||||
for w in works:
|
||||
if orig in w.files:
|
||||
w.add_ref("drive_duplicates", d.rel_inbox)
|
||||
log(f"works: {len(works)} ({n_placeholders} placeholders before files; {matched_existing} placeholders got files)")
|
||||
|
||||
# ---- 4. enrichment
|
||||
stats = {"matched": 0, "low_confidence": 0, "not_found": 0}
|
||||
for n, w in enumerate(works, 1):
|
||||
if args.no_network:
|
||||
break
|
||||
rec, score, src = None, 0.0, None
|
||||
if "paper" in w.tags:
|
||||
rec, score = en.arxiv(w.title, w.refs.get("arxiv"))
|
||||
src = "arxiv"
|
||||
else:
|
||||
for isbn in w.isbns[:3]:
|
||||
rec = en.ol_isbn(isbn)
|
||||
if rec and (sim(w.title, rec["title"]) > 0.6 or sim(w.title, f"{rec['title']}: {rec.get('subtitle') or ''}") > 0.6 or "drive" not in w.origins):
|
||||
score, src = 1.0, "openlibrary"
|
||||
break
|
||||
rec = None
|
||||
if not rec:
|
||||
rec, score = en.ol_search(w.title, w.authors)
|
||||
src = "openlibrary"
|
||||
if score < 0.9:
|
||||
g, gs = en.gb(None, [], w.isbns[0]) if w.isbns else (None, 0.0)
|
||||
if g and sim(w.title, g["title"] or "") < 0.6 and sim(w.title, f"{g['title']}: {g.get('subtitle') or ''}") < 0.6:
|
||||
g, gs = None, 0.0
|
||||
if not g:
|
||||
g, gs = en.gb(w.title, w.authors)
|
||||
if g and gs > score:
|
||||
rec, score, src = g, gs, "googlebooks"
|
||||
if rec and score >= 0.9:
|
||||
w.status = "matched"
|
||||
elif rec and score >= 0.75:
|
||||
w.status = "low_confidence"
|
||||
else:
|
||||
w.status = "not_found"
|
||||
stats[w.status] += 1
|
||||
if rec:
|
||||
w.refs["match"] = {"source": src, "score": round(score, 3), "title": rec.get("title"), "authors": rec.get("authors")}
|
||||
if w.status == "matched":
|
||||
w.source = src
|
||||
if src == "openlibrary" or src == "googlebooks":
|
||||
# keep Stan's/the file's title wording unless it was a lowercase list shorthand or a filename
|
||||
# keep Stan's own list wording; otherwise take the catalogue's properly-cased title
|
||||
if rec.get("title") and ("lists" not in w.origins or w.title.islower()):
|
||||
w.title = rec["title"]
|
||||
w.subtitle = w.subtitle or rec.get("subtitle")
|
||||
elif src == "arxiv":
|
||||
w.title = rec["title"]
|
||||
if rec.get("authors"):
|
||||
w.authors = rec["authors"][:6]
|
||||
w.year = w.year or rec.get("year")
|
||||
if src == "openlibrary" and rec.get("year") and w.year and rec["year"] < w.year:
|
||||
w.year = w.year # edition year stays; first-publish year recorded in refs
|
||||
w.refs["first_publish_year"] = rec["year"]
|
||||
w.publisher = w.publisher or rec.get("publisher")
|
||||
w.page_count = w.page_count or rec.get("pages")
|
||||
w.language = w.language or rec.get("lang")
|
||||
w.description = w.description or rec.get("description")
|
||||
if rec.get("isbn") and rec["isbn"] not in w.isbns:
|
||||
w.isbns.insert(0 if not w.isbns else len(w.isbns), rec["isbn"])
|
||||
w.ol_work = w.ol_work or rec.get("ol_work")
|
||||
w.ol_edition = w.ol_edition or rec.get("ol_edition")
|
||||
w.gb_id = w.gb_id or rec.get("gb_id")
|
||||
w.cover_url = w.cover_url or rec.get("cover")
|
||||
if rec.get("arxiv"):
|
||||
w.refs["arxiv"] = rec["arxiv"]
|
||||
for s in (rec.get("subjects") or [])[:5]:
|
||||
s = s.lower().strip()
|
||||
if 2 < len(s) < 40 and s not in w.tags:
|
||||
w.tags.append(s)
|
||||
if n % 25 == 0:
|
||||
log(f" enriched {n}/{len(works)} ({http.calls} http calls)")
|
||||
if args.no_network:
|
||||
stats = {"skipped": len(works)}
|
||||
log("metadata:", stats)
|
||||
|
||||
# cover fallback for matched works lacking one: Google Books by isbn
|
||||
if not args.no_network:
|
||||
for w in works:
|
||||
if not w.cover_url and w.status == "matched" and "paper" not in w.tags and w.isbns:
|
||||
g, _ = en.gb(None, [], w.isbns[0])
|
||||
if g and g.get("cover"):
|
||||
w.cover_url = g["cover"]
|
||||
w.gb_id = w.gb_id or g.get("gb_id")
|
||||
|
||||
# ---- 5. plan destinations
|
||||
used: set[str] = set()
|
||||
for w in works:
|
||||
author_dir = safe_name(w.authors[0] if w.authors else "Unknown", 60)
|
||||
title_dir = safe_name(w.title, 90)
|
||||
for fr in w.files:
|
||||
base = f"{author_dir}/{title_dir}/{title_dir}"
|
||||
rel = f"{base}.{fr.fmt}"
|
||||
k = 2
|
||||
while rel in used or (library / rel).exists() and not args.reset:
|
||||
rel = f"{base} ({k}).{fr.fmt}"
|
||||
k += 1
|
||||
used.add(rel)
|
||||
fr.dest_rel = rel
|
||||
|
||||
plan = [{"title": w.title, "authors": w.authors, "isbns": w.isbns, "status": w.status, "origins": sorted(w.origins),
|
||||
"files": [{"from": f.rel_inbox, "to": f.dest_rel, "sha256": f.sha256} for f in w.files], "refs": w.refs,
|
||||
"cover": w.cover_url} for w in works]
|
||||
(root / ".cache").mkdir(exist_ok=True)
|
||||
(root / ".cache" / "ingest-plan.json").write_text(json.dumps(plan, indent=1, default=str))
|
||||
if args.dry_run:
|
||||
log("dry run: plan written to", root / ".cache" / "ingest-plan.json")
|
||||
return
|
||||
|
||||
# ---- 6. write DB
|
||||
def isbn13(w):
|
||||
return next((i for i in w.isbns if i.startswith("978") or i.startswith("979")), None)
|
||||
seen13 = set()
|
||||
from datetime import datetime, timezone
|
||||
now = datetime.now(timezone.utc)
|
||||
with conn.transaction():
|
||||
cur = conn.cursor()
|
||||
if args.reset:
|
||||
cur.execute("truncate books.notes, books.progress, books.shelf_works, books.files, books.works restart identity cascade")
|
||||
cur.execute("select slug, id from books.shelves")
|
||||
shelves = dict(cur.fetchall())
|
||||
for w in works:
|
||||
i13 = isbn13(w)
|
||||
if i13 in seen13:
|
||||
w.refs["isbn13_shared_with_other_work"] = i13
|
||||
i13 = None
|
||||
if i13:
|
||||
seen13.add(i13)
|
||||
refs = dict(w.refs)
|
||||
refs["origins"] = sorted(w.origins)
|
||||
if len(w.isbns) > 1:
|
||||
refs["all_isbns"] = w.isbns
|
||||
cur.execute(
|
||||
"""insert into books.works (title, subtitle, authors, isbn10, isbn13, publisher, year, language, page_count,
|
||||
description, tags, cover_url, openlibrary_work_id, openlibrary_edition_id, google_books_id, annas_md5,
|
||||
source_refs, metadata_status, metadata_source, metadata_fetched_at, notes)
|
||||
values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) returning id""",
|
||||
(w.title, w.subtitle, w.authors, to10(i13) if i13 else None, i13, w.publisher,
|
||||
w.year if w.year and 1400 < w.year < 2100 else None, w.language, w.page_count, w.description, w.tags,
|
||||
w.cover_url, w.ol_work, w.ol_edition, w.gb_id, w.md5s, json.dumps(refs, default=str), w.status,
|
||||
w.source, now if w.status != "pending" else None, w.notes))
|
||||
w.db_id = cur.fetchone()[0]
|
||||
for fr in w.files:
|
||||
cur.execute("insert into books.files (work_id, path, format, size_bytes, sha256, md5, source) values (%s,%s,%s,%s,%s,%s,%s)",
|
||||
(w.db_id, fr.dest_rel, fr.fmt, fr.size, fr.sha256, fr.md5, args.source))
|
||||
for s in sorted(w.shelves):
|
||||
if s in shelves:
|
||||
cur.execute("insert into books.shelf_works (shelf_id, work_id) values (%s,%s) on conflict do nothing", (shelves[s], w.db_id))
|
||||
if "done" in w.shelves and w.refs.get("done_at"):
|
||||
cur.execute("insert into books.progress (work_id, percent, finished_at, status) values (%s, 100, %s, 'finished') on conflict do nothing", (w.db_id, w.refs["done_at"]))
|
||||
|
||||
# ---- 7. covers
|
||||
got = 0
|
||||
if not args.no_network:
|
||||
for w in works:
|
||||
if not w.cover_url:
|
||||
continue
|
||||
dest = covers / (hashlib.sha1(w.cover_url.encode()).hexdigest()[:16] + ".jpg")
|
||||
if dest.exists() or http.download(w.cover_url, dest):
|
||||
got += 1
|
||||
with conn.cursor() as cur:
|
||||
cur.execute("update books.works set cover_path=%s where id=%s", (dest.name, w.db_id))
|
||||
conn.commit()
|
||||
|
||||
# ---- 8. move files (inbox -> library); write undo manifest
|
||||
if args.no_move:
|
||||
log("--no-move: DB rows written, files left in inbox")
|
||||
return
|
||||
manifest = root / ".cache" / f"moves-{time.strftime('%Y%m%d-%H%M%S')}.tsv"
|
||||
dupdir = root / "duplicates"
|
||||
qdir = root / "quarantine"
|
||||
with open(manifest, "w") as mf:
|
||||
for w in works:
|
||||
for fr in w.files:
|
||||
dest = library / fr.dest_rel
|
||||
dest.parent.mkdir(parents=True, exist_ok=True)
|
||||
shutil.move(str(fr.path), dest)
|
||||
mf.write(f"{fr.path}\t{dest}\n")
|
||||
for d, _ in dups:
|
||||
dest = dupdir / d.rel_inbox
|
||||
dest.parent.mkdir(parents=True, exist_ok=True)
|
||||
shutil.move(str(d.path), dest)
|
||||
mf.write(f"{d.path}\t{dest}\n")
|
||||
for fr in uniq:
|
||||
if fr.broken and fr.path.exists():
|
||||
dest = qdir / fr.rel_inbox
|
||||
dest.parent.mkdir(parents=True, exist_ok=True)
|
||||
shutil.move(str(fr.path), dest)
|
||||
mf.write(f"{fr.path}\t{dest}\n")
|
||||
for d in sorted(inbox.rglob("*"), reverse=True):
|
||||
if d.is_dir() and not any(d.iterdir()):
|
||||
d.rmdir()
|
||||
|
||||
summary = {
|
||||
"lists_rows": n_lists, "obsidian_md5s": obs_total, "obsidian_resolved": obs_resolved,
|
||||
"obsidian_overlapping_lists": obs_overlap, "placeholders": n_placeholders,
|
||||
"files_total": len(files), "files_unique": len(uniq), "sha256_duplicates": len(dups),
|
||||
"placeholders_with_files": matched_existing, "works_total": len(works),
|
||||
"works_with_files": sum(1 for w in works if w.files), "metadata": stats, "covers": got,
|
||||
"papers": sum(1 for w in works if "paper" in w.tags), "http_calls": http.calls, "manifest": str(manifest),
|
||||
}
|
||||
(root / ".cache" / "ingest-summary.json").write_text(json.dumps(summary, indent=1))
|
||||
print(json.dumps(summary, indent=1))
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,45 +0,0 @@
|
||||
CREATE TABLE IF NOT EXISTS cs_market_items (
|
||||
id serial PRIMARY KEY,
|
||||
market_hash_name text NOT NULL,
|
||||
name text NOT NULL,
|
||||
icon_url text,
|
||||
item_type text,
|
||||
first_seen_at timestamptz NOT NULL DEFAULT now(),
|
||||
last_seen_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS cs_market_items_hash_idx ON cs_market_items (market_hash_name);
|
||||
CREATE INDEX IF NOT EXISTS cs_market_items_name_idx ON cs_market_items (name);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS cs_item_dimensions (
|
||||
item_id integer NOT NULL REFERENCES cs_market_items(id) ON DELETE CASCADE,
|
||||
key text NOT NULL,
|
||||
value text NOT NULL
|
||||
);
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS cs_item_dimensions_unique_idx ON cs_item_dimensions (item_id, key);
|
||||
CREATE INDEX IF NOT EXISTS cs_item_dimensions_filter_idx ON cs_item_dimensions (key, value, item_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS cs_price_streams (
|
||||
source text NOT NULL,
|
||||
currency text NOT NULL,
|
||||
kind text NOT NULL
|
||||
);
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS cs_price_streams_unique_idx
|
||||
ON cs_price_streams (source, currency, kind);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS cs_price_observations (
|
||||
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
item_id integer NOT NULL REFERENCES cs_market_items(id) ON DELETE CASCADE,
|
||||
source text NOT NULL,
|
||||
currency text NOT NULL,
|
||||
observed_at timestamptz NOT NULL,
|
||||
price_cents integer NOT NULL CHECK (price_cents >= 0),
|
||||
volume integer CHECK (volume IS NULL OR volume >= 0),
|
||||
kind text NOT NULL,
|
||||
imported_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS cs_price_observations_unique_idx
|
||||
ON cs_price_observations (item_id, source, currency, observed_at, kind);
|
||||
CREATE INDEX IF NOT EXISTS cs_price_observations_lookup_idx
|
||||
ON cs_price_observations (item_id, source, currency, observed_at);
|
||||
CREATE INDEX IF NOT EXISTS cs_price_observations_time_idx
|
||||
ON cs_price_observations (observed_at);
|
||||
19
scripts/db-meta-sync.ts
Normal file
19
scripts/db-meta-sync.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
// Refreshes meta.columns from the live catalog for every table in meta.tables
|
||||
// (meta.sync() is SECURITY DEFINER and granted to the app role). Idempotent.
|
||||
// bun scripts/db-meta-sync.ts uses DB_URL (personal_app)
|
||||
import postgres from 'postgres';
|
||||
|
||||
const url = process.env.DB_URL;
|
||||
if (!url) throw new Error('Set DB_URL (app role)');
|
||||
const client = postgres(url, { max: 1, connect_timeout: 10, onnotice: () => {} });
|
||||
try {
|
||||
const [registry] = await client`select to_regprocedure('meta.sync(integer)') is not null as ok`;
|
||||
if (!registry.ok) {
|
||||
console.log('meta.sync() not present; skipping');
|
||||
} else {
|
||||
const [row] = await client`select meta.sync() as added`;
|
||||
console.log(`meta.sync(): ${row.added} column(s) added`);
|
||||
}
|
||||
} finally {
|
||||
await client.end();
|
||||
}
|
||||
55
scripts/db-migrate.ts
Normal file
55
scripts/db-migrate.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
// Applies pending Drizzle migrations from ./drizzle. Runs as the schema owner
|
||||
// (DB_MIGRATE_URL, role `personal`); falls back to DB_URL for local dev.
|
||||
//
|
||||
// bun scripts/db-migrate.ts apply pending migrations
|
||||
// bun scripts/db-migrate.ts --init record the baseline as applied without running it
|
||||
// (once, on a database already reorganised by the
|
||||
// hand-written reorg scripts in db/reorg/)
|
||||
import { drizzle } from 'drizzle-orm/postgres-js';
|
||||
import { migrate } from 'drizzle-orm/postgres-js/migrator';
|
||||
import postgres from 'postgres';
|
||||
|
||||
const url = process.env.DB_MIGRATE_URL || process.env.DB_URL;
|
||||
if (!url) throw new Error('Set DB_MIGRATE_URL (schema owner) or DB_URL');
|
||||
const init = process.argv.includes('--init');
|
||||
|
||||
const client = postgres(url, { max: 1, connect_timeout: 10, onnotice: () => {} });
|
||||
try {
|
||||
const db = drizzle({ client });
|
||||
// Safety: never run the baseline on a database that already has data. Existing
|
||||
// databases are brought to the baseline by the reviewed reorg scripts and then
|
||||
// recorded with --init; only a truly empty database gets the baseline executed.
|
||||
const [state] = await client`
|
||||
select
|
||||
to_regclass('drizzle.__drizzle_migrations') is not null as tracked,
|
||||
(select count(*)::int from pg_class c join pg_namespace n on n.oid = c.relnamespace
|
||||
where c.relkind = 'r' and n.nspname not in ('pg_catalog', 'information_schema', 'drizzle')
|
||||
and n.nspname not like 'pg_toast%') as tables`;
|
||||
const recorded = state.tracked
|
||||
? (await client`select count(*)::int as n from drizzle.__drizzle_migrations`)[0].n
|
||||
: 0;
|
||||
if (!init && recorded === 0 && state.tables > 0)
|
||||
throw new Error(
|
||||
`Refusing to run the Drizzle baseline on a non-empty database (${state.tables} tables). ` +
|
||||
'Apply the reorg scripts (db/reorg/) and record the baseline with --init first.'
|
||||
);
|
||||
if (init && state.tables === 0)
|
||||
throw new Error('--init on an empty database: run without --init to create the schema');
|
||||
const result = await migrate(db, {
|
||||
migrationsFolder: new URL('../drizzle', import.meta.url).pathname,
|
||||
migrationsSchema: 'drizzle',
|
||||
migrationsTable: '__drizzle_migrations',
|
||||
...(init ? { init: true } : {})
|
||||
});
|
||||
if (result && 'exitCode' in result) {
|
||||
throw new Error(
|
||||
result.exitCode === 'databaseMigrations'
|
||||
? '--init refused: migrations are already recorded in this database'
|
||||
: '--init refused: more than one local migration exists'
|
||||
);
|
||||
}
|
||||
const [{ n }] = await client`select count(*)::int as n from drizzle.__drizzle_migrations`;
|
||||
console.log(`${init ? 'Baseline recorded' : 'Migrations up to date'}; ${n} recorded`);
|
||||
} finally {
|
||||
await client.end();
|
||||
}
|
||||
112
scripts/db-refresh-dev.sh
Executable file
112
scripts/db-refresh-dev.sh
Executable file
@@ -0,0 +1,112 @@
|
||||
#!/usr/bin/env bash
|
||||
# Refresh the development database (personal_dev) from a fresh production dump.
|
||||
#
|
||||
# Prod is the source of truth for data; dev is a disposable copy of it.
|
||||
#
|
||||
# Required env (put them in .env.dev-refresh or export them; never commit real values):
|
||||
# PROD_DB_URL read access to prod, e.g. postgresql://personal:...@127.0.0.1:5440/personal
|
||||
# DEV_ADMIN_URL superuser on the dev cluster, maintenance DB, e.g. postgresql://postgres:...@127.0.0.1:5440/postgres
|
||||
# (not needed with PG_CONTAINER, which uses the container's local superuser)
|
||||
# Optional:
|
||||
# DEV_DB_NAME target database (default personal_dev)
|
||||
# DEV_OWNER role that owns every object in the dev DB (default personal_dev). It is the
|
||||
# role the dev site logs in as, so it can also run migrations there. It must not
|
||||
# be a member of any prod role; prod's CONNECT is not granted to it.
|
||||
# PG_DOCKER=1 use the postgres:17-alpine image instead of local pg_dump/pg_restore
|
||||
# PG_CONTAINER name of the Postgres container (e.g. postgres-db-1): run every tool inside it
|
||||
# via `docker exec` as the local superuser. URLs are then as seen from inside
|
||||
# the container, e.g. PROD_DB_URL=postgresql://personal:...@127.0.0.1:5432/personal
|
||||
# DOCKER docker command (default "docker"; e.g. "sudo -n docker")
|
||||
#
|
||||
# What it does: dump prod (custom format) -> drop + recreate DEV_DB_NAME owned by DEV_OWNER ->
|
||||
# restore without prod ownership/grants, every object owned by DEV_OWNER -> clear sessions and
|
||||
# verification tokens so prod logins can't be reused in dev. It never writes to prod.
|
||||
set -euo pipefail
|
||||
|
||||
cd "$(dirname "$0")/.."
|
||||
if [[ -f .env.dev-refresh ]]; then set -a; . ./.env.dev-refresh; set +a; fi
|
||||
|
||||
: "${PROD_DB_URL:?set PROD_DB_URL (source, read-only use)}"
|
||||
DEV_DB_NAME="${DEV_DB_NAME:-personal_dev}"
|
||||
DEV_OWNER="${DEV_OWNER:-personal_dev}"
|
||||
DOCKER="${DOCKER:-docker}"
|
||||
if [[ -n "${PG_CONTAINER:-}" ]]; then
|
||||
DEV_ADMIN_URL="${DEV_ADMIN_URL:-postgresql://postgres@localhost/postgres}"
|
||||
fi
|
||||
: "${DEV_ADMIN_URL:?set DEV_ADMIN_URL (superuser URL to the dev cluster maintenance DB)}"
|
||||
|
||||
for ident in "$DEV_DB_NAME" "$DEV_OWNER"; do
|
||||
if [[ ! "$ident" =~ ^[a-z_][a-z0-9_]*$ ]]; then
|
||||
echo "DEV_DB_NAME / DEV_OWNER must be plain lowercase identifiers" >&2
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
case "$DEV_DB_NAME" in
|
||||
personal | personal_prod | postgres | template0 | template1)
|
||||
echo "Refusing to overwrite '$DEV_DB_NAME'" >&2
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
case "$DEV_OWNER" in
|
||||
personal | personal_app | personal_ddl | personal_ro | postgres)
|
||||
echo "Refusing: DEV_OWNER '$DEV_OWNER' is a prod/superuser role" >&2
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
# host:port/db of a URL (no credentials), for the "same database" guard.
|
||||
endpoint() { bun -e 'const u=new URL(process.argv[1]); console.log(`${u.hostname}:${u.port||5432}${u.pathname}`)' "$1"; }
|
||||
with_db() { bun -e 'const u=new URL(process.argv[1]); u.pathname="/"+process.argv[2]; console.log(u.toString())' "$1" "$2"; }
|
||||
|
||||
prod_endpoint="$(endpoint "$PROD_DB_URL")"
|
||||
dev_url="$(with_db "$DEV_ADMIN_URL" "$DEV_DB_NAME")"
|
||||
if [[ "$(endpoint "$dev_url")" == "$prod_endpoint" ]]; then
|
||||
echo "Refusing: target $DEV_DB_NAME is the prod source database ($prod_endpoint)" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Dump goes to stdout and restore reads stdin, so the same commands work locally,
|
||||
# in a throwaway postgres image, or inside the Postgres container.
|
||||
if [[ -n "${PG_CONTAINER:-}" ]]; then
|
||||
pg() { $DOCKER exec -i -u postgres "$PG_CONTAINER" "$@"; }
|
||||
elif [[ "${PG_DOCKER:-0}" == 1 ]]; then
|
||||
pg() { $DOCKER run --rm -i --network host postgres:17-alpine "$@"; }
|
||||
else
|
||||
pg() { "$@"; }
|
||||
fi
|
||||
|
||||
work="$(mktemp -d)"
|
||||
trap 'rm -rf "$work"' EXIT
|
||||
dump="$work/prod.dump"
|
||||
|
||||
echo "Dumping prod ($prod_endpoint)..."
|
||||
pg pg_dump --format=custom --no-password "$PROD_DB_URL" >"$dump"
|
||||
[[ -s "$dump" ]] || { echo "Empty dump; aborting before touching $DEV_DB_NAME" >&2; exit 1; }
|
||||
pg pg_restore --list <"$dump" >/dev/null
|
||||
|
||||
echo "Recreating $DEV_DB_NAME (owner $DEV_OWNER)..."
|
||||
pg psql -v ON_ERROR_STOP=1 -q "$DEV_ADMIN_URL" <<SQL
|
||||
SELECT count(pg_terminate_backend(pid)) AS terminated_sessions FROM pg_stat_activity WHERE datname = '$DEV_DB_NAME' AND pid <> pg_backend_pid();
|
||||
DROP DATABASE IF EXISTS "$DEV_DB_NAME";
|
||||
DO \$\$
|
||||
BEGIN
|
||||
IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = '$DEV_OWNER') THEN
|
||||
EXECUTE format('CREATE ROLE %I NOLOGIN', '$DEV_OWNER');
|
||||
RAISE NOTICE 'created role $DEV_OWNER (NOLOGIN; give it a password + LOGIN for dev use)';
|
||||
END IF;
|
||||
END \$\$;
|
||||
CREATE DATABASE "$DEV_DB_NAME" OWNER "$DEV_OWNER";
|
||||
REVOKE CONNECT, TEMPORARY ON DATABASE "$DEV_DB_NAME" FROM PUBLIC;
|
||||
SQL
|
||||
|
||||
echo "Restoring into $DEV_DB_NAME..."
|
||||
# --no-owner/--no-privileges + --role: everything ends up owned by DEV_OWNER and none of
|
||||
# prod's grants (personal_app, personal_ro, ...) are copied into dev.
|
||||
pg pg_restore --no-password --exit-on-error --no-owner --no-privileges --role="$DEV_OWNER" \
|
||||
--dbname="$dev_url" <"$dump"
|
||||
|
||||
echo "Clearing sessions and verification tokens..."
|
||||
pg psql -v ON_ERROR_STOP=1 -q "$dev_url" -c 'TRUNCATE auth.session, auth.verification'
|
||||
|
||||
pg psql -qAt "$dev_url" -c "SELECT 'drizzle migrations recorded: ' || count(*) FROM drizzle.__drizzle_migrations"
|
||||
echo "Done. Apply pending migrations from your branch with: bun run db:migrate"
|
||||
@@ -17,81 +17,163 @@ if (process.env.ORIGIN !== expectedOrigin || process.env.BETTER_AUTH_URL !== exp
|
||||
const client = postgres(process.env.DB_URL, { max: 1, connect_timeout: 10 });
|
||||
try {
|
||||
await client`select 1`;
|
||||
// Schema layout after the reorg (auth/money/house/ref/games/cs/animaldex/site/meta/lists).
|
||||
const required = [
|
||||
'auth.user',
|
||||
'auth.session',
|
||||
'auth.account',
|
||||
'auth.verification',
|
||||
'money.accounts',
|
||||
'money.transactions',
|
||||
'money.recurring',
|
||||
'money.forecasts',
|
||||
'money.utility_settings',
|
||||
'money.utility_readings',
|
||||
'money.wishlist',
|
||||
'money.insurances',
|
||||
'money.projection_settings',
|
||||
'house.listings',
|
||||
'house.price_history',
|
||||
'house.alert_rules',
|
||||
'house.worker_runs',
|
||||
'ref.pokemon',
|
||||
'ref.pokemon_games',
|
||||
'ref.pokemon_game_entries',
|
||||
'ref.pokemon_locations',
|
||||
'ref.pokemon_encounters',
|
||||
'ref.pokemon_evolutions',
|
||||
'ref.pokemon_research',
|
||||
'ref.pokemon_dex_numbers',
|
||||
'ref.pokemon_moves',
|
||||
'ref.pokemon_learnsets',
|
||||
'ref.pokemon_game_research',
|
||||
'ref.tracker_games',
|
||||
'ref.tracker_items',
|
||||
'ref.tracker_challenges',
|
||||
'games.catch_em_all_entries',
|
||||
'games.catch_em_all_games',
|
||||
'games.catch_em_all_settings',
|
||||
'games.pokemon_journeys',
|
||||
'games.pokemon_catches',
|
||||
'games.pokemon_milestones',
|
||||
'games.pokemon_seen',
|
||||
'games.tracker_progress',
|
||||
'games.tracker_ranks',
|
||||
'games.tracker_weapon_ranks',
|
||||
'games.tracker_challenge_progress',
|
||||
'games.osu_entries',
|
||||
'cs.items',
|
||||
'cs.price_observations',
|
||||
'cs.item_dimensions',
|
||||
'cs.price_streams',
|
||||
'animaldex.animals',
|
||||
'animaldex.sightings',
|
||||
'animaldex.sighting_photos',
|
||||
'site.blog_articles',
|
||||
'site.tasks',
|
||||
'site.topics',
|
||||
'site.task_topics',
|
||||
'meta.tables',
|
||||
'meta.columns'
|
||||
];
|
||||
const missing = await client<{ name: string }[]>`
|
||||
select name from unnest(${required}::text[]) as name where to_regclass(name) is null`;
|
||||
if (missing.length)
|
||||
throw new Error(`Schema reorg/migrations missing: ${missing.map((m) => m.name).join(', ')}`);
|
||||
const [schema] = await client`
|
||||
select
|
||||
(select count(*) from information_schema.columns
|
||||
where table_name = 'user' and column_name = 'role') as user_role,
|
||||
where table_schema = 'auth' and table_name = 'user' and column_name = 'role') as user_role,
|
||||
(select count(*) from information_schema.columns
|
||||
where table_name = 'account' and column_name = 'issuer') as account_issuer,
|
||||
where table_schema = 'money' and table_name = 'projection_settings'
|
||||
and column_name = 'include_recurring' and data_type = 'boolean') as include_recurring_bool,
|
||||
(select count(*) from information_schema.columns
|
||||
where table_name = 'blog_articles' and column_name = 'tags') as blog_tags,
|
||||
to_regclass('public.catch_em_all_settings') is not null as settings_ready,
|
||||
(select count(*) from information_schema.columns where table_name = 'lists' and column_name = 'slug') as list_slug,
|
||||
to_regclass('public.list_item_completions') is not null as list_completions,
|
||||
(select count(*) from information_schema.columns where table_name = 'tasks_to_topics' and column_name = 'id') as topic_link_id,
|
||||
(select count(*) from information_schema.columns
|
||||
where table_name in ('animals', 'sightings', 'photos')
|
||||
and column_name = 'id' and data_type = 'integer') as animal_serial_ids,
|
||||
(select count(*) from information_schema.tables
|
||||
where table_schema = 'public' and table_name in (
|
||||
'money_accounts', 'money_transactions', 'money_recurring', 'money_forecasts',
|
||||
'money_utility_settings', 'money_utility_readings', 'money_wishlist', 'money_insurances',
|
||||
'money_projection_settings'
|
||||
)) as money_tables,
|
||||
(select count(*) from information_schema.columns
|
||||
where table_name = 'money_utility_settings' and column_name = 'billing_start') as money_billing_start,
|
||||
(select count(*) from information_schema.tables
|
||||
where table_schema = 'public' and table_name in (
|
||||
'pokemon_locations', 'pokemon_encounters', 'pokemon_evolutions',
|
||||
'tracker_games', 'tracker_items', 'tracker_progress'
|
||||
)) as game_tracker_tables,
|
||||
(select count(*) from information_schema.tables where table_schema='public' and table_name in (
|
||||
'pokemon_research','pokemon_dex_numbers','pokemon_moves','pokemon_learnsets','pokemon_game_research',
|
||||
'pokemon_journeys','pokemon_catches','pokemon_milestones','tracker_ranks','tracker_weapon_ranks',
|
||||
'tracker_challenges','tracker_challenge_progress','osu_entries','pokemon_seen'
|
||||
)) as tracker_journal_tables,
|
||||
(select count(*) from information_schema.columns where table_name='pokemon_journeys' and column_name='completed') as tracker_completion_column,
|
||||
(select count(*) from information_schema.columns
|
||||
where table_name = 'pokemon_game_entries' and column_name = 'source') as entry_source,
|
||||
(select count(*) from information_schema.columns
|
||||
where (table_name = 'money_accounts' and column_name = 'kind')
|
||||
or (table_name = 'money_recurring' and column_name in ('starts_on', 'ends_on'))
|
||||
or (table_name = 'money_wishlist' and column_name = 'priority')
|
||||
or (table_name = 'money_projection_settings' and column_name = 'include_recurring')) as money_projection_columns
|
||||
where table_schema = 'games' and table_name = 'osu_entries'
|
||||
and column_name in ('accuracy', 'pp', 'rate') and data_type = 'numeric') as osu_numeric,
|
||||
(select count(*) from meta.tables where kind = 'list') as lists,
|
||||
has_function_privilege('meta.create_list(text, text, text, boolean, jsonb, text, text, text, integer)', 'execute') as can_create_list,
|
||||
has_schema_privilege('money', 'create') as app_has_ddl
|
||||
`;
|
||||
if (
|
||||
!Number(schema.user_role) ||
|
||||
!Number(schema.account_issuer) ||
|
||||
!Number(schema.blog_tags) ||
|
||||
!schema.settings_ready ||
|
||||
!Number(schema.list_slug) ||
|
||||
!schema.list_completions ||
|
||||
!Number(schema.topic_link_id) ||
|
||||
Number(schema.animal_serial_ids) !== 3 ||
|
||||
Number(schema.money_tables) !== 9 ||
|
||||
!Number(schema.money_billing_start) ||
|
||||
Number(schema.money_projection_columns) !== 5 ||
|
||||
Number(schema.game_tracker_tables) !== 6 ||
|
||||
Number(schema.tracker_journal_tables) !== 14 ||
|
||||
!Number(schema.tracker_completion_column) ||
|
||||
!Number(schema.entry_source)
|
||||
!Number(schema.include_recurring_bool) ||
|
||||
Number(schema.osu_numeric) !== 3 ||
|
||||
!Number(schema.lists) ||
|
||||
!schema.can_create_list
|
||||
) {
|
||||
throw new Error(
|
||||
'Required migrations or Animaldex ID conversion have not been applied to this database'
|
||||
'The schema reorg has not been applied to this database (or grants are missing)'
|
||||
);
|
||||
}
|
||||
if (schema.app_has_ddl)
|
||||
throw new Error('DB_URL must use the app role (personal_app), not the schema owner');
|
||||
// Private health schema: the app role reads and writes it, nobody else (not even personal_ro).
|
||||
const [health] = await client`
|
||||
select
|
||||
count(*) filter (where has_table_privilege(c.oid, 'select, insert, update, delete')) as app_rw,
|
||||
count(*) filter (
|
||||
where exists (
|
||||
select 1 from aclexplode(c.relacl) a join pg_roles r on r.oid = a.grantee
|
||||
where r.rolname = 'personal_ro'
|
||||
)
|
||||
) as ro_read,
|
||||
count(*) as tables,
|
||||
has_schema_privilege('public', 'health', 'usage') as public_usage
|
||||
from pg_class c join pg_namespace n on n.oid = c.relnamespace
|
||||
where n.nspname = 'health' and c.relkind = 'r'
|
||||
`;
|
||||
if (
|
||||
Number(health.tables) !== 7 ||
|
||||
Number(health.app_rw) !== 7 ||
|
||||
Number(health.ro_read) !== 0 ||
|
||||
health.public_usage
|
||||
)
|
||||
throw new Error(`health schema grants are wrong: ${JSON.stringify(health)}`);
|
||||
// Private task tracker: same rules as health (app role read/write, personal_ro none).
|
||||
const [tasks] = await client`
|
||||
select
|
||||
count(*) filter (where has_table_privilege(c.oid, 'select, insert, update, delete')) as app_rw,
|
||||
count(*) filter (
|
||||
where exists (
|
||||
select 1 from aclexplode(c.relacl) a join pg_roles r on r.oid = a.grantee
|
||||
where r.rolname = 'personal_ro'
|
||||
)
|
||||
) as ro_read,
|
||||
count(*) as tables,
|
||||
has_schema_privilege('public', 'tasks', 'usage') as public_usage
|
||||
from pg_class c join pg_namespace n on n.oid = c.relnamespace
|
||||
where n.nspname = 'tasks' and c.relkind = 'r'
|
||||
`;
|
||||
if (
|
||||
Number(tasks.tables) !== 4 ||
|
||||
Number(tasks.app_rw) !== 4 ||
|
||||
Number(tasks.ro_read) !== 0 ||
|
||||
tasks.public_usage
|
||||
)
|
||||
throw new Error(`tasks schema grants are wrong: ${JSON.stringify(tasks)}`);
|
||||
// Energy tracker reads the ned-collector tables (SELECT only).
|
||||
const [energy] = await client`
|
||||
select count(*) as hours, max(valid_from) as latest from energy.ned_utilization
|
||||
where granularity = 'Hour' and classification = 'Current'
|
||||
and valid_from > now() - interval '7 days'
|
||||
`;
|
||||
if (!Number(energy.hours))
|
||||
console.warn('Warning: energy.ned_utilization has no hourly data from the last 7 days');
|
||||
console.log(
|
||||
`health: ${health.tables} tables, tasks: ${tasks.tables} tables, app read/write, personal_ro none; energy: ${energy.hours} hourly rows, latest ${energy.latest ? new Date(energy.latest).toISOString() : 'none'}`
|
||||
);
|
||||
const [catalog] = await client`
|
||||
select
|
||||
(select count(*) from pokemon) as pokemon,
|
||||
(select count(*) from games where source_id is not null) as games,
|
||||
(select count(*) from pokemon_game_entries) as entries,
|
||||
(select count(*) from pokemon_encounters) as encounters,
|
||||
(select count(*) from tracker_games where franchise = 'call-of-duty') as cod_games,
|
||||
(select count(*) from pokemon_research) as pokemon_research,
|
||||
(select count(*) from pokemon_moves) as pokemon_moves,
|
||||
(select count(*) from pokemon_learnsets) as pokemon_learnsets,
|
||||
(select count(*) from pokemon_dex_numbers) as pokemon_dex_numbers,
|
||||
(select count(*) from tracker_challenges) as tracker_challenges
|
||||
(select count(*) from ref.pokemon) as pokemon,
|
||||
(select count(*) from ref.pokemon_games where source_id is not null) as games,
|
||||
(select count(*) from ref.pokemon_game_entries) as entries,
|
||||
(select count(*) from ref.pokemon_encounters) as encounters,
|
||||
(select count(*) from ref.tracker_games where franchise = 'call-of-duty') as cod_games,
|
||||
(select count(*) from ref.pokemon_research) as pokemon_research,
|
||||
(select count(*) from ref.pokemon_moves) as pokemon_moves,
|
||||
(select count(*) from ref.pokemon_learnsets) as pokemon_learnsets,
|
||||
(select count(*) from ref.pokemon_dex_numbers) as pokemon_dex_numbers,
|
||||
(select count(*) from ref.tracker_challenges) as tracker_challenges
|
||||
`;
|
||||
if (
|
||||
Number(catalog.pokemon) < 1025 ||
|
||||
@@ -109,15 +191,15 @@ try {
|
||||
Number(catalog.tracker_challenges) < 3600
|
||||
)
|
||||
throw new Error(
|
||||
'Run db:migrate:tracker-journal, db:seed:research and db:seed:challenges before deploying the tracker expansion'
|
||||
'Run db:seed:research and db:seed:challenges before deploying the tracker expansion'
|
||||
);
|
||||
if (Number(catalog.cod_games) < 7) {
|
||||
throw new Error('Call of Duty catalog has not been seeded in this database');
|
||||
}
|
||||
const [bo2] = await client`select rules,
|
||||
(select count(*)::int from tracker_items i where i.game_id=g.id and i.kind='weapon') as weapons,
|
||||
(select count(*)::int from tracker_items i where i.game_id=g.id and i.kind='weapon' and i.icon is not null) as pictures
|
||||
from tracker_games g where slug='black-ops-2'`;
|
||||
(select count(*)::int from ref.tracker_items i where i.game_id=g.id and i.kind='weapon') as weapons,
|
||||
(select count(*)::int from ref.tracker_items i where i.game_id=g.id and i.kind='weapon' and i.icon is not null) as pictures
|
||||
from ref.tracker_games g where slug='black-ops-2'`;
|
||||
if (
|
||||
!bo2 ||
|
||||
bo2.rules.rankCaps?.weaponPrestige !== 2 ||
|
||||
|
||||
@@ -1,26 +1,62 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
# Production is the only deployed environment (local dev uses scripts/db-refresh-dev.sh).
|
||||
# deploy-remote.sh <sha> deploy back up, migrate, seed, preflight, switch, health-check
|
||||
# deploy-remote.sh <sha> rollback back up, preflight, switch to an earlier image (no migrations;
|
||||
# the database is not rolled back, restore a backup for that)
|
||||
sha=${1:?commit SHA required}
|
||||
directory=${2:?deployment directory required}
|
||||
origin=${3:?public origin required}
|
||||
mode=${2:-deploy}
|
||||
case "$sha" in
|
||||
*[!0-9a-f]*|'') echo 'Invalid image tag' >&2; exit 2 ;;
|
||||
esac
|
||||
case "$directory:$origin" in
|
||||
/opt/personal-website:https://stanrunge.dev)
|
||||
environment=/etc/personal-website/prod.env ;;
|
||||
/opt/personal-website-staging:https://staging.stanrunge.dev)
|
||||
environment=/etc/personal-website-staging/staging.env ;;
|
||||
*) echo 'Unrecognized deployment target' >&2; exit 2 ;;
|
||||
esac
|
||||
|
||||
image="git.stanrunge.dev/stan/personal-website:$sha"
|
||||
compose="$directory/compose.prod.yml"
|
||||
if [ "$directory" = /opt/personal-website-staging ]; then
|
||||
environment_name=staging
|
||||
else
|
||||
case "$mode" in deploy|rollback) ;; *) echo 'Mode must be deploy or rollback' >&2; exit 2 ;; esac
|
||||
directory=/opt/personal-website
|
||||
origin=https://stanrunge.dev
|
||||
environment=/etc/personal-website/prod.env
|
||||
environment_name=production
|
||||
|
||||
image="git.stanrunge.dev/stan/website:$sha"
|
||||
compose="$directory/compose.prod.yml"
|
||||
compose_files=(-f "$compose")
|
||||
# Images built before the repo rename (stan/personal-website -> stan/website) keep
|
||||
# their old local name; retag so rollbacks to those SHAs still work.
|
||||
legacy_image="git.stanrunge.dev/stan/personal-website:$sha"
|
||||
if ! sudo docker image inspect "$image" >/dev/null 2>&1 &&
|
||||
sudo docker image inspect "$legacy_image" >/dev/null 2>&1; then
|
||||
sudo docker tag "$legacy_image" "$image"
|
||||
fi
|
||||
if ! sudo docker image inspect "$image" >/dev/null 2>&1; then
|
||||
echo "Image $image is not on this host" >&2; exit 1
|
||||
fi
|
||||
# Book files (ebooks + covers) live on the host and are bind-mounted read-only at /books;
|
||||
# the app streams them only to the owner via /trackers/books/files/<id> (no static path).
|
||||
# Layout: $books_host_dir/library/<files.path> and $books_host_dir/covers/<works.cover_path>.
|
||||
# compose.prod.yml is host-managed, so the mount goes in a generated override file (~deploy).
|
||||
books_host_dir=${BOOKS_HOST_DIR:-/home/stan/books}
|
||||
case "$books_host_dir" in /*) ;; *) echo 'BOOKS_HOST_DIR must be absolute' >&2; exit 2 ;; esac
|
||||
# The deploy account can't traverse /home/stan (0750); check from a root container instead.
|
||||
if sudo docker run --rm --entrypoint sh \
|
||||
--mount "type=bind,source=$books_host_dir,target=/books,readonly" "$image" \
|
||||
-c 'test -d /books/library' 2>/dev/null; then
|
||||
# The deploy account may only sudo-tee $directory/.env, so the override lives in its home;
|
||||
# the compose CLI runs as root and can read it from there.
|
||||
books_override="$HOME/compose.books.yml"
|
||||
rm -f "$books_override"
|
||||
cat >"$books_override" <<YAML
|
||||
services:
|
||||
app:
|
||||
environment:
|
||||
BOOKS_ROOT: /books
|
||||
volumes:
|
||||
- type: bind
|
||||
source: $books_host_dir
|
||||
target: /books
|
||||
read_only: true
|
||||
YAML
|
||||
compose_files+=(-f "$books_override")
|
||||
else
|
||||
echo "No $books_host_dir/library on this host; book files will not be readable" >&2
|
||||
fi
|
||||
previous_tag=$(sed -n 's/^IMAGE_TAG=//p' "$directory/.env" | head -n 1)
|
||||
switched=0
|
||||
@@ -28,22 +64,26 @@ rollback() {
|
||||
if [ "$switched" -eq 1 ] && [ -n "$previous_tag" ]; then
|
||||
echo "Health check failed; restoring prior image tag $previous_tag" >&2
|
||||
printf 'IMAGE_TAG=%s\n' "$previous_tag" | sudo tee "$directory/.env" >/dev/null
|
||||
sudo docker compose --env-file "$directory/.env" -f "$compose" up -d
|
||||
sudo docker compose --env-file "$directory/.env" "${compose_files[@]}" up -d
|
||||
fi
|
||||
}
|
||||
trap rollback ERR
|
||||
|
||||
# The deploy account cannot read the env file directly; check inside a container.
|
||||
if ! sudo docker run --rm --env-file "$environment" --entrypoint sh "$image" \
|
||||
-c 'test -n "${DB_MIGRATE_URL:-}"'; then
|
||||
echo "DB_MIGRATE_URL (schema owner) is missing from $environment" >&2; exit 1
|
||||
fi
|
||||
db_version=$(sudo docker run --rm --network backend --env-file "$environment" "$image" \
|
||||
bun scripts/print-db-server-version.ts)
|
||||
case "$db_version" in
|
||||
*[!0-9]*|'') echo 'Could not determine the PostgreSQL server version' >&2; exit 1 ;;
|
||||
esac
|
||||
if [ "$db_version" -ge 100000 ]; then
|
||||
db_major=$((db_version / 10000))
|
||||
else
|
||||
db_major="$((db_version / 10000)).$(((db_version / 100) % 100))"
|
||||
if [ "$db_major" -gt 17 ]; then
|
||||
echo "Server is PostgreSQL $db_major; update the pinned postgres:17-alpine backup image" >&2; exit 1
|
||||
fi
|
||||
backup_image="postgres:${db_major}-alpine"
|
||||
backup_image=postgres:17-alpine
|
||||
backup_dir=/var/backups/personal-website
|
||||
backup_stamp=$(date -u +%Y%m%dT%H%M%SZ)
|
||||
backup_name="${environment_name}-${backup_stamp}-${sha}.dump"
|
||||
@@ -53,36 +93,44 @@ sudo docker pull "$backup_image"
|
||||
sudo docker run --rm --network backend --env-file "$environment" \
|
||||
--volume "$backup_dir:/backups" \
|
||||
--env "BACKUP_FILE=$backup_name" "$backup_image" \
|
||||
sh -eu -c 'chmod 0700 /backups; umask 077; pg_dump --format=custom --no-owner --no-acl --file="/backups/$BACKUP_FILE" "$DB_URL"'
|
||||
sh -eu -c 'chmod 0700 /backups; umask 077; pg_dump --format=custom --no-owner --no-acl --file="/backups/$BACKUP_FILE" "$DB_MIGRATE_URL"'
|
||||
sudo docker run --rm --mount "type=bind,source=$backup_dir,target=/backups" \
|
||||
"$backup_image" pg_restore --list "/backups/$backup_name" >/dev/null
|
||||
echo "Verified $environment_name database backup at $backup_dir/$backup_name"
|
||||
|
||||
# Migrations and seeds (ref.* is read-only for the app) need the owner role.
|
||||
# DB_MIGRATE_URL (role personal) must be set in the env file; DB_URL is the app
|
||||
# role (personal_app), which has no DDL rights.
|
||||
run_database_script() {
|
||||
echo "Running $1"
|
||||
sudo docker run --rm --network backend --env-file "$environment" "$image" bun "$1"
|
||||
sudo docker run --rm --network backend --env-file "$environment" \
|
||||
--entrypoint sh "$image" -eu -c 'DB_URL="$DB_MIGRATE_URL" exec bun "$0"' "$1"
|
||||
}
|
||||
|
||||
run_database_script scripts/migrate-auth-catalog.ts
|
||||
run_database_script scripts/migrate-foundation.ts
|
||||
run_database_script scripts/migrate-money-tracker.ts
|
||||
run_database_script scripts/migrate-game-tracker.ts
|
||||
run_database_script scripts/migrate-tracker-journal.ts
|
||||
run_database_script scripts/migrate-house-tracker.ts
|
||||
if [ "$mode" = deploy ]; then
|
||||
# Schema changes: Drizzle migrations from ./drizzle, as the schema owner.
|
||||
run_database_script scripts/db-migrate.ts
|
||||
# Refresh the generic table GUI's column registry (meta.columns) for tables that migrations
|
||||
# registered in meta.tables. meta.sync() is SECURITY DEFINER and granted to the app role, not
|
||||
# to the schema owner, so run it as DB_URL (personal_app).
|
||||
echo "Running meta.sync()"
|
||||
sudo docker run --rm --network backend --env-file "$environment" "$image" \
|
||||
bun scripts/db-meta-sync.ts
|
||||
run_database_script scripts/seed-catalog.ts
|
||||
run_database_script scripts/seed-cod-catalog.ts
|
||||
run_database_script scripts/seed-pokemon-research.ts
|
||||
run_database_script scripts/seed-cod-challenges.ts
|
||||
run_database_script scripts/seed-cod-art.ts
|
||||
fi
|
||||
|
||||
sudo docker run --rm --network backend --env-file "$environment" "$image" \
|
||||
bun scripts/deploy-preflight.ts "$origin"
|
||||
|
||||
printf 'IMAGE_TAG=%s\n' "$sha" | sudo tee "$directory/.env" >/dev/null
|
||||
switched=1
|
||||
sudo docker compose --env-file "$directory/.env" -f "$compose" up -d
|
||||
sudo docker compose --env-file "$directory/.env" "${compose_files[@]}" up -d
|
||||
|
||||
container=$(sudo docker compose --env-file "$directory/.env" -f "$compose" ps -q app)
|
||||
container=$(sudo docker compose --env-file "$directory/.env" "${compose_files[@]}" ps -q app)
|
||||
expected_image=$(sudo docker image inspect -f '{{.Id}}' "$image")
|
||||
actual_image=$(sudo docker inspect -f '{{.Image}}' "$container")
|
||||
if [ "$actual_image" != "$expected_image" ]; then
|
||||
@@ -96,4 +144,4 @@ curl --retry 6 --retry-delay 2 --retry-all-errors --fail --silent --show-error \
|
||||
curl --retry 6 --retry-delay 2 --retry-all-errors --fail --silent --show-error \
|
||||
"$origin/projects/game-tracker/call-of-duty/black-ops-2" >/dev/null
|
||||
trap - ERR
|
||||
echo "Deployed $sha to $origin"
|
||||
echo "Deployed $sha to $origin ($mode)"
|
||||
|
||||
@@ -1,94 +0,0 @@
|
||||
-- Apply after auth-catalog-migration.sql and before deploying the foundation release.
|
||||
-- Additive domain-schema changes; existing records and numeric IDs are retained.
|
||||
|
||||
ALTER TABLE blog_articles ADD COLUMN IF NOT EXISTS tags text[] NOT NULL DEFAULT '{}'::text[];
|
||||
|
||||
ALTER TABLE lists ADD COLUMN IF NOT EXISTS slug text;
|
||||
UPDATE lists SET slug = COALESCE(NULLIF(trim(both '-' from regexp_replace(lower(name), '[^a-z0-9]+', '-', 'g')), ''), 'list') || '-' || id
|
||||
WHERE slug IS NULL;
|
||||
ALTER TABLE lists ALTER COLUMN slug SET NOT NULL;
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS lists_slug_unique ON lists (slug);
|
||||
ALTER TABLE lists ADD COLUMN IF NOT EXISTS is_progress boolean NOT NULL DEFAULT false;
|
||||
ALTER TABLE lists ADD COLUMN IF NOT EXISTS picture_url text;
|
||||
ALTER TABLE list_items ADD COLUMN IF NOT EXISTS picture_url text;
|
||||
ALTER TABLE list_items ADD COLUMN IF NOT EXISTS description text;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS list_item_completions (
|
||||
id serial PRIMARY KEY,
|
||||
item_id integer NOT NULL REFERENCES list_items(id) ON DELETE CASCADE,
|
||||
user_id text NOT NULL REFERENCES "user"(id) ON DELETE CASCADE,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
updated_at timestamptz NOT NULL DEFAULT now(),
|
||||
CONSTRAINT list_item_completions_item_id_user_id_unique UNIQUE (item_id, user_id)
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS list_item_completions_user_id_idx ON list_item_completions(user_id);
|
||||
|
||||
-- Join/setting records gain the same numeric identity as other site-owned rows.
|
||||
ALTER TABLE tasks_to_topics ADD COLUMN IF NOT EXISTS id serial;
|
||||
DO $$ BEGIN
|
||||
IF EXISTS (SELECT 1 FROM pg_constraint WHERE conrelid = 'tasks_to_topics'::regclass AND conname = 'tasks_to_topics_pkey'
|
||||
AND pg_get_constraintdef(oid) NOT LIKE 'PRIMARY KEY (id)') THEN
|
||||
ALTER TABLE tasks_to_topics DROP CONSTRAINT tasks_to_topics_pkey;
|
||||
END IF;
|
||||
END $$;
|
||||
DO $$ BEGIN
|
||||
IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conrelid = 'tasks_to_topics'::regclass AND contype = 'p') THEN
|
||||
ALTER TABLE tasks_to_topics ADD CONSTRAINT tasks_to_topics_pkey PRIMARY KEY (id);
|
||||
END IF;
|
||||
END $$;
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS tasks_to_topics_task_id_topic_id_unique ON tasks_to_topics(task_id, topic_id);
|
||||
|
||||
ALTER TABLE catch_em_all_settings ADD COLUMN IF NOT EXISTS id serial;
|
||||
DO $$ BEGIN
|
||||
IF EXISTS (SELECT 1 FROM pg_constraint WHERE conrelid = 'catch_em_all_settings'::regclass AND conname = 'catch_em_all_settings_pkey'
|
||||
AND pg_get_constraintdef(oid) NOT LIKE 'PRIMARY KEY (id)') THEN
|
||||
ALTER TABLE catch_em_all_settings DROP CONSTRAINT catch_em_all_settings_pkey;
|
||||
END IF;
|
||||
END $$;
|
||||
DO $$ BEGIN
|
||||
IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conrelid = 'catch_em_all_settings'::regclass AND contype = 'p') THEN
|
||||
ALTER TABLE catch_em_all_settings ADD CONSTRAINT catch_em_all_settings_pkey PRIMARY KEY (id);
|
||||
END IF;
|
||||
END $$;
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS catch_em_all_settings_user_id_unique ON catch_em_all_settings(user_id);
|
||||
|
||||
-- Every site-owned table records creation and latest update. Domain-specific
|
||||
-- event times (submitted_at, seen_at, uploaded_at) remain untouched.
|
||||
DO $$
|
||||
DECLARE table_name text;
|
||||
BEGIN
|
||||
FOREACH table_name IN ARRAY ARRAY[
|
||||
'blog_articles', 'lists', 'list_items', 'list_item_completions',
|
||||
'animals', 'sightings', 'photos', 'tasks', 'topics', 'tasks_to_topics',
|
||||
'pokemon', 'games', 'pokemon_game_entries', 'user_entries', 'user_games',
|
||||
'catch_em_all_settings'
|
||||
] LOOP
|
||||
EXECUTE format('ALTER TABLE %I ADD COLUMN IF NOT EXISTS created_at timestamptz NOT NULL DEFAULT now()', table_name);
|
||||
EXECUTE format('ALTER TABLE %I ADD COLUMN IF NOT EXISTS updated_at timestamptz NOT NULL DEFAULT now()', table_name);
|
||||
END LOOP;
|
||||
END $$;
|
||||
|
||||
-- Preserve the best available creation time for older Animaldex rows.
|
||||
UPDATE animals SET created_at = submitted_at WHERE created_at > submitted_at;
|
||||
UPDATE sightings SET created_at = submitted_at WHERE created_at > submitted_at;
|
||||
UPDATE photos SET created_at = uploaded_at WHERE created_at > uploaded_at;
|
||||
|
||||
CREATE OR REPLACE FUNCTION site_touch_updated_at() RETURNS trigger LANGUAGE plpgsql AS $$
|
||||
BEGIN
|
||||
NEW.updated_at = now();
|
||||
RETURN NEW;
|
||||
END $$;
|
||||
|
||||
DO $$
|
||||
DECLARE table_name text;
|
||||
BEGIN
|
||||
FOREACH table_name IN ARRAY ARRAY[
|
||||
'blog_articles', 'lists', 'list_items', 'list_item_completions',
|
||||
'animals', 'sightings', 'photos', 'tasks', 'topics', 'tasks_to_topics',
|
||||
'pokemon', 'games', 'pokemon_game_entries', 'user_entries', 'user_games',
|
||||
'catch_em_all_settings'
|
||||
] LOOP
|
||||
EXECUTE format('DROP TRIGGER IF EXISTS site_touch_updated_at ON %I', table_name);
|
||||
EXECUTE format('CREATE TRIGGER site_touch_updated_at BEFORE UPDATE ON %I FOR EACH ROW EXECUTE FUNCTION site_touch_updated_at()', table_name);
|
||||
END LOOP;
|
||||
END $$;
|
||||
@@ -1,127 +0,0 @@
|
||||
-- Game tracker: owned Pokémon locations/encounters/evolutions and checklist games.
|
||||
-- Additive and repeatable. Apply before seeding (`npm run db:seed`, `npm run db:seed:cod`).
|
||||
|
||||
ALTER TYPE availability ADD VALUE IF NOT EXISTS 'evolve';
|
||||
ALTER TYPE availability ADD VALUE IF NOT EXISTS 'breed';
|
||||
|
||||
ALTER TABLE pokemon ADD COLUMN IF NOT EXISTS evolves_from integer;
|
||||
ALTER TABLE pokemon ADD COLUMN IF NOT EXISTS evolution_chain integer;
|
||||
ALTER TABLE pokemon ADD COLUMN IF NOT EXISTS is_baby boolean NOT NULL DEFAULT false;
|
||||
ALTER TABLE pokemon ADD COLUMN IF NOT EXISTS is_legendary boolean NOT NULL DEFAULT false;
|
||||
ALTER TABLE pokemon ADD COLUMN IF NOT EXISTS is_mythical boolean NOT NULL DEFAULT false;
|
||||
|
||||
ALTER TABLE games ADD COLUMN IF NOT EXISTS slug text;
|
||||
ALTER TABLE games ADD COLUMN IF NOT EXISTS sort_order integer NOT NULL DEFAULT 0;
|
||||
ALTER TABLE games ADD COLUMN IF NOT EXISTS breeding boolean NOT NULL DEFAULT true;
|
||||
DO $$ BEGIN
|
||||
IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'games_slug_key') THEN
|
||||
ALTER TABLE games ADD CONSTRAINT games_slug_key UNIQUE (slug);
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
-- Seeds used to mark their own rows with a "PokéAPI:" note prefix; anything else
|
||||
-- was hand-maintained and must survive reseeding.
|
||||
DO $$ BEGIN
|
||||
IF NOT EXISTS (SELECT 1 FROM information_schema.columns
|
||||
WHERE table_name = 'pokemon_game_entries' AND column_name = 'source') THEN
|
||||
ALTER TABLE pokemon_game_entries ADD COLUMN source text NOT NULL DEFAULT 'pokeapi';
|
||||
UPDATE pokemon_game_entries SET source = 'manual' WHERE note IS NULL OR note NOT LIKE 'PokéAPI:%';
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS pokemon_locations (
|
||||
id serial PRIMARY KEY,
|
||||
source_id integer UNIQUE,
|
||||
slug text NOT NULL,
|
||||
name text NOT NULL,
|
||||
region text,
|
||||
description text,
|
||||
image_key text,
|
||||
image_credit text,
|
||||
source text NOT NULL DEFAULT 'pokeapi',
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
updated_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS pokemon_encounters (
|
||||
id serial PRIMARY KEY,
|
||||
pokemon_id integer NOT NULL,
|
||||
game_id integer NOT NULL,
|
||||
location_id integer NOT NULL,
|
||||
area text,
|
||||
method text NOT NULL,
|
||||
min_level integer,
|
||||
max_level integer,
|
||||
rarity integer,
|
||||
form text,
|
||||
conditions text[] NOT NULL DEFAULT '{}'::text[],
|
||||
note text,
|
||||
source text NOT NULL DEFAULT 'pokeapi',
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
updated_at timestamptz NOT NULL DEFAULT now(),
|
||||
CONSTRAINT pokemon_encounters_pokemon_id_pokemon_id_fkey FOREIGN KEY (pokemon_id) REFERENCES pokemon(id) ON DELETE CASCADE,
|
||||
CONSTRAINT pokemon_encounters_game_id_games_id_fkey FOREIGN KEY (game_id) REFERENCES games(id) ON DELETE CASCADE,
|
||||
CONSTRAINT pokemon_encounters_location_id_pokemon_locations_id_fkey FOREIGN KEY (location_id) REFERENCES pokemon_locations(id) ON DELETE CASCADE
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS pokemon_encounters_pokemon_id_game_id_index ON pokemon_encounters (pokemon_id, game_id);
|
||||
CREATE INDEX IF NOT EXISTS pokemon_encounters_location_id_index ON pokemon_encounters (location_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS pokemon_evolutions (
|
||||
id serial PRIMARY KEY,
|
||||
from_pokemon_id integer NOT NULL,
|
||||
to_pokemon_id integer NOT NULL,
|
||||
game_id integer NOT NULL,
|
||||
method text NOT NULL,
|
||||
source text NOT NULL DEFAULT 'pokeapi',
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
updated_at timestamptz NOT NULL DEFAULT now(),
|
||||
CONSTRAINT pokemon_evolutions_from_pokemon_id_to_pokemon_id_game_id_method_unique UNIQUE (from_pokemon_id, to_pokemon_id, game_id, method),
|
||||
CONSTRAINT pokemon_evolutions_from_pokemon_id_pokemon_id_fkey FOREIGN KEY (from_pokemon_id) REFERENCES pokemon(id) ON DELETE CASCADE,
|
||||
CONSTRAINT pokemon_evolutions_to_pokemon_id_pokemon_id_fkey FOREIGN KEY (to_pokemon_id) REFERENCES pokemon(id) ON DELETE CASCADE,
|
||||
CONSTRAINT pokemon_evolutions_game_id_games_id_fkey FOREIGN KEY (game_id) REFERENCES games(id) ON DELETE CASCADE
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS pokemon_evolutions_game_id_index ON pokemon_evolutions (game_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS tracker_games (
|
||||
id serial PRIMARY KEY,
|
||||
slug text NOT NULL UNIQUE,
|
||||
franchise text NOT NULL,
|
||||
name text NOT NULL,
|
||||
short_name text NOT NULL,
|
||||
release_year integer,
|
||||
sort_order integer NOT NULL DEFAULT 0,
|
||||
rules jsonb NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
updated_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS tracker_items (
|
||||
id serial PRIMARY KEY,
|
||||
game_id integer NOT NULL,
|
||||
key text NOT NULL,
|
||||
kind text NOT NULL,
|
||||
category text NOT NULL,
|
||||
name text NOT NULL,
|
||||
description text,
|
||||
icon text,
|
||||
post_launch boolean NOT NULL DEFAULT false,
|
||||
sort_order integer NOT NULL DEFAULT 0,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
updated_at timestamptz NOT NULL DEFAULT now(),
|
||||
CONSTRAINT tracker_items_game_id_key_unique UNIQUE (game_id, key),
|
||||
CONSTRAINT tracker_items_game_id_tracker_games_id_fkey FOREIGN KEY (game_id) REFERENCES tracker_games(id) ON DELETE CASCADE
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS tracker_items_game_id_index ON tracker_items (game_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS tracker_progress (
|
||||
id serial PRIMARY KEY,
|
||||
user_id text NOT NULL,
|
||||
item_id integer NOT NULL,
|
||||
milestone text NOT NULL DEFAULT 'done',
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
updated_at timestamptz NOT NULL DEFAULT now(),
|
||||
CONSTRAINT tracker_progress_user_id_item_id_milestone_unique UNIQUE (user_id, item_id, milestone),
|
||||
CONSTRAINT tracker_progress_user_id_user_id_fkey FOREIGN KEY (user_id) REFERENCES "user"(id) ON DELETE CASCADE,
|
||||
CONSTRAINT tracker_progress_item_id_tracker_items_id_fkey FOREIGN KEY (item_id) REFERENCES tracker_items(id) ON DELETE CASCADE
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS tracker_progress_user_id_index ON tracker_progress (user_id);
|
||||
72
scripts/health-import.ts
Normal file
72
scripts/health-import.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
/**
|
||||
* Import health data (JSON or CSV) into the private `health` schema.
|
||||
*
|
||||
* bun scripts/health-import.ts data.json # {"vaccinations":[...],"sleep":[...]}
|
||||
* bun scripts/health-import.ts --table vaccinations rivm.csv # CSV (header row) or a JSON array
|
||||
* bun scripts/health-import.ts --table sleep - < sleep.csv # stdin
|
||||
* add --dry-run to validate without writing
|
||||
*
|
||||
* Column names are the snake_case DB columns (see src/lib/health/tables.ts). Rows with an
|
||||
* `id` update that row; nutrition_daily and sleep upsert on `date`; everything else is
|
||||
* inserted (importing the same file twice duplicates those rows). All-or-nothing: if any
|
||||
* row is invalid, nothing is written. Uses DB_URL (the app role on prod, personal_dev on dev).
|
||||
*/
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import { drizzle } from 'drizzle-orm/postgres-js';
|
||||
import postgres from 'postgres';
|
||||
import { parseCsv, toPayload, validatePayload, writeRows } from '../src/lib/server/health-import';
|
||||
|
||||
let dryRun = false;
|
||||
let table: string | undefined;
|
||||
const files: string[] = [];
|
||||
for (let i = 2; i < process.argv.length; i++) {
|
||||
const arg = process.argv[i];
|
||||
if (arg === '--dry-run') dryRun = true;
|
||||
else if (arg === '--table') table = process.argv[++i];
|
||||
else if (arg.startsWith('--table=')) table = arg.slice(8);
|
||||
else files.push(arg);
|
||||
}
|
||||
if (!files.length) {
|
||||
console.error(
|
||||
'usage: bun scripts/health-import.ts [--table <name>] [--dry-run] <file.json|file.csv|->...'
|
||||
);
|
||||
process.exit(2);
|
||||
}
|
||||
|
||||
const read = (file: string) => (file === '-' ? Bun.stdin.text() : readFile(file, 'utf8'));
|
||||
const merged: Record<string, unknown[]> = {};
|
||||
try {
|
||||
for (const file of files) {
|
||||
const text = await read(file);
|
||||
const csv = file.toLowerCase().endsWith('.csv') || (file === '-' && !/^\s*[[{]/.test(text));
|
||||
const payload = toPayload(csv ? parseCsv(text) : JSON.parse(text), table);
|
||||
for (const [t, rows] of Object.entries(payload)) (merged[t] ??= []).push(...(rows ?? []));
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e instanceof Error ? e.message : e);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const { clean, errors } = validatePayload(toPayload(merged));
|
||||
if (errors.length) {
|
||||
for (const e of errors.slice(0, 50)) console.error(`${e.table} row ${e.row}: ${e.error}`);
|
||||
console.error(`${errors.length} invalid row(s); nothing imported.`);
|
||||
process.exit(1);
|
||||
}
|
||||
const summary = Object.entries(clean)
|
||||
.map(([t, r]) => `${t}: ${r.length}`)
|
||||
.join(', ');
|
||||
if (dryRun) {
|
||||
console.log(`Valid (dry run, nothing written): ${summary}`);
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
if (!process.env.DB_URL) throw new Error('DB_URL is required');
|
||||
const client = postgres(process.env.DB_URL, { max: 1, onnotice: () => {} });
|
||||
try {
|
||||
const counts = await writeRows(drizzle({ client }), clean);
|
||||
for (const [t, c] of Object.entries(counts))
|
||||
console.log(`${t}: ${c.inserted} inserted, ${c.updated} updated`);
|
||||
} finally {
|
||||
await client.end();
|
||||
}
|
||||
@@ -1,111 +0,0 @@
|
||||
-- House tracker: listings from Dutch housing sites, collected by a home worker.
|
||||
-- Idempotent; safe to run on every deploy.
|
||||
|
||||
CREATE TABLE IF NOT EXISTS house_listings (
|
||||
id serial PRIMARY KEY,
|
||||
source text NOT NULL,
|
||||
source_id text NOT NULL,
|
||||
external_id text,
|
||||
url text NOT NULL,
|
||||
offer text NOT NULL CHECK (offer IN ('rent', 'buy')),
|
||||
property_type text NOT NULL,
|
||||
title text,
|
||||
street text,
|
||||
postal_code text,
|
||||
city text,
|
||||
city_slug text,
|
||||
neighbourhood text,
|
||||
price_eur integer CHECK (price_eur IS NULL OR price_eur >= 0),
|
||||
area_m2 integer,
|
||||
rooms integer,
|
||||
bedrooms integer,
|
||||
bathrooms integer,
|
||||
construction_year integer,
|
||||
energy_label text,
|
||||
interior text,
|
||||
deposit_eur integer,
|
||||
available_from text,
|
||||
contract_type text,
|
||||
agent text,
|
||||
image_url text,
|
||||
description text,
|
||||
features jsonb NOT NULL DEFAULT '{}'::jsonb,
|
||||
lat double precision,
|
||||
lng double precision,
|
||||
-- When the site says it was published. first_seen_at is when we noticed.
|
||||
offered_at timestamptz,
|
||||
first_seen_at timestamptz NOT NULL DEFAULT now(),
|
||||
-- fast, slow, or seed (already listed when its search was first polled).
|
||||
first_seen_lane text NOT NULL DEFAULT 'slow',
|
||||
last_seen_at timestamptz NOT NULL DEFAULT now(),
|
||||
detail_fetched_at timestamptz,
|
||||
removed_at timestamptz,
|
||||
alerted_at timestamptz,
|
||||
-- Personal tracking.
|
||||
status text,
|
||||
notes text
|
||||
);
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS house_listings_source_idx ON house_listings (source, source_id);
|
||||
CREATE INDEX IF NOT EXISTS house_listings_active_idx
|
||||
ON house_listings (offer, first_seen_at DESC) WHERE removed_at IS NULL;
|
||||
CREATE INDEX IF NOT EXISTS house_listings_city_idx ON house_listings (source, offer, city_slug);
|
||||
CREATE INDEX IF NOT EXISTS house_listings_details_idx
|
||||
ON house_listings (first_seen_at DESC) WHERE detail_fetched_at IS NULL;
|
||||
|
||||
-- Every price we have seen, so price drops and market trends survive edits.
|
||||
CREATE TABLE IF NOT EXISTS house_price_history (
|
||||
listing_id integer NOT NULL REFERENCES house_listings(id) ON DELETE CASCADE,
|
||||
observed_at timestamptz NOT NULL DEFAULT now(),
|
||||
price_eur integer NOT NULL
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS house_price_history_listing_idx
|
||||
ON house_price_history (listing_id, observed_at);
|
||||
|
||||
-- One row per polled search URL. The first sync of a search only seeds the
|
||||
-- database: alerting on everything already listed would be noise.
|
||||
CREATE TABLE IF NOT EXISTS house_searches (
|
||||
search_url text PRIMARY KEY,
|
||||
source text NOT NULL,
|
||||
lane text NOT NULL,
|
||||
first_synced_at timestamptz NOT NULL DEFAULT now(),
|
||||
last_synced_at timestamptz NOT NULL DEFAULT now(),
|
||||
last_found integer NOT NULL DEFAULT 0
|
||||
);
|
||||
|
||||
-- Worker health: challenges, HTTP errors, parse failures.
|
||||
CREATE TABLE IF NOT EXISTS house_worker_runs (
|
||||
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
source text NOT NULL,
|
||||
lane text NOT NULL,
|
||||
url text NOT NULL,
|
||||
status text NOT NULL,
|
||||
http_status integer,
|
||||
found integer,
|
||||
duration_ms integer,
|
||||
message text,
|
||||
created_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS house_worker_runs_created_idx ON house_worker_runs (created_at DESC);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS house_alert_rules (
|
||||
id serial PRIMARY KEY,
|
||||
name text NOT NULL,
|
||||
enabled boolean NOT NULL DEFAULT true,
|
||||
offer text NOT NULL DEFAULT 'rent' CHECK (offer IN ('rent', 'buy')),
|
||||
min_price integer,
|
||||
max_price integer,
|
||||
min_area integer,
|
||||
min_bedrooms integer,
|
||||
-- Lowercase city names; empty means anywhere.
|
||||
cities text[] NOT NULL DEFAULT '{}',
|
||||
-- Empty means every property type.
|
||||
property_types text[] NOT NULL DEFAULT '{}',
|
||||
created_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
INSERT INTO house_alert_rules (name, offer, min_price, max_price, cities)
|
||||
SELECT 'Gouda area rentals', 'rent', 1300, 1700, ARRAY[
|
||||
'gouda', 'waddinxveen', 'reeuwijk', 'moordrecht', 'nieuwerkerk aan den ijssel',
|
||||
'zevenhuizen', 'bodegraven', 'boskoop', 'oudewater', 'schoonhoven', 'haastrecht'
|
||||
]
|
||||
WHERE NOT EXISTS (SELECT 1 FROM house_alert_rules);
|
||||
@@ -85,7 +85,7 @@ async function itemId(
|
||||
extraDimensions: Dimensions = {}
|
||||
) {
|
||||
const [row] = await sql<{ id: number }[]>`
|
||||
INSERT INTO cs_market_items (market_hash_name, name, icon_url, item_type)
|
||||
INSERT INTO cs.items (market_hash_name, name, icon_url, item_type)
|
||||
VALUES (${name}, ${displayName}, ${iconUrl ?? null}, ${itemType ?? null})
|
||||
ON CONFLICT (market_hash_name) DO UPDATE SET
|
||||
name = EXCLUDED.name,
|
||||
@@ -98,13 +98,13 @@ async function itemId(
|
||||
for (const [key, value] of Object.entries(inferred)) {
|
||||
if (!/^[a-z][a-z0-9_]*$/.test(key) || !value) continue;
|
||||
await sql`
|
||||
INSERT INTO cs_item_dimensions (item_id, key, value) VALUES (${row.id}, ${key}, ${value})
|
||||
INSERT INTO cs.item_dimensions (item_id, key, value) VALUES (${row.id}, ${key}, ${value})
|
||||
ON CONFLICT (item_id, key) DO NOTHING`;
|
||||
}
|
||||
for (const [key, value] of Object.entries(extraDimensions)) {
|
||||
if (!/^[a-z][a-z0-9_]*$/.test(key) || !value) continue;
|
||||
await sql`
|
||||
INSERT INTO cs_item_dimensions (item_id, key, value) VALUES (${row.id}, ${key}, ${value})
|
||||
INSERT INTO cs.item_dimensions (item_id, key, value) VALUES (${row.id}, ${key}, ${value})
|
||||
ON CONFLICT (item_id, key) DO UPDATE SET value = EXCLUDED.value`;
|
||||
}
|
||||
return row.id;
|
||||
@@ -116,7 +116,7 @@ async function saveObservation(row: Observation, knownItemId?: number) {
|
||||
(await itemId(row.marketHashName, row.name, undefined, undefined, row.dimensions));
|
||||
await ensureStream(row.source, row.currency, row.kind);
|
||||
await sql`
|
||||
INSERT INTO cs_price_observations
|
||||
INSERT INTO cs.price_observations
|
||||
(item_id, source, currency, observed_at, price_cents, volume, kind)
|
||||
VALUES (${item}, ${row.source}, ${row.currency}, ${new Date(row.observedAt)},
|
||||
${row.priceCents}, ${row.volume ?? null}, ${row.kind})
|
||||
@@ -128,7 +128,7 @@ async function saveObservation(row: Observation, knownItemId?: number) {
|
||||
async function ensureStream(source: string, currency: string, kind: string) {
|
||||
const key = `${source}\0${currency}\0${kind}`;
|
||||
if (knownStreams.has(key)) return;
|
||||
await sql`INSERT INTO cs_price_streams (source, currency, kind)
|
||||
await sql`INSERT INTO cs.price_streams (source, currency, kind)
|
||||
VALUES (${source}, ${currency}, ${kind}) ON CONFLICT DO NOTHING`;
|
||||
knownStreams.add(key);
|
||||
}
|
||||
@@ -150,7 +150,7 @@ async function saveHistoryRows(
|
||||
kind: 'sale-median'
|
||||
}));
|
||||
for (let offset = 0; offset < rows.length; offset += 500) {
|
||||
await sql`INSERT INTO cs_price_observations ${sql(rows.slice(offset, offset + 500))}
|
||||
await sql`INSERT INTO cs.price_observations ${sql(rows.slice(offset, offset + 500))}
|
||||
ON CONFLICT (item_id, source, currency, observed_at, kind) DO UPDATE SET
|
||||
price_cents = EXCLUDED.price_cents, volume = EXCLUDED.volume`;
|
||||
}
|
||||
@@ -242,7 +242,7 @@ async function history() {
|
||||
if (!process.env.STEAM_SESSIONID || !process.env.STEAM_LOGIN_SECURE)
|
||||
throw new Error('STEAM_SESSIONID and STEAM_LOGIN_SECURE are required for Steam history');
|
||||
const items = await sql<{ id: number; market_hash_name: string }[]>`
|
||||
SELECT id, market_hash_name FROM cs_market_items ORDER BY id LIMIT ${limit} OFFSET ${start}`;
|
||||
SELECT id, market_hash_name FROM cs.items ORDER BY id LIMIT ${limit} OFFSET ${start}`;
|
||||
let done = 0;
|
||||
for (const item of items) {
|
||||
const url = new URL('https://steamcommunity.com/market/pricehistory/');
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
-- One-off bridge from the pre-foundation site schema to the current table names.
|
||||
-- Run after the Animaldex UUID conversion, if needed, and before the auth,
|
||||
-- foundation, and catalog migrations. Review and back up each database first.
|
||||
-- Renaming keeps existing rows and foreign keys; lists did not exist in this schema.
|
||||
|
||||
BEGIN;
|
||||
|
||||
ALTER TABLE public.game RENAME TO games;
|
||||
ALTER TABLE public.pokemon_game_entry RENAME TO pokemon_game_entries;
|
||||
ALTER TABLE public.user_entry RENAME TO user_entries;
|
||||
ALTER TABLE public.user_game RENAME TO user_games;
|
||||
|
||||
CREATE TABLE public.lists (
|
||||
id serial PRIMARY KEY,
|
||||
name text NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE public.list_items (
|
||||
id serial PRIMARY KEY,
|
||||
list_id integer NOT NULL REFERENCES public.lists(id) ON DELETE CASCADE,
|
||||
name text NOT NULL
|
||||
);
|
||||
CREATE INDEX list_items_list_id_idx ON public.list_items(list_id);
|
||||
|
||||
COMMIT;
|
||||
@@ -1 +0,0 @@
|
||||
ALTER TABLE lists ADD COLUMN IF NOT EXISTS picture_url text;
|
||||
@@ -1,14 +0,0 @@
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import postgres from 'postgres';
|
||||
if (!process.env.DB_URL) throw new Error('DB_URL is required');
|
||||
const sql = postgres(process.env.DB_URL, { max: 1 });
|
||||
try {
|
||||
await sql.begin(async (tx) => {
|
||||
await tx.unsafe(
|
||||
await readFile(new URL('./auth-catalog-migration.sql', import.meta.url), 'utf8')
|
||||
);
|
||||
});
|
||||
console.log('Authentication and catalog migration applied');
|
||||
} finally {
|
||||
await sql.end();
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import postgres from 'postgres';
|
||||
|
||||
if (!process.env.DB_URL) throw new Error('DB_URL is required');
|
||||
const client = postgres(process.env.DB_URL, { max: 1 });
|
||||
try {
|
||||
await client.begin(async (tx) => {
|
||||
await tx.unsafe(await readFile(new URL('./cs-prices-migration.sql', import.meta.url), 'utf8'));
|
||||
});
|
||||
console.log('CS prices migration applied');
|
||||
} finally {
|
||||
await client.end();
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import postgres from 'postgres';
|
||||
|
||||
if (!process.env.DB_URL) throw new Error('DB_URL is required');
|
||||
const client = postgres(process.env.DB_URL, { max: 1 });
|
||||
try {
|
||||
await client.begin(async (tx) => {
|
||||
await tx.unsafe(await readFile(new URL('./foundation-migration.sql', import.meta.url), 'utf8'));
|
||||
});
|
||||
console.log('Foundation migration applied');
|
||||
} finally {
|
||||
await client.end();
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import postgres from 'postgres';
|
||||
|
||||
if (!process.env.DB_URL) throw new Error('DB_URL is required');
|
||||
const client = postgres(process.env.DB_URL, { max: 1 });
|
||||
try {
|
||||
await client.begin(async (tx) => {
|
||||
await tx.unsafe(
|
||||
await readFile(new URL('./game-tracker-migration.sql', import.meta.url), 'utf8')
|
||||
);
|
||||
});
|
||||
console.log('Game tracker migration applied');
|
||||
} finally {
|
||||
await client.end();
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import postgres from 'postgres';
|
||||
|
||||
if (!process.env.DB_URL) throw new Error('DB_URL is required');
|
||||
const client = postgres(process.env.DB_URL, { max: 1 });
|
||||
try {
|
||||
await client.begin(async (tx) => {
|
||||
await tx.unsafe(
|
||||
await readFile(new URL('./house-tracker-migration.sql', import.meta.url), 'utf8')
|
||||
);
|
||||
});
|
||||
console.log('House tracker migration applied');
|
||||
} finally {
|
||||
await client.end();
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import postgres from 'postgres';
|
||||
|
||||
if (!process.env.DB_URL) throw new Error('DB_URL is required');
|
||||
const client = postgres(process.env.DB_URL, { max: 1 });
|
||||
try {
|
||||
await client.begin(async (tx) => {
|
||||
await tx.unsafe(
|
||||
await readFile(new URL('./money-tracker-migration.sql', import.meta.url), 'utf8')
|
||||
);
|
||||
});
|
||||
console.log('Money tracker migration applied');
|
||||
} finally {
|
||||
await client.end();
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import postgres from 'postgres';
|
||||
|
||||
if (!process.env.DB_URL) throw new Error('DB_URL is required');
|
||||
const client = postgres(process.env.DB_URL, { max: 1 });
|
||||
try {
|
||||
await client.begin(async (tx) => {
|
||||
await tx.unsafe(
|
||||
await readFile(new URL('./tracker-journal-migration.sql', import.meta.url), 'utf8')
|
||||
);
|
||||
});
|
||||
console.log('Tracker journal migration applied');
|
||||
} finally {
|
||||
await client.end();
|
||||
}
|
||||
@@ -1,120 +0,0 @@
|
||||
CREATE TABLE IF NOT EXISTS money_accounts (
|
||||
id serial PRIMARY KEY,
|
||||
user_id text NOT NULL REFERENCES "user"(id) ON DELETE CASCADE,
|
||||
name text NOT NULL,
|
||||
opening_balance_cents integer NOT NULL DEFAULT 0,
|
||||
kind text NOT NULL DEFAULT 'spendable',
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
updated_at timestamptz NOT NULL DEFAULT now(),
|
||||
UNIQUE (user_id, name)
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS money_accounts_user_id_idx ON money_accounts (user_id);
|
||||
ALTER TABLE money_accounts ADD COLUMN IF NOT EXISTS kind text NOT NULL DEFAULT 'spendable';
|
||||
|
||||
CREATE TABLE IF NOT EXISTS money_transactions (
|
||||
id serial PRIMARY KEY,
|
||||
user_id text NOT NULL REFERENCES "user"(id) ON DELETE CASCADE,
|
||||
account_id integer NOT NULL REFERENCES money_accounts(id),
|
||||
date date NOT NULL,
|
||||
description text NOT NULL,
|
||||
amount_cents integer NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
updated_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS money_transactions_user_date_idx ON money_transactions (user_id, date, id);
|
||||
CREATE INDEX IF NOT EXISTS money_transactions_account_id_idx ON money_transactions (account_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS money_recurring (
|
||||
id serial PRIMARY KEY,
|
||||
user_id text NOT NULL REFERENCES "user"(id) ON DELETE CASCADE,
|
||||
name text NOT NULL,
|
||||
amount_cents integer NOT NULL,
|
||||
cadence text NOT NULL,
|
||||
starts_on date,
|
||||
ends_on date,
|
||||
account_id integer REFERENCES money_accounts(id) ON DELETE SET NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
updated_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS money_recurring_user_id_idx ON money_recurring (user_id);
|
||||
ALTER TABLE money_recurring ADD COLUMN IF NOT EXISTS starts_on date;
|
||||
ALTER TABLE money_recurring ADD COLUMN IF NOT EXISTS ends_on date;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS money_forecasts (
|
||||
id serial PRIMARY KEY,
|
||||
user_id text NOT NULL REFERENCES "user"(id) ON DELETE CASCADE,
|
||||
date date NOT NULL,
|
||||
description text NOT NULL,
|
||||
amount_cents integer NOT NULL,
|
||||
account_id integer REFERENCES money_accounts(id) ON DELETE SET NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
updated_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS money_forecasts_user_date_idx ON money_forecasts (user_id, date);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS money_utility_settings (
|
||||
id serial PRIMARY KEY,
|
||||
user_id text NOT NULL REFERENCES "user"(id) ON DELETE CASCADE,
|
||||
kind text NOT NULL,
|
||||
rate_per_unit numeric(12, 4) NOT NULL DEFAULT 0,
|
||||
monthly_advance_cents integer NOT NULL DEFAULT 0,
|
||||
billing_start date,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
updated_at timestamptz NOT NULL DEFAULT now(),
|
||||
UNIQUE (user_id, kind)
|
||||
);
|
||||
ALTER TABLE money_utility_settings ADD COLUMN IF NOT EXISTS billing_start date;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS money_utility_readings (
|
||||
id serial PRIMARY KEY,
|
||||
user_id text NOT NULL REFERENCES "user"(id) ON DELETE CASCADE,
|
||||
kind text NOT NULL,
|
||||
date date NOT NULL,
|
||||
reading numeric(12, 3) NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
updated_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS money_utility_readings_user_kind_date_idx ON money_utility_readings (user_id, kind, date);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS money_wishlist (
|
||||
id serial PRIMARY KEY,
|
||||
user_id text NOT NULL REFERENCES "user"(id) ON DELETE CASCADE,
|
||||
name text NOT NULL,
|
||||
estimated_cents integer,
|
||||
priority integer NOT NULL DEFAULT 3,
|
||||
notes text,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
updated_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS money_wishlist_user_id_idx ON money_wishlist (user_id);
|
||||
ALTER TABLE money_wishlist ADD COLUMN IF NOT EXISTS priority integer NOT NULL DEFAULT 3;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS money_projection_settings (
|
||||
id serial PRIMARY KEY,
|
||||
user_id text NOT NULL UNIQUE REFERENCES "user"(id) ON DELETE CASCADE,
|
||||
investment_account_id integer REFERENCES money_accounts(id) ON DELETE SET NULL,
|
||||
reserve_cents integer NOT NULL DEFAULT 100000,
|
||||
annual_return_percent numeric(5, 2) NOT NULL DEFAULT 5.00,
|
||||
contribution_mode text NOT NULL DEFAULT 'excess',
|
||||
include_recurring integer NOT NULL DEFAULT 1,
|
||||
fixed_monthly_cents integer NOT NULL DEFAULT 0,
|
||||
end_date date,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
updated_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
ALTER TABLE money_projection_settings ADD COLUMN IF NOT EXISTS include_recurring integer NOT NULL DEFAULT 1;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS money_insurances (
|
||||
id serial PRIMARY KEY,
|
||||
user_id text NOT NULL REFERENCES "user"(id) ON DELETE CASCADE,
|
||||
name text NOT NULL,
|
||||
provider text,
|
||||
coverage text,
|
||||
premium_cents integer,
|
||||
cadence text NOT NULL DEFAULT 'monthly',
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
updated_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS money_insurances_user_id_idx ON money_insurances (user_id);
|
||||
|
||||
ALTER TABLE money_accounts ADD COLUMN IF NOT EXISTS color text;
|
||||
58
scripts/preview.sh
Executable file
58
scripts/preview.sh
Executable file
@@ -0,0 +1,58 @@
|
||||
#!/usr/bin/env bash
|
||||
# Run a branch against a throwaway copy of the dev database (itself a prod copy).
|
||||
#
|
||||
# scripts/preview.sh <branch> [port]
|
||||
#
|
||||
# Creates a git worktree in /tmp, clones DEV_DB_NAME into a temporary database
|
||||
# (CREATE DATABASE ... TEMPLATE), runs that branch's Drizzle migrations on the copy,
|
||||
# builds and serves it on <port> (default 5180). Ctrl-C removes the server,
|
||||
# database and worktree. Prod and the dev DB itself are never modified.
|
||||
#
|
||||
# Uses .env from the main checkout: DB_URL (app role) and DB_MIGRATE_URL (owner role)
|
||||
# pointing at the dev DB, plus DEV_ADMIN_URL (superuser, maintenance DB) for create/drop.
|
||||
set -euo pipefail
|
||||
|
||||
branch="${1:?usage: scripts/preview.sh <branch> [port]}"
|
||||
port="${2:-5180}"
|
||||
repo="$(cd "$(dirname "$0")/.." && pwd)"
|
||||
cd "$repo"
|
||||
set -a
|
||||
. ./.env
|
||||
[[ -f .env.dev-refresh ]] && . ./.env.dev-refresh
|
||||
set +a
|
||||
: "${DB_URL:?}" "${DB_MIGRATE_URL:?}" "${DEV_ADMIN_URL:?set DEV_ADMIN_URL}"
|
||||
DEV_DB_NAME="${DEV_DB_NAME:-personal_dev}"
|
||||
|
||||
safe="$(printf '%s' "$branch" | tr -c 'a-zA-Z0-9' '_' | tr 'A-Z' 'a-z' | cut -c1-40)"
|
||||
db="preview_${safe}"
|
||||
dir="/tmp/personal-website-preview-${safe}"
|
||||
with_db() { bun -e 'const u=new URL(process.argv[1]); u.pathname="/"+process.argv[2]; console.log(u.toString())' "$1" "$2"; }
|
||||
|
||||
server_pid=""
|
||||
cleanup() {
|
||||
[[ -n "$server_pid" ]] && kill "$server_pid" 2>/dev/null || true
|
||||
psql -q "$DEV_ADMIN_URL" -c "DROP DATABASE IF EXISTS \"$db\" WITH (FORCE)" || true
|
||||
git -C "$repo" worktree remove --force "$dir" 2>/dev/null || true
|
||||
echo "Preview cleaned up."
|
||||
}
|
||||
trap cleanup EXIT INT TERM
|
||||
|
||||
git fetch origin "$branch"
|
||||
git worktree add --detach "$dir" "origin/$branch"
|
||||
|
||||
# TEMPLATE needs no other connections to the source; stop `bun run dev` first if it fails.
|
||||
psql -v ON_ERROR_STOP=1 -q "$DEV_ADMIN_URL" -c "CREATE DATABASE \"$db\" TEMPLATE \"$DEV_DB_NAME\" OWNER personal"
|
||||
|
||||
export DB_URL="$(with_db "$DB_URL" "$db")"
|
||||
export DB_MIGRATE_URL="$(with_db "$DB_MIGRATE_URL" "$db")"
|
||||
export ORIGIN="http://localhost:$port" BETTER_AUTH_URL="http://localhost:$port" PORT="$port"
|
||||
|
||||
cd "$dir"
|
||||
cp "$repo/.env" .env
|
||||
bun install --frozen-lockfile
|
||||
bun scripts/db-migrate.ts
|
||||
bun run build
|
||||
echo "Preview of $branch on http://localhost:$port (Ctrl-C to stop and clean up)"
|
||||
bun build/index.js &
|
||||
server_pid=$!
|
||||
wait "$server_pid"
|
||||
@@ -8,11 +8,11 @@ try {
|
||||
await sql.begin(async (tx) => {
|
||||
for (const image of artwork) {
|
||||
const rows =
|
||||
await tx`update tracker_items i set icon=${image.path} from tracker_games g where i.game_id=g.id and g.slug=${image.game} and i.key=${image.key} returning i.id`;
|
||||
await tx`update ref.tracker_items i set icon=${image.path} from ref.tracker_games g where i.game_id=g.id and g.slug=${image.game} and i.key=${image.key} returning i.id`;
|
||||
if (rows.length !== 1) throw new Error(`Unknown artwork target: ${image.game}/${image.key}`);
|
||||
}
|
||||
for (const [source, path] of Object.entries(achievementArt)) {
|
||||
await tx`update tracker_items set icon=${path} where kind='achievement' and icon=${source}`;
|
||||
await tx`update ref.tracker_items set icon=${path} where kind='achievement' and icon=${source}`;
|
||||
}
|
||||
});
|
||||
console.log(
|
||||
|
||||
@@ -22,17 +22,17 @@ try {
|
||||
}[]
|
||||
][]) {
|
||||
const [game] =
|
||||
await tx`select id from tracker_games where slug=${slug} and franchise='call-of-duty'`;
|
||||
await tx`select id from ref.tracker_games where slug=${slug} and franchise='call-of-duty'`;
|
||||
if (!game) throw new Error(`Seed Call of Duty catalog first: ${slug}`);
|
||||
for (let offset = 0; offset < rows.length; offset += 500) {
|
||||
const batch = rows
|
||||
.slice(offset, offset + 500)
|
||||
.map(({ sourceUrl, ...r }) => ({ ...r, game_id: game.id, source_url: sourceUrl }));
|
||||
await tx`insert into tracker_challenges ${tx(batch)} on conflict (game_id,key) do update set name=excluded.name,category=excluded.category,requirement=excluded.requirement,reward=excluded.reward,icon=excluded.icon,target=excluded.target,source_url=excluded.source_url`;
|
||||
await tx`insert into ref.tracker_challenges ${tx(batch)} on conflict (game_id,key) do update set name=excluded.name,category=excluded.category,requirement=excluded.requirement,reward=excluded.reward,icon=excluded.icon,target=excluded.target,source_url=excluded.source_url`;
|
||||
}
|
||||
// Retire removed source definitions only when nobody has saved progress.
|
||||
if (rows.length)
|
||||
await tx`delete from tracker_challenges c where c.game_id=${game.id} and c.key not in ${tx(rows.map((r) => r.key))} and not exists (select 1 from tracker_challenge_progress p where p.challenge_id=c.id)`;
|
||||
await tx`delete from ref.tracker_challenges c where c.game_id=${game.id} and c.key not in ${tx(rows.map((r) => r.key))} and not exists (select 1 from games.tracker_challenge_progress p where p.challenge_id=c.id)`;
|
||||
console.log(`${slug}: ${rows.length} challenge rows`);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -9,10 +9,10 @@ const client = postgres(process.env.DB_URL, { max: 1 });
|
||||
try {
|
||||
await client.begin(async (tx) => {
|
||||
const species = new Map(
|
||||
(await tx`select id, number from pokemon`).map((r) => [r.number, r.id])
|
||||
(await tx`select id, number from ref.pokemon`).map((r) => [r.number, r.id])
|
||||
);
|
||||
const games = new Map(
|
||||
(await tx`select id, source_id from games`).map((r) => [r.source_id, r.id])
|
||||
(await tx`select id, source_id from ref.pokemon_games`).map((r) => [r.source_id, r.id])
|
||||
);
|
||||
const speciesId = (n: number) => {
|
||||
const id = species.get(n);
|
||||
@@ -25,10 +25,10 @@ try {
|
||||
return id;
|
||||
};
|
||||
for (const row of catalog.facts)
|
||||
await tx`insert into pokemon_research (pokemon_id,facts) values (${speciesId(row.number)},${tx.json(row.facts)}) on conflict (pokemon_id) do update set facts=excluded.facts`;
|
||||
await tx`insert into ref.pokemon_research (pokemon_id,facts) values (${speciesId(row.number)},${tx.json(row.facts)}) on conflict (pokemon_id) do update set facts=excluded.facts`;
|
||||
for (const row of catalog.games) {
|
||||
if (!games.has(row.sourceId)) continue;
|
||||
await tx`insert into pokemon_game_research (game_id,version_group,regions) values (${gameId(row.sourceId)},${row.versionGroup},${row.regions}) on conflict (game_id) do update set version_group=excluded.version_group,regions=excluded.regions`;
|
||||
await tx`insert into ref.pokemon_game_research (game_id,version_group,regions) values (${gameId(row.sourceId)},${row.versionGroup},${row.regions}) on conflict (game_id) do update set version_group=excluded.version_group,regions=excluded.regions`;
|
||||
}
|
||||
for (let offset = 0; offset < catalog.dexes.length; offset += 1000) {
|
||||
const batch = catalog.dexes
|
||||
@@ -42,10 +42,10 @@ try {
|
||||
number
|
||||
}));
|
||||
if (batch.length)
|
||||
await tx`insert into pokemon_dex_numbers ${tx(batch)} on conflict (pokemon_id,game_id,dex) do update set number=excluded.number,dex_name=excluded.dex_name`;
|
||||
await tx`insert into ref.pokemon_dex_numbers ${tx(batch)} on conflict (pokemon_id,game_id,dex) do update set number=excluded.number,dex_name=excluded.dex_name`;
|
||||
}
|
||||
for (const row of catalog.moves)
|
||||
await tx`insert into pokemon_moves ${tx(row)} on conflict (id) do update set name=excluded.name,type=excluded.type,category=excluded.category,power=excluded.power,accuracy=excluded.accuracy,pp=excluded.pp,priority=excluded.priority,target=excluded.target`;
|
||||
await tx`insert into ref.pokemon_moves ${tx(row)} on conflict (id) do update set name=excluded.name,type=excluded.type,category=excluded.category,power=excluded.power,accuracy=excluded.accuracy,pp=excluded.pp,priority=excluded.priority,target=excluded.target`;
|
||||
for (let offset = 0; offset < catalog.learnsets.length; offset += 500) {
|
||||
const batch = catalog.learnsets
|
||||
.slice(offset, offset + 500)
|
||||
@@ -54,7 +54,7 @@ try {
|
||||
version_group,
|
||||
moves: tx.json(moves)
|
||||
}));
|
||||
await tx`insert into pokemon_learnsets ${tx(batch)} on conflict (pokemon_id,version_group) do update set moves=excluded.moves`;
|
||||
await tx`insert into ref.pokemon_learnsets ${tx(batch)} on conflict (pokemon_id,version_group) do update set moves=excluded.moves`;
|
||||
}
|
||||
console.log(
|
||||
`Seeded ${catalog.facts.length} profiles, ${catalog.dexes.length} dex entries, ${catalog.moves.length} moves and ${catalog.learnsets.length} versioned learnsets`
|
||||
|
||||
@@ -6,7 +6,7 @@ if (!process.env.DB_URL) throw new Error('DB_URL is required');
|
||||
const sql = postgres(process.env.DB_URL, { max: 1 });
|
||||
try {
|
||||
const users =
|
||||
await sql`update "user" set role = ${role}, updated_at = now() where id = ${userId} returning id, role`;
|
||||
await sql`update auth."user" set role = ${role}, updated_at = now() where id = ${userId} returning id, role`;
|
||||
if (!users.length)
|
||||
throw new Error('User ID not found. Register first and copy the ID from your account page.');
|
||||
console.log(users[0]);
|
||||
|
||||
46
scripts/tasks-import.ts
Normal file
46
scripts/tasks-import.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* Import tasks, goals and areas into the private `tasks` schema (idempotent).
|
||||
*
|
||||
* bun scripts/tasks-import.ts data/tasks/daisy-import.json # insert what is missing
|
||||
* bun scripts/tasks-import.ts --dry-run data/tasks/daisy-import.json # validate only
|
||||
* bun scripts/tasks-import.ts --update data/tasks/daisy-import.json # also overwrite existing rows
|
||||
*
|
||||
* Dedupe: areas on slug, goals on title, tasks on area + title (case-insensitive).
|
||||
* Uses DB_URL (the app role on prod, personal_dev on dev).
|
||||
*/
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import { drizzle } from 'drizzle-orm/postgres-js';
|
||||
import postgres from 'postgres';
|
||||
import { importSeed, seedFile } from '../src/lib/server/tasks';
|
||||
|
||||
const args = process.argv.slice(2);
|
||||
const dryRun = args.includes('--dry-run');
|
||||
const update = args.includes('--update');
|
||||
const files = args.filter((a) => !a.startsWith('--'));
|
||||
if (files.length !== 1) {
|
||||
console.error('usage: bun scripts/tasks-import.ts [--dry-run] [--update] <seed.json>');
|
||||
process.exit(2);
|
||||
}
|
||||
const parsed = seedFile.safeParse(JSON.parse(await readFile(files[0], 'utf8')));
|
||||
if (!parsed.success) {
|
||||
for (const issue of parsed.error.issues.slice(0, 50))
|
||||
console.error(`${issue.path.join('.')}: ${issue.message}`);
|
||||
process.exit(1);
|
||||
}
|
||||
const seed = parsed.data;
|
||||
console.log(
|
||||
`Valid: ${seed.areas.length} areas, ${seed.goals.length} goals, ${seed.tasks.length} tasks`
|
||||
);
|
||||
if (dryRun) process.exit(0);
|
||||
if (!process.env.DB_URL) throw new Error('DB_URL is required');
|
||||
const client = postgres(process.env.DB_URL, { max: 1, onnotice: () => {} });
|
||||
try {
|
||||
const r = await importSeed(drizzle({ client }), seed, { update });
|
||||
console.log(
|
||||
`areas: ${r.areas.inserted} inserted, ${r.areas.existing} existing; ` +
|
||||
`goals: ${r.goals.inserted} inserted, ${r.goals.existing} existing, ${r.goals.updated} updated; ` +
|
||||
`tasks: ${r.tasks.inserted} inserted, ${r.tasks.existing} existing, ${r.tasks.updated} updated`
|
||||
);
|
||||
} finally {
|
||||
await client.end();
|
||||
}
|
||||
79
scripts/ticktick-export.ts
Normal file
79
scripts/ticktick-export.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
/**
|
||||
* Export every TickTick task reachable through the official CLI (Open API v1):
|
||||
* open, won't-do and completed tasks of the inbox and every list, plus completed
|
||||
* history month by month (windows are split further when they hit the API's
|
||||
* 200-result cap). Needs `ticktick auth login` done once; never reads the token.
|
||||
*
|
||||
* bun scripts/ticktick-export.ts ~/.cache/ticktick-export/all.json
|
||||
*
|
||||
* Then: bun scripts/ticktick-import.ts ~/.cache/ticktick-export/all.json
|
||||
*/
|
||||
import { $ } from 'bun';
|
||||
|
||||
const out = process.argv[2];
|
||||
if (!out) {
|
||||
console.error('usage: bun scripts/ticktick-export.ts <out.json> [--since YYYY]');
|
||||
process.exit(2);
|
||||
}
|
||||
const sinceArg = process.argv.indexOf('--since');
|
||||
const since = sinceArg > 0 ? Number(process.argv[sinceArg + 1]) : 2015;
|
||||
const CAP = 200;
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
type Json = any;
|
||||
async function tt(...args: string[]): Promise<Json> {
|
||||
for (let attempt = 0; ; attempt++) {
|
||||
const r = await $`ticktick ${args} --json`.quiet().nothrow();
|
||||
if (r.exitCode === 0) return JSON.parse(r.stdout.toString() || '[]');
|
||||
if (attempt >= 3)
|
||||
throw new Error(
|
||||
`ticktick ${args[0]} ${args[1]} failed: ${r.stderr.toString().slice(0, 300)}`
|
||||
);
|
||||
await Bun.sleep(2000 * (attempt + 1));
|
||||
}
|
||||
}
|
||||
const fmt = (d: Date) => d.toISOString().replace(/\.\d{3}Z$/, '+0000');
|
||||
const byId = new Map<string, Json>();
|
||||
const add = (list: Json[]) => list.filter((t) => !byId.has(t.id) && byId.set(t.id, t)).length;
|
||||
|
||||
const projects: Json[] = await tt('project', 'list');
|
||||
// The inbox is not a project; its id shows up on tasks (inbox<userId>).
|
||||
const probe: Json[] = await tt('task', 'filter', '--status=0,2,-1');
|
||||
const inboxId = probe.map((t) => t.projectId).find((p: string) => p?.startsWith('inbox'));
|
||||
add(probe);
|
||||
const ids = [...(inboxId ? [inboxId] : []), ...projects.map((p) => p.id)];
|
||||
let capped = 0;
|
||||
for (const status of ['0', '-1', '2'])
|
||||
for (const p of ids) {
|
||||
const r = await tt('task', 'filter', '--projects', p, `--status=${status}`);
|
||||
if (r.length >= CAP) capped++;
|
||||
add(r);
|
||||
}
|
||||
for (const p of projects) add((await tt('project', 'data', p.id)).tasks ?? []);
|
||||
|
||||
async function completed(start: Date, end: Date): Promise<void> {
|
||||
const r = await tt('task', 'completed', '--start-date', fmt(start), '--end-date', fmt(end));
|
||||
if (r.length >= CAP && end.getTime() - start.getTime() > 3_600_000) {
|
||||
const mid = new Date(Math.floor((start.getTime() + end.getTime()) / 2000) * 1000);
|
||||
await completed(start, mid);
|
||||
await completed(new Date(mid.getTime() + 1000), end);
|
||||
return;
|
||||
}
|
||||
add(r);
|
||||
}
|
||||
const now = new Date();
|
||||
for (let y = since; y <= now.getUTCFullYear(); y++)
|
||||
for (let m = 0; m < 12; m++) {
|
||||
const start = new Date(Date.UTC(y, m, 1));
|
||||
if (start > now) break;
|
||||
await completed(start, new Date(Date.UTC(y, m + 1, 1) - 1000));
|
||||
}
|
||||
|
||||
const tasks = [...byId.values()];
|
||||
await Bun.write(out, JSON.stringify({ exportedAt: now.toISOString(), inboxId, projects, tasks }));
|
||||
const count = (s: number) => tasks.filter((t) => t.status === s).length;
|
||||
console.log(
|
||||
`${tasks.length} tasks (${count(0)} open, ${count(2)} completed, ${count(-1)} won't do) from ` +
|
||||
`${projects.length} lists + inbox` +
|
||||
(capped ? `; ${capped} status/list queries hit the ${CAP} cap (older ones may be missing)` : '')
|
||||
);
|
||||
65
scripts/ticktick-import.ts
Normal file
65
scripts/ticktick-import.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* Sync TickTick into the private `tasks` schema. Idempotent; rerun after a new export.
|
||||
*
|
||||
* bun scripts/ticktick-import.ts [--dry-run] [--csv backup.csv] ~/.cache/ticktick-export/all.json
|
||||
*
|
||||
* all.json comes from scripts/ticktick-export.ts (Open API). --csv adds the tasks from a
|
||||
* TickTick backup (Settings → Backup & Import) that the API no longer returns (closed lists,
|
||||
* old won't-do tasks); rows the API also has are matched and skipped (see mergeCsv).
|
||||
* Links listed in data/tasks/ticktick-merges.json (plus exact title matches, see
|
||||
* syncTickTick) attach a TickTick task to an existing task instead of duplicating it.
|
||||
* Uses DB_URL (the app role on prod, personal_dev on dev).
|
||||
*/
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import { drizzle } from 'drizzle-orm/postgres-js';
|
||||
import postgres from 'postgres';
|
||||
import {
|
||||
mergeCsv,
|
||||
parseTickTickCsv,
|
||||
syncTickTick,
|
||||
type TtExport,
|
||||
type TtMerge
|
||||
} from '../src/lib/server/ticktick';
|
||||
|
||||
const args = process.argv.slice(2);
|
||||
const dryRun = args.includes('--dry-run');
|
||||
const csvAt = args.indexOf('--csv');
|
||||
const csvPath = csvAt >= 0 ? args[csvAt + 1] : undefined;
|
||||
const files = args.filter((a, i) => !a.startsWith('--') && (csvAt < 0 || i !== csvAt + 1));
|
||||
if (files.length !== 1 || (csvAt >= 0 && !csvPath)) {
|
||||
console.error(
|
||||
'usage: bun scripts/ticktick-import.ts [--dry-run] [--csv backup.csv] <export.json>'
|
||||
);
|
||||
process.exit(2);
|
||||
}
|
||||
let data = JSON.parse(await readFile(files[0], 'utf8')) as TtExport;
|
||||
if (csvPath) {
|
||||
const merged = mergeCsv(data, parseTickTickCsv(await readFile(csvPath, 'utf8')));
|
||||
data = merged.data;
|
||||
console.log(JSON.stringify({ csv: merged.report }));
|
||||
}
|
||||
const merges = JSON.parse(
|
||||
await readFile(new URL('../data/tasks/ticktick-merges.json', import.meta.url), 'utf8')
|
||||
) as TtMerge[];
|
||||
if (!process.env.DB_URL) throw new Error('DB_URL is required');
|
||||
const client = postgres(process.env.DB_URL, { max: 1, onnotice: () => {} });
|
||||
class DryRun extends Error {}
|
||||
try {
|
||||
const db = drizzle({ client });
|
||||
try {
|
||||
const report = await db.transaction(async (tx) => {
|
||||
const r = await syncTickTick(tx, data, { merges });
|
||||
if (dryRun) {
|
||||
console.log(JSON.stringify(r, null, 2));
|
||||
throw new DryRun();
|
||||
}
|
||||
return r;
|
||||
});
|
||||
console.log(JSON.stringify(report, null, 2));
|
||||
} catch (e) {
|
||||
if (!(e instanceof DryRun)) throw e;
|
||||
console.log('(dry run, rolled back)');
|
||||
}
|
||||
} finally {
|
||||
await client.end();
|
||||
}
|
||||
@@ -1,71 +0,0 @@
|
||||
-- Additive, repeatable journal and factual catalog expansion. Existing progress is preserved.
|
||||
CREATE TABLE IF NOT EXISTS pokemon_seen (
|
||||
id serial PRIMARY KEY, user_id text NOT NULL REFERENCES "user"(id) ON DELETE CASCADE,
|
||||
game_id integer NOT NULL REFERENCES games(id) ON DELETE CASCADE,
|
||||
pokemon_id integer NOT NULL REFERENCES pokemon(id) ON DELETE CASCADE,
|
||||
created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now(),
|
||||
UNIQUE(user_id, game_id, pokemon_id)
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS pokemon_seen_user_idx ON pokemon_seen(user_id);
|
||||
CREATE TABLE IF NOT EXISTS pokemon_research (
|
||||
pokemon_id integer PRIMARY KEY REFERENCES pokemon(id) ON DELETE CASCADE, facts jsonb NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS pokemon_dex_numbers (
|
||||
id serial PRIMARY KEY, pokemon_id integer NOT NULL REFERENCES pokemon(id) ON DELETE CASCADE, game_id integer NOT NULL REFERENCES games(id) ON DELETE CASCADE, dex text NOT NULL, dex_name text NOT NULL, number integer NOT NULL, UNIQUE(pokemon_id,game_id,dex)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS pokemon_moves (
|
||||
id integer PRIMARY KEY, name text NOT NULL, type text NOT NULL, category text NOT NULL, power integer, accuracy integer, pp integer, priority integer NOT NULL, target text NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS pokemon_learnsets (
|
||||
id serial PRIMARY KEY, pokemon_id integer NOT NULL REFERENCES pokemon(id) ON DELETE CASCADE, version_group integer NOT NULL, moves jsonb NOT NULL, UNIQUE(pokemon_id,version_group)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS pokemon_game_research (
|
||||
game_id integer PRIMARY KEY REFERENCES games(id) ON DELETE CASCADE, version_group integer NOT NULL, regions text[] NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS pokemon_journeys (
|
||||
id serial PRIMARY KEY, user_id text NOT NULL REFERENCES "user"(id) ON DELETE CASCADE, game_id integer NOT NULL REFERENCES games(id) ON DELETE CASCADE, stage text NOT NULL DEFAULT 'story' CHECK(stage IN ('story','postgame','collection')), location text, created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now(), UNIQUE(user_id,game_id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS pokemon_catches (
|
||||
id serial PRIMARY KEY, user_id text NOT NULL REFERENCES "user"(id) ON DELETE CASCADE, game_id integer NOT NULL REFERENCES games(id) ON DELETE CASCADE, pokemon_id integer NOT NULL REFERENCES pokemon(id) ON DELETE CASCADE, nickname text NOT NULL DEFAULT '', level integer NOT NULL DEFAULT 1 CHECK(level BETWEEN 1 AND 100), moves text[] NOT NULL DEFAULT '{}' CHECK(cardinality(moves)<=4), party_slot integer CHECK(party_slot BETWEEN 1 AND 6), shiny boolean NOT NULL DEFAULT false, nature text, ability text, ball text, location text, obtained text NOT NULL DEFAULT 'caught', stage text NOT NULL DEFAULT 'story' CHECK(stage IN ('story','postgame','collection')), notes text NOT NULL DEFAULT '', caught_on date, created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now(), UNIQUE(user_id,game_id,party_slot)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS pokemon_milestones (
|
||||
id serial PRIMARY KEY, user_id text NOT NULL REFERENCES "user"(id) ON DELETE CASCADE, game_id integer NOT NULL REFERENCES games(id) ON DELETE CASCADE, key text NOT NULL, title text NOT NULL, category text NOT NULL, stage text NOT NULL DEFAULT 'story', done boolean NOT NULL DEFAULT false, notes text NOT NULL DEFAULT '', created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now(), UNIQUE(user_id,game_id,key)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS tracker_ranks (
|
||||
id serial PRIMARY KEY, user_id text NOT NULL REFERENCES "user"(id) ON DELETE CASCADE, game_id integer NOT NULL REFERENCES tracker_games(id) ON DELETE CASCADE, level integer NOT NULL DEFAULT 1 CHECK(level BETWEEN 1 AND 10000), prestige integer NOT NULL DEFAULT 0 CHECK(prestige BETWEEN 0 AND 1000), created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now(), UNIQUE(user_id,game_id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS tracker_weapon_ranks (
|
||||
id serial PRIMARY KEY, user_id text NOT NULL REFERENCES "user"(id) ON DELETE CASCADE, item_id integer NOT NULL REFERENCES tracker_items(id) ON DELETE CASCADE, level integer NOT NULL DEFAULT 1 CHECK(level BETWEEN 1 AND 10000), prestige integer NOT NULL DEFAULT 0 CHECK(prestige BETWEEN 0 AND 1000), created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now(), UNIQUE(user_id,item_id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS tracker_challenges (
|
||||
id serial PRIMARY KEY, game_id integer NOT NULL REFERENCES tracker_games(id) ON DELETE CASCADE, key text NOT NULL, name text NOT NULL, category text NOT NULL, requirement text NOT NULL, reward text, icon text, target integer, source_url text NOT NULL, UNIQUE(game_id,key)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS tracker_challenge_progress (
|
||||
id serial PRIMARY KEY, user_id text NOT NULL REFERENCES "user"(id) ON DELETE CASCADE, challenge_id integer NOT NULL REFERENCES tracker_challenges(id) ON DELETE CASCADE, current integer NOT NULL DEFAULT 0 CHECK(current >= 0), done boolean NOT NULL DEFAULT false, created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now(), UNIQUE(user_id,challenge_id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS osu_entries (
|
||||
id serial PRIMARY KEY, user_id text NOT NULL REFERENCES "user"(id) ON DELETE CASCADE, kind text NOT NULL CHECK(kind IN ('score','goal','training')), title text NOT NULL, mode text NOT NULL DEFAULT 'osu', beatmap_url text, score_url text, goal text NOT NULL DEFAULT 'pass', skill text NOT NULL DEFAULT 'stamina', bpm integer, duration integer, accuracy text, combo integer, misses integer, pp text, mods text NOT NULL DEFAULT 'NM', rate text NOT NULL DEFAULT '1.00', achieved boolean NOT NULL DEFAULT false, played_on date NOT NULL, notes text NOT NULL DEFAULT '', created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS pokemon_dex_game_idx ON pokemon_dex_numbers(game_id);
|
||||
CREATE INDEX IF NOT EXISTS pokemon_catches_owner_idx ON pokemon_catches(user_id,game_id,pokemon_id);
|
||||
CREATE INDEX IF NOT EXISTS osu_entries_owner_idx ON osu_entries(user_id,kind);
|
||||
|
||||
ALTER TABLE pokemon_journeys ADD COLUMN IF NOT EXISTS completed boolean NOT NULL DEFAULT false;
|
||||
-- Historical catches were necessarily seen; repeatable and non-destructive.
|
||||
INSERT INTO pokemon_seen (user_id, game_id, pokemon_id)
|
||||
SELECT u.user_id, e.game_id, e.pokemon_id FROM user_entries u
|
||||
JOIN pokemon_game_entries e ON e.id = u.pokemon_game_entry_id
|
||||
ON CONFLICT DO NOTHING;
|
||||
@@ -1,23 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
sha=${1:?staging commit required}
|
||||
case "$sha" in
|
||||
*[!0-9a-f]*|'') echo 'Invalid image tag' >&2; exit 2 ;;
|
||||
esac
|
||||
directory=/opt/personal-website-staging
|
||||
tag=$(sed -n 's/^IMAGE_TAG=//p' "$directory/.env" | head -n 1)
|
||||
if [ "$tag" != "$sha" ]; then
|
||||
echo "Staging runs $tag, not $sha" >&2
|
||||
exit 1
|
||||
fi
|
||||
image="git.stanrunge.dev/stan/personal-website:$sha"
|
||||
container=$(sudo docker compose --env-file "$directory/.env" -f "$directory/compose.prod.yml" ps -q app)
|
||||
expected=$(sudo docker image inspect -f '{{.Id}}' "$image")
|
||||
actual=$(sudo docker inspect -f '{{.Image}}' "$container")
|
||||
if [ "$actual" != "$expected" ]; then
|
||||
echo 'Staging container differs from the tested image' >&2
|
||||
exit 1
|
||||
fi
|
||||
curl --fail --silent --show-error https://staging.stanrunge.dev/login >/dev/null
|
||||
echo "Verified staging image $sha"
|
||||
@@ -1,6 +1,8 @@
|
||||
import type { Handle } from '@sveltejs/kit/hooks';
|
||||
import { building } from '$app/env';
|
||||
import { auth } from '#lib/server/auth.js';
|
||||
import { isOwner, isOwnerOnlyPath, isOwnerWritePath } from '#lib/server/owner.js';
|
||||
import { error } from '@sveltejs/kit';
|
||||
|
||||
export const handle: Handle = async ({ event, resolve }) => {
|
||||
// Match the endpoint path here: the dev proxy may terminate HTTPS before Vite.
|
||||
@@ -9,5 +11,20 @@ export const handle: Handle = async ({ event, resolve }) => {
|
||||
const session = building ? null : await auth.api.getSession({ headers: event.request.headers });
|
||||
event.locals.user = session?.user ?? null;
|
||||
event.locals.session = session?.session ?? null;
|
||||
return resolve(event);
|
||||
// Owner-only areas (health tracker, book files, reader and book APIs). Page loads,
|
||||
// __data.json and endpoints 404 for non-owners in the route itself (layout load /
|
||||
// endpoint), which renders the same 404 page as an unknown URL. As a backstop, any other
|
||||
// method (form actions, which skip layout loads) is refused here before a route runs,
|
||||
// including writes to the otherwise public /trackers/books pages.
|
||||
const ownerOnly = isOwnerOnlyPath(event.url.pathname);
|
||||
const owner = isOwner(event);
|
||||
const read = event.request.method === 'GET' || event.request.method === 'HEAD';
|
||||
if (!owner && ((ownerOnly && !read) || (!read && isOwnerWritePath(event.url.pathname))))
|
||||
error(404, 'Not Found');
|
||||
const response = await resolve(event);
|
||||
if (ownerOnly) {
|
||||
response.headers.set('x-robots-tag', 'noindex, nofollow, noarchive');
|
||||
response.headers.set('cache-control', 'private, no-store');
|
||||
}
|
||||
return response;
|
||||
};
|
||||
|
||||
133
src/lib/books/BookCover.svelte
Normal file
133
src/lib/books/BookCover.svelte
Normal file
@@ -0,0 +1,133 @@
|
||||
<script lang="ts">
|
||||
// A book cover: the real image when there is one, otherwise a generated cover in a
|
||||
// colour derived from the title (also used when the image fails to load).
|
||||
import { authorLine, coverHue } from './shared.js';
|
||||
|
||||
let {
|
||||
src,
|
||||
title,
|
||||
authors = [],
|
||||
placeholder = false,
|
||||
size = 'md',
|
||||
class: className = ''
|
||||
}: {
|
||||
src: string | null;
|
||||
title: string;
|
||||
authors?: readonly string[];
|
||||
placeholder?: boolean;
|
||||
size?: 'sm' | 'md' | 'lg';
|
||||
class?: string;
|
||||
} = $props();
|
||||
|
||||
let failed = $state<string | null>(null);
|
||||
const showImage = $derived(!!src && failed !== src);
|
||||
const hue = $derived(coverHue(title));
|
||||
</script>
|
||||
|
||||
<div
|
||||
class={['cover', `cover-${size}`, placeholder && 'is-placeholder', className]}
|
||||
style:--hue={hue}
|
||||
>
|
||||
{#if showImage}
|
||||
<img
|
||||
{src}
|
||||
alt=""
|
||||
loading="lazy"
|
||||
decoding="async"
|
||||
referrerpolicy="no-referrer"
|
||||
onerror={() => (failed = src)}
|
||||
/>
|
||||
{:else}
|
||||
<div class="generated" aria-hidden="true">
|
||||
<span class="g-title">{title}</span>
|
||||
{#if authors.length}<span class="g-author">{authorLine(authors)}</span>{/if}
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.cover {
|
||||
position: relative;
|
||||
aspect-ratio: 2 / 3;
|
||||
width: 100%;
|
||||
overflow: hidden;
|
||||
container-type: inline-size;
|
||||
border-radius: 0.375rem 0.625rem 0.625rem 0.375rem;
|
||||
background: var(--site-hover);
|
||||
box-shadow:
|
||||
0 1px 2px rgb(0 0 0 / 0.25),
|
||||
0 8px 24px -12px rgb(0 0 0 / 0.5);
|
||||
}
|
||||
/* Spine highlight, like a real paperback. */
|
||||
.cover::after {
|
||||
content: '';
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
pointer-events: none;
|
||||
background: linear-gradient(
|
||||
90deg,
|
||||
rgb(0 0 0 / 0.28) 0,
|
||||
rgb(255 255 255 / 0.12) 2.5%,
|
||||
rgb(0 0 0 / 0.08) 5%,
|
||||
transparent 9%
|
||||
);
|
||||
border-radius: inherit;
|
||||
}
|
||||
img {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
object-fit: cover;
|
||||
}
|
||||
.generated {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: space-between;
|
||||
padding: 14% 10% 10% 14%;
|
||||
color: hsl(var(--hue) 30% 96%);
|
||||
background:
|
||||
radial-gradient(
|
||||
120% 80% at 100% 0%,
|
||||
hsl(calc(var(--hue) + 40) 60% 45% / 0.55),
|
||||
transparent 60%
|
||||
),
|
||||
linear-gradient(160deg, hsl(var(--hue) 45% 32%), hsl(calc(var(--hue) + 20) 50% 16%));
|
||||
}
|
||||
.g-title {
|
||||
font-family: Georgia, 'Times New Roman', serif;
|
||||
font-weight: 600;
|
||||
line-height: 1.15;
|
||||
/* Scale with the cover's width so thumbnails don't break words mid-way. */
|
||||
font-size: clamp(0.55rem, 12cqi, 1.6rem);
|
||||
display: -webkit-box;
|
||||
-webkit-line-clamp: 6;
|
||||
line-clamp: 6;
|
||||
-webkit-box-orient: vertical;
|
||||
overflow: hidden;
|
||||
overflow-wrap: break-word;
|
||||
hyphens: auto;
|
||||
}
|
||||
.g-author {
|
||||
font-size: clamp(0.5rem, 6.5cqi, 0.85rem);
|
||||
letter-spacing: 0.04em;
|
||||
text-transform: uppercase;
|
||||
opacity: 0.8;
|
||||
}
|
||||
@container (max-width: 90px) {
|
||||
.g-author {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
.is-placeholder {
|
||||
box-shadow: none;
|
||||
outline: 1.5px dashed color-mix(in srgb, var(--site-muted) 60%, transparent);
|
||||
outline-offset: 2px;
|
||||
}
|
||||
.is-placeholder img,
|
||||
.is-placeholder .generated {
|
||||
filter: grayscale(0.85) opacity(0.55);
|
||||
}
|
||||
</style>
|
||||
68
src/lib/books/Stars.svelte
Normal file
68
src/lib/books/Stars.svelte
Normal file
@@ -0,0 +1,68 @@
|
||||
<script lang="ts">
|
||||
// 1-5 star rating. Read-only by default; with `name` it renders radio buttons for a form.
|
||||
import Star from 'lucide-svelte/icons/star';
|
||||
|
||||
let {
|
||||
value,
|
||||
size = 14,
|
||||
name,
|
||||
onchange
|
||||
}: {
|
||||
value: number | null;
|
||||
size?: number;
|
||||
name?: string;
|
||||
onchange?: (value: number | null) => void;
|
||||
} = $props();
|
||||
|
||||
let hover = $state<number | null>(null);
|
||||
const shown = $derived(hover ?? value ?? 0);
|
||||
</script>
|
||||
|
||||
{#if name || onchange}
|
||||
<span class="stars interactive" role="radiogroup" aria-label="Rating">
|
||||
{#each [1, 2, 3, 4, 5] as n (n)}
|
||||
<button
|
||||
type={name ? 'submit' : 'button'}
|
||||
{name}
|
||||
value={value === n ? '' : String(n)}
|
||||
role="radio"
|
||||
aria-checked={value === n}
|
||||
aria-label={`${n} star${n > 1 ? 's' : ''}${value === n ? ' (click to clear)' : ''}`}
|
||||
onpointerenter={() => (hover = n)}
|
||||
onpointerleave={() => (hover = null)}
|
||||
onclick={() => onchange?.(value === n ? null : n)}
|
||||
>
|
||||
<Star {size} class={n <= shown ? 'on' : 'off'} />
|
||||
</button>
|
||||
{/each}
|
||||
</span>
|
||||
{:else if value}
|
||||
<span class="stars" aria-label={`${value} out of 5 stars`} title={`${value}/5`}>
|
||||
{#each [1, 2, 3, 4, 5] as n (n)}
|
||||
<Star {size} class={n <= value ? 'on' : 'off'} aria-hidden="true" />
|
||||
{/each}
|
||||
</span>
|
||||
{/if}
|
||||
|
||||
<style>
|
||||
.stars {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 1px;
|
||||
}
|
||||
.stars :global(.on) {
|
||||
fill: #f5b301;
|
||||
color: #f5b301;
|
||||
}
|
||||
.stars :global(.off) {
|
||||
color: color-mix(in srgb, var(--site-muted) 50%, transparent);
|
||||
}
|
||||
.interactive button {
|
||||
padding: 2px;
|
||||
border-radius: 4px;
|
||||
line-height: 0;
|
||||
}
|
||||
.interactive button:hover {
|
||||
transform: scale(1.12);
|
||||
}
|
||||
</style>
|
||||
56
src/lib/books/progress.test.ts
Normal file
56
src/lib/books/progress.test.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { pagesRead, parseProgress } from './progress';
|
||||
import { parsePdfLocator, pdfLocator } from './shared';
|
||||
|
||||
describe('progress payload', () => {
|
||||
it('accepts a reader save and rounds the percent', () => {
|
||||
expect(
|
||||
parseProgress({
|
||||
fileId: 3,
|
||||
locator: 'epubcfi(/6/4!/4/2)',
|
||||
percent: 12.34567,
|
||||
sessionId: null
|
||||
})
|
||||
).toMatchObject({ fileId: 3, locator: 'epubcfi(/6/4!/4/2)', percent: 12.35 });
|
||||
});
|
||||
it.each([
|
||||
null,
|
||||
'nope',
|
||||
{},
|
||||
{ percent: -1 },
|
||||
{ percent: 101 },
|
||||
{ percent: 'ten' },
|
||||
{ percent: 10, page: 1.5 },
|
||||
{ percent: 10, fileId: -2 },
|
||||
{ percent: 10, locator: 'x'.repeat(2001) }
|
||||
])('rejects %j', (body) => {
|
||||
expect(parseProgress(body)).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('pages read', () => {
|
||||
it('uses the page delta when pages are known', () => {
|
||||
expect(pagesRead({ startPage: 10, endPage: 34 })).toBe(24);
|
||||
});
|
||||
it('scales the percent delta by the page count', () => {
|
||||
expect(pagesRead({ startPercent: 10, endPercent: 20, pageCount: 300 })).toBe(30);
|
||||
});
|
||||
it('never goes negative and needs enough data', () => {
|
||||
expect(pagesRead({ startPage: 50, endPage: 20 })).toBe(0);
|
||||
expect(pagesRead({ startPercent: 40, endPercent: 30, pageCount: 100 })).toBe(0);
|
||||
expect(pagesRead({ startPercent: 10, endPercent: 20 })).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('pdf locators', () => {
|
||||
it('round-trips pages and rects', () => {
|
||||
const loc = pdfLocator(12, [{ x: 0.1, y: 0.2, w: 0.3333333, h: 0.02 }]);
|
||||
expect(loc).toBe('page:12#r=0.1,0.2,0.3333,0.02');
|
||||
expect(parsePdfLocator(loc)).toEqual({
|
||||
page: 12,
|
||||
rects: [{ x: 0.1, y: 0.2, w: 0.3333, h: 0.02 }]
|
||||
});
|
||||
expect(parsePdfLocator('page:3')).toEqual({ page: 3, rects: [] });
|
||||
expect(parsePdfLocator('epubcfi(/6/2)')).toBeNull();
|
||||
});
|
||||
});
|
||||
41
src/lib/books/progress.ts
Normal file
41
src/lib/books/progress.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
// Validation and maths for the reader's progress API (pure, unit tested).
|
||||
import * as z from 'zod';
|
||||
|
||||
export const progressPayload = z.object({
|
||||
fileId: z.number().int().positive().nullish(),
|
||||
locator: z.string().max(2000).nullish(),
|
||||
page: z.number().int().min(0).max(100000).nullish(),
|
||||
percent: z.number().min(0).max(100),
|
||||
sessionId: z.number().int().positive().nullish(),
|
||||
/** Total pages in the opened file (PDF), used for pages-read on sessions. */
|
||||
pageCount: z.number().int().positive().max(100000).nullish()
|
||||
});
|
||||
export type ProgressPayload = z.infer<typeof progressPayload>;
|
||||
|
||||
export function parseProgress(body: unknown): ProgressPayload | null {
|
||||
const result = progressPayload.safeParse(body);
|
||||
if (!result.success) return null;
|
||||
const value = result.data;
|
||||
return { ...value, percent: Math.round(value.percent * 100) / 100 };
|
||||
}
|
||||
|
||||
/**
|
||||
* Pages read in a session: the page delta when both pages are known (PDF), otherwise the
|
||||
* percent delta scaled by the book's page count. Never negative (going back isn't reading).
|
||||
*/
|
||||
export function pagesRead(input: {
|
||||
startPage?: number | null;
|
||||
endPage?: number | null;
|
||||
startPercent?: number | null;
|
||||
endPercent?: number | null;
|
||||
pageCount?: number | null;
|
||||
}): number | null {
|
||||
const { startPage, endPage, startPercent, endPercent, pageCount } = input;
|
||||
if (startPage != null && endPage != null) return Math.max(0, endPage - startPage);
|
||||
if (startPercent != null && endPercent != null && pageCount)
|
||||
return Math.max(0, Math.round(((endPercent - startPercent) / 100) * pageCount));
|
||||
return null;
|
||||
}
|
||||
|
||||
/** A reader session continues if the last save was less than this long ago. */
|
||||
export const SESSION_GAP_MS = 30 * 60 * 1000;
|
||||
255
src/lib/books/reader/EpubReader.svelte
Normal file
255
src/lib/books/reader/EpubReader.svelte
Normal file
@@ -0,0 +1,255 @@
|
||||
<script lang="ts">
|
||||
// Reflowable books (EPUB, MOBI/AZW3, FB2, CBZ) via the vendored foliate-js.
|
||||
import { onMount, untrack } from 'svelte';
|
||||
import { highlightCss, type Highlight, type HighlightColor } from '#lib/books/shared.js';
|
||||
import { wheelZoom, type ViewLayout } from './layout.js';
|
||||
import {
|
||||
fetchWithProgress,
|
||||
themeColors,
|
||||
type EngineProps,
|
||||
type ReaderSettings,
|
||||
type ReaderTheme,
|
||||
type TocItem
|
||||
} from './types.js';
|
||||
|
||||
type FoliateToc = { label?: string; href?: string; subitems?: FoliateToc[] | null };
|
||||
type Annotation = { value: string; color?: string };
|
||||
type Renderer = HTMLElement & {
|
||||
setStyles(css: string): void;
|
||||
getContents(): { doc: Document; index: number }[];
|
||||
};
|
||||
interface FoliateView extends HTMLElement {
|
||||
book: { toc?: FoliateToc[] };
|
||||
renderer: Renderer;
|
||||
open(file: File): Promise<void>;
|
||||
init(options: { lastLocation?: unknown; showTextStart?: boolean }): Promise<void>;
|
||||
close(): void;
|
||||
goTo(target: string | number): Promise<unknown>;
|
||||
goToFraction(fraction: number): Promise<void>;
|
||||
goLeft(): Promise<void>;
|
||||
goRight(): Promise<void>;
|
||||
addAnnotation(annotation: Annotation): Promise<unknown>;
|
||||
deleteAnnotation(annotation: Annotation): Promise<unknown>;
|
||||
getCFI(index: number, range: Range): string;
|
||||
}
|
||||
type DrawFn = (rects: unknown, options: Record<string, unknown>) => SVGElement;
|
||||
|
||||
let {
|
||||
url,
|
||||
name,
|
||||
start,
|
||||
startPercent,
|
||||
highlights,
|
||||
settings,
|
||||
layout,
|
||||
theme,
|
||||
onzoom,
|
||||
onready,
|
||||
onrelocate,
|
||||
onselection,
|
||||
onhighlightclick,
|
||||
onkey,
|
||||
onloading,
|
||||
onerror
|
||||
}: EngineProps = $props();
|
||||
|
||||
let host = $state<FoliateView>();
|
||||
let view = $state<FoliateView>();
|
||||
let highlightDraw: DrawFn | undefined;
|
||||
// Zoom re-lays out the whole section, so follow fast changes (Ctrl+wheel, pinch) lazily.
|
||||
let zoom = $state(untrack(() => layout.zoom));
|
||||
$effect(() => {
|
||||
const z = layout.zoom;
|
||||
const t = setTimeout(() => (zoom = z), 120);
|
||||
return () => clearTimeout(t);
|
||||
});
|
||||
|
||||
const fonts = {
|
||||
serif: 'Charter, "Iowan Old Style", Georgia, "Palatino Linotype", serif',
|
||||
sans: 'system-ui, -apple-system, "Segoe UI", Roboto, sans-serif'
|
||||
};
|
||||
|
||||
function css(s: ReaderSettings, t: ReaderTheme, z: number) {
|
||||
const c = themeColors[t];
|
||||
const family = s.font === 'publisher' ? null : fonts[s.font];
|
||||
return `
|
||||
@namespace epub "http://www.idpf.org/2007/ops";
|
||||
html { color-scheme: ${t === 'dark' ? 'dark' : 'light'}; background: ${c.bg} !important; color: ${c.fg} !important; font-size: ${Math.round(z * 100)}% !important; }
|
||||
body { background: transparent !important; color: inherit !important; ${family ? `font-family: ${family} !important;` : ''} }
|
||||
${family ? 'body :not(pre, code, kbd, samp, tt, pre *, code *) { font-family: inherit !important; }' : ''}
|
||||
p, li, blockquote, dd, dt { line-height: ${s.lineHeight} !important; }
|
||||
p { hyphens: auto; -webkit-hyphens: auto; widows: 2; orphans: 2; }
|
||||
a:any-link { color: ${c.link} !important; }
|
||||
${t === 'light' ? '' : 'body *:not(a, a *) { color: inherit !important; background-color: transparent !important; border-color: currentColor; }'}
|
||||
img, svg, video { max-width: 100%; height: auto; }
|
||||
aside[epub|type~="footnote"], aside[epub|type~="endnote"] { display: none; }
|
||||
::selection { background: rgba(250, 204, 21, 0.45); }`;
|
||||
}
|
||||
|
||||
// Fixed-layout books (comics, FXL EPUB) use foliate's own page fitting and spreads.
|
||||
const isFixed = (v: FoliateView) => v.renderer?.tagName === 'FOLIATE-FXL';
|
||||
|
||||
function apply(v: FoliateView, l: Pick<ViewLayout, 'mode' | 'spread'>, z: number) {
|
||||
const r = v.renderer;
|
||||
if (!isFixed(v)) {
|
||||
r.setAttribute('flow', l.mode === 'scroll' ? 'scrolled' : 'paginated');
|
||||
r.setAttribute('margin', '48px');
|
||||
r.setAttribute('gap', '6%');
|
||||
// Wider columns as the text grows, so a zoomed spread falls back to one column
|
||||
// instead of squeezing a few words per line.
|
||||
r.setAttribute('max-inline-size', `${Math.round(720 * Math.max(1, z))}px`);
|
||||
r.setAttribute('max-column-count', String(l.spread));
|
||||
}
|
||||
r.setStyles?.(css(settings, theme, z));
|
||||
v.style.background = themeColors[theme].bg;
|
||||
}
|
||||
|
||||
$effect(() => {
|
||||
if (view) apply(view, { mode: layout.mode, spread: layout.spread }, zoom);
|
||||
});
|
||||
|
||||
const color = (h: Highlight) =>
|
||||
highlightCss[(h.color as HighlightColor) ?? 'yellow'] ?? highlightCss.yellow;
|
||||
const toToc = (items: FoliateToc[] = []): TocItem[] =>
|
||||
items
|
||||
.filter((i) => i.href)
|
||||
.map((i) => ({
|
||||
label: (i.label ?? '').trim() || 'Untitled',
|
||||
target: i.href!,
|
||||
children: toToc(i.subitems ?? [])
|
||||
}));
|
||||
|
||||
export async function goTo(target: string | number) {
|
||||
await view?.goTo(target);
|
||||
}
|
||||
export async function goToPercent(percent: number) {
|
||||
await view?.goToFraction(Math.min(0.9999, Math.max(0, percent / 100)));
|
||||
}
|
||||
export function next() {
|
||||
void view?.goRight();
|
||||
}
|
||||
export function prev() {
|
||||
void view?.goLeft();
|
||||
}
|
||||
export function addHighlight(h: Highlight) {
|
||||
if (h.locator) void view?.addAnnotation({ value: h.locator, color: color(h) }).catch(() => {});
|
||||
}
|
||||
export function removeHighlight(h: Highlight) {
|
||||
if (h.locator) void view?.deleteAnnotation({ value: h.locator }).catch(() => {});
|
||||
}
|
||||
export function clearSelection() {
|
||||
for (const { doc } of view?.renderer.getContents() ?? []) doc.getSelection()?.removeAllRanges();
|
||||
}
|
||||
|
||||
onMount(() => {
|
||||
const abort = new AbortController();
|
||||
let disposed = false;
|
||||
let el: FoliateView | undefined;
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
const base = '/vendor/foliate-js';
|
||||
const [, overlayer] = await Promise.all([
|
||||
import(/* @vite-ignore */ `${base}/view.js`),
|
||||
import(/* @vite-ignore */ `${base}/overlayer.js`)
|
||||
]);
|
||||
highlightDraw = overlayer.Overlayer.highlight;
|
||||
onloading(0);
|
||||
const blob = await fetchWithProgress(url, onloading, abort.signal);
|
||||
if (disposed) return;
|
||||
onloading(null);
|
||||
|
||||
el = host;
|
||||
if (!el) return;
|
||||
|
||||
el.addEventListener('relocate', (e) => {
|
||||
const d = (e as CustomEvent).detail;
|
||||
onrelocate({
|
||||
percent: Math.max(0, Math.min(100, (d.fraction ?? 0) * 100)),
|
||||
locator: d.cfi,
|
||||
page: null,
|
||||
pageCount: null,
|
||||
label: d.tocItem?.label?.trim() || null
|
||||
});
|
||||
});
|
||||
el.addEventListener('load', (e) => {
|
||||
const { doc, index } = (e as CustomEvent).detail as { doc: Document; index: number };
|
||||
doc.addEventListener('keydown', onkey);
|
||||
// Ctrl+wheel / trackpad pinch inside the book text zooms instead of the page.
|
||||
doc.addEventListener(
|
||||
'wheel',
|
||||
(we) => {
|
||||
if (!we.ctrlKey && !we.metaKey) return;
|
||||
we.preventDefault();
|
||||
onzoom(wheelZoom(layout.zoom, we.deltaY, 'foliate'));
|
||||
},
|
||||
{ passive: false }
|
||||
);
|
||||
let timer: ReturnType<typeof setTimeout>;
|
||||
doc.addEventListener('selectionchange', () => {
|
||||
clearTimeout(timer);
|
||||
timer = setTimeout(() => {
|
||||
const sel = doc.getSelection();
|
||||
const text = sel?.toString().trim();
|
||||
if (!sel || sel.isCollapsed || !sel.rangeCount || !text) return onselection(null);
|
||||
try {
|
||||
onselection({ text, locator: el!.getCFI(index, sel.getRangeAt(0)), page: null });
|
||||
} catch {
|
||||
onselection(null);
|
||||
}
|
||||
}, 250);
|
||||
});
|
||||
});
|
||||
el.addEventListener('create-overlay', () => {
|
||||
for (const h of highlights) addHighlight(h);
|
||||
});
|
||||
el.addEventListener('draw-annotation', (e) => {
|
||||
const { draw, annotation } = (e as CustomEvent).detail as {
|
||||
draw: (fn: DrawFn, opts: Record<string, unknown>) => void;
|
||||
annotation: Annotation;
|
||||
};
|
||||
if (highlightDraw)
|
||||
draw(highlightDraw, { color: annotation.color ?? highlightCss.yellow });
|
||||
});
|
||||
el.addEventListener('show-annotation', (e) => {
|
||||
const value = (e as CustomEvent).detail.value as string;
|
||||
const h = highlights.find((x) => x.locator === value);
|
||||
if (h) onhighlightclick(h.id);
|
||||
});
|
||||
|
||||
await el.open(new File([blob], name, { type: blob.type }));
|
||||
if (disposed) return;
|
||||
apply(el, layout, layout.zoom);
|
||||
view = el;
|
||||
const lastLocation = start ?? (startPercent > 0 ? { fraction: startPercent / 100 } : null);
|
||||
await el.init({ lastLocation, showTextStart: !lastLocation });
|
||||
onready(toToc(el.book.toc), { fixedLayout: isFixed(el) });
|
||||
} catch (e) {
|
||||
if (!disposed && !(e instanceof DOMException && e.name === 'AbortError'))
|
||||
onerror(e instanceof Error ? e.message : 'Could not open this book');
|
||||
}
|
||||
})();
|
||||
|
||||
return () => {
|
||||
disposed = true;
|
||||
abort.abort();
|
||||
el?.close?.();
|
||||
};
|
||||
});
|
||||
</script>
|
||||
|
||||
<div class="host">
|
||||
<foliate-view bind:this={host}></foliate-view>
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.host {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
}
|
||||
.host :global(foliate-view) {
|
||||
display: block;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
}
|
||||
</style>
|
||||
687
src/lib/books/reader/PdfReader.svelte
Normal file
687
src/lib/books/reader/PdfReader.svelte
Normal file
@@ -0,0 +1,687 @@
|
||||
<script lang="ts">
|
||||
// PDFs via pdf.js: continuous scroll or one page/spread at a time, 1 or 2 pages per row,
|
||||
// pages rendered lazily near the viewport, a text layer for selection, and highlights stored
|
||||
// as page-relative rectangles.
|
||||
import { onMount, tick, untrack } from 'svelte';
|
||||
import workerUrl from 'pdfjs-dist/build/pdf.worker.min.mjs?url';
|
||||
import type { PDFDocumentProxy, PDFPageProxy, RenderTask } from 'pdfjs-dist';
|
||||
import {
|
||||
highlightCss,
|
||||
parsePdfLocator,
|
||||
pdfLocator,
|
||||
type Highlight,
|
||||
type HighlightColor,
|
||||
type Rect
|
||||
} from '#lib/books/shared.js';
|
||||
import { clampZoom, pdfRows, rowIndexOf, type Spread } from './layout.js';
|
||||
import { closePdf } from './pdf.js';
|
||||
import { type EngineProps, type TocItem } from './types.js';
|
||||
|
||||
let {
|
||||
url,
|
||||
start,
|
||||
startPercent,
|
||||
highlights,
|
||||
layout,
|
||||
theme,
|
||||
onzoom,
|
||||
onready,
|
||||
onrelocate,
|
||||
onselection,
|
||||
onhighlightclick,
|
||||
onloading,
|
||||
onerror
|
||||
}: EngineProps = $props();
|
||||
|
||||
type PageInfo = { num: number; width: number; height: number }; // at scale 1
|
||||
type Live = { task?: RenderTask; el: HTMLElement; layer: HTMLElement; scale: number };
|
||||
const GAP = 12;
|
||||
|
||||
let scroller: HTMLDivElement;
|
||||
let pdf: PDFDocumentProxy | undefined;
|
||||
let pdfjs: typeof import('pdfjs-dist') | undefined;
|
||||
let pages = $state<PageInfo[]>([]);
|
||||
let width = $state(0);
|
||||
let height = $state(0);
|
||||
let current = 1;
|
||||
let currentRow = $state(0); // paged mode: the row (page or spread) on screen
|
||||
// eslint-disable-next-line svelte/prefer-svelte-reactivity -- plain cache, not reactive state
|
||||
const live = new Map<number, Live>();
|
||||
// eslint-disable-next-line svelte/prefer-svelte-reactivity -- plain cache, not reactive state
|
||||
const pageEls = new Map<number, HTMLElement>();
|
||||
let observer: IntersectionObserver | undefined;
|
||||
|
||||
const paged = $derived(layout.mode === 'paged');
|
||||
// Two pages side by side only when there's room for them.
|
||||
const spread = $derived<Spread>(layout.spread === 2 && width >= 640 ? 2 : 1);
|
||||
const rows = $derived(pdfRows(pages.length, spread));
|
||||
const shownRows = $derived(paged ? rows.slice(currentRow, currentRow + 1) : rows);
|
||||
|
||||
// Zoom 1 = fit the row's width (scroll) or the whole page/spread (paged).
|
||||
const scale = $derived.by(() => {
|
||||
const first = pages[0];
|
||||
if (!first || !width) return 1;
|
||||
const avail = width - 2 * GAP - (spread - 1) * GAP;
|
||||
let fit = Math.min(avail, 920 * spread) / (first.width * spread);
|
||||
if (paged && height > 2 * GAP) fit = Math.min(fit, (height - 2 * GAP) / first.height);
|
||||
return Math.max(0.2, fit * layout.zoom);
|
||||
});
|
||||
|
||||
const byPage = $derived.by(() => {
|
||||
// eslint-disable-next-line svelte/prefer-svelte-reactivity -- plain cache, not reactive state
|
||||
const map = new Map<number, { h: Highlight; rects: Rect[] }[]>();
|
||||
for (const h of highlights) {
|
||||
const loc = parsePdfLocator(h.locator);
|
||||
if (!loc || !loc.rects.length) continue;
|
||||
const list = map.get(loc.page) ?? [];
|
||||
list.push({ h, rects: loc.rects });
|
||||
map.set(loc.page, list);
|
||||
}
|
||||
return map;
|
||||
});
|
||||
const color = (h: Highlight) =>
|
||||
highlightCss[(h.color as HighlightColor) ?? 'yellow'] ?? highlightCss.yellow;
|
||||
|
||||
async function render(num: number) {
|
||||
const el = pageEls.get(num);
|
||||
if (!pdf || !pdfjs || !el || live.has(num)) return;
|
||||
const layer = el.querySelector<HTMLElement>('.layer')!;
|
||||
const entry: Live = { el, layer, scale };
|
||||
live.set(num, entry);
|
||||
const page: PDFPageProxy = await pdf.getPage(num);
|
||||
if (live.get(num) !== entry) return;
|
||||
const s = scale;
|
||||
entry.scale = s;
|
||||
const viewport = page.getViewport({ scale: s });
|
||||
const dpr = Math.min(window.devicePixelRatio || 1, 2);
|
||||
const canvas = document.createElement('canvas');
|
||||
canvas.width = Math.floor(viewport.width * dpr);
|
||||
canvas.height = Math.floor(viewport.height * dpr);
|
||||
const text = document.createElement('div');
|
||||
text.className = 'textLayer';
|
||||
layer.replaceChildren(canvas, text);
|
||||
entry.task = page.render({
|
||||
canvas,
|
||||
viewport,
|
||||
transform: dpr !== 1 ? [dpr, 0, 0, dpr, 0, 0] : undefined
|
||||
});
|
||||
try {
|
||||
await entry.task.promise;
|
||||
if (live.get(num) !== entry) return;
|
||||
await new pdfjs.TextLayer({
|
||||
textContentSource: page.streamTextContent(),
|
||||
container: text,
|
||||
viewport
|
||||
}).render();
|
||||
} catch {
|
||||
// cancelled (scrolled away or re-zoomed)
|
||||
}
|
||||
}
|
||||
|
||||
function unrender(num: number) {
|
||||
const entry = live.get(num);
|
||||
if (!entry) return;
|
||||
entry.task?.cancel();
|
||||
entry.layer.replaceChildren();
|
||||
live.delete(num);
|
||||
}
|
||||
|
||||
function page(node: HTMLElement, num: number) {
|
||||
pageEls.set(num, node);
|
||||
observer?.observe(node);
|
||||
return {
|
||||
destroy() {
|
||||
observer?.unobserve(node);
|
||||
pageEls.delete(num);
|
||||
unrender(num);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// When the zoom, width, spread or mode changes: keep the reading position, resize the page
|
||||
// boxes right away (old canvases stretch meanwhile) and re-render sharp once it settles.
|
||||
const layoutKey = $derived(`${layout.mode}|${spread}|${scale.toFixed(4)}`);
|
||||
let lastKey = '';
|
||||
let keep: { page: number; fraction: number } | null = null;
|
||||
$effect.pre(() => {
|
||||
const key = layoutKey;
|
||||
untrack(() => {
|
||||
// The DOM still has the old layout here, so this reads the position the reader sees.
|
||||
if (lastKey && key !== lastKey && pages.length) keep ??= position();
|
||||
});
|
||||
});
|
||||
$effect(() => {
|
||||
const key = layoutKey;
|
||||
untrack(() => {
|
||||
if (!pages.length || !width) return void (keep = null);
|
||||
// The first layout with pages is the baseline; the initial jump to the saved position
|
||||
// happens in onMount and must not be undone by a "keep position" here.
|
||||
if (!lastKey || key === lastKey) return void ((lastKey = key), (keep = null));
|
||||
lastKey = key;
|
||||
const k = keep;
|
||||
keep = null;
|
||||
if (k) {
|
||||
if (paged) currentRow = rowIndexOf(rows, k.page);
|
||||
void tick().then(() => {
|
||||
place(k.page, k.fraction);
|
||||
onscroll();
|
||||
});
|
||||
}
|
||||
scheduleRerender();
|
||||
});
|
||||
});
|
||||
|
||||
let rerenderTimer: ReturnType<typeof setTimeout> | undefined;
|
||||
function scheduleRerender() {
|
||||
clearTimeout(rerenderTimer);
|
||||
rerenderTimer = setTimeout(() => {
|
||||
for (const [num, entry] of [...live])
|
||||
if (Math.abs(entry.scale - scale) > 0.001) unrender(num);
|
||||
if (!scroller) return;
|
||||
const v = scroller.getBoundingClientRect();
|
||||
for (const [num, node] of pageEls) {
|
||||
const r = node.getBoundingClientRect();
|
||||
if (r.bottom > v.top - v.height && r.top < v.bottom + v.height) void render(num);
|
||||
}
|
||||
}, 160);
|
||||
}
|
||||
|
||||
function position() {
|
||||
if (!scroller) return { page: 1, fraction: 0 };
|
||||
const anchor = scroller.scrollTop + scroller.clientHeight * 0.3;
|
||||
for (const p of pages) {
|
||||
const node = pageEls.get(p.num);
|
||||
if (!node) continue;
|
||||
const top = node.offsetTop;
|
||||
const bottom = top + node.offsetHeight;
|
||||
if (anchor < bottom + 8)
|
||||
return {
|
||||
page: p.num,
|
||||
fraction: Math.max(0, Math.min(1, (anchor - top) / node.offsetHeight))
|
||||
};
|
||||
}
|
||||
return { page: pages.length, fraction: 1 };
|
||||
}
|
||||
|
||||
/** Scroll so `num` (at `fraction` down the page) is in view; it must be in the DOM. */
|
||||
function place(num: number, fraction = 0) {
|
||||
const node = pageEls.get(num);
|
||||
if (!node || !scroller) return;
|
||||
scroller.scrollTop =
|
||||
node.offsetTop +
|
||||
node.offsetHeight * fraction -
|
||||
(fraction ? scroller.clientHeight * 0.3 : GAP);
|
||||
}
|
||||
|
||||
function scrollToPosition(num: number, fraction = 0) {
|
||||
if (!pages.length) return;
|
||||
num = Math.max(1, Math.min(pages.length, num));
|
||||
if (paged) {
|
||||
const r = rowIndexOf(rows, num);
|
||||
if (r !== currentRow) {
|
||||
currentRow = r;
|
||||
void tick().then(() => {
|
||||
place(num, fraction);
|
||||
onscroll();
|
||||
});
|
||||
return;
|
||||
}
|
||||
place(num, fraction);
|
||||
onscroll(); // the scroll position may not change between rows
|
||||
return;
|
||||
}
|
||||
place(num, fraction);
|
||||
}
|
||||
|
||||
function showRow(i: number) {
|
||||
const row = rows[Math.max(0, Math.min(rows.length - 1, i))];
|
||||
if (row) scrollToPosition(row[0]);
|
||||
}
|
||||
|
||||
let frame = 0;
|
||||
function onscroll() {
|
||||
cancelAnimationFrame(frame);
|
||||
frame = requestAnimationFrame(() => {
|
||||
if (!pages.length) return;
|
||||
const { page: num, fraction } = position();
|
||||
current = num;
|
||||
const row = rows[rowIndexOf(rows, num)];
|
||||
onrelocate({
|
||||
percent: Math.min(100, ((num - 1 + fraction) / pages.length) * 100),
|
||||
locator: pdfLocator(num),
|
||||
page: num,
|
||||
pageEnd: row && row.length > 1 ? row[row.length - 1] : null,
|
||||
pageCount: pages.length,
|
||||
label: labelFor(num)
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
let flatToc: { page: number; label: string }[] = [];
|
||||
const labelFor = (num: number) => {
|
||||
let label: string | null = null;
|
||||
for (const item of flatToc) if (item.page <= num) label = item.label;
|
||||
return label;
|
||||
};
|
||||
|
||||
export async function goTo(target: string | number) {
|
||||
if (typeof target === 'number') return scrollToPosition(target);
|
||||
const loc = parsePdfLocator(target);
|
||||
if (!loc) return;
|
||||
const y = loc.rects[0]?.y;
|
||||
if (y != null) scrollToPosition(loc.page, Math.max(0, y - 0.05) || 0.0001);
|
||||
else scrollToPosition(loc.page);
|
||||
}
|
||||
export async function goToPercent(percent: number) {
|
||||
const p = (percent / 100) * pages.length;
|
||||
scrollToPosition(Math.floor(p) + 1, p % 1 || 0);
|
||||
}
|
||||
// Next/prev move a whole row: one page, or two in spread mode.
|
||||
export function next() {
|
||||
if (paged) return showRow(currentRow + 1);
|
||||
showRow(rowIndexOf(rows, current) + 1);
|
||||
}
|
||||
export function prev() {
|
||||
if (paged) return showRow(currentRow - 1);
|
||||
// Scrolled into the current row: back to its top first, else the previous row.
|
||||
const r = rowIndexOf(rows, current);
|
||||
const node = pageEls.get(rows[r]?.[0] ?? 1);
|
||||
const into = node && scroller ? scroller.scrollTop - (node.offsetTop - GAP) : 0;
|
||||
showRow(into > 24 ? r : r - 1);
|
||||
}
|
||||
export function addHighlight() {
|
||||
// Highlights are derived from props; nothing to draw imperatively.
|
||||
}
|
||||
export function removeHighlight() {}
|
||||
export function clearSelection() {
|
||||
document.getSelection()?.removeAllRanges();
|
||||
}
|
||||
|
||||
/** Selection rects as fractions of the page, merged per line. */
|
||||
function selectionRects(range: Range, pageEl: HTMLElement): Rect[] {
|
||||
const box = pageEl.getBoundingClientRect();
|
||||
const rects = [...range.getClientRects()]
|
||||
.filter((r) => r.width > 1 && r.height > 1)
|
||||
.map((r) => ({
|
||||
x: (r.left - box.left) / box.width,
|
||||
y: (r.top - box.top) / box.height,
|
||||
w: r.width / box.width,
|
||||
h: r.height / box.height
|
||||
}))
|
||||
.filter((r) => r.x >= -0.01 && r.y >= -0.01 && r.x < 1 && r.y < 1)
|
||||
.sort((a, b) => a.y - b.y || a.x - b.x);
|
||||
const merged: Rect[] = [];
|
||||
for (const r of rects) {
|
||||
const last = merged.at(-1);
|
||||
if (last && Math.abs(last.y - r.y) < last.h * 0.5 && r.x <= last.x + last.w + 0.02) {
|
||||
const right = Math.max(last.x + last.w, r.x + r.w);
|
||||
const bottom = Math.max(last.y + last.h, r.y + r.h);
|
||||
last.x = Math.min(last.x, r.x);
|
||||
last.y = Math.min(last.y, r.y);
|
||||
last.w = right - last.x;
|
||||
last.h = bottom - last.y;
|
||||
} else merged.push({ ...r });
|
||||
}
|
||||
return merged.slice(0, 60);
|
||||
}
|
||||
|
||||
function hitHighlight(e: MouseEvent, num: number) {
|
||||
if (!document.getSelection()?.isCollapsed) return;
|
||||
const box = (e.currentTarget as HTMLElement).getBoundingClientRect();
|
||||
const x = (e.clientX - box.left) / box.width;
|
||||
const y = (e.clientY - box.top) / box.height;
|
||||
const hit = byPage
|
||||
.get(num)
|
||||
?.find(({ rects }) =>
|
||||
rects.some((r) => x >= r.x && x <= r.x + r.w && y >= r.y && y <= r.y + r.h)
|
||||
);
|
||||
if (hit) onhighlightclick(hit.h.id);
|
||||
}
|
||||
|
||||
async function resolveToc(doc: PDFDocumentProxy): Promise<TocItem[]> {
|
||||
const outline = await doc.getOutline().catch(() => null);
|
||||
if (!outline) return [];
|
||||
let budget = 600;
|
||||
const walk = async (items: typeof outline): Promise<TocItem[]> => {
|
||||
const out: TocItem[] = [];
|
||||
for (const item of items) {
|
||||
if (budget-- <= 0) break;
|
||||
let num: number | null = null;
|
||||
try {
|
||||
const dest =
|
||||
typeof item.dest === 'string' ? await doc.getDestination(item.dest) : item.dest;
|
||||
const ref = dest?.[0];
|
||||
if (ref && typeof ref === 'object') num = (await doc.getPageIndex(ref)) + 1;
|
||||
else if (typeof ref === 'number') num = ref + 1;
|
||||
} catch {
|
||||
num = null;
|
||||
}
|
||||
const children = item.items?.length ? await walk(item.items) : [];
|
||||
if (num) {
|
||||
out.push({ label: item.title.trim() || `Page ${num}`, target: num, children });
|
||||
flatToc.push({ page: num, label: item.title.trim() });
|
||||
} else out.push(...children);
|
||||
}
|
||||
return out;
|
||||
};
|
||||
const toc = await walk(outline);
|
||||
flatToc = flatToc.sort((a, b) => a.page - b.page);
|
||||
return toc;
|
||||
}
|
||||
|
||||
onMount(() => {
|
||||
let disposed = false;
|
||||
let loadingTask: ReturnType<typeof import('pdfjs-dist').getDocument> | undefined;
|
||||
const measure = () => {
|
||||
width = scroller.clientWidth;
|
||||
height = scroller.clientHeight;
|
||||
};
|
||||
const resize = new ResizeObserver(measure);
|
||||
resize.observe(scroller);
|
||||
measure();
|
||||
|
||||
// Ctrl+wheel / trackpad pinch is handled by the shell; here: wheel turns pages in paged
|
||||
// mode when the page fits, and two-finger pinch zooms on touch screens.
|
||||
let wheelAcc = 0;
|
||||
let wheelLock = 0;
|
||||
const onWheel = (e: WheelEvent) => {
|
||||
if (e.ctrlKey || e.metaKey || !paged) return;
|
||||
if (scroller.scrollHeight > scroller.clientHeight + 1) return;
|
||||
e.preventDefault();
|
||||
if (Date.now() < wheelLock) return;
|
||||
wheelAcc += e.deltaY;
|
||||
if (Math.abs(wheelAcc) < 60) return;
|
||||
if (wheelAcc > 0) next();
|
||||
else prev();
|
||||
wheelAcc = 0;
|
||||
wheelLock = Date.now() + 350;
|
||||
};
|
||||
let pinch: { d: number; z: number } | null = null;
|
||||
const dist = (t: TouchList) =>
|
||||
Math.hypot(t[0].clientX - t[1].clientX, t[0].clientY - t[1].clientY) || 1;
|
||||
const onTouchStart = (e: TouchEvent) => {
|
||||
pinch = e.touches.length === 2 ? { d: dist(e.touches), z: layout.zoom } : null;
|
||||
};
|
||||
const onTouchMove = (e: TouchEvent) => {
|
||||
if (!pinch || e.touches.length !== 2) return;
|
||||
e.preventDefault();
|
||||
const z = clampZoom((pinch.z * dist(e.touches)) / pinch.d, 'pdf');
|
||||
if (Math.abs(z - layout.zoom) > 0.01) onzoom(z);
|
||||
};
|
||||
const onTouchEnd = (e: TouchEvent) => {
|
||||
if (e.touches.length < 2) pinch = null;
|
||||
};
|
||||
scroller.addEventListener('wheel', onWheel, { passive: false });
|
||||
scroller.addEventListener('touchstart', onTouchStart, { passive: true });
|
||||
scroller.addEventListener('touchmove', onTouchMove, { passive: false });
|
||||
scroller.addEventListener('touchend', onTouchEnd);
|
||||
scroller.addEventListener('touchcancel', onTouchEnd);
|
||||
observer = new IntersectionObserver(
|
||||
(entries) => {
|
||||
for (const entry of entries) {
|
||||
const num = Number((entry.target as HTMLElement).dataset.page);
|
||||
if (entry.isIntersecting) void render(num);
|
||||
else unrender(num);
|
||||
}
|
||||
},
|
||||
{ root: scroller, rootMargin: '150% 0px' }
|
||||
);
|
||||
|
||||
let selTimer: ReturnType<typeof setTimeout>;
|
||||
const onSelectionChange = () => {
|
||||
clearTimeout(selTimer);
|
||||
selTimer = setTimeout(() => {
|
||||
const sel = document.getSelection();
|
||||
const text = sel?.toString().trim();
|
||||
const node = sel?.anchorNode;
|
||||
const pageEl = (node instanceof Element ? node : node?.parentElement)?.closest<HTMLElement>(
|
||||
'.pdf-page'
|
||||
);
|
||||
if (!sel || sel.isCollapsed || !text || !pageEl || !scroller.contains(pageEl))
|
||||
return onselection(null);
|
||||
const num = Number(pageEl.dataset.page);
|
||||
const rects = selectionRects(sel.getRangeAt(0), pageEl);
|
||||
if (!rects.length) return onselection(null);
|
||||
onselection({ text, locator: pdfLocator(num, rects), page: num });
|
||||
}, 250);
|
||||
};
|
||||
document.addEventListener('selectionchange', onSelectionChange);
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
pdfjs = await import('pdfjs-dist');
|
||||
pdfjs.GlobalWorkerOptions.workerSrc = workerUrl;
|
||||
onloading(0);
|
||||
const task = (loadingTask = pdfjs.getDocument({
|
||||
url,
|
||||
withCredentials: true,
|
||||
rangeChunkSize: 1 << 20
|
||||
}));
|
||||
task.onProgress = ({ loaded, total }: { loaded: number; total: number }) =>
|
||||
onloading(total ? Math.min(1, loaded / total) : null);
|
||||
const doc = await task.promise;
|
||||
if (disposed) return void closePdf(task);
|
||||
pdf = doc;
|
||||
onloading(null);
|
||||
// Use the first page's size for every page up front; real sizes fill in quickly.
|
||||
const first = (await doc.getPage(1)).getViewport({ scale: 1 });
|
||||
pages = Array.from({ length: doc.numPages }, (_, i) => ({
|
||||
num: i + 1,
|
||||
width: first.width,
|
||||
height: first.height
|
||||
}));
|
||||
void (async () => {
|
||||
for (let i = 2; i <= Math.min(doc.numPages, 2000) && !disposed; i++) {
|
||||
const vp = (await doc.getPage(i)).getViewport({ scale: 1 });
|
||||
if (vp.width !== first.width || vp.height !== first.height)
|
||||
pages[i - 1] = { num: i, width: vp.width, height: vp.height };
|
||||
}
|
||||
})();
|
||||
requestAnimationFrame(async () => {
|
||||
const loc = parsePdfLocator(start);
|
||||
if (loc) scrollToPosition(loc.page, loc.rects[0]?.y ?? 0);
|
||||
else if (startPercent > 0) await goToPercent(startPercent);
|
||||
onscroll();
|
||||
onready(await resolveToc(doc));
|
||||
});
|
||||
} catch (e) {
|
||||
if (!disposed) onerror(e instanceof Error ? e.message : 'Could not open this PDF');
|
||||
}
|
||||
})();
|
||||
|
||||
return () => {
|
||||
disposed = true;
|
||||
document.removeEventListener('selectionchange', onSelectionChange);
|
||||
scroller.removeEventListener('wheel', onWheel);
|
||||
scroller.removeEventListener('touchstart', onTouchStart);
|
||||
scroller.removeEventListener('touchmove', onTouchMove);
|
||||
scroller.removeEventListener('touchend', onTouchEnd);
|
||||
scroller.removeEventListener('touchcancel', onTouchEnd);
|
||||
clearTimeout(rerenderTimer);
|
||||
resize.disconnect();
|
||||
observer?.disconnect();
|
||||
for (const num of [...live.keys()]) unrender(num);
|
||||
// pdf.js 6 has no PDFDocumentProxy.destroy(); calling it threw on every unmount.
|
||||
void closePdf(loadingTask);
|
||||
};
|
||||
});
|
||||
</script>
|
||||
|
||||
<div
|
||||
class={['scroller', `t-${theme}`]}
|
||||
style:background={theme === 'light' ? '#e9e9e7' : theme === 'sepia' ? '#e6dcc4' : '#0b0b0c'}
|
||||
bind:this={scroller}
|
||||
{onscroll}
|
||||
>
|
||||
<div class={['pages', paged && 'paged', spread === 2 && 'spread']}>
|
||||
{#each shownRows as row (row[0])}
|
||||
<div class="row">
|
||||
{#each row as num (num)}
|
||||
{@const p = pages[num - 1]}
|
||||
<!-- svelte-ignore a11y_click_events_have_key_events, a11y_no_static_element_interactions -->
|
||||
<div
|
||||
class="pdf-page"
|
||||
data-page={p.num}
|
||||
style:width={`${Math.floor(p.width * scale)}px`}
|
||||
style:height={`${Math.floor(p.height * scale)}px`}
|
||||
style:--scale-factor={scale}
|
||||
style:--total-scale-factor={scale}
|
||||
use:page={p.num}
|
||||
onclick={(e) => hitHighlight(e, p.num)}
|
||||
>
|
||||
<div class="layer"></div>
|
||||
{#each byPage.get(p.num) ?? [] as { h, rects } (h.id)}
|
||||
{#each rects as r, i (i)}
|
||||
<span
|
||||
class="hl"
|
||||
style:left={`${r.x * 100}%`}
|
||||
style:top={`${r.y * 100}%`}
|
||||
style:width={`${r.w * 100}%`}
|
||||
style:height={`${r.h * 100}%`}
|
||||
style:background={color(h)}
|
||||
></span>
|
||||
{/each}
|
||||
{/each}
|
||||
<span class="num">{p.num}</span>
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.scroller {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
overflow: auto;
|
||||
overscroll-behavior: contain;
|
||||
}
|
||||
.pages {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
padding: 12px 12px 96px;
|
||||
min-width: fit-content;
|
||||
}
|
||||
.pages.paged {
|
||||
min-height: 100%;
|
||||
justify-content: center;
|
||||
padding: 12px;
|
||||
}
|
||||
/* rows are not positioned, so page offsetTop stays relative to the scroller */
|
||||
.row {
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
justify-content: center;
|
||||
gap: 12px;
|
||||
}
|
||||
.spread .num {
|
||||
display: none;
|
||||
}
|
||||
.pdf-page {
|
||||
position: relative;
|
||||
background: white;
|
||||
box-shadow:
|
||||
0 1px 3px rgb(0 0 0 / 0.12),
|
||||
0 8px 24px rgb(0 0 0 / 0.08);
|
||||
flex: none;
|
||||
}
|
||||
.t-sepia .pdf-page :global(canvas) {
|
||||
filter: sepia(0.35) brightness(0.97);
|
||||
}
|
||||
.t-dark .pdf-page {
|
||||
background: #1a1a1c;
|
||||
}
|
||||
.t-dark .pdf-page :global(canvas) {
|
||||
filter: invert(0.9) hue-rotate(180deg);
|
||||
}
|
||||
.layer,
|
||||
.layer :global(canvas) {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
}
|
||||
.hl {
|
||||
position: absolute;
|
||||
mix-blend-mode: multiply;
|
||||
opacity: 0.45;
|
||||
border-radius: 2px;
|
||||
pointer-events: none;
|
||||
z-index: 1;
|
||||
}
|
||||
.t-dark .hl {
|
||||
mix-blend-mode: screen;
|
||||
opacity: 0.35;
|
||||
}
|
||||
.num {
|
||||
position: absolute;
|
||||
right: -2.25rem;
|
||||
bottom: 0;
|
||||
font-size: 0.7rem;
|
||||
color: rgb(128 128 128 / 0.8);
|
||||
font-variant-numeric: tabular-nums;
|
||||
}
|
||||
@media (max-width: 1100px) {
|
||||
.num {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
|
||||
/* pdf.js text layer (from pdfjs-dist/web/pdf_viewer.css, trimmed) */
|
||||
.pdf-page :global(.textLayer) {
|
||||
position: absolute;
|
||||
text-align: initial;
|
||||
inset: 0;
|
||||
overflow: clip;
|
||||
opacity: 1;
|
||||
line-height: 1;
|
||||
text-size-adjust: none;
|
||||
forced-color-adjust: none;
|
||||
transform-origin: 0 0;
|
||||
caret-color: CanvasText;
|
||||
z-index: 2;
|
||||
--min-font-size: 1;
|
||||
--text-scale-factor: calc(var(--total-scale-factor) * var(--min-font-size));
|
||||
--min-font-size-inv: calc(1 / var(--min-font-size));
|
||||
--scale-round-x: 1px;
|
||||
--scale-round-y: 1px;
|
||||
}
|
||||
.pdf-page :global(.textLayer :is(span, br)) {
|
||||
color: transparent;
|
||||
position: absolute;
|
||||
white-space: pre;
|
||||
cursor: text;
|
||||
transform-origin: 0% 0%;
|
||||
user-select: text;
|
||||
}
|
||||
.pdf-page :global(.textLayer > :not(.markedContent)),
|
||||
.pdf-page :global(.textLayer .markedContent span:not(.markedContent)) {
|
||||
z-index: 1;
|
||||
--font-height: 0;
|
||||
font-size: calc(var(--text-scale-factor) * var(--font-height));
|
||||
--scale-x: 1;
|
||||
--rotate: 0deg;
|
||||
transform: rotate(var(--rotate)) scaleX(var(--scale-x)) scale(var(--min-font-size-inv));
|
||||
}
|
||||
.pdf-page :global(.textLayer .markedContent) {
|
||||
display: contents;
|
||||
}
|
||||
.pdf-page :global(.textLayer ::selection) {
|
||||
background: rgb(250 204 21 / 0.4);
|
||||
color: transparent;
|
||||
}
|
||||
.pdf-page :global(.textLayer br::selection) {
|
||||
background: transparent;
|
||||
}
|
||||
.pdf-page :global(.textLayer .endOfContent) {
|
||||
display: block;
|
||||
position: absolute;
|
||||
inset: 100% 0 0;
|
||||
z-index: 0;
|
||||
cursor: default;
|
||||
user-select: none;
|
||||
}
|
||||
</style>
|
||||
111
src/lib/books/reader/layout.test.ts
Normal file
111
src/lib/books/reader/layout.test.ts
Normal file
@@ -0,0 +1,111 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import {
|
||||
clampZoom,
|
||||
defaultLayouts,
|
||||
loadLayout,
|
||||
normalizeLayout,
|
||||
pdfRows,
|
||||
rowIndexOf,
|
||||
saveLayout,
|
||||
stepZoom,
|
||||
wheelZoom
|
||||
} from './layout.js';
|
||||
|
||||
function memoryStore(init: Record<string, string> = {}) {
|
||||
const data = new Map(Object.entries(init));
|
||||
return {
|
||||
data,
|
||||
getItem: (k: string) => data.get(k) ?? null,
|
||||
setItem: (k: string, v: string) => void data.set(k, v)
|
||||
};
|
||||
}
|
||||
|
||||
describe('zoom helpers', () => {
|
||||
it('clamps per format and repairs junk', () => {
|
||||
expect(clampZoom(10, 'pdf')).toBe(4);
|
||||
expect(clampZoom(0.1, 'pdf')).toBe(0.5);
|
||||
expect(clampZoom(10, 'foliate')).toBe(2.5);
|
||||
expect(clampZoom(Number.NaN, 'pdf')).toBe(1);
|
||||
expect(clampZoom(-2, 'foliate')).toBe(1);
|
||||
});
|
||||
|
||||
it('steps through presets from any value', () => {
|
||||
expect(stepZoom(1, 1, 'pdf')).toBe(1.1);
|
||||
expect(stepZoom(1, -1, 'pdf')).toBe(0.9);
|
||||
expect(stepZoom(1.03, 1, 'pdf')).toBe(1.1);
|
||||
expect(stepZoom(1.03, -1, 'pdf')).toBe(1);
|
||||
expect(stepZoom(4, 1, 'pdf')).toBe(4);
|
||||
expect(stepZoom(0.5, -1, 'pdf')).toBe(0.5);
|
||||
expect(stepZoom(2.5, 1, 'foliate')).toBe(2.5);
|
||||
expect(stepZoom(0.6, -1, 'foliate')).toBe(0.6);
|
||||
});
|
||||
|
||||
it('wheel zooms smoothly in the right direction', () => {
|
||||
expect(wheelZoom(1, -100, 'pdf')).toBeGreaterThan(1);
|
||||
expect(wheelZoom(1, 100, 'pdf')).toBeLessThan(1);
|
||||
// small trackpad deltas still move
|
||||
expect(wheelZoom(1, -2, 'pdf')).not.toBe(1);
|
||||
expect(wheelZoom(4, -100, 'pdf')).toBe(4);
|
||||
});
|
||||
});
|
||||
|
||||
describe('pdf rows', () => {
|
||||
it('single pages', () => {
|
||||
expect(pdfRows(3, 1)).toEqual([[1], [2], [3]]);
|
||||
expect(pdfRows(0, 2)).toEqual([]);
|
||||
});
|
||||
|
||||
it('spreads keep the cover alone', () => {
|
||||
expect(pdfRows(1, 2)).toEqual([[1]]);
|
||||
expect(pdfRows(4, 2)).toEqual([[1], [2, 3], [4]]);
|
||||
expect(pdfRows(5, 2)).toEqual([[1], [2, 3], [4, 5]]);
|
||||
});
|
||||
|
||||
it('finds the row of a page', () => {
|
||||
const rows = pdfRows(5, 2);
|
||||
expect(rowIndexOf(rows, 1)).toBe(0);
|
||||
expect(rowIndexOf(rows, 3)).toBe(1);
|
||||
expect(rowIndexOf(rows, 4)).toBe(2);
|
||||
expect(rowIndexOf(rows, 99)).toBe(2);
|
||||
expect(rowIndexOf(rows, -1)).toBe(0);
|
||||
expect(rowIndexOf([], 3)).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('layout persistence', () => {
|
||||
it('defaults per format', () => {
|
||||
const s = memoryStore();
|
||||
expect(loadLayout(s, 'pdf', 1)).toEqual(defaultLayouts.pdf);
|
||||
expect(loadLayout(s, 'foliate', 1)).toEqual(defaultLayouts.foliate);
|
||||
});
|
||||
|
||||
it('remembers layout per format and zoom per book', () => {
|
||||
const s = memoryStore();
|
||||
saveLayout(s, 'pdf', 7, { mode: 'paged', spread: 2, zoom: 1.5 });
|
||||
expect(loadLayout(s, 'pdf', 7)).toEqual({ mode: 'paged', spread: 2, zoom: 1.5 });
|
||||
// another PDF inherits the format's last layout
|
||||
expect(loadLayout(s, 'pdf', 8)).toEqual({ mode: 'paged', spread: 2, zoom: 1.5 });
|
||||
saveLayout(s, 'pdf', 8, { mode: 'paged', spread: 2, zoom: 0.75 });
|
||||
expect(loadLayout(s, 'pdf', 7).zoom).toBe(1.5);
|
||||
// formats are independent
|
||||
expect(loadLayout(s, 'foliate', 7)).toEqual(defaultLayouts.foliate);
|
||||
});
|
||||
|
||||
it('migrates the old settings once', () => {
|
||||
const s = memoryStore({
|
||||
'books-reader-settings': JSON.stringify({ flow: 'scrolled', fontSize: 120, pdfZoom: 1.3 })
|
||||
});
|
||||
expect(loadLayout(s, 'foliate', 1)).toEqual({ mode: 'scroll', spread: 2, zoom: 1.2 });
|
||||
expect(loadLayout(s, 'pdf', 1)).toEqual({ mode: 'scroll', spread: 1, zoom: 1.3 });
|
||||
});
|
||||
|
||||
it('survives broken storage', () => {
|
||||
const s = memoryStore({ 'books-reader-layout:pdf': '{nope', 'books-reader-zoom:pdf:1': '"x"' });
|
||||
expect(loadLayout(s, 'pdf', 1)).toEqual(defaultLayouts.pdf);
|
||||
expect(normalizeLayout({ mode: 'x', spread: 3, zoom: 99 }, 'foliate')).toEqual({
|
||||
mode: 'paged',
|
||||
spread: 2,
|
||||
zoom: 2.5
|
||||
});
|
||||
});
|
||||
});
|
||||
124
src/lib/books/reader/layout.ts
Normal file
124
src/lib/books/reader/layout.ts
Normal file
@@ -0,0 +1,124 @@
|
||||
// View layout for the reader: zoom, 1/2-page spreads and scroll vs paged reading.
|
||||
// Pure helpers (unit tested) plus localStorage persistence: mode/spread/zoom are remembered
|
||||
// per format (pdf vs reflowable), and the zoom is also remembered per book.
|
||||
|
||||
export type EngineKind = 'pdf' | 'foliate';
|
||||
export type ViewMode = 'scroll' | 'paged';
|
||||
export type Spread = 1 | 2;
|
||||
export type ViewLayout = { mode: ViewMode; spread: Spread; zoom: number };
|
||||
|
||||
export const defaultLayouts: Record<EngineKind, ViewLayout> = {
|
||||
pdf: { mode: 'scroll', spread: 1, zoom: 1 },
|
||||
foliate: { mode: 'paged', spread: 2, zoom: 1 }
|
||||
};
|
||||
|
||||
/** Zoom limits: PDF zoom scales the page render (1 = fit), foliate zoom scales the text. */
|
||||
export const zoomRange: Record<EngineKind, [min: number, max: number]> = {
|
||||
pdf: [0.5, 4],
|
||||
foliate: [0.6, 2.5]
|
||||
};
|
||||
const ZOOM_STEPS = [0.5, 0.6, 0.67, 0.75, 0.8, 0.9, 1, 1.1, 1.25, 1.5, 1.75, 2, 2.5, 3, 4];
|
||||
|
||||
export function clampZoom(zoom: number, kind: EngineKind): number {
|
||||
const [min, max] = zoomRange[kind];
|
||||
const z = Number.isFinite(zoom) && zoom > 0 ? zoom : 1;
|
||||
return Math.round(Math.min(max, Math.max(min, z)) * 1000) / 1000;
|
||||
}
|
||||
|
||||
/** Next preset zoom level up (dir 1) or down (dir -1) from wherever the zoom is now. */
|
||||
export function stepZoom(zoom: number, dir: 1 | -1, kind: EngineKind): number {
|
||||
const [min, max] = zoomRange[kind];
|
||||
const z = clampZoom(zoom, kind);
|
||||
const next =
|
||||
dir > 0
|
||||
? (ZOOM_STEPS.find((s) => s > z + 0.001) ?? max)
|
||||
: (ZOOM_STEPS.findLast((s) => s < z - 0.001) ?? min);
|
||||
return clampZoom(next, kind);
|
||||
}
|
||||
|
||||
/** Smooth zoom for Ctrl+wheel and trackpad pinch (which browsers report as Ctrl+wheel). */
|
||||
export function wheelZoom(zoom: number, deltaY: number, kind: EngineKind): number {
|
||||
// A mouse notch (~100) is one ~10% step; pinch deltas are small and smooth.
|
||||
const d = Math.max(-50, Math.min(50, deltaY));
|
||||
return clampZoom(zoom * Math.exp(-d * 0.002), kind);
|
||||
}
|
||||
|
||||
/** PDF rows: one page per row, or book spreads with the cover on its own ([1], [2,3], [4,5]…). */
|
||||
export function pdfRows(count: number, spread: Spread): number[][] {
|
||||
const rows: number[][] = [];
|
||||
if (count <= 0) return rows;
|
||||
if (spread === 1) {
|
||||
for (let n = 1; n <= count; n++) rows.push([n]);
|
||||
return rows;
|
||||
}
|
||||
rows.push([1]);
|
||||
for (let n = 2; n <= count; n += 2) rows.push(n + 1 <= count ? [n, n + 1] : [n]);
|
||||
return rows;
|
||||
}
|
||||
|
||||
/** Index of the row containing `page` (clamped into range). */
|
||||
export function rowIndexOf(rows: number[][], page: number): number {
|
||||
if (!rows.length) return 0;
|
||||
const idx = rows.findIndex((r) => r.includes(page));
|
||||
if (idx >= 0) return idx;
|
||||
return page < rows[0][0] ? 0 : rows.length - 1;
|
||||
}
|
||||
|
||||
type Store = Pick<Storage, 'getItem' | 'setItem'>;
|
||||
const formatKey = (kind: EngineKind) => `books-reader-layout:${kind}`;
|
||||
const bookKey = (kind: EngineKind, bookId: number) => `books-reader-zoom:${kind}:${bookId}`;
|
||||
const LEGACY_KEY = 'books-reader-settings';
|
||||
|
||||
function read(store: Store, key: string): unknown {
|
||||
try {
|
||||
return JSON.parse(store.getItem(key) ?? 'null');
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** Sanitises anything (old/broken storage) into a valid layout. */
|
||||
export function normalizeLayout(raw: unknown, kind: EngineKind): ViewLayout {
|
||||
const base = defaultLayouts[kind];
|
||||
const o = raw && typeof raw === 'object' ? (raw as Record<string, unknown>) : {};
|
||||
return {
|
||||
mode: o.mode === 'scroll' || o.mode === 'paged' ? o.mode : base.mode,
|
||||
spread: o.spread === 1 || o.spread === 2 ? o.spread : base.spread,
|
||||
zoom: typeof o.zoom === 'number' ? clampZoom(o.zoom, kind) : base.zoom
|
||||
};
|
||||
}
|
||||
|
||||
export function loadLayout(store: Store, kind: EngineKind, bookId: number): ViewLayout {
|
||||
let saved = read(store, formatKey(kind));
|
||||
if (!saved) {
|
||||
// Carry over the pre-layout settings (flow, fontSize, pdfZoom) once.
|
||||
const legacy = read(store, LEGACY_KEY) as Record<string, unknown> | null;
|
||||
if (legacy && typeof legacy === 'object') {
|
||||
saved =
|
||||
kind === 'pdf'
|
||||
? { zoom: legacy.pdfZoom }
|
||||
: {
|
||||
mode:
|
||||
legacy.flow === 'scrolled'
|
||||
? 'scroll'
|
||||
: legacy.flow === 'paginated'
|
||||
? 'paged'
|
||||
: null,
|
||||
zoom: typeof legacy.fontSize === 'number' ? legacy.fontSize / 100 : null
|
||||
};
|
||||
}
|
||||
}
|
||||
const layout = normalizeLayout(saved, kind);
|
||||
const bookZoom = read(store, bookKey(kind, bookId));
|
||||
if (typeof bookZoom === 'number') layout.zoom = clampZoom(bookZoom, kind);
|
||||
return layout;
|
||||
}
|
||||
|
||||
export function saveLayout(store: Store, kind: EngineKind, bookId: number, layout: ViewLayout) {
|
||||
try {
|
||||
store.setItem(formatKey(kind), JSON.stringify(layout));
|
||||
store.setItem(bookKey(kind, bookId), JSON.stringify(layout.zoom));
|
||||
} catch {
|
||||
// storage full or disabled: the layout still applies for this visit
|
||||
}
|
||||
}
|
||||
39
src/lib/books/reader/pdf.test.ts
Normal file
39
src/lib/books/reader/pdf.test.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { closePdf } from './pdf.js';
|
||||
|
||||
// A tiny one-page PDF (pdf.js tolerates the missing xref table).
|
||||
const PDF =
|
||||
'%PDF-1.4\n1 0 obj<</Type/Catalog/Pages 2 0 R>>endobj\n' +
|
||||
'2 0 obj<</Type/Pages/Kids[3 0 R]/Count 1>>endobj\n' +
|
||||
'3 0 obj<</Type/Page/Parent 2 0 R/MediaBox[0 0 200 300]>>endobj\ntrailer<</Root 1 0 R>>\n%%EOF';
|
||||
|
||||
const open = async () => {
|
||||
const pdfjs = await import('pdfjs-dist/legacy/build/pdf.mjs');
|
||||
return pdfjs.getDocument({ data: new TextEncoder().encode(PDF) });
|
||||
};
|
||||
|
||||
describe('closePdf', () => {
|
||||
it('the document itself has no destroy() in pdf.js 6 (the old teardown threw)', async () => {
|
||||
const task = await open();
|
||||
const doc = await task.promise;
|
||||
expect(doc.numPages).toBe(1);
|
||||
expect((doc as unknown as { destroy?: unknown }).destroy).toBeUndefined();
|
||||
await closePdf(task);
|
||||
});
|
||||
|
||||
it('destroys a loaded document via its loading task, idempotently', async () => {
|
||||
const task = await open();
|
||||
await task.promise;
|
||||
await closePdf(task);
|
||||
expect(task.destroyed).toBe(true);
|
||||
await expect(closePdf(task)).resolves.toBeUndefined();
|
||||
});
|
||||
|
||||
it('can close while still loading, or with nothing open', async () => {
|
||||
const task = await open();
|
||||
task.promise.catch(() => {}); // rejected by the destroy
|
||||
await closePdf(task);
|
||||
expect(task.destroyed).toBe(true);
|
||||
await expect(closePdf(undefined)).resolves.toBeUndefined();
|
||||
});
|
||||
});
|
||||
18
src/lib/books/reader/pdf.ts
Normal file
18
src/lib/books/reader/pdf.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
// pdf.js lifecycle helpers for the reader.
|
||||
|
||||
type LoadingTask = { destroyed: boolean; destroy(): Promise<void> };
|
||||
|
||||
/**
|
||||
* Closes a PDF opened with pdfjs.getDocument(). pdf.js 6 removed PDFDocumentProxy.destroy();
|
||||
* the loading task owns the document, its network requests and the worker, so tearing it
|
||||
* down works both mid-load and after. Never throws: it runs while the reader unmounts
|
||||
* (e.g. the back button), where an exception would break the navigation.
|
||||
*/
|
||||
export async function closePdf(task: LoadingTask | null | undefined): Promise<void> {
|
||||
if (!task || task.destroyed) return;
|
||||
try {
|
||||
await task.destroy();
|
||||
} catch {
|
||||
// already torn down
|
||||
}
|
||||
}
|
||||
89
src/lib/books/reader/types.ts
Normal file
89
src/lib/books/reader/types.ts
Normal file
@@ -0,0 +1,89 @@
|
||||
// Contract between the reader shell (the /read page) and the EPUB/PDF engines.
|
||||
import type { Highlight } from '#lib/books/shared.js';
|
||||
import type { ViewLayout } from './layout.js';
|
||||
|
||||
export type TocItem = { label: string; target: string | number; children: TocItem[] };
|
||||
|
||||
export type Relocation = {
|
||||
percent: number; // 0-100
|
||||
locator: string; // EPUB CFI or `page:N`
|
||||
page: number | null; // PDF page (1-based), or null for reflowable books
|
||||
pageEnd?: number | null; // last page shown when a PDF spread shows two
|
||||
pageCount: number | null;
|
||||
label: string | null; // current chapter
|
||||
};
|
||||
|
||||
export type TextSelection = { text: string; locator: string; page: number | null };
|
||||
|
||||
export type ReaderTheme = 'light' | 'sepia' | 'dark';
|
||||
export type ReaderSettings = {
|
||||
theme: 'auto' | ReaderTheme;
|
||||
lineHeight: number;
|
||||
font: 'publisher' | 'serif' | 'sans';
|
||||
};
|
||||
export const defaultSettings: ReaderSettings = {
|
||||
theme: 'auto',
|
||||
lineHeight: 1.5,
|
||||
font: 'publisher'
|
||||
};
|
||||
|
||||
export const themeColors: Record<ReaderTheme, { bg: string; fg: string; link: string }> = {
|
||||
light: { bg: '#fdfdfb', fg: '#1c1c1c', link: '#2563eb' },
|
||||
sepia: { bg: '#f4ecd8', fg: '#433422', link: '#8a4b14' },
|
||||
dark: { bg: '#121214', fg: '#d8d8d8', link: '#7aa8ff' }
|
||||
};
|
||||
|
||||
/** What both engines expose to the shell through bind:this. */
|
||||
export interface ReaderEngine {
|
||||
goTo(target: string | number): Promise<void>;
|
||||
goToPercent(percent: number): Promise<void>;
|
||||
next(): void;
|
||||
prev(): void;
|
||||
addHighlight(h: Highlight): void;
|
||||
removeHighlight(h: Highlight): void;
|
||||
clearSelection(): void;
|
||||
}
|
||||
|
||||
export type EngineProps = {
|
||||
url: string;
|
||||
name: string;
|
||||
start: string | null;
|
||||
startPercent: number;
|
||||
highlights: Highlight[];
|
||||
settings: ReaderSettings;
|
||||
/** Zoom, 1/2-page spread and scroll vs paged (see layout.ts). */
|
||||
layout: ViewLayout;
|
||||
theme: ReaderTheme;
|
||||
/** Ctrl+wheel / pinch inside the engine asks the shell for a new zoom. */
|
||||
onzoom: (zoom: number) => void;
|
||||
onready: (toc: TocItem[], info?: { fixedLayout?: boolean }) => void;
|
||||
onrelocate: (r: Relocation) => void;
|
||||
onselection: (s: TextSelection | null) => void;
|
||||
onhighlightclick: (id: number) => void;
|
||||
onkey: (e: KeyboardEvent) => void;
|
||||
onloading: (fraction: number | null) => void;
|
||||
onerror: (message: string) => void;
|
||||
};
|
||||
|
||||
/** Downloads a file with progress, so big books show a loading bar instead of a blank page. */
|
||||
export async function fetchWithProgress(
|
||||
url: string,
|
||||
onprogress: (fraction: number | null) => void,
|
||||
signal?: AbortSignal
|
||||
): Promise<Blob> {
|
||||
const res = await fetch(url, { signal, credentials: 'same-origin' });
|
||||
if (!res.ok) throw new Error(res.status === 404 ? 'File not found' : `HTTP ${res.status}`);
|
||||
const total = Number(res.headers.get('content-length')) || 0;
|
||||
if (!res.body || !total) return res.blob();
|
||||
const reader = res.body.getReader();
|
||||
const chunks: Uint8Array<ArrayBuffer>[] = [];
|
||||
let loaded = 0;
|
||||
for (;;) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
chunks.push(value);
|
||||
loaded += value.byteLength;
|
||||
onprogress(Math.min(1, loaded / total));
|
||||
}
|
||||
return new Blob(chunks, { type: res.headers.get('content-type') ?? '' });
|
||||
}
|
||||
104
src/lib/books/shared.ts
Normal file
104
src/lib/books/shared.ts
Normal file
@@ -0,0 +1,104 @@
|
||||
// Shared (client + server) types and helpers for the book tracker.
|
||||
export const readingStatuses = ['want_to_read', 'reading', 'finished', 'abandoned'] as const;
|
||||
export type ReadingStatus = (typeof readingStatuses)[number];
|
||||
|
||||
export const statusLabels: Record<ReadingStatus, string> = {
|
||||
want_to_read: 'want to read',
|
||||
reading: 'reading',
|
||||
finished: 'finished',
|
||||
abandoned: 'abandoned'
|
||||
};
|
||||
|
||||
export function isReadingStatus(value: unknown): value is ReadingStatus {
|
||||
return typeof value === 'string' && (readingStatuses as readonly string[]).includes(value);
|
||||
}
|
||||
|
||||
/** Formats the in-browser reader can open. MOBI/AZW3/FB2/CBZ go through foliate-js too. */
|
||||
export const readableFormats = ['epub', 'pdf', 'mobi', 'azw3', 'azw', 'fb2', 'cbz'] as const;
|
||||
export function isReadable(format: string): boolean {
|
||||
return (readableFormats as readonly string[]).includes(format.toLowerCase());
|
||||
}
|
||||
/** Preferred file to open when a work has several formats. */
|
||||
export const formatPreference = ['epub', 'azw3', 'mobi', 'azw', 'fb2', 'pdf', 'cbz'];
|
||||
|
||||
/** One card in the library. Public fields only, unless noted. */
|
||||
export type BookCard = {
|
||||
id: number;
|
||||
title: string;
|
||||
subtitle: string | null;
|
||||
authors: string[];
|
||||
year: number | null;
|
||||
tags: string[];
|
||||
pageCount: number | null;
|
||||
cover: string | null; // URL (our cover endpoint or a remote cover), or null for a generated one
|
||||
status: ReadingStatus | null; // null: never touched
|
||||
percent: number;
|
||||
rating: number | null;
|
||||
lastReadAt: string | null;
|
||||
finishedAt: string | null;
|
||||
addedAt: string;
|
||||
// Owner only (empty / false for everyone else):
|
||||
formats: string[];
|
||||
hasFile: boolean;
|
||||
};
|
||||
|
||||
export type LibraryStats = {
|
||||
total: number;
|
||||
finishedThisYear: number;
|
||||
pagesThisYear: number;
|
||||
reading: number;
|
||||
year: number;
|
||||
};
|
||||
|
||||
export type Highlight = {
|
||||
id: number;
|
||||
fileId: number | null;
|
||||
locator: string | null;
|
||||
page: number | null;
|
||||
highlight: string | null;
|
||||
note: string | null;
|
||||
color: string | null;
|
||||
createdAt: string;
|
||||
};
|
||||
|
||||
export const highlightColors = ['yellow', 'green', 'blue', 'pink', 'purple'] as const;
|
||||
export type HighlightColor = (typeof highlightColors)[number];
|
||||
export const highlightCss: Record<HighlightColor, string> = {
|
||||
yellow: '#facc15',
|
||||
green: '#4ade80',
|
||||
blue: '#60a5fa',
|
||||
pink: '#f472b6',
|
||||
purple: '#c084fc'
|
||||
};
|
||||
|
||||
/** Deterministic hue for generated covers, so a book always gets the same colour. */
|
||||
export function coverHue(title: string): number {
|
||||
let hash = 0;
|
||||
for (const char of title) hash = (hash * 31 + char.codePointAt(0)!) >>> 0;
|
||||
return hash % 360;
|
||||
}
|
||||
|
||||
export function authorLine(authors: readonly string[]): string {
|
||||
if (authors.length <= 2) return authors.join(' & ');
|
||||
return `${authors[0]} et al.`;
|
||||
}
|
||||
|
||||
/** PDF highlight locators: `page:12` or `page:12#r=x,y,w,h;x,y,w,h` (rects as page fractions). */
|
||||
export type Rect = { x: number; y: number; w: number; h: number };
|
||||
export function pdfLocator(page: number, rects: Rect[] = []): string {
|
||||
const r = rects
|
||||
.map((rect) => [rect.x, rect.y, rect.w, rect.h].map((n) => +n.toFixed(4)).join(','))
|
||||
.join(';');
|
||||
return r ? `page:${page}#r=${r}` : `page:${page}`;
|
||||
}
|
||||
export function parsePdfLocator(locator: string | null): { page: number; rects: Rect[] } | null {
|
||||
const match = /^page:(\d+)(?:#r=([\d.,;-]+))?$/.exec(locator ?? '');
|
||||
if (!match) return null;
|
||||
const rects = (match[2] ?? '')
|
||||
.split(';')
|
||||
.filter(Boolean)
|
||||
.map((part) => part.split(',').map(Number))
|
||||
.filter((n) => n.length === 4 && n.every(Number.isFinite))
|
||||
.map(([x, y, w, h]) => ({ x, y, w, h }));
|
||||
return { page: Number(match[1]), rects };
|
||||
}
|
||||
63
src/lib/components/CatalogGrid.svelte
Normal file
63
src/lib/components/CatalogGrid.svelte
Normal file
@@ -0,0 +1,63 @@
|
||||
<script lang="ts">
|
||||
// Card grid shared by the home page, /projects and /trackers.
|
||||
import { resolve } from '$app/paths';
|
||||
import ArrowRight from 'lucide-svelte/icons/arrow-right';
|
||||
import ExternalLink from 'lucide-svelte/icons/external-link';
|
||||
import * as Card from '#lib/components/ui/card/index.js';
|
||||
import ProjectIcon from '#lib/components/ProjectIcon.svelte';
|
||||
|
||||
type Item = { name: string; subtitle: string; href: string; external: boolean };
|
||||
let {
|
||||
items,
|
||||
subtitleClass = 'mt-1 block text-sm text-muted-foreground'
|
||||
}: { items: readonly Item[]; subtitleClass?: string } = $props();
|
||||
</script>
|
||||
|
||||
<ul class="grid gap-3 sm:grid-cols-2">
|
||||
{#each items as project (project.name)}
|
||||
<li>
|
||||
<Card.Root class="h-full py-0 transition-colors hover:bg-muted/50"
|
||||
><Card.Content class="p-0">
|
||||
{#if project.external}
|
||||
<a
|
||||
href={project.href}
|
||||
rel="external"
|
||||
class="flex min-h-28 items-start justify-between gap-4 p-5"
|
||||
>
|
||||
<span class="flex min-w-0 items-start gap-3">
|
||||
<span
|
||||
class="mt-0.5 flex size-10 shrink-0 items-center justify-center rounded-md border border-border bg-[var(--site-surface)] text-[var(--site-muted)]"
|
||||
>
|
||||
<ProjectIcon name={project.name} class="size-5" />
|
||||
</span>
|
||||
<span class="min-w-0"
|
||||
><span class="block font-medium">{project.name}</span><span class={subtitleClass}
|
||||
>{project.subtitle}</span
|
||||
></span
|
||||
>
|
||||
</span><ExternalLink size={18} aria-hidden="true" class="text-[var(--site-muted)]" />
|
||||
</a>
|
||||
{:else}
|
||||
<a
|
||||
href={resolve(project.href as '/')}
|
||||
class="flex min-h-28 items-start justify-between gap-4 p-5"
|
||||
>
|
||||
<span class="flex min-w-0 items-start gap-3">
|
||||
<span
|
||||
class="mt-0.5 flex size-10 shrink-0 items-center justify-center rounded-md border border-border bg-[var(--site-surface)] text-[var(--site-muted)]"
|
||||
>
|
||||
<ProjectIcon name={project.name} class="size-5" />
|
||||
</span>
|
||||
<span class="min-w-0"
|
||||
><span class="block font-medium">{project.name}</span><span class={subtitleClass}
|
||||
>{project.subtitle}</span
|
||||
></span
|
||||
>
|
||||
</span><ArrowRight size={18} aria-hidden="true" class="text-[var(--site-muted)]" />
|
||||
</a>
|
||||
{/if}
|
||||
</Card.Content></Card.Root
|
||||
>
|
||||
</li>
|
||||
{/each}
|
||||
</ul>
|
||||
59
src/lib/components/ListExtraFields.svelte
Normal file
59
src/lib/components/ListExtraFields.svelte
Normal file
@@ -0,0 +1,59 @@
|
||||
<script lang="ts">
|
||||
// Form inputs for a list's own typed columns (meta.columns), posted as `extra:<column>`.
|
||||
import { Input } from '#lib/components/ui/input/index.js';
|
||||
import { Textarea } from '#lib/components/ui/textarea/index.js';
|
||||
import { NativeSelect } from '#lib/components/ui/native-select/index.js';
|
||||
import { Checkbox } from '#lib/components/ui/checkbox/index.js';
|
||||
import type { ExtraValue, ListColumn } from '#lib/server/lists.js';
|
||||
|
||||
let { columns, values = {} }: { columns: ListColumn[]; values?: Record<string, ExtraValue> } =
|
||||
$props();
|
||||
const str = (v: ExtraValue | undefined) => (v === null || v === undefined ? '' : String(v));
|
||||
</script>
|
||||
|
||||
{#each columns as column (column.name)}
|
||||
{@const name = `extra:${column.name}`}
|
||||
{#if column.uiType === 'bool'}
|
||||
<label class="text-sm"
|
||||
><Checkbox {name} value="on" checked={values[column.name] === true} /> {column.label}</label
|
||||
>
|
||||
{:else if column.uiType === 'select' && column.options.length}
|
||||
<label
|
||||
>{column.label}<NativeSelect
|
||||
{name}
|
||||
class="block w-full rounded border p-2"
|
||||
value={str(values[column.name])}
|
||||
>
|
||||
<option value="">—</option>
|
||||
{#each column.options as option (option)}<option value={option}>{option}</option>{/each}
|
||||
</NativeSelect></label
|
||||
>
|
||||
{:else if column.uiType === 'longtext'}
|
||||
<label
|
||||
>{column.label}<Textarea
|
||||
{name}
|
||||
rows={3}
|
||||
maxlength={2000}
|
||||
class="block w-full rounded border p-2"
|
||||
value={str(values[column.name])}
|
||||
/></label
|
||||
>
|
||||
{:else if ['text', 'url', 'image', 'number', 'cents', 'date', 'select'].includes(column.uiType)}
|
||||
<label
|
||||
>{column.label}<Input
|
||||
{name}
|
||||
type={column.uiType === 'number' || column.uiType === 'cents'
|
||||
? 'number'
|
||||
: column.uiType === 'date'
|
||||
? 'date'
|
||||
: column.uiType === 'url' || column.uiType === 'image'
|
||||
? 'url'
|
||||
: 'text'}
|
||||
step={column.uiType === 'number' ? 'any' : undefined}
|
||||
maxlength={column.uiType === 'text' ? 2000 : undefined}
|
||||
class="block w-full rounded border p-2"
|
||||
value={str(values[column.name])}
|
||||
/></label
|
||||
>
|
||||
{/if}
|
||||
{/each}
|
||||
@@ -8,13 +8,16 @@
|
||||
</script>
|
||||
|
||||
<script lang="ts">
|
||||
import BookOpen from 'lucide-svelte/icons/book-open';
|
||||
import CandlestickChart from 'lucide-svelte/icons/candlestick-chart';
|
||||
import ChartNoAxesCombined from 'lucide-svelte/icons/chart-no-axes-combined';
|
||||
import FolderKanban from 'lucide-svelte/icons/folder-kanban';
|
||||
import Gamepad2 from 'lucide-svelte/icons/gamepad-2';
|
||||
import HeartPulse from 'lucide-svelte/icons/heart-pulse';
|
||||
import House from 'lucide-svelte/icons/house';
|
||||
import Globe2 from 'lucide-svelte/icons/globe-2';
|
||||
import Languages from 'lucide-svelte/icons/languages';
|
||||
import ListChecks from 'lucide-svelte/icons/list-checks';
|
||||
import Music2 from 'lucide-svelte/icons/music-2';
|
||||
import PawPrint from 'lucide-svelte/icons/paw-print';
|
||||
import Trophy from 'lucide-svelte/icons/trophy';
|
||||
@@ -30,9 +33,12 @@
|
||||
'derby lens': ChartNoAxesCombined,
|
||||
'world opinions': Globe2,
|
||||
'language learner': Languages,
|
||||
'money tracker': Wallet,
|
||||
money: Wallet,
|
||||
'cs prices': CandlestickChart,
|
||||
'house tracker': House
|
||||
'house tracker': House,
|
||||
lists: ListChecks,
|
||||
health: HeartPulse,
|
||||
books: BookOpen
|
||||
};
|
||||
|
||||
const Icon = $derived(icons[name as keyof typeof icons] ?? FolderKanban);
|
||||
|
||||
155
src/lib/components/data/FieldInput.svelte
Normal file
155
src/lib/components/data/FieldInput.svelte
Normal file
@@ -0,0 +1,155 @@
|
||||
<script lang="ts">
|
||||
import type { Col } from './data-format.js';
|
||||
|
||||
let {
|
||||
column,
|
||||
value = $bindable(null),
|
||||
lookupUrl,
|
||||
autofocus = false,
|
||||
compact = false,
|
||||
onsave,
|
||||
oncancel
|
||||
}: {
|
||||
column: Col;
|
||||
value: string | null;
|
||||
lookupUrl?: string;
|
||||
autofocus?: boolean;
|
||||
compact?: boolean;
|
||||
onsave?: (move?: 'next' | 'prev') => void;
|
||||
oncancel?: () => void;
|
||||
} = $props();
|
||||
|
||||
const kind = $derived(column.kind);
|
||||
const long = $derived(
|
||||
kind === 'json' ||
|
||||
column.uiType === 'longtext' ||
|
||||
(kind === 'text' && (value?.length ?? 0) > 80)
|
||||
);
|
||||
const listId = `fk-${Math.random().toString(36).slice(2)}`;
|
||||
let options = $state.raw<{ value: string; label: string }[]>([]);
|
||||
let timer: ReturnType<typeof setTimeout> | undefined;
|
||||
|
||||
async function lookup(q: string) {
|
||||
if (!column.fk || !lookupUrl) return;
|
||||
const url = `${lookupUrl}?column=${encodeURIComponent(column.name)}&q=${encodeURIComponent(q)}`;
|
||||
const res = await fetch(url).catch(() => null);
|
||||
if (res?.ok) options = (await res.json()).options;
|
||||
}
|
||||
function queueLookup(q: string) {
|
||||
clearTimeout(timer);
|
||||
timer = setTimeout(() => lookup(q), 200);
|
||||
}
|
||||
|
||||
function focusOnMount(node: HTMLElement) {
|
||||
if (!autofocus) return;
|
||||
node.focus();
|
||||
if (node instanceof HTMLInputElement && node.type === 'text') node.select();
|
||||
if (column.fk) lookup('');
|
||||
}
|
||||
|
||||
function keydown(e: KeyboardEvent) {
|
||||
if (e.key === 'Escape') {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
oncancel?.();
|
||||
} else if (e.key === 'Enter' && (!long || e.ctrlKey || e.metaKey)) {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
onsave?.();
|
||||
} else if (e.key === 'Tab' && onsave) {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
onsave(e.shiftKey ? 'prev' : 'next');
|
||||
}
|
||||
}
|
||||
|
||||
const base =
|
||||
'w-full rounded border border-[var(--site-border)] bg-[var(--site-bg)] px-2 py-1 text-sm focus:outline-2 focus:outline-[var(--site-text)]';
|
||||
const placeholder = $derived(
|
||||
value === null
|
||||
? column.hasDefault
|
||||
? `default${column.defaultExpr ? `: ${column.defaultExpr}` : ''}`
|
||||
: 'null'
|
||||
: ''
|
||||
);
|
||||
</script>
|
||||
|
||||
<div class={['flex items-start gap-1', compact ? '' : 'w-full']}>
|
||||
{#if kind === 'bool'}
|
||||
<select
|
||||
class={base}
|
||||
value={value ?? ''}
|
||||
onchange={(e) => (value = e.currentTarget.value === '' ? null : e.currentTarget.value)}
|
||||
onkeydown={keydown}
|
||||
{@attach focusOnMount}
|
||||
aria-label={column.label}
|
||||
>
|
||||
{#if column.nullable || column.hasDefault}<option value=""
|
||||
>{column.hasDefault ? '(default)' : 'null'}</option
|
||||
>{/if}
|
||||
<option value="true">true</option>
|
||||
<option value="false">false</option>
|
||||
</select>
|
||||
{:else if kind === 'enum' || (column.uiType === 'select' && column.enumValues.length)}
|
||||
<select
|
||||
class={base}
|
||||
value={value ?? ''}
|
||||
onchange={(e) => (value = e.currentTarget.value === '' ? null : e.currentTarget.value)}
|
||||
onkeydown={keydown}
|
||||
{@attach focusOnMount}
|
||||
aria-label={column.label}
|
||||
>
|
||||
{#if column.nullable || column.hasDefault}<option value=""
|
||||
>{column.hasDefault ? '(default)' : 'null'}</option
|
||||
>{/if}
|
||||
{#each column.enumValues as v (v)}<option value={v}>{v}</option>{/each}
|
||||
</select>
|
||||
{:else if long}
|
||||
<textarea
|
||||
class={[base, 'min-h-24 font-mono']}
|
||||
rows={kind === 'json' ? 6 : 3}
|
||||
value={value ?? ''}
|
||||
{placeholder}
|
||||
oninput={(e) => (value = e.currentTarget.value)}
|
||||
onkeydown={keydown}
|
||||
{@attach focusOnMount}
|
||||
aria-label={column.label}></textarea>
|
||||
{:else}
|
||||
<input
|
||||
class={base}
|
||||
type={kind === 'date'
|
||||
? 'date'
|
||||
: kind === 'datetime'
|
||||
? 'datetime-local'
|
||||
: kind === 'time'
|
||||
? 'time'
|
||||
: kind === 'number'
|
||||
? 'number'
|
||||
: 'text'}
|
||||
step={kind === 'number' ? 'any' : kind === 'datetime' || kind === 'time' ? 1 : undefined}
|
||||
list={column.fk ? listId : undefined}
|
||||
value={value ?? ''}
|
||||
{placeholder}
|
||||
oninput={(e) => {
|
||||
value = e.currentTarget.value;
|
||||
if (column.fk) queueLookup(e.currentTarget.value);
|
||||
}}
|
||||
onkeydown={keydown}
|
||||
{@attach focusOnMount}
|
||||
aria-label={column.label}
|
||||
/>
|
||||
{#if column.fk}
|
||||
<datalist id={listId}>
|
||||
{#each options as o (o.value)}<option value={o.value}>{o.label}</option>{/each}
|
||||
</datalist>
|
||||
{/if}
|
||||
{/if}
|
||||
{#if column.nullable && value !== null}
|
||||
<button
|
||||
type="button"
|
||||
class="shrink-0 rounded px-1.5 py-1 text-xs text-[var(--site-muted)] hover:bg-[var(--site-hover)]"
|
||||
title="Set to NULL"
|
||||
onclick={() => (value = null)}>null</button
|
||||
>
|
||||
{/if}
|
||||
</div>
|
||||
72
src/lib/components/data/data-format.ts
Normal file
72
src/lib/components/data/data-format.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
// Client-side value helpers for the /lists/db grid (shared by the grid and FieldInput).
|
||||
export type Col = {
|
||||
name: string;
|
||||
label: string;
|
||||
type: string;
|
||||
kind: string;
|
||||
uiType: string | null;
|
||||
nullable: boolean;
|
||||
hasDefault: boolean;
|
||||
defaultExpr: string | null;
|
||||
readOnly: boolean;
|
||||
enumValues: string[];
|
||||
pk: boolean;
|
||||
fk: { schema: string; table: string; column: string } | null;
|
||||
help: string | null;
|
||||
};
|
||||
|
||||
const pad = (n: number) => String(n).padStart(2, '0');
|
||||
const withTz = (c: Col) => c.type.includes('with time zone');
|
||||
|
||||
/** Raw JSON value from the server -> editor string (null stays null). */
|
||||
export function toEditor(c: Col, v: unknown): string | null {
|
||||
if (v === null || v === undefined) return null;
|
||||
if (c.kind === 'json') return JSON.stringify(v, null, 2);
|
||||
if (c.kind === 'array') return JSON.stringify(v);
|
||||
if (c.kind === 'datetime' && typeof v === 'string') {
|
||||
if (!withTz(c)) return v.slice(0, 19);
|
||||
const d = new Date(v);
|
||||
if (Number.isNaN(d.getTime())) return v;
|
||||
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}T${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`;
|
||||
}
|
||||
if (c.kind === 'date' && typeof v === 'string') return v.slice(0, 10);
|
||||
return String(v);
|
||||
}
|
||||
|
||||
/** Editor string -> value sent to the server. */
|
||||
export function fromEditor(c: Col, s: string | null): string | null {
|
||||
if (s === null) return null;
|
||||
if (c.kind === 'datetime' && withTz(c) && s.trim()) {
|
||||
const d = new Date(s);
|
||||
return Number.isNaN(d.getTime()) ? s : d.toISOString();
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
/** Short display text for a grid cell. */
|
||||
export function display(c: Col, v: unknown): string {
|
||||
if (v === null || v === undefined) return '';
|
||||
if (c.kind === 'bool') return v ? '✓' : '✗';
|
||||
if (c.uiType === 'cents' && typeof v === 'number') return `€${(v / 100).toFixed(2)}`;
|
||||
if (c.kind === 'datetime' && typeof v === 'string') {
|
||||
const d = new Date(v);
|
||||
if (Number.isNaN(d.getTime())) return v;
|
||||
return withTz(c)
|
||||
? d.toLocaleString('en-GB', { dateStyle: 'medium', timeStyle: 'short' })
|
||||
: v.replace('T', ' ').slice(0, 16);
|
||||
}
|
||||
if (typeof v === 'object') {
|
||||
const s = JSON.stringify(v);
|
||||
return s.length > 120 ? `${s.slice(0, 117)}…` : s;
|
||||
}
|
||||
const s = String(v);
|
||||
return s.length > 160 ? `${s.slice(0, 157)}…` : s;
|
||||
}
|
||||
|
||||
export function shortType(t: string): string {
|
||||
return t
|
||||
.replace('timestamp with time zone', 'timestamptz')
|
||||
.replace('timestamp without time zone', 'timestamp')
|
||||
.replace('character varying', 'varchar')
|
||||
.replace('double precision', 'float8');
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user