Skip to content

Commit

Permalink
add database migration for group feature
Browse files Browse the repository at this point in the history
  • Loading branch information
denis99999 committed Feb 12, 2020
1 parent d3011ff commit 311b077
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions lib/db/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,79 @@ dbapi.unlockBookingObjects = function() {
dbapi.createBootStrap = function(env) {
const now = Date.now()

function updateUsersForMigration(group) {
return dbapi.getUsers().then(function(users) {
return Promise.map(users, function(user) {
return db.run(r.table('users').get(user.email).update({
privilege: user.email !== group.owner.email ? apiutil.USER : apiutil.ADMIN
, groups: {
subscribed: []
, lock: false
, quotas: {
allocated: {
number: group.envUserGroupsNumber
, duration: group.envUserGroupsDuration
}
, consumed: {
number: 0
, duration: 0
}
, defaultGroupsNumber: user.email !== group.owner.email ?
0 :
group.envUserGroupsNumber
, defaultGroupsDuration: user.email !== group.owner.email ?
0 :
group.envUserGroupsDuration
, defaultGroupsRepetitions: user.email !== group.owner.email ?
0 :
group.envUserGroupsRepetitions
, repetitions: group.envUserGroupsRepetitions
}
}
}))
.then(function(stats) {
if (stats.replaced) {
return dbapi.addGroupUser(group.id, user.email)
}
return stats
})
})
})
}

function getDevices() {
return db.run(r.table('devices'))
.then(function(cursor) {
return cursor.toArray()
})
}

function updateDevicesForMigration(group) {
return getDevices().then(function(devices) {
return Promise.map(devices, function(device) {
return db.run(r.table('devices').get(device.serial).update({
group: {
id: group.id
, name: group.name
, lifeTime: group.dates[0]
, owner: group.owner
, origin: group.id
, class: group.class
, repetitions: group.repetitions
, originName: group.name
, lock: false
}}
))
.then(function(stats) {
if (stats.replaced) {
return dbapi.addOriginGroupDevice(group, device.serial)
}
return stats
})
})
})
}

return dbapi.createGroup({
name: env.STF_ROOT_GROUP_NAME
, owner: {
Expand Down Expand Up @@ -65,6 +138,12 @@ dbapi.createBootStrap = function(env) {
, email: group.owner.email
, ip: '127.0.0.1'
})
.then(function() {
return updateUsersForMigration(group)
})
.then(function() {
return updateDevicesForMigration(group)
})
.then(function() {
return dbapi.reserveUserGroupInstance(group.owner.email)
})
Expand Down

0 comments on commit 311b077

Please sign in to comment.