-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(article-versioning): migrate articles to new drafts
- Loading branch information
Showing
2 changed files
with
53 additions
and
0 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
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,50 @@ | ||
const chunk = require('lodash/chunk') | ||
const { v4: uuidv4 } = require('uuid') | ||
|
||
const article_table = 'article' | ||
const draft_table = 'draft' | ||
|
||
const CHUNK_SIZE = 10 | ||
|
||
exports.up = async (knex) => { | ||
// Gather articles without `draft_id` | ||
const articles = await knex(article_table).select('id') | ||
const chunks = chunk(articles, CHUNK_SIZE) | ||
|
||
for (const ids of chunks) { | ||
// Gather articles with fields to be migrated | ||
const items = await knex(article_table) | ||
.select( | ||
'id', | ||
'draft_id', | ||
|
||
'word_count', | ||
'data_hash', | ||
'media_hash', | ||
'language' | ||
) | ||
.whereIn( | ||
'id', | ||
ids.map(({ id }) => id) | ||
) | ||
|
||
// Update drafts | ||
await Promise.all( | ||
items.map(async (item) => { | ||
const result = await knex(draft_table) | ||
.where({ id: item.draft_id }) | ||
.update({ | ||
article_id: item.id, | ||
word_count: item.word_count, | ||
data_hash: item.data_hash, | ||
media_hash: item.media_hash, | ||
language: item.language, | ||
}) | ||
.returning('id') | ||
console.log(`article (${item.id}) updated to draft (${result[0]})`) | ||
}) | ||
) | ||
} | ||
} | ||
|
||
exports.down = async (knex) => {} |