-
Notifications
You must be signed in to change notification settings - Fork 5
Fix/dreamsync recommendations #691
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
coodos
wants to merge
6
commits into
main
Choose a base branch
from
fix/dreamsync-recommendations
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
33789f0
feat: switch to summary for generating wishlists
coodos a4057fe
chore: add migration script
coodos 4e92c08
chore: add db migration
coodos 344b9da
chore: add db migration
coodos a01e4d6
fix: wishlist filtering
coodos 41a6292
chore: error logs
coodos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -5,6 +5,8 @@ node_modules | |
| .pnp | ||
| .pnp.js | ||
|
|
||
| .docusaurus | ||
|
|
||
| .env.development | ||
|
|
||
| # Local env files | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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,93 @@ | ||
| import "reflect-metadata"; | ||
| import path from "node:path"; | ||
| import { config } from "dotenv"; | ||
| import { AppDataSource } from "../src/database/data-source"; | ||
| import { WishlistSummaryService } from "../src/services/WishlistSummaryService"; | ||
| import { Wishlist } from "../src/database/entities/Wishlist"; | ||
|
|
||
| // Load environment variables | ||
| config({ path: path.resolve(__dirname, "../../../.env") }); | ||
|
|
||
| async function migrateSummaries() { | ||
| console.log("Starting migration: Converting text summaries to AI-generated arrays...\n"); | ||
|
|
||
| try { | ||
| // Initialize database connection | ||
| await AppDataSource.initialize(); | ||
| console.log("Database connection established\n"); | ||
|
|
||
| const wishlistRepository = AppDataSource.getRepository(Wishlist); | ||
| const summaryService = WishlistSummaryService.getInstance(); | ||
|
|
||
| // Get all wishlists that need migration (have text summaries or no summaries) | ||
| const wishlists = await wishlistRepository.find({ | ||
| where: { isActive: true }, | ||
| relations: ["user"], | ||
| order: { updatedAt: "DESC" }, | ||
| }); | ||
|
|
||
| console.log(`Found ${wishlists.length} wishlists to process\n`); | ||
|
|
||
| let processedCount = 0; | ||
| let skippedCount = 0; | ||
| let errorCount = 0; | ||
|
|
||
| for (const wishlist of wishlists) { | ||
| try { | ||
| // Check if already has array summaries | ||
| const hasArraySummaries = | ||
| Array.isArray(wishlist.summaryWants) && wishlist.summaryWants.length > 0 && | ||
| Array.isArray(wishlist.summaryOffers) && wishlist.summaryOffers.length > 0; | ||
|
|
||
| if (hasArraySummaries) { | ||
| skippedCount++; | ||
| continue; | ||
| } | ||
|
|
||
| console.log(`Processing wishlist ${wishlist.id}...`); | ||
| console.log(` Raw content: ${wishlist.content.substring(0, 200)}${wishlist.content.length > 200 ? '...' : ''}`); | ||
|
|
||
| // Use the summary service to generate arrays from the raw content | ||
| const summary = await summaryService.summarizeWishlistContent(wishlist.content); | ||
|
|
||
| console.log(` Generated Wants: ${JSON.stringify(summary.summaryWants)}`); | ||
| console.log(` Generated Offers: ${JSON.stringify(summary.summaryOffers)}`); | ||
|
|
||
| // Save the arrays to the database | ||
| wishlist.summaryWants = summary.summaryWants; | ||
| wishlist.summaryOffers = summary.summaryOffers; | ||
| await wishlistRepository.save(wishlist); | ||
|
|
||
| console.log(` Saved successfully\n`); | ||
| processedCount++; | ||
|
|
||
| } catch (error) { | ||
| console.error(` Error processing wishlist ${wishlist.id}:`, error); | ||
| errorCount++; | ||
| } | ||
| } | ||
|
|
||
| console.log("Migration completed:"); | ||
| console.log(` Processed: ${processedCount}`); | ||
| console.log(` Skipped (already has arrays): ${skippedCount}`); | ||
| console.log(` Errors: ${errorCount}\n`); | ||
|
|
||
| } catch (error) { | ||
| console.error("Migration failed:", error); | ||
| process.exit(1); | ||
| } finally { | ||
| await AppDataSource.destroy(); | ||
| console.log("Database connection closed"); | ||
| } | ||
| } | ||
|
|
||
| // Run migration | ||
| migrateSummaries() | ||
| .then(() => { | ||
| console.log("Migration script completed"); | ||
| process.exit(0); | ||
| }) | ||
| .catch((error) => { | ||
| console.error("Migration script failed:", error); | ||
| process.exit(1); | ||
| }); |
This file contains hidden or 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
20 changes: 20 additions & 0 deletions
20
platforms/dreamsync-api/src/database/migrations/1768904445609-migration.ts
This file contains hidden or 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,20 @@ | ||
| import { MigrationInterface, QueryRunner } from "typeorm"; | ||
|
|
||
| export class Migration1768904445609 implements MigrationInterface { | ||
| name = 'Migration1768904445609' | ||
|
|
||
| public async up(queryRunner: QueryRunner): Promise<void> { | ||
| await queryRunner.query(`ALTER TABLE "wishlists" DROP COLUMN "summaryWants"`); | ||
| await queryRunner.query(`ALTER TABLE "wishlists" ADD "summaryWants" jsonb`); | ||
| await queryRunner.query(`ALTER TABLE "wishlists" DROP COLUMN "summaryOffers"`); | ||
| await queryRunner.query(`ALTER TABLE "wishlists" ADD "summaryOffers" jsonb`); | ||
| } | ||
|
|
||
| public async down(queryRunner: QueryRunner): Promise<void> { | ||
| await queryRunner.query(`ALTER TABLE "wishlists" DROP COLUMN "summaryOffers"`); | ||
| await queryRunner.query(`ALTER TABLE "wishlists" ADD "summaryOffers" text`); | ||
| await queryRunner.query(`ALTER TABLE "wishlists" DROP COLUMN "summaryWants"`); | ||
| await queryRunner.query(`ALTER TABLE "wishlists" ADD "summaryWants" text`); | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Data loss risk: Dropping columns destroys existing summaries.
The migration drops
summaryWantsandsummaryOfferscolumns before re-adding them asjsonb. Any existing text summaries in production will be permanently lost. Themigrate-summaries.tsscript cannot help because it runs after the migration has already deleted the data.Consider using
ALTER COLUMN ... TYPE jsonb USINGto preserve and convert existing data:🐛 Proposed fix to preserve existing data
public async up(queryRunner: QueryRunner): Promise<void> { - await queryRunner.query(`ALTER TABLE "wishlists" DROP COLUMN "summaryWants"`); - await queryRunner.query(`ALTER TABLE "wishlists" ADD "summaryWants" jsonb`); - await queryRunner.query(`ALTER TABLE "wishlists" DROP COLUMN "summaryOffers"`); - await queryRunner.query(`ALTER TABLE "wishlists" ADD "summaryOffers" jsonb`); + // Convert existing text to jsonb arrays, wrapping single strings in arrays + await queryRunner.query(` + ALTER TABLE "wishlists" + ALTER COLUMN "summaryWants" TYPE jsonb + USING CASE + WHEN "summaryWants" IS NULL THEN NULL + ELSE to_jsonb(ARRAY["summaryWants"]) + END + `); + await queryRunner.query(` + ALTER TABLE "wishlists" + ALTER COLUMN "summaryOffers" TYPE jsonb + USING CASE + WHEN "summaryOffers" IS NULL THEN NULL + ELSE to_jsonb(ARRAY["summaryOffers"]) + END + `); }Alternatively, if re-generating all summaries is intentional, document this clearly and ensure the migration script runs immediately after on deployment.
🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nack this data is essentially platform level logic which can be discarded and is not important, the only truly important thing is keeping the actual wishlist safe
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@coodos, understood! SincesummaryWantsandsummaryOffersare derived from the wishlist content and can be regenerated via themigrate-summaries.tsscript, dropping them is safe. The source data remains intact. Thanks for the clarification.✏️ Learnings added