From 55e502f34fd38c187f89691a86eacb85131ee764 Mon Sep 17 00:00:00 2001 From: Christoph Herrmann Date: Mon, 13 Sep 2021 18:01:02 +0200 Subject: [PATCH] close #62 Add command to create a new migration file --- bin/migrate_make.js | 37 +++++++++++++++++++++++++++++++++++++ bin/templates/migration.js | 3 +++ bin/templates/migration.sql | 0 package.json | 6 +++++- 4 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 bin/migrate_make.js create mode 100644 bin/templates/migration.js create mode 100644 bin/templates/migration.sql diff --git a/bin/migrate_make.js b/bin/migrate_make.js new file mode 100644 index 0000000..ebc84a3 --- /dev/null +++ b/bin/migrate_make.js @@ -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`) diff --git a/bin/templates/migration.js b/bin/templates/migration.js new file mode 100644 index 0000000..cf4192f --- /dev/null +++ b/bin/templates/migration.js @@ -0,0 +1,3 @@ +module.exports = async sql => sql.query( + sql`` +) diff --git a/bin/templates/migration.sql b/bin/templates/migration.sql new file mode 100644 index 0000000..e69de29 diff --git a/package.json b/package.json index a7a0376..54283aa 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,8 @@ "access": "public" }, "bin": { - "migrate": "./bin/migrate.js" + "migrate": "./bin/migrate.js", + "migrate:make": "./bin/migrate_make.js" }, "scripts": { "lint": "standard", @@ -71,6 +72,9 @@ "bin/", "src/" ], + "exclude": [ + "bin/templates/" + ], "all": true, "reporter": [ "text",