Skip to content

Commit 71bebb4

Browse files
authored
chore(migrations): avoid using vendors.txt to dictate Vendor id in seeder (#1621)
1 parent 06be1cb commit 71bebb4

File tree

3 files changed

+64
-32
lines changed

3 files changed

+64
-32
lines changed

server/seeders/20240903165406-add-vendors.js

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,65 @@ module.exports = {
77
async up(queryInterface, Sequelize) {
88
const vendorLines = await getUsersFromFile('vendors.txt');
99

10+
const vendorToUsers = new Map();
1011
for (const line of vendorLines) {
1112
const [username, companyName] = line.split('|');
1213
if (username && companyName) {
13-
// Create vendor if it doesn't exist
14-
const [vendor] = await queryInterface.sequelize.query(
15-
`INSERT INTO "Vendor" (name, "createdAt", "updatedAt")
16-
VALUES (:name, NOW(), NOW())
17-
ON CONFLICT (name) DO UPDATE SET name = :name
18-
RETURNING id`,
19-
{
20-
replacements: { name: companyName },
21-
type: Sequelize.QueryTypes.INSERT
22-
}
23-
);
14+
if (!vendorToUsers.has(companyName)) {
15+
vendorToUsers.set(companyName, []);
16+
}
17+
vendorToUsers.get(companyName).push(username);
18+
}
19+
}
2420

25-
// Associate user with vendor if user exists
26-
await queryInterface.sequelize.query(
27-
`UPDATE "User" SET "vendorId" = :vendorId
21+
// Create vendor if it doesn't exist
22+
for (const [companyName, companyInfo] of Object.entries(
23+
VENDOR_NAME_TO_AT_MAPPING
24+
)) {
25+
const [vendor] = await queryInterface.sequelize.query(
26+
`INSERT INTO "Vendor" (id, name, "createdAt", "updatedAt")
27+
VALUES (:id, :name, NOW(), NOW())
28+
ON CONFLICT (name) DO UPDATE SET name = :name
29+
RETURNING id`,
30+
{
31+
replacements: { id: companyInfo.id, name: companyName },
32+
type: Sequelize.QueryTypes.INSERT
33+
}
34+
);
35+
36+
// Update sequence to ensure nextval is curr + 1
37+
await queryInterface.sequelize.query(
38+
`SELECT setval(pg_get_serial_sequence('"Vendor"', 'id'), :id, false)`,
39+
{
40+
replacements: { id: vendor[0].id },
41+
type: Sequelize.QueryTypes.SELECT
42+
}
43+
);
44+
45+
// Associate user with vendor if user exists
46+
const usernames = vendorToUsers.get(companyName);
47+
if (usernames) {
48+
for (const username of usernames) {
49+
await queryInterface.sequelize.query(
50+
`UPDATE "User" SET "vendorId" = :vendorId
2851
WHERE username = :username`,
52+
{
53+
replacements: { vendorId: vendor[0].id, username },
54+
type: Sequelize.QueryTypes.UPDATE
55+
}
56+
);
57+
}
58+
}
59+
60+
for (const atName of companyInfo.ats) {
61+
await queryInterface.sequelize.query(
62+
`UPDATE "At" SET "vendorId" = :vendorId
63+
WHERE name = :atName`,
2964
{
30-
replacements: { vendorId: vendor[0].id, username },
65+
replacements: { vendorId: vendor[0].id, atName },
3166
type: Sequelize.QueryTypes.UPDATE
3267
}
3368
);
34-
35-
if (VENDOR_NAME_TO_AT_MAPPING[companyName]) {
36-
for (const atName of VENDOR_NAME_TO_AT_MAPPING[companyName]) {
37-
await queryInterface.sequelize.query(
38-
`UPDATE "At" SET "vendorId" = :vendorId
39-
WHERE name = :atName`,
40-
{
41-
replacements: { vendorId: vendor[0].id, atName },
42-
type: Sequelize.QueryTypes.UPDATE
43-
}
44-
);
45-
}
46-
}
4769
}
4870
}
4971
},

server/seeders/20241030144705-addNvAccessVendor.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
module.exports = {
55
async up(queryInterface, Sequelize) {
66
return queryInterface.sequelize.transaction(async transaction => {
7-
// Create NVDA vendor and retrieve id to use with 'At' table
7+
// Insert vendor if it doesn't exist, or get existing id
88
const [vendor] = await queryInterface.sequelize.query(
99
`insert into "Vendor" (name, "createdAt", "updatedAt")
1010
values ('nvAccess', now(), now())
11+
on conflict (name) do update set name = excluded.name
1112
returning id`,
1213
{
1314
type: Sequelize.QueryTypes.INSERT,

server/util/constants.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,18 @@
22
const NO_OUTPUT_STRING = 'No output was detected.';
33

44
const VENDOR_NAME_TO_AT_MAPPING = {
5-
vispero: ['JAWS'],
6-
nvAccess: ['NVDA'],
7-
apple: ['VoiceOver for macOS']
5+
vispero: {
6+
id: 1,
7+
ats: ['JAWS']
8+
},
9+
apple: {
10+
id: 4,
11+
ats: ['VoiceOver for macOS']
12+
},
13+
nvAccess: {
14+
id: 6,
15+
ats: ['NVDA']
16+
}
817
};
918

1019
module.exports = {

0 commit comments

Comments
 (0)