Skip to content

Commit

Permalink
close #62 Add command to create a new migration file
Browse files Browse the repository at this point in the history
  • Loading branch information
Sharaal committed Sep 13, 2021
1 parent 43d4a13 commit 55e502f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
37 changes: 37 additions & 0 deletions bin/migrate_make.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env node

let debug
try {
debug = require('debug')('migrate:make')
} catch (e) {
debug = console.log
}

const fs = require('fs')
const path = require('path')

let file = process.argv[2]
if (!file) {
debug('name for the migration needs to be given as argument')
process.exit(1)
}
if (!file.includes('.')) {
file += '.sql'
}
if (!file.endsWith('.js') && !file.endsWith('.sql')) {
debug('file ending needs to be ".js" or ".sql" (default if no file ending is given)')
process.exit(1)
}

const currentUnixTimestamp = Math.floor(Date.now() / 1000)
file = `${currentUnixTimestamp}_${file}`

const directory = path.join(process.cwd(), 'migrations')
if (file.endsWith('.js')) {
fs.copyFileSync(path.join(__dirname, 'templates/migration.js'), path.join(directory, file))
}
if (file.endsWith('.sql')) {
fs.copyFileSync(path.join(__dirname, 'templates/migration.sql'), path.join(directory, file))
}

debug(`file "${file}" is created in the migrations directory`)
3 changes: 3 additions & 0 deletions bin/templates/migration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = async sql => sql.query(
sql``
)
Empty file added bin/templates/migration.sql
Empty file.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"access": "public"
},
"bin": {
"migrate": "./bin/migrate.js"
"migrate": "./bin/migrate.js",
"migrate:make": "./bin/migrate_make.js"
},
"scripts": {
"lint": "standard",
Expand Down Expand Up @@ -71,6 +72,9 @@
"bin/",
"src/"
],
"exclude": [
"bin/templates/"
],
"all": true,
"reporter": [
"text",
Expand Down

0 comments on commit 55e502f

Please sign in to comment.