Skip to content

Instantly share code, notes, and snippets.

@bulkan
Last active January 14, 2024 05:48
Show Gist options
  • Save bulkan/8503515 to your computer and use it in GitHub Desktop.
Save bulkan/8503515 to your computer and use it in GitHub Desktop.
Using Sequelize migrations with an existing database

Install

I'm sure you know know how to install packages but here is the command for the sake of completeness

npm install sequelize async

The first migration

First initilize the migrations structure

sequelize --init

Then create the initial migration, but dont edit this file as we will use it create the SequelizeMeta table.

sequelize -c initial

Create another migration

sequelize -c create-tables

Now dump your database without the data. With mysqldump

mysqldump -d --compact --compatible=mysql323 ${dbname}|egrep -v "(^SET|^/\*\!)".

We need to remove the `SET

Save this dump to the migrations folder and name it intial.sql

Edit the last migration that was created to look like;

var async = require('async')
  , fs = require('fs');

module.exports = {
  up: function(migration, DataTypes, done) {
    var db = migration.migrator.sequelize;

    async.waterfall([
      function(cb){
        fs.readFile(__dirname + '/initial.sql', function(err, data){
          if (err) throw err;
          cb(null, data.toString());
        });
      },

      function(initialSchema, cb){
        // need to split on ';' to get the individual CREATE TABLE sql
        // as db.query can execute on query at a time
        var tables = initialSchema.split(';');

        function createTable(tableSql, doneInsert){
          db.query(tableSql);
        }

        async.each(tables, createTable, cb);
      }
    ], done);
  },

  down: function(migration, DataTypes, done) {
    migration.showAllTables().success(function(tableNames){
    
      // Dont drop the SequelizeMeta table 
      var tables = tableNames.filter(function(name){
        return name.toLowerCase() !== 'sequelizemeta';
      });

      function dropTable(tableName, cb){
        migration.dropTable(tableName);
        cb();
      }

      async.each(tables, dropTable, done);
    });
  }
}

On the migrations up function we use async.waterfall to orchestrate a the async calls;

  • read in the initial.sql file
  • need to split the initial.sql and retrieve each CREATE TABLE queries as db.query can execute on query at a time
  • using async.each run each of these queries

On the migrations down function we just remove all tables that is not the SequelizeMeta table. For some reason migration.dropAllTables() remove this table and messes up the migrations. Not sure if this is the correct behavior.

@fedikhatib
Copy link

2024 working great thanks

@bulkan
Copy link
Author

bulkan commented Jan 14, 2024

@fedikhatib that is surprising that it's working in 2024 ! I haven't even used Sequelize since this was written.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment