1+ import { Kysely , sql } from 'kysely'
2+
3+ export async function up ( db : Kysely < any > ) : Promise < void > {
4+ // User Table
5+ await db . schema
6+ . createTable ( 'user' )
7+ . addColumn ( 'id' , 'varchar(255)' , ( col ) => col . primaryKey ( ) )
8+ . addColumn ( 'name' , 'text' , ( col ) => col . notNull ( ) )
9+ . addColumn ( 'email' , 'text' , ( col ) => col . notNull ( ) . unique ( ) )
10+ . addColumn ( 'email_verified' , 'boolean' , ( col ) => col . notNull ( ) )
11+ . addColumn ( 'image' , 'text' )
12+ . addColumn ( 'created_at' , 'timestamp' , ( col ) =>
13+ col . notNull ( ) . defaultTo ( sql `CURRENT_TIMESTAMP` )
14+ )
15+ . addColumn ( 'updated_at' , 'timestamp' , ( col ) =>
16+ col . notNull ( ) . defaultTo ( sql `CURRENT_TIMESTAMP` )
17+ )
18+ . execute ( )
19+
20+ // Session Table
21+ await db . schema
22+ . createTable ( 'session' )
23+ . addColumn ( 'id' , 'varchar(255)' , ( col ) => col . primaryKey ( ) )
24+ . addColumn ( 'expires_at' , 'timestamp' , ( col ) => col . notNull ( ) )
25+ . addColumn ( 'token' , 'text' , ( col ) => col . notNull ( ) . unique ( ) )
26+ . addColumn ( 'created_at' , 'timestamp' , ( col ) =>
27+ col . notNull ( ) . defaultTo ( sql `CURRENT_TIMESTAMP` )
28+ )
29+ . addColumn ( 'updated_at' , 'timestamp' , ( col ) =>
30+ col . notNull ( ) . defaultTo ( sql `CURRENT_TIMESTAMP` )
31+ )
32+ . addColumn ( 'ip_address' , 'text' )
33+ . addColumn ( 'user_agent' , 'text' )
34+ . addColumn ( 'user_id' , 'varchar(255)' , ( col ) =>
35+ col . references ( 'user.id' ) . onDelete ( 'cascade' ) . notNull ( )
36+ )
37+ . execute ( )
38+
39+ // Account Table
40+ await db . schema
41+ . createTable ( 'account' )
42+ . addColumn ( 'id' , 'varchar(255)' , ( col ) => col . primaryKey ( ) )
43+ . addColumn ( 'account_id' , 'text' , ( col ) => col . notNull ( ) )
44+ . addColumn ( 'provider_id' , 'text' , ( col ) => col . notNull ( ) )
45+ . addColumn ( 'user_id' , 'varchar(255)' , ( col ) =>
46+ col . references ( 'user.id' ) . onDelete ( 'cascade' ) . notNull ( )
47+ )
48+ . addColumn ( 'access_token' , 'text' )
49+ . addColumn ( 'refresh_token' , 'text' )
50+ . addColumn ( 'id_token' , 'text' )
51+ . addColumn ( 'access_token_expires_at' , 'timestamp' )
52+ . addColumn ( 'refresh_token_expires_at' , 'timestamp' )
53+ . addColumn ( 'scope' , 'text' )
54+ . addColumn ( 'password' , 'text' )
55+ . addColumn ( 'created_at' , 'timestamp' , ( col ) =>
56+ col . notNull ( ) . defaultTo ( sql `CURRENT_TIMESTAMP` )
57+ )
58+ . addColumn ( 'updated_at' , 'timestamp' , ( col ) =>
59+ col . notNull ( ) . defaultTo ( sql `CURRENT_TIMESTAMP` )
60+ )
61+ . execute ( )
62+
63+ // Verification Table
64+ await db . schema
65+ . createTable ( 'verification' )
66+ . addColumn ( 'id' , 'varchar(255)' , ( col ) => col . primaryKey ( ) )
67+ . addColumn ( 'identifier' , 'text' , ( col ) => col . notNull ( ) )
68+ . addColumn ( 'value' , 'text' , ( col ) => col . notNull ( ) )
69+ . addColumn ( 'expires_at' , 'timestamp' , ( col ) => col . notNull ( ) )
70+ . addColumn ( 'created_at' , 'timestamp' )
71+ . addColumn ( 'updated_at' , 'timestamp' )
72+ . execute ( )
73+
74+ // Skill Table
75+ await db . schema
76+ . createTable ( 'skill' )
77+ . addColumn ( 'id' , 'serial' , ( col ) => col . primaryKey ( ) )
78+ . addColumn ( 'name' , 'text' , ( col ) => col . notNull ( ) . unique ( ) )
79+ . addColumn ( 'description' , 'text' )
80+ . addColumn ( 'created_at' , 'timestamp' , ( col ) =>
81+ col . notNull ( ) . defaultTo ( sql `CURRENT_TIMESTAMP` )
82+ )
83+ . addColumn ( 'updated_at' , 'timestamp' , ( col ) =>
84+ col . notNull ( ) . defaultTo ( sql `CURRENT_TIMESTAMP` )
85+ )
86+ . execute ( )
87+
88+ // UserSkill Table
89+ await db . schema
90+ . createTable ( 'user_skill' )
91+ . addColumn ( 'id' , 'serial' , ( col ) => col . primaryKey ( ) )
92+ . addColumn ( 'user_id' , 'varchar(255)' , ( col ) =>
93+ col . references ( 'user.id' ) . onDelete ( 'cascade' ) . notNull ( )
94+ )
95+ . addColumn ( 'skill_id' , 'integer' , ( col ) =>
96+ col . references ( 'skill.id' ) . onDelete ( 'cascade' ) . notNull ( )
97+ )
98+ . addColumn ( 'acquired_at' , 'timestamp' )
99+ . addColumn ( 'level' , 'integer' )
100+ . execute ( )
101+
102+ // Certification Table
103+ await db . schema
104+ . createTable ( 'certification' )
105+ . addColumn ( 'id' , 'serial' , ( col ) => col . primaryKey ( ) )
106+ . addColumn ( 'name' , 'text' , ( col ) => col . notNull ( ) )
107+ . addColumn ( 'issuer' , 'text' )
108+ . execute ( )
109+
110+ // UserCertification Table
111+ await db . schema
112+ . createTable ( 'user_certification' )
113+ . addColumn ( 'id' , 'serial' , ( col ) => col . primaryKey ( ) )
114+ . addColumn ( 'user_id' , 'varchar(255)' , ( col ) =>
115+ col . references ( 'user.id' ) . onDelete ( 'cascade' ) . notNull ( )
116+ )
117+ . addColumn ( 'cert_id' , 'integer' , ( col ) =>
118+ col . references ( 'certification.id' ) . onDelete ( 'cascade' ) . notNull ( )
119+ )
120+ . addColumn ( 'issued_at' , 'timestamp' )
121+ . addColumn ( 'expires_at' , 'timestamp' )
122+ . execute ( )
123+
124+ // Role Table
125+ await db . schema
126+ . createTable ( 'role' )
127+ . addColumn ( 'id' , 'serial' , ( col ) => col . primaryKey ( ) )
128+ . addColumn ( 'name' , 'text' , ( col ) => col . notNull ( ) )
129+ . addColumn ( 'description' , 'text' )
130+ . execute ( )
131+
132+ // Client Table
133+ await db . schema
134+ . createTable ( 'client' )
135+ . addColumn ( 'id' , 'serial' , ( col ) => col . primaryKey ( ) )
136+ . addColumn ( 'name' , 'text' , ( col ) => col . notNull ( ) )
137+ . addColumn ( 'description' , 'text' )
138+ . execute ( )
139+
140+ // Project Table
141+ await db . schema
142+ . createTable ( 'project' )
143+ . addColumn ( 'id' , 'serial' , ( col ) => col . primaryKey ( ) )
144+ . addColumn ( 'name' , 'text' , ( col ) => col . notNull ( ) )
145+ . addColumn ( 'description' , 'text' )
146+ . addColumn ( 'started_at' , 'timestamp' )
147+ . addColumn ( 'ended_at' , 'timestamp' )
148+ . execute ( )
149+
150+ // ClientProject Table
151+ await db . schema
152+ . createTable ( 'client_project' )
153+ . addColumn ( 'id' , 'serial' , ( col ) => col . primaryKey ( ) )
154+ . addColumn ( 'client_id' , 'integer' , ( col ) =>
155+ col . references ( 'client.id' ) . onDelete ( 'cascade' ) . notNull ( )
156+ )
157+ . addColumn ( 'project_id' , 'integer' , ( col ) =>
158+ col . references ( 'project.id' ) . onDelete ( 'cascade' ) . notNull ( )
159+ )
160+ . execute ( )
161+
162+ // ProjectUser Table
163+ await db . schema
164+ . createTable ( 'project_user' )
165+ . addColumn ( 'id' , 'serial' , ( col ) => col . primaryKey ( ) )
166+ . addColumn ( 'project_id' , 'integer' , ( col ) =>
167+ col . references ( 'project.id' ) . onDelete ( 'cascade' ) . notNull ( )
168+ )
169+ . addColumn ( 'user_id' , 'varchar(255)' , ( col ) =>
170+ col . references ( 'user.id' ) . onDelete ( 'cascade' ) . notNull ( )
171+ )
172+ . addColumn ( 'role_id' , 'integer' , ( col ) =>
173+ col . references ( 'role.id' ) . onDelete ( 'cascade' ) . notNull ( )
174+ )
175+ . execute ( )
176+ }
177+
178+ export async function down ( db : Kysely < any > ) : Promise < void > {
179+ await db . schema . dropTable ( 'project_user' ) . execute ( )
180+ await db . schema . dropTable ( 'client_project' ) . execute ( )
181+ await db . schema . dropTable ( 'project' ) . execute ( )
182+ await db . schema . dropTable ( 'client' ) . execute ( )
183+ await db . schema . dropTable ( 'role' ) . execute ( )
184+ await db . schema . dropTable ( 'user_certification' ) . execute ( )
185+ await db . schema . dropTable ( 'certification' ) . execute ( )
186+ await db . schema . dropTable ( 'user_skill' ) . execute ( )
187+ await db . schema . dropTable ( 'skill' ) . execute ( )
188+ await db . schema . dropTable ( 'verification' ) . execute ( )
189+ await db . schema . dropTable ( 'account' ) . execute ( )
190+ await db . schema . dropTable ( 'session' ) . execute ( )
191+ await db . schema . dropTable ( 'user' ) . execute ( )
192+ }
0 commit comments