Skip to content

Commit

Permalink
fix(generators): Fix Knex migration generated filename (#3033)
Browse files Browse the repository at this point in the history
  • Loading branch information
daffl authored Jan 31, 2023
1 parent cf09e8f commit 1ac18a7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
16 changes: 5 additions & 11 deletions packages/generators/src/authentication/templates/knex.tpl.ts
Original file line number Diff line number Diff line change
@@ -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 = ({
Expand Down Expand Up @@ -46,16 +46,10 @@ export const generate = (ctx: AuthenticationGeneratorContext) =>
renderSource(
migrationTemplate,
toFile(
toFile<AuthenticationGeneratorContext>('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<AuthenticationGeneratorContext>(
'migrations',
async () => `${yyyymmddhhmmss(1200)}_authentication`
)
)
)
)
Expand Down
17 changes: 17 additions & 0 deletions packages/generators/src/commons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
)
}
9 changes: 2 additions & 7 deletions packages/generators/src/service/type/knex.tpl.ts
Original file line number Diff line number Diff line change
@@ -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 = ({
Expand Down Expand Up @@ -84,11 +84,6 @@ export const generate = (ctx: ServiceGeneratorContext) =>
.then(
renderSource(
migrationTemplate,
toFile<ServiceGeneratorContext>('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<ServiceGeneratorContext>('migrations', ({ kebabName }) => `${yyyymmddhhmmss()}_${kebabName}`)
)
)

0 comments on commit 1ac18a7

Please # to comment.