Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor Prisma updates #2166

Merged
merged 2 commits into from
Jul 9, 2024
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
13 changes: 9 additions & 4 deletions waspc/src/Wasp/Project/Analyze.hs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ analyzePackageJsonContent waspProjectDir =
Just packageJsonFile -> readPackageJsonFile packageJsonFile
Nothing -> return $ Left [fileNotFoundMessage]
where
fileNotFoundMessage = "couldn't find package.json file in the " ++ toFilePath waspProjectDir ++ " directory"
fileNotFoundMessage = "Couldn't find the package.json file in the " ++ toFilePath waspProjectDir ++ " directory"

findPackageJsonFile :: Path' Abs (Dir WaspProjectDir) -> IO (Maybe (Path' Abs File'))
findPackageJsonFile waspProjectDir = findFileInWaspProjectDir waspProjectDir packageJsonInWaspProjectDir
Expand All @@ -152,11 +152,16 @@ analyzePrismaSchema waspProjectDir = do

case Psl.Parser.parsePrismaSchema prismaSchemaContent of
Left err ->
return (Left [waspCouldntParsePrismaSchemaMessage ++ "\n\n" ++ show err], [])
return (Left [couldntParsePrismaSchemaMessage ++ "\n\n" ++ show err], [])
Right parsedPrismaSchema -> return $ runValidation PslV.validatePrismaSchema parsedPrismaSchema
Nothing -> return (Left ["Couldn't find the Prisma schema file in the " ++ toFilePath waspProjectDir ++ " directory"], [])
Nothing -> return (Left [couldntFindPrismaSchemaMessage], [])
where
waspCouldntParsePrismaSchemaMessage = "Wasp couldn't parse your Prisma schema file, please check if you have any errors in it."
couldntParsePrismaSchemaMessage = "Wasp couldn't parse your schema.prisma file, please check if you have any errors in it."

-- NOTE: linking here to migration docs because I think it's the most common reason why schema.prisma file is missing.
-- After people mostly start using 0.14.0+ they will have schema.prisma file, so this message will be less relevant.
-- If we see that this message is still relevant, we can change it to be more general.
couldntFindPrismaSchemaMessage = "Couldn't find the schema.prisma file in the " ++ toFilePath waspProjectDir ++ " directory. \nRead more: https://wasp-lang.dev/docs/migrate-from-0-13-to-0-14#migrate-to-the-new-schemaprisma-file"

runValidation :: (result -> [ValidationError]) -> result -> (Either [CompileError] result, [CompileWarning])
runValidation getErrorsAndWarnings result =
Expand Down
290 changes: 207 additions & 83 deletions web/docs/migrate-from-0-13-to-0-14.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,113 +92,237 @@ Wasp introduced a much simpler API for accessing user auth fields like `username
### Migrate to the new `schema.prisma` file
To use the new `schema.prisma` file, you need to move your entities from the `.wasp` file to the `schema.prisma` file.

1. **Create a new `schema.prisma` file**
1\. **Create a new `schema.prisma` file**

Create a new file named `schema.prisma` in the root of your project:

```c
.
├── main.wasp
...
// highlight-next-line
├── schema.prisma
├── src
├── tsconfig.json
└── vite.config.ts
```
2\. **Add the `datasource` block** to the `schema.prisma` file

Create a new file named `schema.prisma` in the root of your project:
This block specifies the database type and connection URL:

```c
.
├── main.wasp
...
// highlight-next-line
├── schema.prisma
├── src
├── tsconfig.json
└── vite.config.ts
```
2. **Add the `datasource` block** to the `schema.prisma` file
<Tabs groupId="db">
<TabItem value="sqlite" label="Sqlite">

This block specifies the database type and connection URL:
```prisma title="schema.prisma"
datasource db {
provider = "sqlite"
url = env("DATABASE_URL")
}
```
</TabItem>
<TabItem value="postgresql" label="PostgreSQL">

```prisma title="schema.prisma"
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
```

- The `provider` should be either `"postgresql"` or `"sqlite"`.

- The `url` must be set to `env("DATABASE_URL")` so that Wasp can inject the database URL from the environment variables.
```prisma title="schema.prisma"
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
```
</TabItem>
</Tabs>

3. **Add the `generator` block** to the `schema.prisma` file
- The `provider` should be either `"postgresql"` or `"sqlite"`.

This block specifies the Prisma Client generator Wasp uses:
- The `url` must be set to `env("DATABASE_URL")` so that Wasp can inject the database URL from the environment variables.

```prisma title="schema.prisma"
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
3\. **Add the `generator` block** to the `schema.prisma` file

