diff --git a/packages/generators/src/authentication/templates/knex.tpl.ts b/packages/generators/src/authentication/templates/knex.tpl.ts index eda0838971..048c414ae7 100644 --- a/packages/generators/src/authentication/templates/knex.tpl.ts +++ b/packages/generators/src/authentication/templates/knex.tpl.ts @@ -1,5 +1,5 @@ import { generator, when, toFile } from '@feathershq/pinion' -import { getDatabaseAdapter, renderSource } from '../../commons' +import { getDatabaseAdapter, renderSource, yyyymmddhhmmss } from '../../commons' import { AuthenticationGeneratorContext } from '../index' const migrationTemplate = ({ @@ -46,16 +46,10 @@ export const generate = (ctx: AuthenticationGeneratorContext) => renderSource( migrationTemplate, toFile( - toFile('migrations', () => { - // Probably not great but it works to align with the Knex migration file format - // We add a few seconds so that the migrations run in the correct order - const migrationDate = new Date(Date.now() + 10000) - .toISOString() - .replace(/\D/g, '') - .substring(0, 14) - - return `${migrationDate}_authentication` - }) + toFile( + 'migrations', + async () => `${yyyymmddhhmmss(1200)}_authentication` + ) ) ) ) diff --git a/packages/generators/src/commons.ts b/packages/generators/src/commons.ts index 62eb4d081a..345ebfa20c 100644 --- a/packages/generators/src/commons.ts +++ b/packages/generators/src/commons.ts @@ -282,3 +282,20 @@ export const injectSource = * @returns Wether the file exists or not */ export const fileExists = (...filenames: string[]) => fs.existsSync(join(...filenames)) + +/** + * The helper used by Knex to create migration names + * @returns The current date and time in the format `YYYYMMDDHHMMSS` + */ +export const yyyymmddhhmmss = (offset = 0) => { + const now = new Date(Date.now() + offset) + + return ( + now.getUTCFullYear().toString() + + (now.getUTCMonth() + 1).toString().padStart(2, '0') + + now.getUTCDate().toString().padStart(2, '0') + + now.getUTCHours().toString().padStart(2, '0') + + now.getUTCMinutes().toString().padStart(2, '0') + + now.getUTCSeconds().toString().padStart(2, '0') + ) +} diff --git a/packages/generators/src/service/type/knex.tpl.ts b/packages/generators/src/service/type/knex.tpl.ts index 99018f24cb..6a70145b3d 100644 --- a/packages/generators/src/service/type/knex.tpl.ts +++ b/packages/generators/src/service/type/knex.tpl.ts @@ -1,5 +1,5 @@ import { generator, toFile } from '@feathershq/pinion' -import { renderSource } from '../../commons' +import { renderSource, yyyymmddhhmmss } from '../../commons' import { ServiceGeneratorContext } from '../index' const migrationTemplate = ({ @@ -84,11 +84,6 @@ export const generate = (ctx: ServiceGeneratorContext) => .then( renderSource( migrationTemplate, - toFile('migrations', ({ kebabName }) => { - // Probably not great but it works to align with the Knex migration file format - const migrationDate = new Date().toISOString().replace(/\D/g, '').substring(0, 14) - - return `${migrationDate}_${kebabName}` - }) + toFile('migrations', ({ kebabName }) => `${yyyymmddhhmmss()}_${kebabName}`) ) )