Coder Social home page Coder Social logo

migrate's Introduction

Prisma


Declarative data modeling & database migrations

Get startedFeaturesDocsWorkflowSupported databases


Prisma Migrate is a powerful database schema migration tool. It uses a declarative data modelling syntax to describe your database schema. Prisma Migrate also stores your entire migration history and easily lets you revert and replay migrations. When migrating your database with Prisma Migrate, you can run provide before- and after-hooks to execute scripts, e.g. to populate the database with required values during a migration.

WARNING: Prisma Migrate is currently in an experimental state. The version available has a number of limitations that make it unsuitable for production workloads, including missing features, limited performance and stability issues.

Docs

Features

  • Declarative data modelling syntax
  • Supports relational and document databases (more coming soon)
  • Keeps a migration history
  • Before- and after hooks to run scripts for complex migrations
  • Simple defaults, optional complexity for advanced use cases
  • Revert and replay migrations
  • Works with existing databases using schema introspection
  • CLI to support all major workflows

Docs

You can find comprehensive documentation for Prisma Migrate in the Prisma 2 docs.

The Prisma Migrate workflow

1. Configure database access

Specify the connection details for your database as a data source in your Prisma schema file. The connection details might differ per database, but most commonly you'll provide the following:

  • Host: The IP address or domain name of the machine where your database server is running.
  • Port: The port on which your database server is listening.
  • User & password: Credentials for your database server.

Here is an example project file that connects to a local PostgreSQL database:

// schema.prisma

datasource postgres {
  url      = "postgresql://user:password@localhost:5432"
  provider = "postgres"
}

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

2. Define initial data model

The data model definition is a declarative and human-readable representation of your database schema. Here is the project file from above extended with a sample data model:

// schema.prisma

datasource postgres {
  url      = "postgresql://user:password@localhost:5432"
  provider = "postgres"
}

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

model User {
  id        Int      @id
  createdAt DateTime @default(now())
  email     String   @unique
  name      String?
  role      Role     @default(USER)
  posts     Post[]
}

model Post {
  id         Int        @id
  createdAt  DateTime   @default(now())
  updatedAt  DateTime   @updatedAt
  author     User
  title      String
  published  Boolean    @default(false)
}

enum Role {
  USER
  ADMIN
}

Option A: Starting with an existing database

If you want to use Prisma Migrate with an existing database, you can introspect your database schema using the Prisma 2 CLI. This generates a declarative data model which provides the foundation for future migrations.

Option B: Start from scratch

When starting from scratch, you can write your own data model definition inside your Prisma schema file. You can then use the Prisma Migrate CLI commands to migrate your database (Prisma Migrate maps your data model definition to the schema of the underlying database).

3. Adjust the data model

Instead of sending SQL migration statements to the database, you need to adjust the data model file to describe your desired database schema. You can express any schema migration you like using the new data model, this includes for example adding a new model, removing a model or updating the fields of a model. You can also add indexes or validation constraints in the data model.

You can create a new migration for your change by running prisma2 migrate save:

prisma2 migrate save --name "add-comment-model" --experimental

4. Migrate your database (apply data model changes)

Once you're happy with the changes, you can use the Prisma CLI to migrate your database (i.e. map the adjusted data model to your database). Prisma Migrate's migration engine will generate the corresponding SQL statements and send them to the database for you.

prisma2 migrate up --experimental

Supported databases

Prisma Client JS can be used with the following databases:

  • MySQL
  • PostgreSQL
  • MongoDB (coming very soon)

More databases that will be supported in the future are:

  • MS SQL
  • Oracle
  • Neo4J
  • FaunaDB
  • ...

Contributing

Read more about how to contribute to Prisma Migrate here

Build status

migrate's People

Contributors

timsuchanek avatar prisma-bot avatar jolg42 avatar nikolasburk avatar matthewmueller avatar ejoebstl avatar janpio avatar errorname avatar tomhoule avatar jebrial avatar schickling avatar kripod avatar kuldar avatar sergioramos avatar tejasq avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.