From 8c6485155e553000c8491fcf6b0dd25a86a62513 Mon Sep 17 00:00:00 2001 From: "Harish.K" Date: Tue, 16 Feb 2016 13:01:17 +0530 Subject: [PATCH] Initial commit --- data/migration-file.js.ect | 42 ++++++++++++ src/gen-migrations.js | 129 +++++++++++++++++++++++++++++++++++++ 2 files changed, 171 insertions(+) create mode 100644 data/migration-file.js.ect create mode 100644 src/gen-migrations.js diff --git a/data/migration-file.js.ect b/data/migration-file.js.ect new file mode 100644 index 0000000..0144b23 --- /dev/null +++ b/data/migration-file.js.ect @@ -0,0 +1,42 @@ +/* This file is auto-generated using https://github.com/harish2704/sequelize-migration-generator. */ + +'use strict'; + +module.exports = { + up: function (queryInterface, Sequelize) { + return queryInterface.createTable('<%- @model.tableName %>', { +<% for field in @model.fields : %> + <%- field.name %>: { + type: Sequelize.<%- field.type %><%- field.values %><%- field.length %><%- '.UNSIGNED' if field.unsigned %>, +<% if field.references?.model : %> + references: <%- JSON.stringify field.references %>, +<% end %> +<% for opt in @fieldOpts : %> +<% if not field[opt].isEmpty() : %> + <%- opt %> : <%- field[opt] %>, +<% end %> +<% end %> + }, + +<% end %> + })<% if ( Object.keys @model.uniqueKeys ).length : %> +<% for keyName, keyOpts of @model.uniqueKeys : %> + + .then( function(){ + return queryInterface.addIndex( + '<%- @model.tableName %>', + <%- JSON.stringify keyOpts.fields %>, + { + indexName: '<%- keyName %>', + indicesType: 'UNIQUE' + } + ) +<% end %> + })<% end %>; + }, + + down: function (queryInterface, Sequelize) { + return queryInterface.dropTable( '<%- @model.tableName %>' ); + } +}; + diff --git a/src/gen-migrations.js b/src/gen-migrations.js new file mode 100644 index 0000000..2896e9b --- /dev/null +++ b/src/gen-migrations.js @@ -0,0 +1,129 @@ + +var util = require('util'); + +if( typeof exports == 'undefined' ){ global.exports = {}; } + + +function Val( val ){ + this.val = val; +} +Val.prototype = { + isEmpty: function(){ + if( this.val == null ){ + return true; + } + switch ( this.val.constructor.name ) { + case 'Boolean': + return false; + case 'Array': + return this.val.length == 0; + default: + return !Boolean( this.val ); + } + }, + toString: function(){ + return this.isEmpty()? '' : this._toCode(); + }, + _toCode: function(){ + return this.toCode ? this.toCode() : this.val; + } +}; + + +function QuotedVal( val ){ + Val.call( this, val ); +} +util.inherits( QuotedVal, Val ); +QuotedVal.prototype.toCode = function(){ + return "'" + this.val + "'"; +}; + + +function LengthVal( val ){ + Val.call( this, val ); +} +util.inherits( LengthVal, Val ); +LengthVal.prototype.toCode = function(){ + return '( ' + this.val +' )'; +}; + + +function ValuesVal( val ){ + Val.call( this, val ); +} +util.inherits( ValuesVal, Val ); +ValuesVal.prototype.toCode = function(){ + return '( ' + JSON.stringify( this.val ).slice(1,-1) +' )'; +}; + + + +function Field( attr ){ + var options = attr.type.options || {}; + + this.type = attr.type.key; + this.name = attr.field; + + this.autoIncrement = new Val( attr.autoIncrement ); + this.allowNull = new Val( attr.allowNull ); + this.primaryKey = new Val( attr.primaryKey ); + this.onDelete = new QuotedVal( attr.onDelete ); + this.onUpdate = new QuotedVal( attr.onUpdate ); + + this.references = attr.references; + + this.unsigned = options.unsigned; + this.values = new ValuesVal( options.values ); + this.length = new LengthVal( options.length ); + +} + +Field.opts = [ + 'autoIncrement', + 'allowNull', + 'primaryKey', + 'onDelete', + 'onUpdate' +]; + +exports.Field = Field; + + + + +function Model( model ){ + this.tableName = model.tableName; + this.uniqueKeys = model.uniqueKeys; + this.fields = Object.keys( model.attributes ).map( function( key ){ + return new Field( model.attributes[key] ); + }); +} +exports.Model = Model; + + + + +if( require.main == module ){ + + var models = require('./models'); + var ect = require('ect')(); + var fs = require( 'fs'); + + [ + 'model1', + 'model2', + 'model3', + 'model4', + 'model5', + + ].forEach( function(modelName, i ){ + var m = new Model( models[modelName] ); + var out = ect.render('./data/migration-file.js.ect', { model: m, fieldOpts: Field.opts }); + var timestamp = new Date( Date.now() + i ).toISOString().replace(/[-:.TZ]/g, ''); + fs.writeFileSync( './migrations/' + timestamp + '-create_table_' + modelName + '_mi-generated.js', out ); + }); + +} + + +