-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from cmgriffing/issue-5
feat: add runtime db migrations using kysely
- Loading branch information
Showing
8 changed files
with
186 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#!/usr/bin/env node | ||
|
||
import fs from "fs"; | ||
import path from "path"; | ||
import { fileURLToPath } from "url"; | ||
|
||
const __filename = fileURLToPath(import.meta.url); | ||
const __dirname = path.dirname(__filename); | ||
|
||
const name = process.argv[2]; | ||
const date = new Date().toISOString(); | ||
|
||
const template = ` | ||
import { Kysely, sql } from 'kysely' | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export async function up(db: Kysely<any>): Promise<void> { | ||
// await db.schema | ||
// .createTable('person') | ||
// .addColumn('id', 'integer', (col) => col.primaryKey()) | ||
// .addColumn('first_name', 'text', (col) => col.notNull()) | ||
// .addColumn('last_name', 'text') | ||
// .addColumn('gender', 'text', (col) => col.notNull()) | ||
// .addColumn('created_at', 'text', (col) => | ||
// col.defaultTo(sql\`CURRENT_TIMESTAMP\`).notNull() | ||
// ) | ||
// .execute() | ||
// await db.schema | ||
// .createTable('pet') | ||
// .addColumn('id', 'integer', (col) => col.primaryKey()) | ||
// .addColumn('name', 'text', (col) => col.notNull().unique()) | ||
// .addColumn('owner_id', 'integer', (col) => | ||
// col.references('person.id').onDelete('cascade').notNull() | ||
// ) | ||
// .addColumn('species', 'text', (col) => col.notNull()) | ||
// .execute() | ||
// await db.schema | ||
// .createIndex('pet_owner_id_index') | ||
// .on('pet') | ||
// .column('owner_id') | ||
// .execute() | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export async function down(db: Kysely<any>): Promise<void> { | ||
// await db.schema.dropTable('pet').execute() | ||
// await db.schema.dropTable('person').execute() | ||
} | ||
`; | ||
|
||
fs.writeFileSync( | ||
path.resolve(__dirname, "../src/data/migrations", `${date}-${name}.ts`), | ||
template | ||
); | ||
|
||
fs.appendFileSync( | ||
path.resolve(__dirname, "../src/data/migrations/index.ts"), | ||
`\nexport * as migration${date | ||
.replaceAll("-", "") | ||
.replaceAll(":", "") | ||
.replaceAll(".", "")} from "./${date}-${name}.ts"` | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/data/migrations/2024-01-23T23:15:28.004Z-initial-schema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { Kysely } from "kysely"; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export async function up(db: Kysely<any>): Promise<void> { | ||
await db.schema | ||
.createTable("books") | ||
.addColumn("id", "integer", (col) => col.primaryKey().autoIncrement()) | ||
.addColumn("title", "text", (col) => col.notNull()) | ||
.addColumn("created_at", "integer", (col) => | ||
col.defaultTo(Date.now()).notNull() | ||
) | ||
.addColumn("modified_at", "integer", (col) => | ||
col.defaultTo(Date.now()).notNull() | ||
) | ||
.ifNotExists() | ||
.execute(); | ||
|
||
await db.schema | ||
.createTable("chapters") | ||
.addColumn("id", "integer", (col) => col.primaryKey().autoIncrement()) | ||
.addColumn("label", "text", (col) => col.notNull()) | ||
.addColumn("sort_order", "real", (col) => col.notNull()) | ||
.addColumn("created_at", "integer", (col) => | ||
col.defaultTo(Date.now()).notNull() | ||
) | ||
.addColumn("modified_at", "integer", (col) => | ||
col.defaultTo(Date.now()).notNull() | ||
) | ||
.addColumn("book_id", "integer", (col) => col.notNull().onDelete("cascade")) | ||
.addForeignKeyConstraint("book_id_fk", ["book_id"], "books", ["id"]) | ||
.ifNotExists() | ||
.execute(); | ||
|
||
await db.schema | ||
.createTable("snippets") | ||
.addColumn("id", "integer", (col) => col.primaryKey().autoIncrement()) | ||
.addColumn("label", "text", (col) => col.notNull()) | ||
.addColumn("content", "text", (col) => col.notNull().defaultTo("")) | ||
.addColumn("sort_order", "real", (col) => col.notNull()) | ||
.addColumn("created_at", "integer", (col) => | ||
col.defaultTo(Date.now()).notNull() | ||
) | ||
.addColumn("modified_at", "integer", (col) => | ||
col.defaultTo(Date.now()).notNull() | ||
) | ||
.addColumn("recorded_at", "integer", (col) => col.defaultTo(0)) | ||
.addColumn("processed_at", "integer", (col) => col.defaultTo(0)) | ||
.addColumn("finished_at", "integer", (col) => col.defaultTo(0)) | ||
.addColumn("raw_recording_content", "text", (col) => col.defaultTo("")) | ||
.addColumn("chapter_id", "integer", (col) => | ||
col.notNull().onDelete("cascade") | ||
) | ||
.addForeignKeyConstraint("chapter_id_fk", ["chapter_id"], "chapters", [ | ||
"id", | ||
]) | ||
.ifNotExists() | ||
.execute(); | ||
|
||
await db.schema | ||
.createTable("settings") | ||
.addColumn("id", "integer", (col) => col.primaryKey().autoIncrement()) | ||
.addColumn("user_id", "integer", (col) => col.notNull().unique()) | ||
.addColumn("threads", "integer", (col) => col.notNull().defaultTo(2)) | ||
.addColumn("selected_model", "text", (col) => col.notNull().defaultTo("")) | ||
.addColumn("created_at", "integer", (col) => | ||
col.defaultTo(Date.now()).notNull() | ||
) | ||
.addColumn("modified_at", "integer", (col) => | ||
col.defaultTo(Date.now()).notNull() | ||
) | ||
.ifNotExists() | ||
.execute(); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export async function down(db: Kysely<any>): Promise<void> { | ||
await db.schema.dropTable("books").execute(); | ||
await db.schema.dropTable("chapters").execute(); | ||
await db.schema.dropTable("snippets").execute(); | ||
await db.schema.dropTable("settings").execute(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * as migration20240123T231528004Z from "./2024-01-23T23:15:28.004Z-initial-schema.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { MigrationProvider } from "kysely"; | ||
import * as migrations from "./index"; | ||
|
||
export class RuntimeMigrationProvider implements MigrationProvider { | ||
async getMigrations() { | ||
return migrations; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters