@tryghost/mg-context 0.11.2 → 0.11.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/README.md +45 -2
  2. package/build/index.d.ts +1 -0
  3. package/build/index.js.map +1 -1
  4. package/build/lib/AuthorContext.d.ts +3 -0
  5. package/build/lib/AuthorContext.js +44 -1
  6. package/build/lib/AuthorContext.js.map +1 -1
  7. package/build/lib/MigrateBase.d.ts +33 -0
  8. package/build/lib/MigrateBase.js +102 -5
  9. package/build/lib/MigrateBase.js.map +1 -1
  10. package/build/lib/MigrateContext.d.ts +6 -1
  11. package/build/lib/MigrateContext.js +55 -31
  12. package/build/lib/MigrateContext.js.map +1 -1
  13. package/build/lib/PostContext.d.ts +3 -2
  14. package/build/lib/PostContext.js +40 -22
  15. package/build/lib/PostContext.js.map +1 -1
  16. package/build/lib/TagContext.d.ts +2 -0
  17. package/build/lib/TagContext.js +16 -1
  18. package/build/lib/TagContext.js.map +1 -1
  19. package/build/lib/database.d.ts +17 -0
  20. package/build/lib/database.js +10 -1
  21. package/build/lib/database.js.map +1 -1
  22. package/build/lib/db-helpers.js +8 -3
  23. package/build/lib/db-helpers.js.map +1 -1
  24. package/build/lib/zod-schema-utils.js.map +1 -1
  25. package/build/test/author-context.test.js +130 -34
  26. package/build/test/author-context.test.js.map +1 -1
  27. package/build/test/lib/NoErrorThrownError.js.map +1 -1
  28. package/build/test/migrate-base.test.js +102 -9
  29. package/build/test/migrate-base.test.js.map +1 -1
  30. package/build/test/migrate-context.test.js +221 -8
  31. package/build/test/migrate-context.test.js.map +1 -1
  32. package/build/test/post-context.test.js +54 -13
  33. package/build/test/post-context.test.js.map +1 -1
  34. package/build/test/tag-context.test.js +24 -10
  35. package/build/test/tag-context.test.js.map +1 -1
  36. package/build/test/tasks.test.js +2 -0
  37. package/build/test/tasks.test.js.map +1 -1
  38. package/package.json +4 -7
@@ -134,7 +134,10 @@ describe('MigrateContext', () => {
134
134
  const allPosts = await instance.getAllPosts();
135
135
  assert.equal(allPosts[0].data.status, 'published');
136
136
  assert.equal(allPosts[1].data.status, 'published');
137
- assert.deepEqual(progressCalls, [[1, 2], [2, 2]]);
137
+ assert.deepEqual(progressCalls, [
138
+ [1, 2],
139
+ [2, 2]
140
+ ]);
138
141
  await instance.close();
139
142
  });
140
143
  describe('findPosts', () => {
@@ -479,7 +482,10 @@ describe('MigrateContext', () => {
479
482
  progressCalls.push([processed, total]);
480
483
  });
481
484
  await instance.forEachGhostPost(async () => { }, { batchSize: 2 });
482
- assert.deepEqual(progressCalls, [[2, 3], [3, 3]]);
485
+ assert.deepEqual(progressCalls, [
486
+ [2, 3],
487
+ [3, 3]
488
+ ]);
483
489
  await instance.close();
484
490
  });
