Skip to content

Commit 07caa6a

Browse files
authored
fix(timestamps): support for timestamps in migration tables (#899)
1 parent 2cba77f commit 07caa6a

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

Diff for: src/core/migrator.js

+2
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ export function ensureCurrentMetaSchema(migrator) {
7777
if (columns.length === 1 && columns[0] === columnName) {
7878
return;
7979
} else if (columns.length === 3 && columns.indexOf('createdAt') >= 0) {
80+
// If found createdAt - indicate we have timestamps enabled
81+
helpers.umzug.enableTimestamps();
8082
return;
8183
}
8284
})

Diff for: src/helpers/umzug-helper.js

+11
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ const storageJsonName = {
1515
seeder: 'sequelize-data.json',
1616
};
1717

18+
let timestampsDefault = false;
19+
1820
module.exports = {
1921
getStorageOption(property, fallback) {
2022
return helpers.config.readConfig()[property] || fallback;
@@ -41,6 +43,14 @@ module.exports = {
4143
return this.getStorageOption(type + 'StorageTableSchema', undefined);
4244
},
4345

46+
enableTimestamps() {
47+
timestampsDefault = true;
48+
},
49+
50+
getTimestamps(type) {
51+
return this.getStorageOption(type + 'Timestamps', timestampsDefault);
52+
},
53+
4454
getStorageOptions(type, extraOptions) {
4555
const options = {};
4656

@@ -49,6 +59,7 @@ module.exports = {
4959
} else if (this.getStorage(type) === 'sequelize') {
5060
options.tableName = this.getTableName(type);
5161
options.schema = this.getSchema(type);
62+
options.timestamps = this.getTimestamps(type);
5263
}
5364

5465
_.assign(options, extraOptions);

Diff for: test/db/migrate/schema/add_timestamps.test.js

+20
Original file line numberDiff line numberDiff line change
@@ -118,5 +118,25 @@ const gulp = require('gulp');
118118
})
119119
);
120120
});
121+
122+
it('run migration again with timestamp fields present', function (done) {
123+
gulp
124+
.src(Support.resolveSupportPath('tmp'))
125+
.pipe(helpers.runCli('db:migrate'))
126+
.pipe(
127+
helpers.teardown(() => {
128+
helpers
129+
.execQuery(
130+
this.sequelize,
131+
this.queryGenerator.selectQuery('SequelizeMeta'),
132+
{ raw: true, type: 'SELECT' }
133+
)
134+
.then((items) => {
135+
expect(items.length).to.equal(2);
136+
done();
137+
});
138+
})
139+
);
140+
});
121141
});
122142
});

0 commit comments

Comments
 (0)