Skip to content

Commit f74be7d

Browse files
fix(db): drop startup backfill per review (#1656)
Remove backfillRepoDates and startup hooks; existing-repo migration will follow in a separate PR with a versioned migration framework. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent d702e19 commit f74be7d

7 files changed

Lines changed: 0 additions & 171 deletions

File tree

src/db/file/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ export const {
3333
addUserCanAuthorise,
3434
removeUserCanPush,
3535
removeUserCanAuthorise,
36-
backfillRepoDates,
3736
deleteRepo,
3837
} = repo;
3938

src/db/file/repo.ts

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -230,48 +230,6 @@ export const removeUserCanPush = async (_id: string, user: string): Promise<void
230230
});
231231
};
232232

233-
/**
234-
* Backfill missing dateCreated/lastModified on existing NeDB repos.
235-
* Idempotent: only updates docs missing either field. Called from adaptor init.
236-
*/
237-
export const backfillRepoDates = async (
238-
fallbackIso = new Date(0).toISOString(),
239-
): Promise<number> => {
240-
return new Promise<number>((resolve, reject) => {
241-
db.find(
242-
{ $or: [{ dateCreated: { $exists: false } }, { lastModified: { $exists: false } }] },
243-
(err: Error | null, docs: Repo[]) => {
244-
/* istanbul ignore if */
245-
if (err) {
246-
reject(err);
247-
return;
248-
}
249-
let updated = 0;
250-
const pending = docs.length;
251-
if (pending === 0) {
252-
resolve(0);
253-
return;
254-
}
255-
docs.forEach((doc) => {
256-
const patch = {
257-
dateCreated: doc.dateCreated ?? fallbackIso,
258-
lastModified: doc.lastModified ?? doc.dateCreated ?? fallbackIso,
259-
};
260-
db.update({ _id: doc._id }, { $set: patch }, {}, (updateErr) => {
261-
/* istanbul ignore if */
262-
if (updateErr) {
263-
reject(updateErr);
264-
return;
265-
}
266-
updated += 1;
267-
if (updated === pending) resolve(updated);
268-
});
269-
});
270-
},
271-
);
272-
});
273-
};
274-
275233
export const deleteRepo = async (_id: string): Promise<void> => {
276234
return new Promise<void>((resolve, reject) => {
277235
db.remove({ _id: _id }, (err) => {

src/db/index.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,10 @@ const start = () => {
3737
if (config.getDatabase().type === 'mongo') {
3838
console.log('Loading MongoDB database adaptor');
3939
_sink = mongo;
40-
// #1486: idempotent backfill for existing repos missing date fields
41-
void mongo.backfillRepoDates().then((n) => {
42-
if (n > 0) console.log(`Backfilled dateCreated/lastModified on ${n} repo(s)`);
43-
});
4440
} else if (config.getDatabase().type === 'fs') {
4541
console.log('Loading neDB database adaptor');
4642
initializeFolders();
4743
_sink = neDb;
48-
void neDb.backfillRepoDates().then((n) => {
49-
if (n > 0) console.log(`Backfilled dateCreated/lastModified on ${n} repo(s)`);
50-
});
5144
} else {
5245
console.error(`Unsupported database type: ${config.getDatabase().type}`);
5346
process.exit(1);

src/db/mongo/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ export const {
3333
addUserCanAuthorise,
3434
removeUserCanPush,
3535
removeUserCanAuthorise,
36-
backfillRepoDates,
3736
deleteRepo,
3837
} = repo;
3938

src/db/mongo/repo.ts

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -96,32 +96,6 @@ export const removeUserCanAuthorise = async (_id: string, user: string): Promise
9696
);
9797
};
9898

99-
/**
100-
* Backfill missing dateCreated/lastModified on existing Mongo repos.
101-
* Idempotent; safe to run on every startup.
102-
*/
103-
export const backfillRepoDates = async (
104-
fallbackIso = new Date(0).toISOString(),
105-
): Promise<number> => {
106-
const collection = await connect(collectionName);
107-
const result = await collection.updateMany(
108-
{
109-
$or: [{ dateCreated: { $exists: false } }, { lastModified: { $exists: false } }],
110-
},
111-
[
112-
{
113-
$set: {
114-
dateCreated: { $ifNull: ['$dateCreated', fallbackIso] },
115-
lastModified: {
116-
$ifNull: ['$lastModified', { $ifNull: ['$dateCreated', fallbackIso] }],
117-
},
118-
},
119-
},
120-
],
121-
);
122-
return result.modifiedCount;
123-
};
124-
12599
export const deleteRepo = async (_id: string): Promise<void> => {
126100
const collection = await connect(collectionName);
127101
await collection.deleteMany({ _id: new ObjectId(_id) });

test/db/db.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@ import { SAMPLE_REPO } from '../../src/proxy/constants';
1919

2020
vi.mock('../../src/db/mongo', () => ({
2121
getRepoByUrl: vi.fn(),
22-
backfillRepoDates: vi.fn().mockResolvedValue(0),
2322
}));
2423

2524
vi.mock('../../src/db/file', () => ({
2625
getRepoByUrl: vi.fn(),
27-
backfillRepoDates: vi.fn().mockResolvedValue(0),
2826
}));
2927

3028
vi.mock('../../src/config', () => ({

test/db/repo.date-fields.test.ts

Lines changed: 0 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,6 @@ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
1818
import * as repoModule from '../../src/db/file/repo';
1919
import { Repo } from '../../src/db/types';
2020

21-
vi.mock('../../src/db/mongo/helper', () => ({
22-
connect: vi.fn(),
23-
}));
24-
25-
import { connect } from '../../src/db/mongo/helper';
26-
import * as mongoRepo from '../../src/db/mongo/repo';
27-
2821
describe('Repo dateCreated / lastModified (#1486)', () => {
2922
beforeEach(() => {
3023
vi.clearAllMocks();
@@ -115,89 +108,4 @@ describe('Repo dateCreated / lastModified (#1486)', () => {
115108

116109
expect(sorted.map((r) => r.name)).toEqual(['stale', 'mid', 'fresh']);
117110
});
118-
119-
it('NeDB backfillRepoDates fills missing fields without dropping other props', async () => {
120-
const docs: Repo[] = [
121-
{
122-
project: 'finos',
123-
name: 'legacy',
124-
url: 'https://github.com/finos/legacy.git',
125-
users: { canPush: ['bob'], canAuthorise: [] },
126-
_id: 'legacy-id',
127-
},
128-
{
129-
project: 'finos',
130-
name: 'partial',
131-
url: 'https://github.com/finos/partial.git',
132-
users: { canPush: [], canAuthorise: [] },
133-
dateCreated: '2021-06-01T00:00:00.000Z',
134-
_id: 'partial-id',
135-
},
136-
];
137-
138-
vi.spyOn(repoModule.db, 'find').mockImplementation((_q: unknown, cb: any) => cb(null, docs));
139-
140-
const patches: Array<{ id: string; set: Record<string, string> }> = [];
141-
vi.spyOn(repoModule.db, 'update').mockImplementation(
142-
(q: any, update: any, _o: unknown, cb: any) => {
143-
patches.push({ id: q._id, set: update.$set });
144-
cb(null, 1);
145-
},
146-
);
147-
148-
const fallback = '1970-01-01T00:00:00.000Z';
149-
const updated = await repoModule.backfillRepoDates(fallback);
150-
151-
expect(updated).toBe(2);
152-
expect(patches).toEqual(
153-
expect.arrayContaining([
154-
{
155-
id: 'legacy-id',
156-
set: { dateCreated: fallback, lastModified: fallback },
157-
},
158-
{
159-
id: 'partial-id',
160-
set: {
161-
dateCreated: '2021-06-01T00:00:00.000Z',
162-
lastModified: '2021-06-01T00:00:00.000Z',
163-
},
164-
},
165-
]),
166-
);
167-
});
168-
169-
it('NeDB backfillRepoDates is a no-op when all repos already have dates', async () => {
170-
vi.spyOn(repoModule.db, 'find').mockImplementation((_q: unknown, cb: any) => cb(null, []));
171-
const updateSpy = vi.spyOn(repoModule.db, 'update');
172-
173-
const updated = await repoModule.backfillRepoDates();
174-
175-
expect(updated).toBe(0);
176-
expect(updateSpy).not.toHaveBeenCalled();
177-
});
178-
179-
it('Mongo backfillRepoDates uses updateMany with $ifNull pipeline', async () => {
180-
const updateMany = vi.fn().mockResolvedValue({ modifiedCount: 3 });
181-
vi.mocked(connect).mockResolvedValue({ updateMany } as any);
182-
183-
const fallback = '1970-01-01T00:00:00.000Z';
184-
const updated = await mongoRepo.backfillRepoDates(fallback);
185-
186-
expect(updated).toBe(3);
187-
expect(updateMany).toHaveBeenCalledWith(
188-
{
189-
$or: [{ dateCreated: { $exists: false } }, { lastModified: { $exists: false } }],
190-
},
191-
[
192-
{
193-
$set: {
194-
dateCreated: { $ifNull: ['$dateCreated', fallback] },
195-
lastModified: {
196-
$ifNull: ['$lastModified', { $ifNull: ['$dateCreated', fallback] }],
197-
},
198-
},
199-
},
200-
],
201-
);
202-
});
203111
});

0 commit comments

Comments
 (0)