40 lines
1.1 KiB
TypeScript
40 lines
1.1 KiB
TypeScript
|
import {Inject, Injectable} from '../di'
|
||
|
import {ConstraintType, DatabaseService, FieldType, Migration, Schema} from '../orm'
|
||
|
|
||
|
/**
|
||
|
* Migration that creates the sessions table used by the ORMSession backend.
|
||
|
*/
|
||
|
@Injectable()
|
||
|
export default class CreateSessionsTableMigration extends Migration {
|
||
|
@Inject()
|
||
|
protected readonly db!: DatabaseService
|
||
|
|
||
|
async up(): Promise<void> {
|
||
|
const schema: Schema = this.db.get().schema()
|
||
|
const table = await schema.table('sessions')
|
||
|
|
||
|
table.primaryKey('session_uuid', FieldType.varchar)
|
||
|
.required()
|
||
|
|
||
|
table.column('session_data')
|
||
|
.type(FieldType.json)
|
||
|
.required()
|
||
|
.default('{}')
|
||
|
|
||
|
table.constraint('session_uuid_ck')
|
||
|
.type(ConstraintType.Check)
|
||
|
.expression('LENGTH(session_uuid) > 0')
|
||
|
|
||
|
await schema.commit(table)
|
||
|
}
|
||
|
|
||
|
async down(): Promise<void> {
|
||
|
const schema: Schema = this.db.get().schema()
|
||
|
const table = await schema.table('sessions')
|
||
|
|
||
|
table.dropIfExists()
|
||
|
|
||
|
await schema.commit(table)
|
||
|
}
|
||
|
}
|