One App shouldnt break other app, or the entire cal.com build. Also, handle slug change in seeding update (#8294)

8195-display-error-input-fields-booking-form
Hariom Balhara 2023-04-20 10:11:14 +05:30 committed by GitHub
parent 4577de3b17
commit 33e394d5ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 10 deletions

View File

@ -232,5 +232,12 @@
"slug": "discord", "slug": "discord",
"type": "discord_video", "type": "discord_video",
"isTemplate": false "isTemplate": false
},
{
"dirName": "sylapsvideo",
"categories": ["video"],
"slug": "sylapsvideo",
"type": "sylaps_video",
"isTemplate": false
} }
] ]

View File

@ -157,16 +157,62 @@ async function createApp(
keys?: Prisma.AppCreateInput["keys"], keys?: Prisma.AppCreateInput["keys"],
isTemplate?: boolean isTemplate?: boolean
) { ) {
await prisma.app.upsert({ try {
where: { slug }, const foundApp = await prisma.app.findFirst({
create: { slug, dirName, categories, keys, enabled: true }, /**
update: { dirName, categories, keys }, * slug and dirName both are unique and any of them can be used to find the app uniquely
* Using both here allows us to rename(after the app has been seeded already) `slug` or `dirName` while still finding the app to apply the change on.
* Note: dirName is legacy and it is same as slug for all apps created through App-Store Cli.
* - Take the case there was an app with slug `myvideo` and dirName `dirName-1` and that got seeded. Now, someone wants to rename the slug to `my-video`(more readable) for the app keeping dirName same.
* This would make this fn to be called with slug `my-video` and dirName `dirName-1`.
* Now, we can find the app because dirName would still match.
* - Similar, if someone changes dirName keeping slug same, we can find the app because slug would still match.
* - If both dirName and slug are changed, it will be added as a new entry in the DB.
*/
where: {
OR: [
{
slug,
},
{
dirName,
},
],
},
}); });
// We need to enable seeded apps as they are used in tests.
const data = { slug, dirName, categories, keys, enabled: true };
if (!foundApp) {
await prisma.app.create({
data,
});
console.log(`📲 Created ${isTemplate ? "template" : "app"}: '${slug}'`);
} else {
// We know that the app exists, so either it would have the same slug or dirName
// Because update query can't have both slug and dirName, try to find the app to update by slug and dirName one by one
// if there would have been a unique App.uuid, that never changes, we could have used that in update query.
await prisma.app.update({
where: { slug: foundApp.slug },
data,
});
await prisma.app.update({
where: { dirName: foundApp.dirName },
data,
});
console.log(`📲 Updated ${isTemplate ? "template" : "app"}: '${slug}'`);
}
await prisma.credential.updateMany({ await prisma.credential.updateMany({
// Credential should stop using type and instead use an App.uuid to refer to app deterministically. That uuid would never change even if slug/dirName changes.
// This would allow credentials to be not orphaned when slug(appId) changes.
where: { type }, where: { type },
data: { appId: slug }, data: { appId: slug },
}); });
console.log(`📲 Upserted ${isTemplate ? "template" : "app"}: '${slug}'`); } catch (e) {
console.log(`Could not upsert app: ${slug}. Error:`, e);
}
} }
export default async function main() { export default async function main() {