From 10a24557482744754d11fa47d687287faf9b2144 Mon Sep 17 00:00:00 2001 From: Stephan B Valois <83061036+ScopeSV@users.noreply.github.com> Date: Sat, 4 Nov 2023 17:28:56 +0100 Subject: [PATCH 1/3] down structure on add field migration --- src/handlers/migrationHandler.test.ts | 18 +++++++++ src/handlers/migrationHandler.ts | 58 ++++++++++++++++++++++++++- src/template/TemplateBuilder.ts | 7 ++++ 3 files changed, 81 insertions(+), 2 deletions(-) diff --git a/src/handlers/migrationHandler.test.ts b/src/handlers/migrationHandler.test.ts index e5677e0..a609183 100644 --- a/src/handlers/migrationHandler.test.ts +++ b/src/handlers/migrationHandler.test.ts @@ -27,6 +27,9 @@ describe('migrationHandler', () => { expect(template).toContain( "await db.schema\n\t\t.createTable('todo')\n\t\t.execute()" ) + expect(template).toContain( + "await db.schema\n\t\t.dropTable('todo')\n\t\t.execute()" + ) }) it('creates a table with columns', () => { const template = generateTemplate('create_todo', [ @@ -36,6 +39,9 @@ describe('migrationHandler', () => { expect(template).toContain( "await db.schema\n\t\t.createTable('todo')\n\t\t.addColumn('id', 'uuid')\n\t\t.addColumn('name', 'string')\n\t\t.execute()" ) + expect(template).toContain( + "await db.schema\n\t\t.dropTable('todo')\n\t\t.execute()" + ) }) it('creates a table with multiple columns', () => { const template = generateTemplate('create_todo', [ @@ -46,6 +52,9 @@ describe('migrationHandler', () => { expect(template).toContain( "await db.schema\n\t\t.createTable('todo')\n\t\t.addColumn('id', 'uuid')\n\t\t.addColumn('name', 'string')\n\t\t.addColumn('complete', 'boolean')\n\t\t.execute()" ) + expect(template).toContain( + "await db.schema\n\t\t.dropTable('todo')\n\t\t.execute()" + ) }) }) describe('AddFields', () => { @@ -56,6 +65,9 @@ describe('migrationHandler', () => { expect(template).toContain( "await db.schema\n\t\t.alterTable('todo')\n\t\t.addColumn('complete', 'boolean')\n\t\t.execute()" ) + expect(template).toContain( + "await db.schema\n\t\t.alterTable('todo')\n\t\t.dropColumn('complete')\n\t\t.execute()" + ) }) it('adds multiple columns', () => { const template = generateTemplate('add_complete_to_todo', [ @@ -65,6 +77,9 @@ describe('migrationHandler', () => { expect(template).toContain( "await db.schema\n\t\t.alterTable('todo')\n\t\t.addColumn('complete', 'boolean')\n\t\t.addColumn('name', 'string')\n\t\t.execute()" ) + expect(template).toContain( + "await db.schema\n\t\t.alterTable('todo')\n\t\t.dropColumn('name')\n\t\t.dropColumn('complete')\n\t\t.execute()" + ) }) it('adds a column with not null', () => { const template = generateTemplate('add_complete_to_todo', [ @@ -74,6 +89,9 @@ describe('migrationHandler', () => { expect(template).toContain( "await db.schema\n\t\t.alterTable('todo')\n\t\t.addColumn('complete', 'boolean', (col) => col.notNull())\n\t\t.execute()" ) + expect(template).toContain( + "await db.schema\n\t\t.alterTable('todo')\n\t\t.dropColumn('complete')\n\t\t.execute()" + ) }) it('adds an end if cant find the action', () => { const template = generateTemplate('do_stuff', []).getTemplate() diff --git a/src/handlers/migrationHandler.ts b/src/handlers/migrationHandler.ts index 96f3f14..1c9e4a3 100644 --- a/src/handlers/migrationHandler.ts +++ b/src/handlers/migrationHandler.ts @@ -21,12 +21,34 @@ const parseInput = (input: string) => { return { tableName, type } } +const getOpositeMigrationType = (type: string): string => { + switch (type) { + case 'createTable': + return 'dropTable' + case 'alterTable': + return 'alterTable' + case 'dropTable': + return 'createTable' + default: + return '' + } +} + const buildTableAction = ( b: TemplateBuilder, action: string, tableName: string, options: string[], - operation: (columnName: string, columnType: string, opts?: string[]) => void + operation: ( + columnName: string, + columnType: string, + opts?: string[] + ) => void, + downOperation?: ( + columnName: string, + columnType: string, + opts?: string[] + ) => void ): void => { b.indent(1) .addLine('await db.schema') @@ -40,6 +62,27 @@ const buildTableAction = ( b.indent(2).do() b.addEnd() + + if (downOperation) { + const downAction = getOpositeMigrationType(action) + b.addDown() + .indent(1) + .addLine('await db.schema') + .indent(2) + .addLine(`.${downAction}('${tableName}')`) + + if (action !== 'createTable') { + for (const option of options.slice().reverse()) { + const [columnName, columnType, ...stuff] = option.split(':') + downOperation(columnName, columnType, stuff) + } + + b.indent(2).do() + b.addEnd() + } else { + b.indent(2).do().addEnd() + } + } } export const generateTemplate = ( @@ -51,6 +94,7 @@ export const generateTemplate = ( b.addHeader().addUp() + console.log('type: ', type) switch (type) { case Commands.AddFields: buildTableAction( @@ -60,6 +104,9 @@ export const generateTemplate = ( options, (columnName, columnType, opts?: string[]) => { b.indent(2).addColumn(columnName, columnType, opts) + }, + (columnName, columnType, opts?: string[]) => { + b.indent(2).dropColumn(columnName) } ) break @@ -71,7 +118,11 @@ export const generateTemplate = ( tableName, options, (columnName, columnType) => { + console.log('ready to create a table') b.indent(2).addColumn(columnName, columnType) + }, + (columnName, columnType, opts?: string[]) => { + b.indent(2).dropColumn(columnName).indent(2).do().addEnd() } ) break @@ -84,16 +135,19 @@ export const generateTemplate = ( options, (columnName) => { b.indent(2).dropColumn(columnName) + }, + (columnName, columnType, opts?: string[]) => { + b.indent(2).addColumn(columnName, '') } ) break default: b.addEnd() + b.addDown().addEnd() break } - b.addDown().addEnd() return b } diff --git a/src/template/TemplateBuilder.ts b/src/template/TemplateBuilder.ts index eb112d1..f3b3aeb 100644 --- a/src/template/TemplateBuilder.ts +++ b/src/template/TemplateBuilder.ts @@ -18,6 +18,7 @@ class TemplateBuilder { addEnd() { this.template += '}\n\n' + return this } addHeader() { @@ -69,8 +70,14 @@ class TemplateBuilder { return this } + dropTable(tableName: string) { + this.template += `.dropTable('${tableName}')\n` + return this + } + do() { this.template += '.execute()\n' + return this } getTemplate() { From 42a2aab773c17cb8311bd40c894279620ba69cba Mon Sep 17 00:00:00 2001 From: Stephan B Valois <83061036+ScopeSV@users.noreply.github.com> Date: Sat, 4 Nov 2023 18:15:13 +0100 Subject: [PATCH 2/3] feat: down functions and transpile to js --- README.md | 8 -------- src/file-writer/FileWriter.ts | 17 +++++++++++++++++ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 8b971d1..729b5a9 100644 --- a/README.md +++ b/README.md @@ -44,12 +44,6 @@ $ npx kyseline migration:make remove_foo_from_footable foo Add additional notes about how to deploy this on a live system --> - - @@ -62,8 +56,6 @@ The project uses [SemVer](http://semver.org/) for versioning. For the versions a - **Stephan Valois** - - - ## License This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details diff --git a/src/file-writer/FileWriter.ts b/src/file-writer/FileWriter.ts index 2822e05..88d0e87 100644 --- a/src/file-writer/FileWriter.ts +++ b/src/file-writer/FileWriter.ts @@ -1,6 +1,7 @@ import fs from 'fs' import path from 'path' import { ConfigParser } from '../config-file/ConfigParser' +import ts from 'typescript' class FileWriter { constructor( @@ -27,6 +28,18 @@ class FileWriter { return fs.existsSync(tsConfigPath) } + #transpileToJs = () => { + const defaultOptions = { + module: ts.ModuleKind.ESNext, + target: ts.ScriptTarget.ESNext, + jsx: ts.JsxEmit.Preserve, + } + + this.template = ts.transpileModule(this.template, { + compilerOptions: defaultOptions, + }).outputText + } + determineFileExtension = () => { const cfg = this.configParser.getConfig() if (this.#hasTsConfigInCurrentDir() && cfg.useJsExtension !== true) { @@ -44,6 +57,10 @@ class FileWriter { const filename = `${this.getCurrTimeStamp()}_${this.command}.${fileExt}` const filePath = path.join(dir, filename) + if (fileExt === 'js') { + this.#transpileToJs() + } + fs.writeFile(filePath, this.template, (err: any) => { if (err) { console.error(err) From 50807df3a95d7cb443402839445465ec06f9dd8c Mon Sep 17 00:00:00 2001 From: Stephan B Valois <83061036+ScopeSV@users.noreply.github.com> Date: Sat, 4 Nov 2023 18:16:57 +0100 Subject: [PATCH 3/3] remove log --- src/handlers/migrationHandler.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/handlers/migrationHandler.ts b/src/handlers/migrationHandler.ts index 1c9e4a3..26e3197 100644 --- a/src/handlers/migrationHandler.ts +++ b/src/handlers/migrationHandler.ts @@ -94,7 +94,6 @@ export const generateTemplate = ( b.addHeader().addUp() - console.log('type: ', type) switch (type) { case Commands.AddFields: buildTableAction(