## Change your schema

Use migrations for changes after the initial database setup.

### 1. Edit the schema

Add or update application tables in `src/db/schema.ts`. Export each table and include it in the existing `schema` object.

Keep the authentication exports from `src/db/auth-schema.ts`. PlanetScale also includes its relation exports; Turso configures relational queries with `defineRelations(schema)`.

### 2. Generate SQL

```sh npm
npm run db:generate
```

```sh bun
bun run db:generate
```

```sh pnpm
pnpm run db:generate
```

```sh yarn
yarn run db:generate
```

Open the new migration in `drizzle/` and review its SQL before applying it.

### 3. Apply the migration

Development:

```sh npm
npm run db:migrate
```

```sh bun
bun run db:migrate
```

```sh pnpm
pnpm run db:migrate
```

```sh yarn
yarn run db:migrate
```

Staging:

```sh npm
npm run db:migrate:staging
```

```sh bun
bun run db:migrate:staging
```

```sh pnpm
pnpm run db:migrate:staging
```

```sh yarn
yarn run db:migrate:staging
```

Production:

```sh npm
npm run db:migrate:production
```

```sh bun
bun run db:migrate:production
```

```sh pnpm
pnpm run db:migrate:production
```

```sh yarn
yarn run db:migrate:production
```

Confirm the target database and prepare a backup/recovery procedure before production changes. Apply migrations sequentially, outside deployment builds.

Commit the schema, `drizzle/` directory, and `.dinvolt/initial-database.json` together. All environments share this migration directory; each database tracks its own applied migrations.

### Inspect the database

Open Drizzle Studio for development:

```sh npm
npm run db:studio
```

```sh bun
bun run db:studio
```

```sh pnpm
pnpm run db:studio
```

```sh yarn
yarn run db:studio
```

Check migration consistency without connecting to a database:

```sh npm
npm run db:check
```

```sh bun
bun run db:check
```

```sh pnpm
pnpm run db:check
```

```sh yarn
yarn run db:check
```

Schema generation also works without a database connection. Studio and migration commands require credentials.


