docs/1.30/datamodel-and-migrations/migrations-POSTGRES-asd4.mdx
import Warning from 'components/Markdown/Warning' import Code from 'components/Markdown/Code'
export const meta = { title: "Migrations (PostgreSQL)", position: 240, articleGroup: "Migrations", technology: "postgres", technologyOrder: 1, }
You can configure Prisma with your PostgreSQL database in either of two ways:
prisma deploy command). You can achieve this by setting the migrations flag in the PRISMA_CONFIG variable of your Prisma server to true.migrations flag in the PRISMA_CONFIG variable of your Prisma server to false.Note that the
migrationsflag will be removed at some point and Prisma's migration model will become more flexible. This means you can either use the Prisma CLI to perform migrations withprisma deployor perform your migrations manually. Learn more in this GitHub issue or explore the spec for the upcoming Prisma version.
To enable database migrations, set the migrations property to true:
<Code hideCopy={true} lines={'7'}>
PRISMA_CONFIG: |
managementApiSecret: __YOUR_MANAGEMENT_API_SECRET__
port: 4466
databases:
default:
connector: postgres
migrations: true
host: __YOUR_DATABASE_HOST__
port: __YOUR_DATABASE_PORT__
user: __YOUR_DATABASE_USER__
password: __YOUR_DATABASE_PASSWORD__
connectionLimit: __YOUR_CONNECTION_LIMIT__
If database migrations are enabled, you use SDL to define and migrate your database schema. There are two steps to every migration:
prisma deploy to apply the changes and perform the migration of the underlying databasePrisma uses temporary directives to perform one-time migration operations. After deploying a service that contain a temporary directive, a temporary directive needs to be manually removed from the datamodel file.
The temporary directive @rename(oldName: String!) is used to rename a type or a field of a type.
If the @rename directive is not used, Prisma deletes old type/field before creating the new one, resulting in loss of data!
Renaming a type
Here is an example scenario:
type Post {
text: String
}
Rename the Post type to Story
@rename directive# renaming the `Post` type to `Story`
type Story @rename(oldName: "Post") {
text: String
}
prisma deployAfter having the saved the datamodel file with these changes, you need to run:
prisma deploy
@rename directive manuallytype Story {
text: String
}
Renaming a field
Here is an example scenario:
type Post {
text: String
}
Rename the text field to content
@rename directive# renaming the `text` field to `content`
type Post {
content: String @rename(oldName: "text")
}
prisma deployAfter having the saved the datamodel file with these changes, you need to run:
prisma deploy
@rename directive manuallytype Post {
content: String
}
@relation directive.To disable database migrations, set the migrations property to false:
<Code hideCopy={true} lines={'7'}>
PRISMA_CONFIG: |
managementApiSecret: __YOUR_MANAGEMENT_API_SECRET__
port: 4466
databases:
default:
connector: postgres
migrations: false
host: __YOUR_DATABASE_HOST__
port: __YOUR_DATABASE_PORT__
user: __YOUR_DATABASE_USER__
password: __YOUR_DATABASE_PASSWORD__
connectionLimit: __YOUR_CONNECTION_LIMIT__
When migrations are disabled, you need to manually ensure that your datamodel is in sync with the unterlying database schema, otherwise the Prisma API might exhibit undefined behaviour!
This means you need to ensure that:
@pgTable directive@pgRelation directiveNote that you can exlude tables and/or fields from your database in the datamodel. For example, if you have a user table with a password column, you don't need to include the password field in your datamodel, or you can leave out the user type altogether. In that case, the structues that are not defined in the datamodel will not be exposed by Prisma.
You can use the prisma introspect command to generate a datamodel based on your current database schema. Learn more about database introspection with PostgreSQL here.
The prisma introspect command introspects your database schema and creates a datamodel file based on that. The name of the datamodel file will include a timestamp so that the generated datamodel doesn't conflict with your existing one, e.g. datamodel-1547547645423.prisma.
prisma introspect does not change your database schema. It only generates the temporary datamodel file that reflects the current database schema. You can use this temporary file as a helper to adjust your actual datamodel by copying and pasting the new types and fields into it. Once you're happy with the adjusted datamodel, you need to run prisma deploy to actually apply the changes and update the Prisma API.