Skip to content

Commit

Permalink
Merge pull request #34 from partio-scout/feature/database-refactor
Browse files Browse the repository at this point in the history
Feature/database refactor
  • Loading branch information
MikaKattainen authored Jan 24, 2022
2 parents 8adf70c + 1873b22 commit b3f5ab6
Show file tree
Hide file tree
Showing 6 changed files with 265 additions and 145 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ yarn-error.log*

# VS code folder
.vscode

*.pem
42 changes: 42 additions & 0 deletions migrations/1642151908120_create-task-entries-history-table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* eslint-disable camelcase */

exports.shorthands = undefined

exports.up = (pgm) => {
pgm.createTable('task_entries_history', {
id: 'id',
user_guid: {
type: 'text',
notNull: true,
},
created_at: {
type: 'timestamp',
default: pgm.func('NOW()'),
},
created_by: {
type: 'text',
notNull: true,
},
task_guid: {
type: 'text',
notNull: true,
},
completion_status: {
type: 'completion_status',
notNull: true,
},
})
}

exports.down = async (pgm) => {
// Make sure we don't have any data we can lose in the history table when doing a migrate down
const hasRows =
(await pgm.db.query('SELECT id FROM task_entries_history')).rows.length > 0
if (hasRows) {
throw new Error(`
Migrate down aborted - possible lose of data if task_entries_history table is dropped: \n
Make sure you've moved all the history data from task_entries_history to task_entries and emptied the task_entries_history table.
`)
}
pgm.dropTable('task_entries_history')
}
39 changes: 39 additions & 0 deletions migrations/1642589908180_migrate-task-entry-history-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* eslint-disable camelcase */

exports.shorthands = undefined

exports.up = async (pgm) => {
// Select all unique entries from the task_entries table
const uniques = await pgm.db.query(
'SELECT user_guid, task_guid FROM task_entries GROUP BY user_guid, task_guid;'
)

const promises = uniques.rows.map(async (data) => {
// Select all the rows for the specific users specific task
const toMoveList = await pgm.db.query(
'SELECT * FROM task_entries WHERE user_guid=$1 AND task_guid=$2 ORDER BY created_at DESC;',
[data.user_guid, data.task_guid]
)

// Move all but the latest entry (first one in the list) to the history table
const movePromises = toMoveList.rows.slice(1).map(async (toMove) => {
// Add a row to the history table
await pgm.db.query(
'INSERT INTO task_entries_history (user_guid, created_at, created_by, task_guid, completion_status) VALUES ($1, $2, $3, $4, $5);',
[
toMove.user_guid,
toMove.created_at,
toMove.created_by,
toMove.task_guid,
toMove.completion_status,
]
)
// Delete the original from the entries table
await pgm.db.query('DELETE FROM task_entries WHERE id = $1;', [toMove.id])
})
await Promise.all(movePromises)
})
await Promise.all(promises)
}

exports.down = (pgm) => {}
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
"express-session": "^1.17.0",
"node-cron": "^3.0.0",
"node-pg-migrate": "5.10.0",
"passport": "^0.4.1",
"passport-saml": "3.1.1",
"passport-saml-metadata": "2.4.1",
"passport": "0.5.2",
"passport-saml": "3.2.0",
"passport-saml-metadata": "2.5.0",
"pg": "8.7.1",
"pg-promise": "10.11.0",
"regenerator-runtime": "^0.13.5",
Expand All @@ -46,7 +46,7 @@
"@babel/preset-env": "7.15.0",
"eslint": "7.32.0",
"husky": "^7.0.0",
"nodemon": "2.0.12",
"nodemon": "2.0.15",
"prettier": "2.3.2",
"pretty-quick": "3.1.1"
}
Expand Down
40 changes: 39 additions & 1 deletion src/database/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ export async function postTaskEntry(taskEntry) {
} = taskEntry

try {
const old_task_entries = await db.any(
'SELECT * FROM task_entries WHERE task_guid = $1 AND user_guid = $2',
[task_guid, user_guid.toString()]
)

if (old_task_entries) {
const deletePromises = old_task_entries.map(archiveTaskEntry)
await Promise.all(deletePromises)
}
const data = await db.one(
'INSERT INTO task_entries(user_guid, created_by, task_guid, completion_status) VALUES ($1, $2, $3, $4) RETURNING id',
[user_guid, created_by, task_guid, completion_status]
Expand All @@ -48,10 +57,39 @@ export async function postTaskEntry(taskEntry) {

return entry
} catch (error) {
console.log('error', error)
console.log('post taskentry - error', error)
}
}

const archiveTaskEntry = async (entry) => {
await addTaskEntryToArchive(entry)
return await deleteTaskEntry(entry.id)
}

export async function addTaskEntryToArchive(taskEntry) {
const { user_guid, created_at, created_by, task_guid, completion_status } =
taskEntry

try {
const data = await db.one(
'INSERT INTO task_entries_history(user_guid, created_at, created_by, task_guid, completion_status) VALUES ($1, $2, $3, $4, $5) RETURNING id',
[user_guid, created_at, created_by, task_guid, completion_status]
)

const entry = await db.one(
'SELECT * FROM task_entries_history WHERE id = $1',
data.id
)
return entry
} catch (error) {
console.log('add Taskentry to archive - error', error)
}
}

const deleteTaskEntry = (id) => {
return db.result('DELETE FROM task_entries WHERE id = $1', id)
}

export async function getTaskEntries(user_guid) {
try {
const data = await db.any(
Expand Down
Loading

0 comments on commit b3f5ab6

Please sign in to comment.