Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 42 additions & 5 deletions content/800-guides/010-data-migration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ Start with a basic schema containing a Post model:

```prisma
generator client {
provider = "prisma-client-js"
provider = "prisma-client"
output = "./generated/prisma"
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

model Post {
Expand All @@ -61,7 +61,36 @@ model Post {
}
```

### 1.2. Create a development branch
### 1.2. Configure Prisma

Create a `prisma.config.ts` file in the root of your project with the following content:

```typescript file=prisma.config.ts
import 'dotenv/config'
import { defineConfig, env } from 'prisma/config';

export default defineConfig({
schema: 'prisma/schema.prisma',
migrations: {
path: 'prisma/migrations',
},
datasource: {
url: env('DATABASE_URL'),
},
});
```

:::note

You'll need to install the `dotenv` package to load environment variables. If you haven't already, install it using your package manager:

```bash
npm install dotenv
```

:::

### 1.3. Create a development branch

Create a new branch for your changes:

Expand Down Expand Up @@ -108,9 +137,17 @@ npx prisma migrate dev --name add-status-column
Create a new TypeScript file for the data migration:

```typescript
import { PrismaClient } from '@prisma/client'
import { PrismaClient } from '../generated/prisma/client'
import { PrismaPg } from '@prisma/adapter-pg'
import 'dotenv/config'

const adapter = new PrismaPg({
connectionString: process.env.DATABASE_URL,
})

const prisma = new PrismaClient()
const prisma = new PrismaClient({
adapter,
})

async function main() {
await prisma.$transaction(async (tx) => {
Expand Down
29 changes: 29 additions & 0 deletions content/800-guides/020-implementing-schema-changes.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,35 @@ Source-controlling the `schema.prisma` file is not enough - you must include you
- Customized migrations contain information that cannot be represented in the Prisma schema
- The `prisma migrate deploy` command only runs migration files

### 1.3. Configure Prisma

Create a `prisma.config.ts` file in the root of your project with the following content:

```typescript file=prisma.config.ts
import 'dotenv/config'
import { defineConfig, env } from 'prisma/config';

export default defineConfig({
schema: 'prisma/schema.prisma',
migrations: {
path: 'prisma/migrations',
},
datasource: {
url: env('DATABASE_URL'),
},
});
```

:::note

You'll need to install the `dotenv` package to load environment variables. If you haven't already, install it using your package manager:

```bash
npm install dotenv
```

:::

## 2. Incorporate team changes

### 2.1. Pull latest changes
Expand Down
29 changes: 29 additions & 0 deletions content/800-guides/030-migrate-from-typeorm.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,35 @@ Update the `DATABASE_URL` in the `.env` file with your database connection strin
DATABASE_URL="postgresql://USER:PASSWORD@HOST:PORT/DATABASE"
```

### 2.3. Configure Prisma

Create a `prisma.config.ts` file in the root of your project with the following content:

```typescript file=prisma.config.ts
import 'dotenv/config'
import { defineConfig, env } from 'prisma/config';

export default defineConfig({
schema: 'prisma/schema.prisma',
migrations: {
path: 'prisma/migrations',
},
datasource: {
url: env('DATABASE_URL'),
},
});
```

:::note

You'll need to install the `dotenv` package to load environment variables. If you haven't already, install it using your package manager:

```bash
npm install dotenv
```

:::

## 3. Migrate the database schema

### 3.1. Introspect your database
Expand Down
33 changes: 31 additions & 2 deletions content/800-guides/040-migrate-from-sequelize.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ The Prisma schema currently looks as follows:

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

generator client {
provider = "prisma-client-js"
provider = "prisma-client"
output = "./generated/prisma"
}
```

Expand All @@ -78,6 +78,35 @@ Update the `DATABASE_URL` in the `.env` file with your database connection strin
DATABASE_URL="postgresql://USER:PASSWORD@HOST:PORT/DATABASE"
```

### 1.3. Configure Prisma

Create a `prisma.config.ts` file in the root of your project with the following content:

```typescript file=prisma.config.ts
import 'dotenv/config'
import { defineConfig, env } from 'prisma/config';

export default defineConfig({
schema: 'prisma/schema.prisma',
migrations: {
path: 'prisma/migrations',
},
datasource: {
url: env('DATABASE_URL'),
},
});
```

:::note

You'll need to install the `dotenv` package to load environment variables. If you haven't already, install it using your package manager:

```bash
npm install dotenv
```

:::

## 2. Migrate the database schema

### 2.1. Introspect your database
Expand Down
29 changes: 29 additions & 0 deletions content/800-guides/050-migrate-from-mongoose.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,35 @@ Update the `DATABASE_URL` in the `.env` file with your MongoDB connection string
DATABASE_URL="mongodb://USER:PASSWORD@HOST:PORT/DATABASE"
```

### 1.3. Configure Prisma

Create a `prisma.config.ts` file in the root of your project with the following content:

```typescript file=prisma.config.ts
import 'dotenv/config'
import { defineConfig, env } from 'prisma/config';