485
491
  it('Does not persist callback mutations', async () => {
@@ -531,7 +537,14 @@ describe('MigrateContext', () => {
531
537
  assert.equal(writtenFiles[0].name, 'posts.json');
532
538
  const ghostJSON = JSON.parse(await readFile(writtenFiles[0].path, 'utf-8'));
533
539
  assert.deepEqual(Object.keys(ghostJSON), ['meta', 'data']);
534
- assert.deepEqual(Object.keys(ghostJSON.data), ['posts', 'users', 'tags', 'posts_authors', 'posts_tags', 'posts_meta']);
540
+ assert.deepEqual(Object.keys(ghostJSON.data), [
541
+ 'posts',
542
+ 'users',
543
+ 'tags',
544
+ 'posts_authors',
545
+ 'posts_tags',
546
+ 'posts_meta'
547
+ ]);
535
548
  assert.deepEqual(ghostJSON.data.posts.length, 2);
536
549
  assert.equal(ghostJSON.data.posts_meta.length, 1);
537
550
  assert.equal(ghostJSON.data.posts_meta[0].meta_title, 'SEO Title');
@@ -807,6 +820,44 @@ describe('MigrateContext', () => {
807
820
  assert.equal(post2.dbId, post1.dbId);
808
821
  await instance.close();
809
822
  });
823
+ it('Does not record a slug rename for a post skipped by lookup_key', async () => {
824
+ const instance = new MigrateContext();
825
+ await instance.init();
826
+ const post1 = await instance.addPost({ lookupKey: 'https://example.com/post-1' });
827
+ post1.set('title', 'First Post');
828
+ post1.set('slug', 'same-slug');
829
+ post1.set('created_at', new Date('2023-11-23T12:00:00.000Z'));
830
+ await post1.save(instance.db);
831
+ const post2 = await instance.addPost({ lookupKey: 'https://example.com/post-1' });
832
+ post2.set('title', 'Duplicate Post');
833
+ post2.set('slug', 'same-slug');
834
+ post2.set('created_at', new Date('2023-11-24T12:00:00.000Z'));
835
+ await post2.save(instance.db);
836
+ // The skipped post never claimed a slug, so no rename was recorded
837
+ assert.equal(instance.db.slugRenames.length, 0);
838
+ // ...and it still holds the slug it was given
839
+ assert.equal(post2.data.slug, 'same-slug');
840
+ const duplicates = await instance.deduplicateSlugs();
841
+ assert.equal(duplicates.length, 0);
842
+ await instance.close();
843
+ });
844
+ it('Still deduplicates slugs across posts with different lookup_keys', async () => {
845
+ const instance = new MigrateContext();
846
+ await instance.init();
847
+ const post1 = await instance.addPost({ lookupKey: 'https://example.com/post-1' });
848
+ post1.set('title', 'First Post');
849
+ post1.set('slug', 'same-slug');
850
+ post1.set('created_at', new Date('2023-11-23T12:00:00.000Z'));
851
+ await post1.save(instance.db);
852
+ const post2 = await instance.addPost({ lookupKey: 'https://example.com/post-2' });
853
+ post2.set('title', 'Second Post');
854
+ post2.set('slug', 'same-slug');
855
+ post2.set('created_at', new Date('2023-11-24T12:00:00.000Z'));
856
+ await post2.save(instance.db);
857
+ assert.equal(instance.db.slugRenames.length, 1);
858
+ assert.equal(post2.data.slug, 'same-slug-2');
859
+ await instance.close();
860
+ });
810
861
  it('Posts without lookup_key always insert normally', async () => {
811
862
  const instance = new MigrateContext();
812
863
  await instance.init();
@@ -1052,7 +1103,9 @@ describe('MigrateContext', () => {
1052
1103
  it('Throws when init() is called twice without close()', async () => {
1053
1104
  const instance = new MigrateContext();
1054
1105
  await instance.init();
1055
- await assert.rejects(() => instance.init(), { message: 'MigrateContext is already initialized. Call close() before reinitializing.' });
1106
+ await assert.rejects(() => instance.init(), {
1107
+ message: 'MigrateContext is already initialized. Call close() before reinitializing.'
1108
+ });
1056
1109
  await instance.close();
1057
1110
  });
1058
1111
  });
@@ -1077,7 +1130,11 @@ describe('MigrateContext', () => {
1077
1130
  // Insert author directly into DB, bypassing the cache
1078
1131
  instance.db.stmts.insertAuthor.run(JSON.stringify({ name: 'Pre-existing', slug: 'pre-author', email: 'pre@example.com' }), 'pre-author', 'Pre-existing', 'pre@example.com', null);
1079
1132
  // Save an AuthorContext with the same slug — should hit the existing.update branch
1080
- const author = new AuthorContext({ name: 'Updated Pre-existing', slug: 'pre-author', email: 'pre@example.com' });
1133
+ const author = new AuthorContext({
1134
+ name: 'Updated Pre-existing',
1135
+ slug: 'pre-author',
1136
+ email: 'pre@example.com'
1137
+ });
1081
1138
  await author.save(instance.db);
1082
1139
  assert.ok(author.dbId);
1083
1140
  const found = await instance.findAuthors({ slug: 'pre-author' });
@@ -1154,7 +1211,10 @@ describe('MigrateContext', () => {
1154
1211
  await post.save(instance.db);
1155
1212
  const loaded = await instance.getAllPosts();
1156
1213
  assert.equal(loaded.length, 1);
1157
- assert.deepEqual(loaded[0].webscrapeData, { meta_title: 'Scraped Title', og_image: 'https://example.com/img.jpg' });
1214
+ assert.deepEqual(loaded[0].webscrapeData, {
1215
+ meta_title: 'Scraped Title',
1216
+ og_image: 'https://example.com/img.jpg'
1217
+ });
1158
1218
  await instance.close();
1159
1219
  });
1160
1220
  it('webscrape_data defaults to null', async () => {
@@ -1647,7 +1707,8 @@ describe('MigrateContext', () => {
1647
1707
  const empty = new MigrateContext();
1648
1708
  await empty.init();
1649
1709
  let count = 0;
1650
- for await (const _post of empty.streamPosts()) { // eslint-disable-line no-unused-vars
1710
+ for await (const _post of empty.streamPosts()) {
1711
+ // eslint-disable-line no-unused-vars
1651
1712
  count += 1;
1652
1713
  }
1653
1714
  assert.equal(count, 0);
@@ -1817,7 +1878,12 @@ describe('MigrateContext', () => {
1817
1878
  const dir = join(tmpdir(), `mg-filter-single-date-${Date.now()}`);
1818
1879
  const files = await ctx.writeGhostJson(dir, {
1819
1880
  batchSize: 1,
1820
- filter: { createdAt: { after: new Date('2023-12-14T00:00:00.000Z'), before: new Date('2023-12-16T00:00:00.000Z') } }
1881
+ filter: {
1882
+ createdAt: {
1883
+ after: new Date('2023-12-14T00:00:00.000Z'),
1884
+ before: new Date('2023-12-16T00:00:00.000Z')
1885
+ }
1886
+ }
1821
1887
  });
1822
1888
  assert.equal(files.length, 1);
1823
1889
  assert.equal(files[0].posts, 1);
@@ -1985,6 +2051,96 @@ describe('MigrateContext', () => {
1985
2051
  });
1986
2052
  });
1987
2053
  describe('deduplicateSlugs', () => {
2054
+ it('Stores the pre-deduplication slug in original_slug', async () => {
2055
+ const instance = new MigrateContext();
2056
+ await instance.init();
2057
+ const post1 = await instance.addPost();
2058
+ post1.set('title', 'Older Post');
2059
+ post1.set('slug', 'my-post');
2060
+ post1.set('created_at', new Date('2023-01-01T00:00:00.000Z'));
2061
+ await post1.save(instance.db);
2062
+ const post2 = await instance.addPost();
2063
+ post2.set('title', 'Newer Post');
2064
+ post2.set('slug', 'my-post');
2065
+ post2.set('created_at', new Date('2023-06-01T00:00:00.000Z'));
2066
+ await post2.save(instance.db);
2067
+ const rows = instance.db.db.prepare('SELECT slug, original_slug FROM Posts ORDER BY id ASC').all();
2068
+ assert.equal(rows.length, 2);
2069
+ assert.equal(rows[0].slug, 'my-post');
2070
+ assert.equal(rows[0].original_slug, 'my-post');
2071
+ assert.equal(rows[1].slug, 'my-post-2');
2072
+ assert.equal(rows[1].original_slug, 'my-post');
2073
+ await instance.close();
2074
+ });
2075
+ it('Leaves original_slug untouched when a post is updated', async () => {
2076
+ const instance = new MigrateContext();
2077
+ await instance.init();
2078
+ const post1 = await instance.addPost();
2079
+ post1.set('title', 'Older Post');
2080
+ post1.set('slug', 'my-post');
2081
+ post1.set('created_at', new Date('2023-01-01T00:00:00.000Z'));
2082
+ await post1.save(instance.db);
2083
+ const post2 = await instance.addPost();
2084
+ post2.set('title', 'Newer Post');
2085
+ post2.set('slug', 'my-post');
2086
+ post2.set('created_at', new Date('2023-06-01T00:00:00.000Z'));
2087
+ await post2.save(instance.db);
2088
+ post2.set('slug', 'renamed-by-hand');
2089
+ await post2.save(instance.db);
2090
+ const row = instance.db.db
2091
+ .prepare('SELECT slug, original_slug FROM Posts WHERE id = ?')
2092
+ .get(post2.dbId);
2093
+ assert.equal(row.slug, 'renamed-by-hand');
2094
+ assert.equal(row.original_slug, 'my-post');
2095
+ await instance.close();
2096
+ });
2097
+ it('Reports renames from an earlier run when resuming an existing dbPath', async () => {
2098
+ const filePath = join(tmpdir(), `mg-context-dedup-resume-${Date.now()}.sqlite`);
2099
+ const firstRun = new MigrateContext({ dbPath: filePath });
2100
+ await firstRun.init();
2101
+ const post1 = await firstRun.addPost({ source: { url: 'https://example.com/blog/my-post' } });
2102
+ post1.set('title', 'First');
2103
+ post1.set('slug', 'my-post');
2104
+ post1.set('created_at', new Date('2023-01-01T00:00:00.000Z'));
2105
+ await post1.save(firstRun.db);
2106
+ const post2 = await firstRun.addPost({ source: { url: 'https://example.com/news/my-post' } });
2107
+ post2.set('title', 'Second');
2108
+ post2.set('slug', 'my-post');
2109
+ post2.set('created_at', new Date('2023-06-01T00:00:00.000Z'));
2110
+ await post2.save(firstRun.db);
2111
+ await firstRun.close();
2112
+ // Resume against the same database — nothing is inserted this run, so the
2113
+ // in-memory renames are empty, but the group is still reported from the DB
2114
+ const secondRun = new MigrateContext({ dbPath: filePath });
2115
+ await secondRun.init();
2116
+ assert.equal(secondRun.db.slugRenames.length, 0);
2117
+ const duplicates = await secondRun.deduplicateSlugs();
2118
+ assert.equal(duplicates.length, 2);
2119
+ assert.equal(duplicates[0].oldSlug, 'my-post');
2120
+ assert.equal(duplicates[0].newSlug, 'my-post');
2121
+ assert.equal(duplicates[0].url, 'https://example.com/blog/my-post');
2122
+ assert.equal(duplicates[1].oldSlug, 'my-post');
2123
+ assert.equal(duplicates[1].newSlug, 'my-post-2');
2124
+ assert.equal(duplicates[1].url, 'https://example.com/news/my-post');
2125
+ await secondRun.close();
2126
+ await unlink(filePath);
2127
+ });
2128
+ it('Ignores a slug that was changed after insert', async () => {
2129
+ const instance = new MigrateContext();
2130
+ await instance.init();
2131
+ const post = await instance.addPost();
2132
+ post.set('title', 'Only Post');
2133
+ post.set('slug', 'original-slug');
2134
+ post.set('created_at', new Date('2023-01-01T00:00:00.000Z'));
2135
+ await post.save(instance.db);
2136
+ // Renaming after insert leaves original_slug behind, but no post holds
2137
+ // "original-slug" any more, so this was not a duplicate-slug collision
2138
+ post.set('slug', 'renamed-by-hand');
2139
+ await post.save(instance.db);
2140
+ const duplicates = await instance.deduplicateSlugs();
2141
+ assert.equal(duplicates.length, 0);
2142
+ await instance.close();
2143
+ });
1988
2144
  it('Returns empty array when no duplicates exist', async () => {
1989
2145
  const instance = new MigrateContext();
1990
2146
  await instance.init();
@@ -2264,5 +2420,62 @@ describe('MigrateContext', () => {
2264
2420
  await instance.close();
2265
2421
  });
2266
2422
  });
2423
+ describe('sanitizedFields', () => {
2424
+ it('Reports values trimmed on the way into the database', async () => {
2425
+ const instance = new MigrateContext();
2426
+ await instance.init();
2427
+ const post = await instance.addPost();
2428
+ post.set('title', 'a'.repeat(300));
2429
+ post.set('slug', 'long-title-post');
2430
+ post.set('created_at', new Date('2023-01-01T00:00:00.000Z'));
2431
+ post.addTag({ name: 'b'.repeat(200), slug: 'long-tag' });
2432
+ post.addAuthor({ name: 'Jane', slug: 'jane', email: 'hello@world.com123' });
2433
+ await post.save(instance.db);
2434
+ const byField = new Map(instance.sanitizedFields.map((f) => [`${f.context}.${f.field}`, f]));
2435
+ assert.equal(byField.get('PostContext.title').reason, 'truncated');
2436
+ assert.equal(byField.get('PostContext.title').newValue.length, 255);
2437
+ assert.equal(byField.get('PostContext.title').slug, 'long-title-post');
2438
+ assert.equal(byField.get('TagContext.name').reason, 'truncated');
2439
+ assert.equal(byField.get('TagContext.name').newValue.length, 191);
2440
+ assert.equal(byField.get('TagContext.name').slug, 'long-tag');
2441
+ assert.equal(byField.get('AuthorContext.email').reason, 'replaced');
2442
+ assert.equal(byField.get('AuthorContext.email').newValue, 'hello-world@example.com');
2443
+ // The truncated values are what actually landed in the database
2444
+ const stored = await instance.findPosts({ slug: 'long-title-post' });
2445
+ assert.equal(stored[0].data.title.length, 255);
2446
+ assert.equal(stored[0].data.tags[0].data.name.length, 191);
2447
+ assert.equal(stored[0].data.authors[0].data.email, 'hello-world@example.com');
2448
+ await instance.close();
2449
+ });
2450
+ it('Is empty when nothing needed changing', async () => {
2451
+ const instance = new MigrateContext();
2452
+ await instance.init();
2453
+ const post = await instance.addPost();
2454
+ post.set('title', 'Fine');
2455
+ post.set('slug', 'fine');
2456
+ post.set('created_at', new Date('2023-01-01T00:00:00.000Z'));
2457
+ await post.save(instance.db);
2458
+ assert.equal(instance.sanitizedFields.length, 0);
2459
+ await instance.close();
2460
+ });
2461
+ it('Keeps a deduplicated slug within the maximum length', async () => {
2462
+ const instance = new MigrateContext();
2463
+ await instance.init();
2464
+ // A slug already at the maximum length — the "-2" must not push it over
2465
+ const longSlug = 'a'.repeat(191);
2466
+ for (const title of ['First', 'Second']) {
2467
+ const post = await instance.addPost();
2468
+ post.set('title', title);
2469
+ post.set('slug', longSlug);
2470
+ post.set('created_at', new Date('2023-01-01T00:00:00.000Z'));
2471
+ await post.save(instance.db);
2472
+ }
2473
+ const rows = instance.db.db.prepare('SELECT slug FROM Posts ORDER BY id ASC').all();
2474
+ assert.equal(rows[0].slug, longSlug);
2475
+ assert.equal(rows[1].slug, `${'a'.repeat(189)}-2`);
2476
+ assert.equal(rows[1].slug.length, 191);
2477
+ await instance.close();
2478
+ });
2479
+ });
2267
2480
  });
2268
2481
  //# sourceMappingURL=migrate-context.test.js.map