Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: upgrade migration skeleton #909

Merged
merged 2 commits into from
Jun 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .github/stale.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Number of days of inactivity before an issue becomes stale
daysUntilStale: 60
# Number of days of inactivity before a stale issue is closed
daysUntilClose: 7
# Issues with these labels will never be considered stale
exemptLabels:
- bug
- feature
- semver:major
# Comment to post when marking an issue as stale. Set to `false` to disable
markComment: >
This issue has been automatically marked as stale because it has not had
recent activity. It will be closed if no further activity occurs. Thank you
for your contributions.
# Comment to post when closing a stale issue. Set to `false` to disable
closeComment: false
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@
"gulp": "^4.0.0",
"mocha": "^6.0.0",
"mysql2": "latest",
"nodeify": "^1.0.1",
"pg": "latest",
"pg-hstore": "latest",
"sequelize": "latest",
"sqlite3": "latest",
"through2": "^3.0.0"
},
"options": {
"mocha": "--require scripts/mocha-bootload.js --check-leaks --exit --timeout 30000 --colors --recursive --reporter spec"
"mocha": "--require scripts/mocha-bootload.js --bail --check-leaks --exit --timeout 30000 --colors --recursive --reporter spec"
},
"eslintIgnore": [
"test/support",
Expand Down
49 changes: 24 additions & 25 deletions src/assets/migrations/create-table.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
'use strict';

module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface
.createTable('<%= tableName %>', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},

<% attributes.forEach(function(attribute) { %>
<%= attribute.fieldName %>: {
type: Sequelize.<%= attribute.dataFunction ? `${attribute.dataFunction.toUpperCase()}(Sequelize.${attribute.dataType.toUpperCase()})` : attribute.dataValues ? `${attribute.dataType.toUpperCase()}(${attribute.dataValues})` : attribute.dataType.toUpperCase() %>
},
<% }) %>
up: async (queryInterface, Sequelize) => {
await queryInterface.createTable('<%= tableName %>', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},

<%= createdAt %>: {
allowNull: false,
type: Sequelize.DATE
<% attributes.forEach(function(attribute) { %>
<%= attribute.fieldName %>: {
type: Sequelize.<%= attribute.dataFunction ? `${attribute.dataFunction.toUpperCase()}(Sequelize.${attribute.dataType.toUpperCase()})` : attribute.dataValues ? `${attribute.dataType.toUpperCase()}(${attribute.dataValues})` : attribute.dataType.toUpperCase() %>
},
<% }) %>

<%= createdAt %>: {
allowNull: false,
type: Sequelize.DATE
},

<%= updatedAt %>: {
allowNull: false,
type: Sequelize.DATE
}
});
<%= updatedAt %>: {
allowNull: false,
type: Sequelize.DATE
}
});
},

down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('<%= tableName %>');
down: async (queryInterface, Sequelize) => {
await queryInterface.dropTable('<%= tableName %>');
}
};
30 changes: 14 additions & 16 deletions src/assets/migrations/skeleton.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
'use strict';

