-
Notifications
You must be signed in to change notification settings - Fork 17
/
1633233019501_add_avatar_columns.ts
44 lines (39 loc) · 1.22 KB
/
1633233019501_add_avatar_columns.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { extname } from 'path'
import mimeTypes from 'mime-types'
import BaseSchema from '@ioc:Adonis/Lucid/Schema'
export default class Users extends BaseSchema {
protected tableName = 'users'
public async up() {
this.schema.alterTable(this.tableName, (table) => {
table.json('avatar').nullable()
})
/**
* Migrating avatar_filename to an avatar JSON property. This
* will allow us to use "attachment_lite"
*/
this.defer(async (connection) => {
const users = await connection.query().select(['avatar_filename', 'id']).from('users')
await Promise.all(
users.map((user) => {
return connection
.from('users')
.where('id', user.id)
.update({
avatar: user.avatar_filename
? {
name: user.avatar_filename,
extname: extname(user.avatar_filename),
size: 1024,
mimeType: mimeTypes.lookup(extname(user.avatar_filename)),
}
: null,
})
})
)
})
this.schema.alterTable(this.tableName, (table) => {
table.dropColumn('avatar_filename')
})
}
public async down() {}
}