Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Schema and data migrations for node js

Is there any tool that works similar to Django South, but for Node?

Now I'm working with Sequelize. If I got it right, Sequelize does not have an option to create migration files based on existing models. So, to create a new model/table, the steps are:

  • Create model with sequelize model:create <model meta>.
  • Edit generated migration file - add actual code for creating tables in DB under up section.
  • Run migration with sequelize db:migrate.

I'm looking for something that can create migration files based on existing models, manage it similar to what South can do for Django.

Is there any option?

like image 838
DanDuh Avatar asked Sep 09 '15 06:09

DanDuh


People also ask

What is schema in data migration?

Database migrations, also known as schema migrations, database schema migrations, or simply migrations, are controlled sets of changes developed to modify the structure of the objects within a relational database.

How do you schema migration?

Migrations are performed programmatically by using a schema migration tool. When invoked with a specified desired schema version, the tool automates the successive application or reversal of an appropriate sequence of schema changes until it is brought to the desired state.

What are migrations in node JS?

A migration file contains code to apply the changes, and code to remove the changes again. With that format, it's possible to switch (migrate) between one set of changes and another pretty seamlessly. Again, there are tons of modules out there that provide database migrations for node. js .

What is schema in model in node JS?

A mongoose schema defines the shape of documents inside a particular collection.


1 Answers

I have written a step-by-step guide on how to auto-create migrations with Sequelize in another post. Here is a summary...

The closest thing with Sequelize is Sequelize Auto Migrations.

It allows you to have an iteration cycle like the following:

  1. Create/update model -- by hand or with sequelize-cli)
  2. Run makemigrations to auto-generate up and down migrations
  3. Repeat as necessary

While this is very helpful, I've found it to be lacking in some critical areas:

  1. The down migrations can be created incorrectly. So it may try to drop a table before its dependent tables have been dropped.
  2. There are certain configurations around multi-field indexes that it has not correctly output.

There are currently 10 outstanding PRs, so it seems like a few additional contributors are attempting to make it more production-ready... but I've yet to find anything as clean and reliable as Django Migrations (formerly Django South).

like image 68
PaulMest Avatar answered Oct 01 '22 04:10

PaulMest