export default defineConfig({
schema: 'prisma/schema.prisma',
migrations: {
path: 'prisma/migrations',
},
datasource: {
url: env('DATABASE_URL'),
},
});
```

:::note

You'll need to install the `dotenv` package to load environment variables. If you haven't already, install it using your package manager:

```bash
npm install dotenv
```

:::

## 2. Migrate the database schema

### 2.1. Introspect your database
Expand Down
55 changes: 44 additions & 11 deletions content/800-guides/060-migrate-from-drizzle.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ The Prisma schema currently looks as follows:

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

generator client {
provider = "prisma-client-js"
provider = "prisma-client"
output = "./generated/prisma"
}
```

Expand All @@ -107,7 +107,6 @@ If you're not using PostgreSQL, you need to adjust the `provider` field on the `
```prisma file=schema.prisma showLineNumbers
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
```

Expand All @@ -118,7 +117,6 @@ datasource db {
```prisma file=schema.prisma showLineNumbers
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
}
```

Expand All @@ -129,7 +127,6 @@ datasource db {
```prisma file=schema.prisma showLineNumbers
datasource db {
provider = "sqlserver"
url = env("DATABASE_URL")
}
```

Expand All @@ -140,7 +137,6 @@ datasource db {
```prisma file=schema.prisma showLineNumbers
datasource db {
provider = "sqlite"
url = env("DATABASE_URL")
}
```

Expand All @@ -150,7 +146,36 @@ datasource db {

Once that's done, you can configure your [database connection URL](/orm/reference/connection-urls) in the `.env` file. Drizzle and Prisma ORM use the same format for connection URLs, so your existing connection URL should work fine.

### 2.3. Introspect your database using Prisma ORM
### 2.3. Configure Prisma

Create a `prisma.config.ts` file in the root of your project with the following content:

```typescript file=prisma.config.ts
import 'dotenv/config'
import { defineConfig, env } from 'prisma/config';

export default defineConfig({
schema: 'prisma/schema.prisma',
migrations: {
path: 'prisma/migrations',
},
datasource: {
url: env('DATABASE_URL'),
},
});
```

:::note

You'll need to install the `dotenv` package to load environment variables. If you haven't already, install it using your package manager:

```bash
npm install dotenv
```

:::

### 2.4. Introspect your database using Prisma ORM

With your connection URL in place, you can [introspect](/orm/prisma-schema/introspection) your database to generate your Prisma models:

Expand All @@ -170,7 +195,7 @@ model todo {

The generated Prisma model represents a database table. Prisma models are the foundation for your programmatic Prisma Client API which allows you to send queries to your database.

### 2.4. Create a baseline migration
### 2.5. Create a baseline migration

To continue using Prisma Migrate to evolve your database schema, you will need to [baseline your database](/orm/prisma-migrate/getting-started).

Expand Down Expand Up @@ -202,7 +227,7 @@ The command will mark `0_init` as applied by adding it to the `_prisma_migration

You now have a baseline for your current database schema. To make further changes to your database schema, you can update your Prisma schema and use `prisma migrate dev` to apply the changes to your database.

### 2.5. Adjust the Prisma schema (optional)
### 2.6. Adjust the Prisma schema (optional)

Models that are generated via introspection currently _exactly_ map to your database tables. In this section, you'll learn how you can adjust the naming of the Prisma models to adhere to [Prisma ORM's naming conventions](/orm/reference/prisma-schema-reference#naming-conventions).

Expand Down Expand Up @@ -254,9 +279,17 @@ touch db/prisma.ts
Now, instantiate `PrismaClient` and export it from the file so you can use it in your route handlers later:

```ts copy file=db/prisma.ts showLineNumbers
import { PrismaClient } from '@prisma/client'
import { PrismaClient } from '../generated/prisma/client'
import { PrismaPg } from '@prisma/adapter-pg'
import 'dotenv/config'

const adapter = new PrismaPg({
connectionString: process.env.DATABASE_URL,
})

export const prisma = new PrismaClient()
export const prisma = new PrismaClient({
adapter,
})
```

### 4.1. Replacing `getData` queries
Expand Down
11 changes: 8 additions & 3 deletions content/800-guides/070-cloudflare-d1.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,12 @@ In your Prisma schema, set the `provider` of the `datasource` to `sqlite`. If yo

```prisma file=prisma/schema.prisma
generator client {
provider = "prisma-client-js"
provider = "prisma-client"
output = "../src/generated/prisma"
}

datasource db {
provider = "sqlite"
url = env("DATABASE_URL")
}

//add-start
Expand Down Expand Up @@ -179,7 +178,7 @@ CLOUDFLARE_D1_TOKEN="F8Cg..."

Ensure that you have a `prisma.config.ts` file set up in the root of your project with a [driver adapter](/orm/reference/prisma-config-reference#adapter-removed) defined.

```ts
```typescript file=prisma.config.ts
import type { PrismaConfig } from 'prisma';
import { PrismaD1 } from '@prisma/adapter-d1';

Expand All @@ -191,6 +190,12 @@ export default {
adapter: true,
},
schema: 'prisma/schema.prisma',
migrations: {
path: 'prisma/migrations',
},
datasource: {
url: process.env.DATABASE_URL!,
},
async adapter() {
return new PrismaD1({
CLOUDFLARE_D1_TOKEN: process.env.CLOUDFLARE_D1_TOKEN!,
Expand Down
Loading
Loading