-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: create the migrations for the conversion of tables to ulid
Every text id in the database will need to be converted to the new `ulid` type once the extensiion allows for joins. This conversion set will update all of the types, and set up the defaults to be the new `gen_ulid` method that the extension provides. Once everything is in working order, I'll create another migration that drops the original `ulid()` function I was working with.
- Loading branch information
Showing
5 changed files
with
144 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ erDiagram | |
string type | ||
} | ||
Deity_Domain { | ||
ulid id | ||
ulid deity_id | ||
ulid domain_id | ||
} | ||
|
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 |
---|---|---|
|
@@ -28,6 +28,7 @@ erDiagram | |
string type | ||
} | ||
Deity_Domain { | ||
ulid id | ||
ulid deity_id | ||
ulid domain_id | ||
} | ||
|
137 changes: 137 additions & 0 deletions
137
libs/db/migrations/src/01H5TV87JJ3CE73WBGZZWQYBP4-convert-to-new-ulid-type.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,137 @@ | ||
import { | ||
AlterColumnBuilder, | ||
AlterColumnBuilderCallback, | ||
Kysely, | ||
sql, | ||
} from 'kysely'; | ||
|
||
const convertToUlid = ( | ||
column: string | ||
): [string, AlterColumnBuilderCallback] => [ | ||
column, | ||
(col: AlterColumnBuilder) => | ||
col.setDataType(sql`ulid USING (${column}::ulid)`), | ||
]; | ||
|
||
const setUlidDefault = ( | ||
column: string | ||
): [string, AlterColumnBuilderCallback] => [ | ||
column, | ||
(col: AlterColumnBuilder) => col.setDefault(sql`gen_ulid()`), | ||
]; | ||
|
||
const migrateTableColumnToUlid = async ( | ||
db: Kysely<any>, | ||
table: string, | ||
column: string, | ||
setDefault = false | ||
): Promise<void> => { | ||
let command = db.schema | ||
.alterTable(table) | ||
.alterColumn(...convertToUlid(column)); | ||
if (setDefault) { | ||
command = command.alterColumn(...setUlidDefault(column)); | ||
} | ||
await command.execute(); | ||
}; | ||
|
||
const recreateForeignKey = async ( | ||
db: Kysely<any>, | ||
table: string, | ||
column: string, | ||
targetTable: string, | ||
targetColumn: string | ||
): Promise<void> => { | ||
await db.schema | ||
.alterTable(table) | ||
.addForeignKeyConstraint(`${table}_${column}_fkey`, [column], targetTable, [ | ||
targetColumn, | ||
]) | ||
.execute(); | ||
}; | ||
|
||
export const up = async (db: Kysely<any>): Promise<void> => { | ||
await sql`CREATE EXTENSION ulid;`.execute(db); | ||
await db.schema | ||
.alterTable('deity_domain') | ||
.dropConstraint('deity_domain_deity_id_fkey') | ||
.execute(); | ||
await db.schema | ||
.alterTable('deity_domain') | ||
.dropConstraint('deity_domain_domain_id_fkey') | ||
.execute(); | ||
await migrateTableColumnToUlid(db, 'domain', 'id', true); | ||
await migrateTableColumnToUlid(db, 'deity', 'id', true); | ||
await migrateTableColumnToUlid(db, 'deity_domain', 'id', true); | ||
await migrateTableColumnToUlid(db, 'deity_domain', 'deity_id'); | ||
await migrateTableColumnToUlid(db, 'deity_domain', 'domain_id'); | ||
await recreateForeignKey(db, 'deity_domain', 'deity_id', 'deity', 'id'); | ||
await recreateForeignKey(db, 'deity_domain', 'domain_id', 'domain', 'id'); | ||
await db.schema | ||
.alterTable('deity') | ||
.dropConstraint('deity_category_fkey') | ||
.execute(); | ||
await db.schema | ||
.alterTable('detiy') | ||
.dropConstraint('deity_location_fkey') | ||
.execute(); | ||
await migrateTableColumnToUlid(db, 'deity_category', 'id', true); | ||
await migrateTableColumnToUlid(db, 'deity', 'category'); | ||
await migrateTableColumnToUlid(db, 'deity', 'location'); | ||
await recreateForeignKey(db, 'deity', 'category', 'deity_category', 'id'); | ||
await migrateTableColumnToUlid(db, 'location', 'id', true); | ||
await recreateForeignKey(db, 'deity', 'location', 'location', 'id'); | ||
await db.schema | ||
.alterTable('racial_ability') | ||
.dropConstraint('racial_ability_race_id_fkey') | ||
.execute(); | ||
await migrateTableColumnToUlid(db, 'race', 'id', true); | ||
await migrateTableColumnToUlid(db, 'racial_ability', 'id', true); | ||
await migrateTableColumnToUlid(db, 'racial_ability', 'race_id'); | ||
await recreateForeignKey(db, 'racial_ability', 'race_id', 'race', 'id'); | ||
await db.schema | ||
.alterTable('user_permission') | ||
.dropConstraint('user_permission_role_id_fkey') | ||
.execute(); | ||
await db.schema | ||
.alterTable('user_permission') | ||
.dropConstraint('user_permission_user_id_fkey') | ||
.execute(); | ||
await db.schema | ||
.alterTable('login_method') | ||
.dropConstraint('login_method_user_id_fkey') | ||
.execute(); | ||
await db.schema | ||
.alterTable('local_login') | ||
.dropConstraint('local_login_login_method_id_fkey') | ||
.execute(); | ||
await migrateTableColumnToUlid(db, 'role', 'id', true); | ||
await migrateTableColumnToUlid(db, 'local_login', 'id', true); | ||
await migrateTableColumnToUlid(db, 'local_login', 'login_method_id'); | ||
await migrateTableColumnToUlid(db, 'login_method', 'id', true); | ||
await migrateTableColumnToUlid(db, 'login_method', 'user_id'); | ||
await migrateTableColumnToUlid(db, 'user_account', 'id', true); | ||
await migrateTableColumnToUlid(db, 'user_permission', 'id', true); | ||
await migrateTableColumnToUlid(db, 'user_permission', 'user_id'); | ||
await migrateTableColumnToUlid(db, 'user_permission', 'role_id'); | ||
await recreateForeignKey(db, 'user_permission', 'role_id', 'role', 'id'); | ||
await recreateForeignKey( | ||
db, | ||
'user_permission', | ||
'user_id', | ||
'user_account', | ||
'id' | ||
); | ||
await recreateForeignKey(db, 'login_method', 'user_id', 'user_account', 'id'); | ||
await recreateForeignKey( | ||
db, | ||
'local_login', | ||
'login_method_id', | ||
'login_method', | ||
'id' | ||
); | ||
}; | ||
|
||
export const down = async (db: Kysely<any>) => { | ||
await sql`DROP EXTENSION ulid`.execute(db); | ||
}; |
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.