-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from partio-scout/feature/database-refactor
Feature/database refactor
- Loading branch information
Showing
6 changed files
with
265 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,5 @@ yarn-error.log* | |
|
||
# VS code folder | ||
.vscode | ||
|
||
*.pem |
42 changes: 42 additions & 0 deletions
42
migrations/1642151908120_create-task-entries-history-table.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
39
migrations/1642589908180_migrate-task-entry-history-data.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) => {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.