-
Notifications
You must be signed in to change notification settings - Fork 23
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 #3068 from quantified-uncertainty/model-revision-s…
…eeds2 Adds simple migration and edits pages for seeds in db
- Loading branch information
Showing
20 changed files
with
142 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@quri/squiggle-lang": patch | ||
"@quri/squiggle-components": patch | ||
--- | ||
|
||
Seeds should be stored for Model Revisions |
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 was deleted.
Oops, something went wrong.
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
3 changes: 3 additions & 0 deletions
3
packages/hub/prisma/migrations/20240217215104_squiggle_snippet_seed/migration.sql
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,3 @@ | ||
ALTER TABLE "SquiggleSnippet" ADD COLUMN "seed" TEXT DEFAULT 'DEFAULT_SEED'; | ||
UPDATE "SquiggleSnippet" SET "seed" = 'DEFAULT_SEED' WHERE "seed" IS NULL; | ||
ALTER TABLE "SquiggleSnippet" ALTER COLUMN "seed" SET NOT NULL; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { PrismaClient } from "@prisma/client"; | ||
import { NextRequest, NextResponse } from "next/server"; | ||
|
||
import { generateSeed } from "@quri/squiggle-lang"; | ||
|
||
const prisma = new PrismaClient(); | ||
|
||
async function updateSquiggleSnippetsSeedForModels() { | ||
// Retrieve all models | ||
const models = await prisma.model.findMany({ | ||
include: { | ||
revisions: { | ||
include: { | ||
squiggleSnippet: true, | ||
}, | ||
orderBy: { | ||
createdAt: "desc", | ||
}, | ||
take: 1, | ||
}, | ||
}, | ||
}); | ||
|
||
for (const model of models) { | ||
const lastRevision = model.revisions[0]; | ||
|
||
// Check if the last revision's SquiggleSnippet has the DEFAULT_SEED | ||
if ( | ||
lastRevision.squiggleSnippet && | ||
lastRevision.squiggleSnippet.seed === "DEFAULT_SEED" | ||
) { | ||
const newSeed = generateSeed(); | ||
|
||
// Update all SquiggleSnippets for all revisions of the current model | ||
for (const revision of model.revisions) { | ||
if (revision.squiggleSnippet) { | ||
await prisma.squiggleSnippet.update({ | ||
where: { | ||
id: revision.squiggleSnippet.id, | ||
}, | ||
data: { | ||
seed: newSeed, | ||
}, | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
export async function POST(req: NextRequest) { | ||
const adminToken = req.headers.get("x-admin-token"); | ||
const secretToken = process.env["ADMIN_SECRET_TOKEN"]; | ||
|
||
if (!secretToken || adminToken !== secretToken) { | ||
return new NextResponse(null, { | ||
status: 401, | ||
statusText: "Unauthorized access.", | ||
}); | ||
} | ||
|
||
try { | ||
await updateSquiggleSnippetsSeedForModels(); | ||
return new NextResponse( | ||
JSON.stringify({ | ||
message: | ||
"Successfully updated seeds for models where the last revision had DEFAULT_SEED.", | ||
}), | ||
{ status: 200 } | ||
); | ||
} catch (error) { | ||
console.error("An error occurred:", error); | ||
return new NextResponse( | ||
JSON.stringify({ | ||
error: "An error occurred while updating seeds.", | ||
}), | ||
{ status: 500 } | ||
); | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import sample from "lodash/sample.js"; | ||
|
||
import { seedWords } from "./seedWords.js"; | ||
|
||
export function generateSeed(): string { | ||
return [ | ||
sample(seedWords)?.toUpperCase(), | ||
sample(seedWords)?.toUpperCase(), | ||
sample(seedWords)?.toUpperCase(), | ||
].join("_"); | ||
} |
File renamed without changes.