forked from athenekilta/ilmomasiina
-
Notifications
You must be signed in to change notification settings - Fork 9
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 #116 from Tietokilta/fix/answer-semicolons
Stop splitting strings by magic semicolons
- Loading branch information
Showing
23 changed files
with
216 additions
and
104 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
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
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
14 changes: 7 additions & 7 deletions
14
packages/ilmomasiina-backend/src/models/migrations/0002-add-event-endDate.ts
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 |
---|---|---|
@@ -1,18 +1,18 @@ | ||
import { DataTypes, Sequelize } from 'sequelize'; | ||
import { RunnableMigration } from 'umzug'; | ||
import { DataTypes } from 'sequelize'; | ||
|
||
const migration: RunnableMigration<Sequelize> = { | ||
import { defineMigration } from './util'; | ||
|
||
export default defineMigration({ | ||
name: '0002-add-event-endDate', | ||
async up({ context: sequelize }) { | ||
async up({ context: { sequelize, transaction } }) { | ||
const query = sequelize.getQueryInterface(); | ||
await query.addColumn( | ||
'event', | ||
'endDate', | ||
{ | ||
type: DataTypes.DATE, | ||
}, | ||
{ transaction }, | ||
); | ||
}, | ||
}; | ||
|
||
export default migration; | ||
}); |
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
63 changes: 63 additions & 0 deletions
63
packages/ilmomasiina-backend/src/models/migrations/0004-answers-to-json.ts
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,63 @@ | ||
import { QueryTypes } from 'sequelize'; | ||
|
||
import { defineMigration } from './util'; | ||
|
||
type RawQuestion = { | ||
id: string; | ||
type: string; | ||
options: string; | ||
}; | ||
|
||
type RawAnswer = { | ||
id: string; | ||
answer: string; | ||
type: string; | ||
}; | ||
|
||
/* eslint-disable no-await-in-loop */ | ||
|
||
export default defineMigration({ | ||
name: '0004-answers-to-json', | ||
async up({ context: { sequelize, transaction } }) { | ||
const query = sequelize.getQueryInterface(); | ||
// Handle different quoting for Postgres & MySQL | ||
const q = ([name]: TemplateStringsArray) => query.quoteIdentifiers(name); | ||
// Convert question options to JSON | ||
const questions = await sequelize.query<RawQuestion>( | ||
`SELECT ${q`id`}, ${q`type`}, ${q`options`} FROM ${q`question`}`, | ||
{ type: QueryTypes.SELECT, transaction }, | ||
); | ||
for (const row of questions) { | ||
let optionsJson = null; | ||
if (row.type === 'checkbox' || row.type === 'select') { | ||
optionsJson = row.options ? JSON.stringify(row.options.split(';')) : JSON.stringify(['']); | ||
} | ||
await query.bulkUpdate( | ||
'question', | ||
{ options: optionsJson }, | ||
{ id: row.id }, | ||
{ transaction }, | ||
); | ||
} | ||
// Convert answers to JSON | ||
const answers = await sequelize.query<RawAnswer>( | ||
`SELECT ${q`answer.id`}, ${q`answer.answer`}, ${q`question.type`} ` | ||
+ `FROM ${q`answer`} ` | ||
+ `LEFT JOIN ${q`question`} ON ${q`answer.questionId`} = ${q`question.id`}`, | ||
{ type: QueryTypes.SELECT, transaction }, | ||
); | ||
for (const row of answers) { | ||
// Non-checkbox question -> "entire answer" | ||
// Non-empty answer to checkbox question -> ["ans1", "ans2"] | ||
// Empty answer to checkbox question -> [] | ||
const answer = row.type === 'checkbox' ? row.answer.split(';').filter(Boolean) : row.answer; | ||
const answerJson = JSON.stringify(answer); | ||
await query.bulkUpdate( | ||
'answer', | ||
{ answer: answerJson }, | ||
{ id: row.id }, | ||
{ transaction }, | ||
); | ||
} | ||
}, | ||
}); |
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
25 changes: 25 additions & 0 deletions
25
packages/ilmomasiina-backend/src/models/migrations/util.ts
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,25 @@ | ||
import { Sequelize, Transaction } from 'sequelize'; | ||
import { RunnableMigration } from 'umzug'; | ||
|
||
export type MigrationContext = { | ||
sequelize: Sequelize; | ||
transaction: Transaction; | ||
}; | ||
|
||
export function defineMigration(migration: RunnableMigration<MigrationContext>): RunnableMigration<Sequelize> { | ||
return { | ||
...migration, | ||
up: ({ context: sequelize, ...params }) => ( | ||
sequelize.transaction((transaction) => migration.up({ | ||
...params, | ||
context: { sequelize, transaction }, | ||
})) | ||
), | ||
down: migration.down && (({ context: sequelize, ...params }) => ( | ||
sequelize.transaction((transaction) => migration.down!({ | ||
...params, | ||
context: { sequelize, transaction }, | ||
})) | ||
)), | ||
}; | ||
} |
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
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.