Skip to content

Commit 5151ff7

Browse files
committed
refactor(migrations): update regex
1 parent d865dda commit 5151ff7

File tree

4 files changed

+28
-28
lines changed

4 files changed

+28
-28
lines changed

src/migrations/1755499552000-update-attachment-urls.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ export class UpdateAttachmentUrls1755499552000 implements MigrationInterface {
2424
for (const resource of resources) {
2525
const originalContent = resource.content;
2626

27-
// Replace /api/v1/attachments/images/{id}.{extension} and
28-
// /api/v1/attachments/media/{id}.{extension} with attachments/{id}.{extension}
27+
// Replace /api/v1/attachments/images/{id} and
28+
// /api/v1/attachments/media/{id} with attachments/{id}
2929
const updatedContent = originalContent.replace(
30-
/\/api\/v1\/attachments\/(images|media)\/([a-zA-Z0-9_-]+\.[a-zA-Z0-9]+)/g,
30+
/\/api\/v1\/attachments\/(images|media)\/([a-zA-Z0-9\._-]+)/g,
3131
'attachments/$2',
3232
);
3333

src/migrations/1755504936756-scan-resource-attachments.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ export class ScanResourceAttachments1755504936756
2626
for (const resource of resources) {
2727
const content = resource.content;
2828

29-
// Find all attachment references in the format: attachments/{id}.{extension}
29+
// Find all attachment references in the format: attachments/{id}
3030
const attachmentMatches = content.match(
31-
/attachments\/([a-zA-Z0-9_-]+)\.[a-zA-Z0-9]+/g,
31+
/attachments\/([a-zA-Z0-9\._-]+)/g,
3232
);
3333

3434
for (const match of attachmentMatches || []) {

src/migrations/scan-resource-attachments.e2e-spec.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ describe('ScanResourceAttachments Migration E2E', () => {
5353
await queryRunner.connect();
5454
await queryRunner.startTransaction();
5555

56-
// Create a test namespace that resources can reference
56+
// Create a test namespace specific to this migration test
5757
await queryRunner.query(`
58-
INSERT INTO namespaces (id, name) VALUES ('test-ns', 'Test Namespace')
58+
INSERT INTO namespaces (id, name) VALUES ('scan-resource-attachments-test', 'ScanResourceAttachments Migration Test')
5959
ON CONFLICT (id) DO NOTHING
6060
`);
6161
});
@@ -74,7 +74,7 @@ describe('ScanResourceAttachments Migration E2E', () => {
7474
// Setup: Insert resource with attachment reference
7575
await queryRunner.query(`
7676
INSERT INTO resources (id, name, namespace_id, resource_type, content) VALUES
77-
('res1', 'Test Document', 'test-ns', 'doc', 'Here is an image: attachments/abc123.jpg')
77+
('res1', 'Test Document', 'scan-resource-attachments-test', 'doc', 'Here is an image: attachments/abc123.jpg')
7878
`);
7979

8080
// Execute migration
@@ -84,13 +84,13 @@ describe('ScanResourceAttachments Migration E2E', () => {
8484
// Verify resource_attachments table entry was created for our specific resource
8585
const relations = await queryRunner.query(`
8686
SELECT namespace_id, resource_id, attachment_id FROM resource_attachments
87-
WHERE namespace_id = 'test-ns' AND resource_id = 'res1' AND attachment_id = 'abc123.jpg'
87+
WHERE namespace_id = 'scan-resource-attachments-test' AND resource_id = 'res1' AND attachment_id = 'abc123.jpg'
8888
ORDER BY attachment_id
8989
`);
9090

9191
expect(relations).toHaveLength(1);
9292
expect(relations[0]).toEqual({
93-
namespace_id: 'test-ns',
93+
namespace_id: 'scan-resource-attachments-test',
9494
resource_id: 'res1',
9595
attachment_id: 'abc123.jpg',
9696
});
@@ -100,7 +100,7 @@ describe('ScanResourceAttachments Migration E2E', () => {
100100
// Setup: Insert resource with multiple attachment references
101101
await queryRunner.query(`
102102
INSERT INTO resources (id, name, namespace_id, resource_type, content) VALUES
103-
('res1', 'Multi-attachment Doc', 'test-ns', 'doc',
103+
('res1', 'Multi-attachment Doc', 'scan-resource-attachments-test', 'doc',
104104
'Images: attachments/photo1.png and attachments/photo2.gif
105105
Audio: attachments/sound.wav
106106
Video: attachments/clip.mp4')
@@ -113,7 +113,7 @@ describe('ScanResourceAttachments Migration E2E', () => {
113113
// Verify all relations were created
114114
const relations = await queryRunner.query(`
115115
SELECT attachment_id FROM resource_attachments
116-
WHERE namespace_id = 'test-ns' AND resource_id = 'res1'
116+
WHERE namespace_id = 'scan-resource-attachments-test' AND resource_id = 'res1'
117117
ORDER BY attachment_id
118118
`);
119119

@@ -129,7 +129,7 @@ describe('ScanResourceAttachments Migration E2E', () => {
129129
// Setup: Insert resource with different file types
130130
await queryRunner.query(`
131131
INSERT INTO resources (id, name, namespace_id, resource_type, content) VALUES
132-
('res1', 'Various Extensions', 'test-ns', 'doc',
132+
('res1', 'Various Extensions', 'scan-resource-attachments-test', 'doc',
133133
'Files: attachments/image.jpeg attachments/doc.pdf attachments/audio.mp3
134134
attachments/video.webm attachments/data.json attachments/archive.zip
135135
attachments/drawing.svg attachments/presentation.pptx')
@@ -142,7 +142,7 @@ describe('ScanResourceAttachments Migration E2E', () => {
142142
// Verify all relations were created
143143
const relations = await queryRunner.query(`
144144
SELECT attachment_id FROM resource_attachments
145-
WHERE namespace_id = 'test-ns' AND resource_id = 'res1'
145+
WHERE namespace_id = 'scan-resource-attachments-test' AND resource_id = 'res1'
146146
ORDER BY attachment_id
147147
`);
148148

@@ -162,7 +162,7 @@ describe('ScanResourceAttachments Migration E2E', () => {
162162
// Setup: Insert resource with complex attachment filenames
163163
await queryRunner.query(`
164164
INSERT INTO resources (id, name, namespace_id, resource_type, content) VALUES
165-
('res1', 'Complex Filenames', 'test-ns', 'doc',
165+
('res1', 'Complex Filenames', 'scan-resource-attachments-test', 'doc',
166166
'Files: attachments/file-with-hyphens.png attachments/file_with_underscores.jpg
167167
attachments/MiXeD_CaSe-File123.jpeg attachments/very-long_filename-with-123_numbers.webm')
168168
`);
@@ -174,7 +174,7 @@ describe('ScanResourceAttachments Migration E2E', () => {
174174
// Verify all relations were created
175175
const relations = await queryRunner.query(`
176176
SELECT attachment_id FROM resource_attachments
177-
WHERE namespace_id = 'test-ns' AND resource_id = 'res1'
177+
WHERE namespace_id = 'scan-resource-attachments-test' AND resource_id = 'res1'
178178
ORDER BY attachment_id
179179
`);
180180

src/migrations/update-attachment-urls.e2e-spec.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ describe('UpdateAttachmentUrls Migration E2E', () => {
5151
await queryRunner.connect();
5252
await queryRunner.startTransaction();
5353

54-
// Create a test namespace that resources can reference
54+
// Create a test namespace specific to this migration test
5555
await queryRunner.query(`
56-
INSERT INTO namespaces (id, name) VALUES ('test-ns', 'Test Namespace')
56+
INSERT INTO namespaces (id, name) VALUES ('update-attachment-urls-test', 'UpdateAttachmentUrls Migration Test')
5757
ON CONFLICT (id) DO NOTHING
5858
`);
5959
});
@@ -72,9 +72,9 @@ describe('UpdateAttachmentUrls Migration E2E', () => {
7272
// Setup: Insert resources with old-style image attachment URLs
7373
await queryRunner.query(`
7474
INSERT INTO resources (id, name, namespace_id, resource_type, content) VALUES
75-
('res1', 'Test Document 1', 'test-ns', 'doc', 'Here is an image: /api/v1/attachments/images/abc123.jpg'),
76-
('res2', 'Test Document 2', 'test-ns', 'doc', 'Multiple images: /api/v1/attachments/images/test1.png and /api/v1/attachments/images/test2.gif'),
77-
('res3', 'Test Document 3', 'test-ns', 'doc', 'No attachments here')
75+
('res1', 'Test Document 1', 'update-attachment-urls-test', 'doc', 'Here is an image: /api/v1/attachments/images/abc123.jpg'),
76+
('res2', 'Test Document 2', 'update-attachment-urls-test', 'doc', 'Multiple images: /api/v1/attachments/images/test1.png and /api/v1/attachments/images/test2.gif'),
77+
('res3', 'Test Document 3', 'update-attachment-urls-test', 'doc', 'No attachments here')
7878
`);
7979

8080
// Execute migration
@@ -84,7 +84,7 @@ describe('UpdateAttachmentUrls Migration E2E', () => {
8484
// Verify results for our specific test resources only
8585
const results = await queryRunner.query(`
8686
SELECT id, content FROM resources
87-
WHERE namespace_id = 'test-ns' AND id IN ('res1', 'res2', 'res3')
87+
WHERE namespace_id = 'update-attachment-urls-test' AND id IN ('res1', 'res2', 'res3')
8888
ORDER BY id
8989
`);
9090

@@ -103,9 +103,9 @@ describe('UpdateAttachmentUrls Migration E2E', () => {
103103
// Setup: Insert resources with old-style media attachment URLs
104104
await queryRunner.query(`
105105
INSERT INTO resources (id, name, namespace_id, resource_type, content) VALUES
106-
('res1', 'Audio Document', 'test-ns', 'doc', 'Audio file: /api/v1/attachments/media/audio123.wav'),
107-
('res2', 'Video Document', 'test-ns', 'doc', 'Video: /api/v1/attachments/media/video456.mp4'),
108-
('res3', 'Mixed Media', 'test-ns', 'doc', 'Mixed: /api/v1/attachments/images/pic.jpg and /api/v1/attachments/media/sound.mp3')
106+
('res1', 'Audio Document', 'update-attachment-urls-test', 'doc', 'Audio file: /api/v1/attachments/media/audio123.wav'),
107+
('res2', 'Video Document', 'update-attachment-urls-test', 'doc', 'Video: /api/v1/attachments/media/video456.mp4'),
108+
('res3', 'Mixed Media', 'update-attachment-urls-test', 'doc', 'Mixed: /api/v1/attachments/images/pic.jpg and /api/v1/attachments/media/sound.mp3')
109109
`);
110110

111111
// Execute migration
@@ -115,7 +115,7 @@ describe('UpdateAttachmentUrls Migration E2E', () => {
115115
// Verify results for our specific test resources only
116116
const results = await queryRunner.query(`
117117
SELECT id, content FROM resources
118-
WHERE namespace_id = 'test-ns' AND id IN ('res1', 'res2', 'res3')
118+
WHERE namespace_id = 'update-attachment-urls-test' AND id IN ('res1', 'res2', 'res3')
119119
ORDER BY id
120120
`);
121121

@@ -133,7 +133,7 @@ describe('UpdateAttachmentUrls Migration E2E', () => {
133133
// Setup: Insert resources with mix of old and new URLs
134134
await queryRunner.query(`
135135
INSERT INTO resources (id, name, namespace_id, resource_type, content) VALUES
136-
('res1', 'Mixed URLs', 'test-ns', 'doc',
136+
('res1', 'Mixed URLs', 'update-attachment-urls-test', 'doc',
137137
'Old: /api/v1/attachments/images/old.jpg New: attachments/new.png Already converted: attachments/existing.gif')
138138
`);
139139

@@ -155,7 +155,7 @@ describe('UpdateAttachmentUrls Migration E2E', () => {
155155
// Setup: Insert resources with other API URLs that should not be changed
156156
await queryRunner.query(`
157157
INSERT INTO resources (id, name, namespace_id, resource_type, content) VALUES
158-
('res1', 'Other APIs', 'test-ns', 'doc',
158+
('res1', 'Other APIs', 'update-attachment-urls-test', 'doc',
159159
'These should not change: /api/v1/resources/123 /api/v1/auth/login /api/v1/users/profile
160160
But this should: /api/v1/attachments/images/photo.jpg
161161
And this: /api/v1/attachments/media/video.mp4')

0 commit comments

Comments
 (0)