# Design This file fixes the parts that are easy to get wrong. Choose the language and framework that fit the person's project; nothing here depends on them. ## The registry One entry for each table, as data. For example: | Table | Find a person's rows by | Export | At deletion | Never export | | --- | --- | --- | --- | --- | | `users` | `id` | yes | last; see R7 | `password_hash` | | `sessions` | `user_id` | no | `delete` | `token_hash` | | `notes` | `created_by` | yes | `delete` | | | `comments` | `author_id` | yes | `anonymize`: set `author_id` to null, keep the text | | | `invoices` | `user_id` | yes | `keep`: "Tax law requires invoices for 10 years." | | | `audit_entries` | `actor_id` | no | `anonymize`: set `actor_id` to null | | The person's app has its own tables. Register what is there. Where the right choice is not clear from the code, choose `delete`, and list the table in the README under "Check these choices". The schema test finds every foreign key that points at `users`, and every column named `user_id`, and looks each table up in the registry. ## Data model Use the database in the `database` input. Three tables are added. | Table | Columns | | --- | --- | | `data_exports` | `id`, `user_id`, `state`, `file_key`, `requested_by`, `created_at`, `ready_at`, `expires_at` | | `deletion_requests` | `id`, `user_id`, `state`, `requested_by`, `note`, `requested_at`, `due_at`, `cancelled_at`, `completed_at` | | `deletion_records` | `id`, `user_id` (not a reference), `completed_at`, `counts` (JSON) | `users` gains one column, `deletion_due_at`. The sign-in guard checks it: when it is set, only the cancel page and sign-out are reachable. ## Building the export - Write the zip as a stream to storage, one table at a time, reading rows in pages by primary key. - Use the `export_format` input to decide whether CSV copies are added. In CSV, put a single quote in front of any cell that starts with `=`, `+`, `-` or `@`. - Store the file under a random key in private storage. Serve it through the app after checking the signed-in user, never by a public address. ## Carrying out a deletion 1. Ask `deletion_blockers`. If it gives a reason, move `due_at` a day on and stop. 2. For each registry entry, in order of dependency, children before parents: act on up to 1,000 rows at a time, each batch in its own transaction, and add to the counts. 3. Delete uploaded files from storage. A file that is already gone counts as deleted. 4. Handle the `users` row. 5. Write the `deletion_records` row and mark the request completed. Every step looks only at what is still there, so running the job again after a crash repeats nothing harmful. The counts are kept on the request while the job runs, so a second run adds to them. ## Email Five messages: export ready, deletion scheduled, deletion cancelled, deletion put off, and deletion completed, which is sent just before the address is removed. Put sending behind one small interface so tests capture messages without a network. Use the `app_name` input in each. ## Configuration | Secret | Purpose | | --- | --- | | `DATABASE_URL` | Connection string for the chosen database. | | `EMAIL_API_KEY` | Sends the emails. Optional in development. | | `EMAIL_API_URL` | Where `send_email` posts each message. Optional in development. | | `EMAIL_FROM` | The address every email is sent from. Optional in development. | ## Sending email Send every email through one function, `send_email(to, subject, text)`. - With `EMAIL_API_URL` and `EMAIL_API_KEY` set, it makes one `POST` to `EMAIL_API_URL` with the header `Authorization: Bearer ` and the JSON body `{"from": EMAIL_FROM, "to": ..., "subject": ..., "text": ...}`. Any 2xx answer means sent. The person points the address at their email service, or at a few lines that translate for it. - With neither set, outside production, the message goes to a local outbox that tests read. It is not printed: a message can hold a link that signs someone in. In production the app refuses to start without both. ## Named here - Progress is kept in a `progress` column on `deletion_requests`: the counts so far and the last key done in each table. A run that was stopped carries on after that key. ## What this spec leaves to you Where this spec names something, use that name, so that two builds of it agree. Where it does not, the choice is yours. List what you chose in the README.