@@ -163,34 +163,67 @@ changes completely. Migrations need to be "frozen in time".
163163The migrations can use the [ Kysely.schema] ( https://koskimas.github.io/kysely/classes/SchemaModule.html )
164164module to modify the schema. Migrations can also run normal queries to modify the data.
165165
166+ ### PostgreSQL migration example
167+
166168``` ts
167169import { Kysely } from ' kysely'
168170
169171export async function up(db : Kysely <any >): Promise <void > {
170172 await db .schema
171173 .createTable (' person' )
172- .addColumn (' id' , ' integer ' , (col ) => col . increments () .primaryKey ())
174+ .addColumn (' id' , ' serial ' , (col ) => col .primaryKey ())
173175 .addColumn (' first_name' , ' varchar' )
174176 .addColumn (' last_name' , ' varchar' )
175177 .addColumn (' gender' , ' varchar(50)' )
176178 .execute ()
177179
178180 await db .schema
179181 .createTable (' pet' )
180- .addColumn (' id' , ' integer ' , (col ) => col . increments () .primaryKey ())
182+ .addColumn (' id' , ' serial ' , (col ) => col .primaryKey ())
181183 .addColumn (' name' , ' varchar' , (col ) => col .notNull ().unique ())
182184 .addColumn (' owner_id' , ' integer' , (col ) =>
183185 col .references (' person.id' ).onDelete (' cascade' )
184186 )
185187 .addColumn (' species' , ' varchar' )
186- // Older MySQL versions don't support column-level foreign key
187- // constraint definitions and you need to add them using the table
188- // builder by uncommenting the following:
189- //
190- // .addForeignKeyConstraint(
191- // 'pet_owner_id_fk', ['owner_id'], 'person', ['id'],
192- // (cb) => cb.onDelete('cascade')
193- // )
188+ .execute ()
189+
190+ await db .schema
191+ .createIndex (' pet_owner_id_index' )
192+ .on (' pet' )
193+ .column (' owner_id' )
194+ .execute ()
195+ }
196+
197+ export async function down(db : Kysely <any >): Promise <void > {
198+ await db .schema .dropTable (' pet' ).execute ()
199+ await db .schema .dropTable (' person' ).execute ()
200+ }
201+ ```
202+
203+ ### MySQL migration example
204+
205+ ``` ts
206+ import { Kysely } from ' kysely'
207+
208+ export async function up(db : Kysely <any >): Promise <void > {
209+ await db .schema
210+ .createTable (' person' )
211+ .addColumn (' id' , ' integer' , (col ) => col .increments ().primaryKey ())
212+ .addColumn (' first_name' , ' varchar' )
213+ .addColumn (' last_name' , ' varchar' )
214+ .addColumn (' gender' , ' varchar(50)' )
215+ .execute ()
216+
217+ await db .schema
218+ .createTable (' pet' )
219+ .addColumn (' id' , ' integer' , (col ) => col .increments ().primaryKey ())
220+ .addColumn (' name' , ' varchar' , (col ) => col .notNull ().unique ())
221+ .addColumn (' owner_id' , ' integer' )
222+ .addColumn (' species' , ' varchar' )
223+ .addForeignKeyConstraint (
224+ ' pet_owner_id_fk' , [' owner_id' ], ' person' , [' id' ],
225+ (cb ) => cb .onDelete (' cascade' )
226+ )
194227 .execute ()
195228
196229 await db .schema
0 commit comments