-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate-migration.ts
More file actions
32 lines (26 loc) · 1.2 KB
/
create-migration.ts
File metadata and controls
32 lines (26 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import appRootPath from 'app-root-path';
import { writeFileSync } from 'fs';
import { resolve } from 'path';
import slugify from 'slugify';
const name = `${new Date().toISOString()}${process.argv[2] ? `-${slugify(process.argv[2].trim()).toLowerCase()}` : ''}`;
const initialCode = String.raw`import { Kysely, sql } from 'kysely';
import { Database } from '../database';
export async function up(db: Kysely<Database>) {
await db.schema
.createTable('NewTable')
.addColumn('id', 'uuid', (col) => col.primaryKey().defaultTo(sql${'`'}gen_random_uuid()${'`'}))
.addColumn('name', 'varchar', (col) => col.notNull())
.addColumn('description', 'varchar', (col) => col.notNull())
.addColumn('dateCreated', 'timestamptz', (col) => col.notNull().defaultTo(sql${'`'}clock_timestamp()${'`'}))
.addColumn('dateUpdated', 'timestamptz')
.addColumn('dateDeleted', 'timestamptz')
.execute();
}
export async function down(db: Kysely<Database>) {
await db.schema.dropTable('NewTable').execute();
}
`;
const filename = `${name}.ts`;
const fullPath = resolve(appRootPath.path, 'src', 'db', 'migrations', filename);
writeFileSync(fullPath, initialCode);
console.log(`Migration ${filename} created successfully`);