// highlight-start
generator client {
provider = "prisma-client-js"
}
// highlight-end
```
This block specifies the Prisma Client generator Wasp uses:

- The `provider` should be set to `"prisma-client-js"`.
<Tabs groupId="db">
<TabItem value="sqlite" label="Sqlite">

4. **Move your entities** to the `schema.prisma` file
```prisma title="schema.prisma"
datasource db {
provider = "sqlite"
url = env("DATABASE_URL")
}

Move the entities from the `.wasp` file to the `schema.prisma` file:
// highlight-start
generator client {
provider = "prisma-client-js"
}
// highlight-end
```
</TabItem>
<TabItem value="postgresql" label="PostgreSQL">

```prisma title="schema.prisma"
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

generator client {
provider = "prisma-client-js"
}
```prisma title="schema.prisma"
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

// There are some example entities, you should move your entities here
// highlight-start
model User {
id Int @id @default(autoincrement())
tasks Task[]
}
// highlight-start
generator client {
provider = "prisma-client-js"
}
// highlight-end
```
</TabItem>
</Tabs>

- The `provider` should be set to `"prisma-client-js"`.

4\. **Move your entities** to the `schema.prisma` file

Move the entities from the `.wasp` file to the `schema.prisma` file:

<Tabs groupId="db">
<TabItem value="sqlite" label="Sqlite">

model Task {
id Int @id @default(autoincrement())
description String
isDone Boolean
userId Int
user User @relation(fields: [userId], references: [id])
```prisma title="schema.prisma"
datasource db {
provider = "sqlite"
url = env("DATABASE_URL")
}

generator client {
provider = "prisma-client-js"
}

// There are some example entities, you should move your entities here
// highlight-start
model User {
id Int @id @default(autoincrement())
tasks Task[]
}

model Task {
id Int @id @default(autoincrement())
description String
isDone Boolean
userId Int
user User @relation(fields: [userId], references: [id])
}
// highlight-end
```
</TabItem>
<TabItem value="postgresql" label="PostgreSQL">


```prisma title="schema.prisma"
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

generator client {
provider = "prisma-client-js"
}

// There are some example entities, you should move your entities here
// highlight-start
model User {
id Int @id @default(autoincrement())
tasks Task[]
}

model Task {
id Int @id @default(autoincrement())
description String
isDone Boolean
userId Int
user User @relation(fields: [userId], references: [id])
}
// highlight-end
```
</TabItem>
</Tabs>


When moving the entities over, you'll need to change `entity` to `model` and remove the `=psl` and `psl=` tags.

If you had the following in the `.wasp` file:
```wasp title="main.wasp"
entity Task {=psl
// Stays the same
psl=}
```

... it would look like this in the `schema.prisma` file:
```prisma title="schema.prisma"
model Task {
// Stays the same
}
```

5\. **Remove `app.db.system`** field from the Wasp file

We now configure the DB system in the `schema.prisma` file, so there is no need for that field in the Wasp file.

```wasp title="main.wasp"
app MyApp {
// ...
db: {
// highlight-next-line
system: PostgreSQL,
}
// highlight-end
```

When moving the entities over, you'll need to change `entity` to `model` and remove the `=psl` and `psl=` tags.

If you had the following in the `.wasp` file:
```wasp title="main.wasp"
entity Task {=psl
// Stays the same
psl=}
```

... it would look like this in the `schema.prisma` file:
```prisma title="schema.prisma"
model Task {
// Stays the same
}
```

6\. **Migrate Prisma preview features config** to the `schema.prisma` file

If you didn't use any Prisma preview features, you can skip this step.

If you had the following in the `.wasp` file:

```wasp title="main.wasp"
app MyApp {
// ...
db: {
// highlight-start
prisma: {
clientPreviewFeatures: ["postgresqlExtensions"]
dbExtensions: [
{ name: "hstore", schema: "myHstoreSchema" },
{ name: "pg_trgm" },
{ name: "postgis", version: "2.1" },
]
}
// highlight-end
}
```
}
```

... it will become this:

5. **Run the Wasp CLI** to generate the Prisma client
```prisma title="schema.prisma"
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
// highlight-next-line
extensions = [hstore(schema: "myHstoreSchema"), pg_trgm, postgis(version: "2.1")]
}

generator client {
provider = "prisma-client-js"
// highlight-next-line
previewFeatures = ["postgresqlExtensions"]
}
```

Regenerate the Prisma client by running the following command:
7\. **Run the Wasp CLI** to generate the Prisma client

```bash
wasp db migrate-dev
```
Regenerate the Prisma client by running the following command:

This command generates the Prisma client based on the `schema.prisma` file.
```bash
wasp db migrate-dev
```

Now you are using the new `schema.prisma` file!
This command generates the Prisma client based on the `schema.prisma` file.

Read more about the [Prisma Schema File](./data-model/prisma-file.md) and how Wasp uses it to generate the database schema and Prisma client.

Expand Down
Loading