@@ -2,61 +2,65 @@ import { Kysely, sql } from 'kysely'
22import { createTableMigration } from '../migration.util'
33
44export async function up ( db : Kysely < unknown > ) : Promise < void > {
5- await createTableMigration ( db , 'users' )
6- . addColumn ( 'email' , 'text' , ( col ) => col . unique ( ) . notNull ( ) )
7- . addColumn ( 'first_name' , 'text' , ( col ) => col . notNull ( ) )
8- . addColumn ( 'last_name' , 'text' , ( col ) => col . notNull ( ) )
9- . addColumn ( 'role' , 'text' , ( col ) => col . defaultTo ( 'user' ) . notNull ( ) )
10- . addColumn ( 'password_hash' , 'text' , ( col ) => col . notNull ( ) )
11- . execute ( )
5+ await db . transaction ( ) . execute ( async ( tx ) => {
6+ await createTableMigration ( tx , 'users' )
7+ . addColumn ( 'email' , 'text' , ( col ) => col . unique ( ) . notNull ( ) )
8+ . addColumn ( 'first_name' , 'text' , ( col ) => col . notNull ( ) )
9+ . addColumn ( 'last_name' , 'text' , ( col ) => col . notNull ( ) )
10+ . addColumn ( 'role' , 'text' , ( col ) => col . defaultTo ( 'user' ) . notNull ( ) )
11+ . addColumn ( 'password_hash' , 'text' , ( col ) => col . notNull ( ) )
12+ . execute ( )
1213
13- await createTableMigration ( db , 'projects' )
14- . addColumn ( 'name' , 'text' , ( col ) => col . unique ( ) . notNull ( ) )
15- . addColumn ( 'base_language' , 'integer' , ( col ) =>
16- col . references ( 'languages.id' ) . onDelete ( 'restrict' ) . notNull ( )
17- )
18- . execute ( )
14+ await createTableMigration ( tx , 'projects' )
15+ . addColumn ( 'name' , 'text' , ( col ) => col . unique ( ) . notNull ( ) )
16+ . addColumn ( 'base_language' , 'integer' , ( col ) =>
17+ col . references ( 'languages.id' ) . onDelete ( 'restrict' ) . notNull ( )
18+ )
19+ . execute ( )
1920
20- await createTableMigration ( db , 'languages' )
21- . addColumn ( 'code' , 'text' , ( col ) => col . unique ( ) . notNull ( ) )
22- . addColumn ( 'fallback_language' , 'integer' , ( col ) => col . references ( 'languages.id' ) )
23- . addColumn ( 'project_id' , 'integer' , ( col ) =>
24- col . references ( 'project.id' ) . onDelete ( 'cascade' ) . notNull ( )
25- )
26- . execute ( )
21+ await createTableMigration ( tx , 'languages' )
22+ . addColumn ( 'code' , 'text' , ( col ) => col . unique ( ) . notNull ( ) )
23+ . addColumn ( 'fallback_language' , 'integer' , ( col ) => col . references ( 'languages.id' ) )
24+ . addColumn ( 'project_id' , 'integer' , ( col ) =>
25+ col . references ( 'project.id' ) . onDelete ( 'cascade' ) . notNull ( )
26+ )
27+ . execute ( )
2728
28- await createTableMigration ( db , 'keys' )
29- . addColumn ( 'project_id' , 'integer' , ( col ) =>
30- col . references ( 'projects.id' ) . onDelete ( 'cascade' ) . notNull ( )
31- )
32- . addColumn ( 'name' , 'text' , ( col ) => col . unique ( ) . notNull ( ) )
33- . execute ( )
29+ await createTableMigration ( tx , 'keys' )
30+ . addColumn ( 'project_id' , 'integer' , ( col ) =>
31+ col . references ( 'projects.id' ) . onDelete ( 'cascade' ) . notNull ( )
32+ )
33+ . addColumn ( 'name' , 'text' , ( col ) => col . unique ( ) . notNull ( ) )
34+ . execute ( )
3435
35- await createTableMigration ( db , 'translations' )
36- . addColumn ( 'key_id' , 'integer' , ( col ) =>
37- col . references ( 'keys.id' ) . onDelete ( 'cascade' ) . notNull ( )
38- )
39- . addColumn ( 'language_id' , 'integer' , ( col ) =>
40- col . references ( 'languages.id' ) . onDelete ( 'cascade' ) . notNull ( )
41- )
42- . addColumn ( 'value' , 'text' )
43- . execute ( )
36+ await createTableMigration ( tx , 'translations' )
37+ . addColumn ( 'key_id' , 'integer' , ( col ) =>
38+ col . references ( 'keys.id' ) . onDelete ( 'cascade' ) . notNull ( )
39+ )
40+ . addColumn ( 'language_id' , 'integer' , ( col ) =>
41+ col . references ( 'languages.id' ) . onDelete ( 'cascade' ) . notNull ( )
42+ )
43+ . addColumn ( 'value' , 'text' )
44+ . execute ( )
4445
45- await createTableMigration ( db , 'projects_users' , false , false )
46- . addColumn ( 'project_id' , 'integer' , ( col ) => col . references ( 'projects.id' ) . notNull ( ) )
47- . addColumn ( 'user_id' , 'integer' , ( col ) => col . references ( 'user.id' ) . notNull ( ) )
48- . addColumn ( 'permission' , 'text' , ( col ) =>
49- col . check ( sql `permission in ('READONLY', 'WRITE', 'ADMIN')` )
50- )
51- . addPrimaryKeyConstraint ( 'projects_users_pk' , [ 'project_id' , 'user_id' ] )
52- . execute ( )
46+ await createTableMigration ( tx , 'projects_users' , false , false )
47+ . addColumn ( 'project_id' , 'integer' , ( col ) => col . references ( 'projects.id' ) . notNull ( ) )
48+ . addColumn ( 'user_id' , 'integer' , ( col ) => col . references ( 'user.id' ) . notNull ( ) )
49+ . addColumn ( 'permission' , 'text' , ( col ) =>
50+ col . check ( sql `permission in ('READONLY', 'WRITE', 'ADMIN')` )
51+ )
52+ . addPrimaryKeyConstraint ( 'projects_users_pk' , [ 'project_id' , 'user_id' ] )
53+ . execute ( )
54+ } )
5355}
5456
5557export async function down ( db : Kysely < unknown > ) : Promise < void > {
56- await db . schema . dropTable ( 'users' ) . execute ( )
57- await db . schema . dropTable ( 'projects_users' ) . execute ( )
58- await db . schema . dropTable ( 'translations' ) . execute ( )
59- await db . schema . dropTable ( 'keys' ) . execute ( )
60- await db . schema . dropTable ( 'languages' ) . execute ( )
61- await db . schema . dropTable ( 'projects' ) . execute ( )
58+ await db . transaction ( ) . execute ( async ( tx ) => {
59+ await tx . schema . dropTable ( 'users' ) . execute ( )
60+ await tx . schema . dropTable ( 'projects_users' ) . execute ( )
61+ await tx . schema . dropTable ( 'translations' ) . execute ( )
62+ await tx . schema . dropTable ( 'keys' ) . execute ( )
63+ await tx . schema . dropTable ( 'languages' ) . execute ( )
64+ await tx . schema . dropTable ( 'projects' ) . execute ( )
65+ } )
6266}
0 commit comments