|
1 |
| -import type { Migration } from 'kysely' |
| 1 | +import type { Kysely, Migration } from 'kysely' |
2 | 2 |
|
3 | 3 | import type { DatabaseDriverName } from '~/lib/db/drivers'
|
4 | 4 | import { sql } from 'kysely'
|
5 | 5 |
|
| 6 | +async function createCacheKeysTable({ |
| 7 | + db, |
| 8 | + dbType, |
| 9 | +}: { |
| 10 | + db: Kysely<any> |
| 11 | + dbType: DatabaseDriverName |
| 12 | +}) { |
| 13 | + let query = db.schema |
| 14 | + .createTable('cache_keys') |
| 15 | + .addColumn('id', 'varchar(255)', (col) => col.notNull().primaryKey()) |
| 16 | + .addColumn('key', 'text', (col) => col.notNull()) |
| 17 | + .addColumn('version', 'text', (col) => col.notNull()) |
| 18 | + .addColumn('updated_at', 'text', (col) => col.notNull()) |
| 19 | + .addColumn('accessed_at', 'text', (col) => col.notNull()) |
| 20 | + |
| 21 | + if (dbType === 'mysql') query = query.modifyEnd(sql`engine=InnoDB CHARSET=latin1`) |
| 22 | + |
| 23 | + await query.ifNotExists().execute() |
| 24 | +} |
| 25 | + |
6 | 26 | export function migrations(dbType: DatabaseDriverName) {
|
7 | 27 | return {
|
8 | 28 | $0_cache_keys_table: {
|
9 | 29 | async up(db) {
|
10 |
| - let query = db.schema |
11 |
| - .createTable('cache_keys') |
12 |
| - .addColumn('id', 'varchar(255)', (col) => col.notNull().primaryKey()) |
13 |
| - .addColumn('key', 'text', (col) => col.notNull()) |
14 |
| - .addColumn('version', 'text', (col) => col.notNull()) |
15 |
| - .addColumn('updated_at', 'text', (col) => col.notNull()) |
16 |
| - .addColumn('accessed_at', 'text', (col) => col.notNull()) |
17 |
| - |
18 |
| - if (dbType === 'mysql') query = query.modifyEnd(sql`engine=InnoDB CHARSET=latin1`) |
19 |
| - |
20 |
| - await query.ifNotExists().execute() |
| 30 | + await createCacheKeysTable({ db, dbType }) |
21 | 31 | },
|
22 | 32 | async down(db) {
|
23 | 33 | await db.schema.dropTable('cache_keys').ifExists().execute()
|
@@ -76,5 +86,58 @@ export function migrations(dbType: DatabaseDriverName) {
|
76 | 86 | await db.schema.alterTable('upload_parts').dropColumn('e_tag').execute()
|
77 | 87 | },
|
78 | 88 | },
|
| 89 | + $4_cache_entry_created_at: { |
| 90 | + async up(db) { |
| 91 | + await db |
| 92 | + .insertInto('cache_keys') |
| 93 | + .values({ |
| 94 | + id: '', |
| 95 | + key: '', |
| 96 | + version: '', |
| 97 | + updated_at: new Date().toISOString(), |
| 98 | + accessed_at: new Date().toISOString(), |
| 99 | + }) |
| 100 | + .execute() |
| 101 | + await db.schema.alterTable('cache_keys').addColumn('created_at', 'text').execute() |
| 102 | + |
| 103 | + await db |
| 104 | + .updateTable('cache_keys') |
| 105 | + .set({ |
| 106 | + created_at: new Date().toISOString(), |
| 107 | + }) |
| 108 | + .execute() |
| 109 | + |
| 110 | + if (dbType === 'mysql') |
| 111 | + await db.schema |
| 112 | + .alterTable('cache_keys') |
| 113 | + .modifyColumn('created_at', 'text', (c) => c.notNull()) |
| 114 | + .execute() |
| 115 | + else if (dbType === 'postgres') |
| 116 | + await db.schema |
| 117 | + .alterTable('cache_keys') |
| 118 | + .alterColumn('created_at', (c) => c.setNotNull()) |
| 119 | + .execute() |
| 120 | + else { |
| 121 | + // rename old table |
| 122 | + await db.schema.alterTable('cache_keys').renameTo('old_cache_keys').execute() |
| 123 | + // recreate table |
| 124 | + await createCacheKeysTable({ db, dbType }) |
| 125 | + |
| 126 | + // add not null column |
| 127 | + await db.schema |
| 128 | + .alterTable('cache_keys') |
| 129 | + .addColumn('created_at', 'text', (c) => c.notNull()) |
| 130 | + .execute() |
| 131 | + |
| 132 | + // migrate old data |
| 133 | + await db |
| 134 | + .insertInto('cache_keys') |
| 135 | + .expression((e) => e.selectFrom('old_cache_keys').selectAll()) |
| 136 | + .execute() |
| 137 | + |
| 138 | + await db.schema.dropTable('old_cache_keys').execute() |
| 139 | + } |
| 140 | + }, |
| 141 | + }, |
79 | 142 | } satisfies Record<string, Migration>
|
80 | 143 | }
|
0 commit comments