Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add user to flow to StorageFlows table, to record who triggered auto snapshot #4518

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion forge/db/controllers/ProjectSnapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,12 @@ const instanceAutoSnapshotUtils = {
// 2. log the snapshot creation in audit log
// 3. delete older auto snapshots if the limit is reached (10)
// do NOT delete any snapshots that are currently in use by an target (instance/device/device group)
const user = meta?.user || { id: null } // if no user is available, use `null` (system user)
const currentFlow = await app.db.models.StorageFlow.byProject(project.id)
let flowUser = null
if (currentFlow.UserId !== null) {
flowUser = currentFlow.UserId
}
const user = meta?.user || { id: flowUser } // if no user is available, use `null` (system user)

// 1. create a snapshot from the instance
const snapShot = await app.db.controllers.ProjectSnapshot.createSnapshot(
Expand Down
21 changes: 21 additions & 0 deletions forge/db/migrations/20240920-02-add-flow-user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Add User to StorageFlows table
*/
const { DataTypes } = require('sequelize')

module.exports = {
up: async (context) => {
await context.addColumn('StorageFlows', 'UserId', {
type: DataTypes.INTEGER,
allowNull: true,
defaultValue: null,
references: { model: 'Users', key: 'id' },
// CHECK ME
onDelete: 'SET NULL',
// CHECK ME
onUpdate: 'CASCADE'
Comment on lines +13 to +16
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// CHECK ME
onDelete: 'SET NULL',
// CHECK ME
onUpdate: 'CASCADE'
onDelete: 'SET NULL',
onUpdate: 'CASCADE'

happy with cascades

})
},
down: async (context) => {
}
}
3 changes: 2 additions & 1 deletion forge/db/models/StorageFlow.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ module.exports = {
},
associations: function (M) {
this.belongsTo(M.Project)
this.belongsTo(M.User)
},
finders: function (M) {
return {
static: {
byProject: async (project) => {
return this.findOne({
where: { ProjectId: project },
attributes: ['id', 'flow', 'updatedAt']
attributes: ['id', 'flow', 'updatedAt', 'UserId']
})
}
}
Expand Down
6 changes: 6 additions & 0 deletions forge/routes/storage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,20 @@ module.exports = async function (app) {

app.post('/:projectId/flows', async (request, response) => {
const id = request.params.projectId
let UserId = null
if (request.headers['ff-user']) {
UserId = app.db.models.User.decodeHashid(request.headers['ff-user'])[0]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
UserId = app.db.models.User.decodeHashid(request.headers['ff-user'])[0]
UserId = app.db.models.User.decodeHashid(request.headers['ff-user'])?.[0] || null

}
// Check if the project exists first
let flow = await app.db.models.StorageFlow.byProject(id)
if (flow) {
flow.flow = JSON.stringify(request.body)
flow.UserId = UserId
await flow.save()
} else {
flow = await app.db.models.StorageFlow.create({
flow: JSON.stringify(request.body),
UserId,
ProjectId: id
})

Expand Down
Loading