-
Notifications
You must be signed in to change notification settings - Fork 7
fix: fixed the image array to JSON error. #1087
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
Sahil2004
wants to merge
4
commits into
main
Choose a base branch
from
fix/imageArrayToJSON
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
4 commits
Select commit
Hold shift + click to select a range
899ce94
fix: fixed the image array to JSON error.
Sahil2004 cfd7942
fix: fixed the image array to JSON error.
Sahil2004 e9fbaf6
fix: made it more robust.
Sahil2004 210f87b
fix: moved to batch updates instead of all at once which would had ca…
Sahil2004 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
145 changes: 145 additions & 0 deletions
145
platforms/pictique/api/src/database/migrations/1784133148233-ReencodePostImages.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,145 @@ | ||
| import { MigrationInterface, QueryRunner } from "typeorm"; | ||
|
|
||
| /** | ||
| * Re-encode the `posts.images` column from the legacy `simple-array` format | ||
| * (naive comma-join) to `simple-json` (JSON.stringify/parse). | ||
| * | ||
| * Both column types compile down to a plain `text` column, so there is no | ||
| * schema change — only the stored payloads need converting. Without this, rows | ||
| * written under the old encoding would throw a JSON.parse error the first time | ||
| * the entity is read back after the decorator switches to `simple-json`. | ||
| * | ||
| * Base64 data URLs (`data:<mime>;base64,<payload>`) always contain a comma in | ||
| * their MIME prefix, so the old comma-join is ambiguous. We recover the | ||
| * original array by splitting only on commas that immediately precede a new | ||
| * `data:` URL — the `data:` token cannot occur inside a base64 payload (whose | ||
| * alphabet excludes `:`), so this boundary is unambiguous for image posts. | ||
| * Values that are already valid JSON arrays are left untouched (idempotent). | ||
| */ | ||
| export class ReencodePostImages1784133148233 implements MigrationInterface { | ||
|
|
||
| // Number of rows loaded per keyset-paginated batch. Kept modest because | ||
| // each image payload can be a multi-MB base64 data URL. | ||
| private static readonly BATCH_SIZE = 200; | ||
|
|
||
| public async up(queryRunner: QueryRunner): Promise<void> { | ||
| const batchSize = ReencodePostImages1784133148233.BATCH_SIZE; | ||
| // Keyset cursor: the all-zero UUID is the lower bound, so `id > cursor` | ||
| // starts from the first row. Paging by id (never by the mutated | ||
| // `images` column) means re-encoded rows can't reappear in a later | ||
| // batch, so the walk always terminates. | ||
| let cursor = "00000000-0000-0000-0000-000000000000"; | ||
| let batch: Array<{ id: string; images: string | null }>; | ||
|
|
||
| do { | ||
| batch = await queryRunner.query( | ||
| `SELECT "id", "images" FROM "posts" | ||
| WHERE "images" IS NOT NULL AND "id" > $1 | ||
| ORDER BY "id" ASC | ||
| LIMIT $2`, | ||
| [cursor, batchSize], | ||
| ); | ||
| if (batch.length === 0) break; | ||
| cursor = batch[batch.length - 1].id; | ||
|
|
||
| const updates: Array<{ id: string; images: string }> = []; | ||
| for (const row of batch) { | ||
| if (typeof row.images !== "string") continue; | ||
| const reencoded = this.reencode(row.images); | ||
| // `undefined` = already valid JSON, no write needed. | ||
| if (reencoded !== undefined) { | ||
| updates.push({ id: row.id, images: reencoded }); | ||
| } | ||
| } | ||
|
|
||
| if (updates.length > 0) { | ||
| // Single bulk UPDATE per batch via a VALUES join, instead of | ||
| // one round-trip per row. | ||
| const valuesSql = updates | ||
| .map((_, i) => `($${i * 2 + 1}::uuid, $${i * 2 + 2}::text)`) | ||
| .join(", "); | ||
| const params = updates.flatMap((u) => [u.id, u.images]); | ||
| await queryRunner.query( | ||
| `UPDATE "posts" AS p | ||
| SET "images" = v.images | ||
| FROM (VALUES ${valuesSql}) AS v(id, images) | ||
| WHERE p."id" = v.id`, | ||
| params, | ||
| ); | ||
| } | ||
| } while (batch.length === batchSize); | ||
| } | ||
|
|
||
| /** | ||
| * Convert a single legacy `images` value to its `simple-json` encoding. | ||
| * Returns the new string to store, or `undefined` when the value is already | ||
| * a valid JSON array and should be left untouched. | ||
| */ | ||
| private reencode(raw: string): string | undefined { | ||
| // Empty string is how simple-array encoded an empty array. Left as "", | ||
| // simple-json would choke on JSON.parse("") — convert to "[]". | ||
| if (raw === "") return "[]"; | ||
|
|
||
| // Already migrated (valid JSON array) — leave as-is. | ||
| if (raw.trimStart().startsWith("[")) { | ||
| try { | ||
| JSON.parse(raw); | ||
| return undefined; | ||
| } catch { | ||
| // Not actually valid JSON; fall through and re-encode. | ||
| } | ||
| } | ||
|
|
||
| // Reconstruct the array from the legacy comma-joined string. | ||
| // Walk comma-separated tokens: a base64 data URL got split across two | ||
| // tokens ("data:<mime>;base64" + "<payload>") by the comma in its own | ||
| // prefix, so rejoin that pair. Any other value (e.g. a Firebase/HTTP | ||
| // download URL) contains no internal comma and stands alone. This | ||
| // recovers pure-data, pure-URL, and mixed posts in any order. | ||
| const tokens = raw.split(","); | ||
| const images: string[] = []; | ||
| for (let i = 0; i < tokens.length; i++) { | ||
| const token = tokens[i].trim(); | ||
| if (token === "") continue; | ||
|
|
||
| if (token.startsWith("data:")) { | ||
| const payload = (tokens[i + 1] ?? "").trim(); | ||
| images.push(payload ? `${token},${payload}` : token); | ||
| i++; // consume the payload token | ||
| } else { | ||
| images.push(token); | ||
| } | ||
| } | ||
|
|
||
| return JSON.stringify(images); | ||
| } | ||
|
|
||
| public async down(queryRunner: QueryRunner): Promise<void> { | ||
| // Reverse: re-encode JSON arrays back to the legacy comma-joined format. | ||
| const rows: Array<{ id: string; images: string | null }> = | ||
| await queryRunner.query( | ||
| `SELECT "id", "images" FROM "posts" WHERE "images" IS NOT NULL AND "images" <> ''`, | ||
| ); | ||
|
|
||
| for (const row of rows) { | ||
| const raw = row.images; | ||
| if (raw === null || raw === "") continue; | ||
|
|
||
| let images: string[]; | ||
| try { | ||
| const parsed = JSON.parse(raw); | ||
| if (!Array.isArray(parsed)) continue; | ||
| images = parsed; | ||
| } catch { | ||
| // Not JSON — already in legacy format. | ||
| continue; | ||
| } | ||
|
|
||
| await queryRunner.query( | ||
| `UPDATE "posts" SET "images" = $1 WHERE "id" = $2`, | ||
| [images.join(","), row.id], | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| } | ||
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.