# Application database (MySQL + Prisma)

Every application from `/contact#apply` is saved to MySQL in the `applications` table by `src/app/api/apply/route.ts`, then emailed through ZeptoMail. The same database holds:
- Publisher Hub accounts: `users`, `sessions`, and `auth_tokens` for email confirmation and password reset;
- contact messages: `contact_messages`.

`docs/api.md` lists every API.

- The schema is in `prisma/schema.prisma`.
- The SQL that creates the table is in `prisma/migrations/20261027000000_applications/migration.sql`.
- The app connects with `DATABASE_URL`. Without it, nothing is stored and applications are only emailed.

## What is stored

There is one row per application:

| Column | Holds |
| --- | --- |
| `id` | Reference shown in the emails, such as `MZ-20261027-1A2B3C4D` |
| `submission_id` | Unique per form submission; a repeat is not stored twice |
| `name`, `email`, `property`, `property_type`, `traffic`, `goal` | The visitor's answers |
| `selected_products` | Products they added, as `A \| B \| C` |
| `inquiry`, `family`, `route` | The inquiry path, the solution family they came from, and the team it's routed to |
| `source`, `page_host`, `user_agent` | Where the application came from |
| `ip_hash` | SHA-256 of the visitor's IP, used only for the rate limit of 5 per 10 minutes. The IP itself isn't stored. |
| `team_email_status`, `applicant_email_status` | `PENDING`, `SENT`, `FAILED` or `SKIPPED` |
| `email_error` | Why an email failed, if it did |
| `created_at`, `updated_at` | Timestamps |

Publisher Hub sites are in `sites`, one row per site: `user_id`, `domain` (as entered, such as `https://example.com/blog`), `host`, `category`, `types` (as `A|B`), `key` (the 32-character key in the site's Monetize code) and `installed_at` (when the installation check last found the code). A domain can be added once per account. Deleting an account deletes its sites.

The visitor sees "Application submitted" when the row was saved or the team email was sent. If the database is down, the route gives up on it within about 4 seconds and continues with email.

## Rikin's Mac: MySQL in ~/mysql

MySQL 8.4.13 runs from Oracle's macOS archive in `~/mysql`. It doesn't need an admin password, and it listens only on `127.0.0.1:3306`.

- **Start:** `~/mysql/bin/mysqld --defaults-file=$HOME/mysql/my.cnf --daemonize`
- **Stop:** `~/mysql/bin/mysqladmin --defaults-file=$HOME/mysql/my.cnf -uroot -p shutdown`
- **Is it running?** `~/mysql/bin/mysqladmin --defaults-file=$HOME/mysql/my.cnf ping`
- **Passwords:** `~/mysql/credentials.txt`, readable only by you. The app user's password is also in `DATABASE_URL` in `.env.local`.
- **Data and log:** `~/mysql/data` and `~/mysql/logs/error.log`.

It doesn't start by itself after a restart. Run the start command first.

**In MySQL Workbench:**
1. Click + next to MySQL Connections.
2. Fill in:
   - Name: `monetize (local)`
   - Hostname: `127.0.0.1`
   - Port: `3306`
   - Username: `monetize`. Use `root` instead for admin tasks.
3. Click "Store in Keychain…" and enter the password from `credentials.txt`, then Test Connection.
4. Open the connection. The data is in `monetize` → Tables → `applications`.

## Set up MySQL on a Mac with MySQL Workbench

MySQL Workbench is only a client, so it needs a MySQL server to connect to.

1. Install **MySQL Community Server 8.4 LTS** from <https://dev.mysql.com/downloads/mysql/> (macOS, ARM DMG). During setup, choose "Use Strong Password Encryption" and set a root password.
2. In System Settings → MySQL, check that the server is running on port 3306.
3. In MySQL Workbench, add a connection to `127.0.0.1:3306` as `root`.
4. Open a query tab and create the database and an app user. Use your own password:

   ```sql
   CREATE DATABASE monetize CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
   CREATE USER 'monetize'@'localhost' IDENTIFIED BY 'choose-a-password';
   GRANT ALL PRIVILEGES ON monetize.* TO 'monetize'@'localhost';
   ```

5. Add this to `.env.local`, which git ignores:

   ```
   DATABASE_URL="mysql://monetize:choose-a-password@127.0.0.1:3306/monetize"
   ```

6. Create the table. Use one of these:
   - From the project folder, run `npm run db:migrate`. This applies `prisma/migrations` and records it in `_prisma_migrations`.
   - In Workbench, run `prisma/migrations/20261027000000_applications/migration.sql` against the `monetize` database. If you do this, later Prisma migrations need `npx prisma migrate resolve --applied 20261027000000_applications` once.
7. Restart `npm run dev`. Submit the form, then in Workbench run `SELECT * FROM monetize.applications ORDER BY created_at DESC;`.

## On a server (staging or production)

- Set `DATABASE_URL` in the server's environment. Add `?ssl=true` if the database requires TLS.
- Run `npm ci` and then `npm run db:migrate` before `npm run start`. `npm ci` also generates the Prisma client.

## Changing the schema

1. Edit `prisma/schema.prisma`.
2. Run `npm run db:migrate:dev -- --name what-changed` against a local database. This creates a new migration folder.
3. Commit the migration.
4. Run `npm run db:migrate` on each server.
