Skip to content

Auto-Create Migrations #257

@jgr3go

Description

@jgr3go

I know this has been discussed before, but it would be really helpful if sequelize could auto-create migration scripts based on differences in sequelizer and the database.

I currently have a fork I'm testing out that attempts to solve this issue, and I wanted to open up the discussion again to make sure this would be accepted by sequelize/the community.

Right now the rules put into place for sequelize migration:create:auto does the following (note, this treats sequelize as canon, so it will attempt to resolve the migrations to create a state in the database that matches sequelizer imports):

  1. Read model.js files from a new config/models.json, and import the model/column definitions into sequelizer.
  2. Read all tables from the database and import the model/column definitions
  3. Compare:
    • If a column exists in the database and not sequelizer, create removeColumn() migration
    • If a column exists in sequelizer and not database, create addColumn() migration
    • If a table exists in database and not in sequelizer, print a WARNING, unless --drop flag is present, in which case create a dropTable() migration
    • If a table exists in sequelizer and not database, do nothing. It's currently assumed sequelizer.sync() will take care of this.
  4. Keep track of auto-migrations by filename convention: {timestamp}-{model}-auto-{version}, e.g. 20160129135511-users-auto-0002.js . If there were multiple changes, it will chain them all together in the same model file, relative to the models they're affecting. It will also clean any auto migration files that haven't been migrated to the database yet.

This works very similar to how Django's South works, but it's a little less fleshed out. It (currently) doesn't handle column alters, indexes, associations, etc. Before I (or someone else I suppose) continues development on it I'd like to start a discussion to see if this is worth pursuing and if the owners have any hesitations on this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions