# Database (/docs/self-hosting/database)

Goatside keeps everything in one [Turso](https://turso.tech) database. The schema and its
migrations are written with [Drizzle](https://orm.drizzle.team) and live in `packages/db`.

## Migrations [#migrations]

Vercel doesn't migrate the database, so an update that changes the tables needs its
migrations run too. You can run them from GitHub Actions or from your computer. If a
deploy is ahead of its database, Goatside opens on a page that says how many migrations
haven't run, instead of failing on every page.

### Your URL and token [#your-url-and-token]

Both ways need your database's URL and token.

<Tabs groupId="database" persist items={["Vercel Marketplace", "Turso directly"]}>
<Tab value="Vercel Marketplace">

They're your Vercel project's `TURSO_DATABASE_URL` and `TURSO_AUTH_TOKEN`. In the
project, open **Storage** and select the database to see them. Or copy them into your
clone with the Vercel CLI:

```sh
cd apps/goatside
vercel link    # first time only
vercel env pull .env.production.local --environment=production
```

Delete that file once you've used the values. Git ignores it, but it holds your
production token.

</Tab>
<Tab value="Turso directly">

```sh
turso db show goatside --url
turso db tokens create goatside
```

</Tab>
</Tabs>

### With GitHub Actions [#with-github-actions]

Your fork can apply migrations on every push to `main`, once the checks pass.

1. Open your fork's **Actions** tab and enable workflows. GitHub turns them off on
   new forks, and until you do, nothing runs.
2. Under **Settings → Secrets and variables → Actions**, add `TURSO_DATABASE_URL` and
   `TURSO_AUTH_TOKEN` as repository secrets.

To migrate without pushing, open **CI** on the **Actions** tab and choose **Run
workflow** on `main`. Without the secrets, the migrate job skips its work and leaves a
notice on the run.

Vercel deploys the same push on its own and can finish first. Until the migration lands,
usually a minute or two, Goatside shows that it's waiting for it and checks again every
15 seconds. If you'd rather avoid that, run the migration by hand before you push.

### From your computer [#from-your-computer]

From your clone:

```sh
TURSO_DATABASE_URL=libsql://goatside-<you>.turso.io \
TURSO_AUTH_TOKEN=<token> \
pnpm db:migrate
```

Running it again is safe. It skips migrations that are already applied.

Without `TURSO_DATABASE_URL`, `pnpm db:migrate` targets the local database at
`http://127.0.0.1:8080`. It doesn't read `.env.local`, so pass the URL every time you
mean production.

## What's set up on sign-in [#whats-set-up-on-sign-in]

Every successful sign-in makes sure your settings and default categories exist. It only
adds what's missing, so it never undoes a rename or an archived category.

- Spending categories: Food, Groceries, Transport, Bills, Subscriptions, Shopping,
  Health, Fun, Others.
- Income categories: Salary, Freelance, Gifts, Others.

## Locally [#locally]

`pnpm dev` starts a local libSQL server on `:8080` with the data in
`packages/db/.data/local.db`. You can also run it alone with `pnpm db:dev`.

| Command            | What it does                                                     |
| ------------------ | ---------------------------------------------------------------- |
| `pnpm db:migrate`  | Applies migrations                                               |
| `pnpm db:generate` | Writes a new migration after you change `packages/db/src/schema` |
| `pnpm db:studio`   | Opens Drizzle Studio to browse the data                          |

To try Goatside with three months of sample data, start a second local database and fill
it. The demo script refuses remote URLs, and a database that already has entries.

```sh
turso dev --db-file packages/db/.data/demo.db --port 8090
TURSO_DATABASE_URL=http://127.0.0.1:8090 pnpm db:migrate
TURSO_DATABASE_URL=http://127.0.0.1:8090 pnpm --filter @workspace/db demo
```

## Backups [#backups]

Your ledger is only in your database, so keep a copy. Turso keeps point-in-time history
you can restore from, and how far back depends on your plan. With the Turso CLI, you can
also export the database as a SQLite file at any time:

```sh
turso db export goatside
```

For a spreadsheet of your entries, use **Export** on the Transactions page.
