Skip to content

Commit e076d40

Browse files
committed
add migration to move seqqc data from reports to reports_seq_qc
1 parent 64b740e commit e076d40

1 file changed

Lines changed: 92 additions & 0 deletions

File tree

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
const {v4: uuidv4} = require('uuid');
2+
3+
module.exports = {
4+
up: async (queryInterface, Sq) => {
5+
await queryInterface.sequelize.transaction(async (transaction) => {
6+
const reports = await queryInterface.sequelize.query(
7+
`
8+
SELECT id, seq_qc
9+
FROM reports
10+
WHERE seq_qc IS NOT NULL;
11+
`,
12+
{
13+
type: queryInterface.sequelize.QueryTypes.SELECT,
14+
transaction,
15+
},
16+
);
17+
18+
const now = new Date();
19+
const rows = [];
20+
21+
for (const report of reports) {
22+
for (const seqQCItem of report.seq_qc) {
23+
rows.push({
24+
ident: uuidv4(),
25+
created_at: now,
26+
updated_at: now,
27+
report_id: report.id,
28+
reads: seqQCItem.Reads,
29+
bio_qc: seqQCItem.bioQC,
30+
lab_qc: seqQCItem.labQC,
31+
sample: seqQCItem.Sample,
32+
library: seqQCItem.Library,
33+
coverage: seqQCItem.Coverage,
34+
input_ng: seqQCItem.Input_ng,
35+
input_ug: seqQCItem.Input_ug,
36+
protocol: seqQCItem.Protocol,
37+
sample_name: seqQCItem['Sample Name'],
38+
duplicate_reads_perc: seqQCItem.Duplicate_Reads_Perc,
39+
});
40+
}
41+
}
42+
43+
if (rows.length !== 0) {
44+
await queryInterface.bulkInsert('reports_seqqc', rows, {transaction});
45+
}
46+
47+
await queryInterface.removeColumn('reports', 'seq_qc', {transaction});
48+
});
49+
},
50+
51+
down: async (queryInterface, Sq) => {
52+
await queryInterface.sequelize.transaction(async (transaction) => {
53+
await queryInterface.addColumn('reports', 'seq_qc', {
54+
type: Sq.JSONB,
55+
defaultValue: null,
56+
}, {transaction});
57+
58+
await queryInterface.sequelize.query(`
59+
WITH restored_seq_qc AS (
60+
SELECT
61+
report_id,
62+
jsonb_agg(jsonb_build_object(
63+
'Reads', reads,
64+
'bioQC', bio_qc,
65+
'labQC', lab_qc,
66+
'Sample', sample,
67+
'Library', library,
68+
'Coverage', coverage,
69+
'Input_ng', input_ng,
70+
'Input_ug', input_ug,
71+
'Protocol', protocol,
72+
'Sample Name', sample_name,
73+
'Duplicate_Reads_Perc', duplicate_reads_perc
74+
)) AS seq_qc
75+
FROM reports_seqqc
76+
GROUP BY report_id
77+
)
78+
UPDATE reports r
79+
SET seq_qc = restored_seq_qc.seq_qc
80+
FROM restored_seq_qc
81+
WHERE r.id = restored_seq_qc.report_id;
82+
`, {transaction});
83+
84+
await queryInterface.sequelize.query(`
85+
DELETE FROM reports_seqqc rs
86+
USING reports r
87+
WHERE rs.report_id = r.id
88+
AND r.seq_qc IS NOT NULL;
89+
`, {transaction});
90+
});
91+
},
92+
};

0 commit comments

Comments
 (0)