Skip to content

Commit

Permalink
Merge pull request #7 from mozilla/master
Browse files Browse the repository at this point in the history
move sentence migration after migration from S3
  • Loading branch information
reinhart1010 committed Jan 21, 2018
2 parents 9c6ce16 + ae200a6 commit ae30a0d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
11 changes: 6 additions & 5 deletions server/src/lib/model/db/migrate-data/migrate-clip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ export async function migrateClip(
[clip.client_id, clip.original_sentence_id, clip.path, sentenceText]
);

const [[sentence]] = (await connection.query(
'SELECT * FROM sentences WHERE id = ?',
[clip.original_sentence_id]
)) as any;
const [
[sentence],
] = (await connection.query('SELECT * FROM sentences WHERE id = ?', [
clip.original_sentence_id,
])) as any;

if (sentence) {
await insertClip(sentence.text);
Expand All @@ -41,7 +42,7 @@ export async function migrateClip(

if (sentenceText) {
await connection.execute(
'INSERT INTO sentences (id, text) VALUES (?, ?) ON DUPLICATE KEY UPDATE id = id',
'INSERT INTO sentences (id, text, is_used) VALUES (?, ?, FALSE) ON DUPLICATE KEY UPDATE is_used = FALSE',
[clip.original_sentence_id, sentenceText]
);
await insertClip(sentenceText);
Expand Down
12 changes: 10 additions & 2 deletions server/src/lib/model/db/migrate-data/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ function print(...args: any[]) {
console.log.apply(console, args);
}

/**
* Migrates all the data from S3 into the database
* Most sentences are retrieved from disk (inside the data dir), but some might be missing and will be fetched from S3.
* Initially that sentence migration happened first, to reduce the number of roundtrips. But since we don't get whether
* the sentence is_used from S3, we have to migrate the sentences from S3 first, and then set the is_used flag based
* on whether the sentence sits in the "not-used" dir or not.
*/
export async function migrate(connection: IConnection) {
print('starting');

try {
await migrateSentences(connection, print);

const gen = fetchS3Data(print);

const votesWithUnknownClips = [];
Expand Down Expand Up @@ -53,6 +58,9 @@ export async function migrate(connection: IConnection) {
print('processed', processedObjectsCount, 'objects from S3');
}
}

await migrateSentences(connection, print);

const [[row]] = await connection.execute(
'SELECT ' +
'(SELECT COUNT(*) FROM sentences) AS sentencesCount,' +
Expand Down

0 comments on commit ae30a0d

Please sign in to comment.