module.exports = {
up: (queryInterface, Sequelize) => {
/*
Add altering commands here.
Return a promise to correctly handle asynchronicity.

Example:
return queryInterface.createTable('users', { id: Sequelize.INTEGER });
*/
up: async (queryInterface, Sequelize) => {
/**
* Add altering commands here.
*
* Example:
* await queryInterface.createTable('users', { id: Sequelize.INTEGER });
*/
},

down: (queryInterface, Sequelize) => {
/*
Add reverting commands here.
Return a promise to correctly handle asynchronicity.

Example:
return queryInterface.dropTable('users');
*/
down: async (queryInterface, Sequelize) => {
/**
* Add reverting commands here.
*
* Example:
* await queryInterface.dropTable('users');
*/
}
};
21 changes: 16 additions & 5 deletions src/assets/models/model.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
'use strict';

const { Model } = require('sequelize');

module.exports = (sequelize, DataTypes) => {
const <%= name %> = sequelize.define('<%= name %>', {
class <%= name %> extends Model {
/**
* Helper method for defining associations.
* This method is not part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate (models) {
// define association here
}
};

<%= name %>.init({
<% attributes.forEach(function(attribute, index) { %>
<%= attribute.fieldName %>: DataTypes.<%= attribute.dataFunction ? `${attribute.dataFunction.toUpperCase()}(DataTypes.${attribute.dataType.toUpperCase()})` : attribute.dataValues ? `${attribute.dataType.toUpperCase()}(${attribute.dataValues})` : attribute.dataType.toUpperCase() %>
<%= (Object.keys(attributes).length - 1) > index ? ',' : '' %>
<% }) %>
}, {
sequelize,
modelName: '<%= name %>',
<%= underscored ? 'underscored: true,' : '' %>
});

<%= name %>.associate = function(models) {
// associations can be defined here
};

return <%= name %>;
};
34 changes: 16 additions & 18 deletions src/assets/seeders/skeleton.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
'use strict';

module.exports = {
up: (queryInterface, Sequelize) => {
/*
Add altering commands here.
Return a promise to correctly handle asynchronicity.

Example:
return queryInterface.bulkInsert('People', [{
name: 'John Doe',
isBetaMember: false
}], {});
up: async (queryInterface, Sequelize) => {
/**
* Add seed commands here.
*
* Example:
* await queryInterface.bulkInsert('People', [{
* name: 'John Doe',
* isBetaMember: false
* }], {});
*/
},

down: (queryInterface, Sequelize) => {
/*
Add reverting commands here.
Return a promise to correctly handle asynchronicity.

Example:
return queryInterface.bulkDelete('People', null, {});
*/
down: async (queryInterface, Sequelize) => {
/**
* Add commands to revert seed here.
*
* Example:
* await queryInterface.bulkDelete('People', null, {});
*/
}
};
1 change: 0 additions & 1 deletion src/commands/model_generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ exports.handler = function (args) {
ensureMigrationsFolder();
checkModelFileExistence(args);


try {
helpers.model.generateFile(args);
} catch (err) {
Expand Down
15 changes: 2 additions & 13 deletions src/core/migrator.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Umzug from 'umzug';
import _ from 'lodash';
import { promisify } from 'util';
import pTry from 'p-try';

import helpers from '../helpers/index';
Expand Down Expand Up @@ -34,10 +33,7 @@ function getSequelizeInstance () {
export function getMigrator (type, args) {
return pTry(() => {
if (!(helpers.config.configFileExists() || args.url)) {
helpers.view.error(
'Cannot find "' + helpers.config.getConfigFile() +
'". Have you run "sequelize init"?'
);
helpers.view.error(`Cannot find "${helpers.config.getConfigFile()}". Have you run "sequelize init"?`);
process.exit(1);
}

Expand All @@ -49,14 +45,7 @@ export function getMigrator (type, args) {
migrations: {
params: [sequelize.getQueryInterface(), Sequelize],
path: helpers.path.getPath(type),
pattern: /\.c?js$/,
wrap: fun => {
if (fun.length === 3) {
return promisify(fun);
} else {
return fun;
}
}
pattern: /\.c?js$/
}
});

Expand Down
14 changes: 5 additions & 9 deletions src/sequelize.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import helpers from './helpers/index';

helpers.view.teaser();

const cli = yargs
yargs
.help()
.version()
.command('db:migrate', 'Run pending migrations', migrate)
Expand All @@ -42,11 +42,7 @@ const cli = yargs
.command(['model:generate', 'model:create'], 'Generates a model and its migration', modelGenerate)
.command(['seed:generate', 'seed:create'], 'Generates a new seed file', seedGenerate)
.wrap(yargs.terminalWidth())
.strict();

const args = cli.argv;

// if no command then show help
if (!args._[0]) {
cli.showHelp();
}
.demandCommand(1, 'Please specify a command')
.help()
.strict()
.argv;
3 changes: 1 addition & 2 deletions test/db/migrate-json.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ const _ = require('lodash');
done();
});
}, {
migrationFile: 'new/*createPerson',
config: { promisifyMigrations: false }
migrationFile: 'new/*createPerson'
});
});
});
Expand Down
3 changes: 1 addition & 2 deletions test/db/migrate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@ const _ = require('lodash');
done();
});
}, {
migrationFile: 'new/*createPerson',
config: { promisifyMigrations: false }
migrationFile: 'new/*createPerson'
});
});
});
Expand Down
4 changes: 2 additions & 2 deletions test/migration/create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ const gulp = require('gulp');
.src(Support.resolveSupportPath('tmp', 'migrations'))
.pipe(helpers.readFile('*-' + migrationFile))
.pipe(helpers.expect(stdout => {
expect(stdout).to.contain('up: (queryInterface, Sequelize) => {');
expect(stdout).to.contain('down: (queryInterface, Sequelize) => {');
expect(stdout).to.contain('up: async (queryInterface, Sequelize) => {');
expect(stdout).to.contain('down: async (queryInterface, Sequelize) => {');
}))
.pipe(helpers.teardown(done));
});
Expand Down
12 changes: 6 additions & 6 deletions test/model/create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ const _ = require('lodash');
gulp
.src(Support.resolveSupportPath('tmp', 'models'))
.pipe(helpers.readFile('user.js'))
.pipe(helpers.ensureContent('sequelize.define(\'User\''))
.pipe(helpers.ensureContent('class User extends Model {'))
.pipe(helpers.ensureContent('first_name: DataTypes.STRING'))
.pipe(helpers.ensureContent('last_name: DataTypes.STRING'))
.pipe(helpers.ensureContent('bio: DataTypes.TEXT'))
Expand Down Expand Up @@ -160,7 +160,7 @@ const _ = require('lodash');
gulp
.src(Support.resolveSupportPath('tmp', 'migrations'))
.pipe(helpers.readFile('*-create-user.js'))
.pipe(helpers.ensureContent('return queryInterface'))
.pipe(helpers.ensureContent('await queryInterface'))
.pipe(helpers.ensureContent('.createTable(\'Users\', {'))
.pipe(helpers.ensureContent(
'first_name: {\n type: Sequelize.STRING\n },'
Expand Down Expand Up @@ -209,9 +209,9 @@ const _ = require('lodash');
attributes
};

const targetContent = attrUnd.underscored ?
'underscored: true'
: '{});';
const targetContent = attrUnd.underscored
? 'modelName: \'User\',\n underscored: true,\n });'
: 'modelName: \'User\',\n });';

if ( attrUnd.underscored ) {
flags.underscored = attrUnd.underscored;
Expand All @@ -224,7 +224,7 @@ const _ = require('lodash');
.src(Support.resolveSupportPath('tmp', 'models'))
.pipe(helpers.readFile('user.js'))
.pipe(helpers.ensureContent(targetContent))
.pipe(helpers.ensureContent('.associate'))
.pipe(helpers.ensureContent('static associate'))
.pipe(helpers.teardown(done));
});
});
Expand Down
4 changes: 2 additions & 2 deletions test/seed/create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ const gulp = require('gulp');
.src(Support.resolveSupportPath('tmp', 'seeders'))
.pipe(helpers.readFile('*-' + seedFile))
.pipe(helpers.expect(stdout => {
expect(stdout).to.contain('up: (queryInterface, Sequelize) => {');
expect(stdout).to.contain('down: (queryInterface, Sequelize) => {');
expect(stdout).to.contain('up: async (queryInterface, Sequelize) => {');
expect(stdout).to.contain('down: async (queryInterface, Sequelize) => {');
}))
.pipe(helpers.teardown(done));
});
Expand Down
29 changes: 13 additions & 16 deletions test/support/assets/migrations/20111117063700-createPerson.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
'use strict';

var nodeify = require('nodeify');
"use strict";

module.exports = {
up: function (migration, DataTypes, done) {
nodeify(migration
.createTable('Person', {
name: DataTypes.STRING,
isBetaMember: {
type: DataTypes.BOOLEAN,
defaultValue: false,
allowNull: false
}
}), done);
up: function (migration, DataTypes) {
return migration.createTable("Person", {
name: DataTypes.STRING,
isBetaMember: {
type: DataTypes.BOOLEAN,
defaultValue: false,
allowNull: false,
},
});
},

down: function (migration, DataTypes, done) {
nodeify(migration.dropTable('Person'), done);
}
down: function (migration, DataTypes) {
return migration.dropTable("Person");
},
};
Loading