Skip to content

Commit

Permalink
feat(article-versioning): migrate articles to new drafts
Browse files Browse the repository at this point in the history
  • Loading branch information
robertu7 committed Sep 28, 2020
1 parent e798622 commit 0ce1c8f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
3 changes: 3 additions & 0 deletions db/migrations/20200925181638_alter_draft_as_article.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ exports.up = async (knex) => {
// Draft
await knex.schema.table(draft_table, (t) => {
// drop deprecated
t.dropColumn('upstream_id')
t.dropColumn('scheduled_at')

// new from article
Expand Down Expand Up @@ -49,5 +50,7 @@ exports.down = async (knex) => {
t.dropColumn('word_count')

t.timestamp('scheduled_at')
t.bigInteger('upstream_id').unsigned()
t.foreign('upstream_id').references('id').inTable(article_table)
})
}
50 changes: 50 additions & 0 deletions db/migrations/20200925214015_migrate_article_to_draft.js
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) => {}

0 comments on commit 0ce1c8f

Please sign in to comment.