Files
medusa-store/docs/content/advanced/backend/migrations/index.md
Shahed Nasser 589cb18f98 docs: improved SEO of documentation (#3117)
* docs: added description to documentation pages

* docs: added more descriptions

* docs: finished improving meta description

* docs: added searchbox structured data

* docs: added breadcrumbs structured data

* docs: added how to structured data

* docs: improved 404 page

* docs: added how-to frontmatter option
2023-01-26 15:58:33 +02:00

2.3 KiB
Raw Blame History

description, addHowToData
description addHowToData
Learn how to create a migration in the Medusa server. This guide explains how to write and run migrations. true

How to Create Migrations

In this document, youll learn how to create a Migration using Typeorm on your Medusa server.

Create Migration File

To create a migration that makes changes to your Medusa schema, run the following command:

npx typeorm migration:create -n UserChanged --dir src/migrations

This will create the migration file in the path you specify. You can use this without the need to install Typeorm's CLI tool. You can then go ahead and make changes to it as necessary.

The migration file must be inside the src/migrations directory. When you run the build command, it will be transpiled into the directory dist/migrations. The migrations run command can only pick up migrations under the dist/migrations directory on a Medusa server. This applies to migrations created in a Medusa server, and not in a Medusa plugin. For plugins, check out the Plugin's Structure section.

:::tip

You can alternatively use Typeorm's generate command to generate a Migration file from existing database tables, which requires setting up a data source in Typeorm. Check out Typeorm's documentation to learn how to create a data source, then use the generate command.

:::


Write Migration File

The migration file contains the necessary commands to create the database columns, foreign keys, and more.

You can learn more about writing the migration file in You can learn more about writing migrations in Typeorms Documentation.


Build Files

Before you can run the migrations you need to run the build command to transpile the TypeScript files to JavaScript files:

npm run build

Run Migration

The last step is to run the migration with the command detailed earlier

medusa migrations run

If you check your database now you should see that the change defined by the migration has been applied successfully.


Whats Next