|
| 1 | +import { DataSource, QueryRunner } from 'typeorm'; |
| 2 | +import { ScanResourceAttachments1755504936756 } from './1755504936756-scan-resource-attachments'; |
| 3 | + |
| 4 | +// Import all prior migrations in chronological order |
| 5 | +import { Init1751900000000 } from './1751900000000-init'; |
| 6 | +import { UserOptions1751904560034 } from './1751904560034-user-options'; |
| 7 | +import { Tags1751905414493 } from './1751905414493-tags'; |
| 8 | +import { UserBindings1752652489640 } from './1752652489640-user-bindings.ts'; |
| 9 | +import { NullUserEmail1752814358259 } from './1752814358259-null-user-email'; |
| 10 | +import { Shares1753866547335 } from './1753866547335-shares'; |
| 11 | +import { ApiKeys1754550165406 } from './1754550165406-api-keys'; |
| 12 | +import { ResourceAttachments1755059371000 } from './1755059371000-resource-attachments'; |
| 13 | +import { AddTagIdsToResources1755248141570 } from './1755248141570-add-tag-ids-to-resources'; |
| 14 | +import { CleanResourceNames1755396702021 } from './1755396702021-clean-resource-names'; |
| 15 | +import { UpdateAttachmentUrls1755499552000 } from './1755499552000-update-attachment-urls'; |
| 16 | + |
| 17 | +describe('ScanResourceAttachments Migration E2E', () => { |
| 18 | + let dataSource: DataSource; |
| 19 | + let queryRunner: QueryRunner; |
| 20 | + |
| 21 | + beforeAll(async () => { |
| 22 | + dataSource = new DataSource({ |
| 23 | + type: 'postgres', |
| 24 | + url: process.env.OBB_POSTGRES_URL, |
| 25 | + entities: [], |
| 26 | + migrations: [ |
| 27 | + Init1751900000000, |
| 28 | + UserOptions1751904560034, |
| 29 | + Tags1751905414493, |
| 30 | + UserBindings1752652489640, |
| 31 | + NullUserEmail1752814358259, |
| 32 | + Shares1753866547335, |
| 33 | + ApiKeys1754550165406, |
| 34 | + ResourceAttachments1755059371000, |
| 35 | + AddTagIdsToResources1755248141570, |
| 36 | + CleanResourceNames1755396702021, |
| 37 | + UpdateAttachmentUrls1755499552000, |
| 38 | + ], |
| 39 | + synchronize: false, |
| 40 | + migrationsRun: false, |
| 41 | + }); |
| 42 | + await dataSource.initialize(); |
| 43 | + |
| 44 | + // Enable UUID extension |
| 45 | + await dataSource.query('CREATE EXTENSION IF NOT EXISTS "uuid-ossp"'); |
| 46 | + |
| 47 | + // Run migrations manually |
| 48 | + await dataSource.runMigrations(); |
| 49 | + }); |
| 50 | + |
| 51 | + beforeEach(async () => { |
| 52 | + queryRunner = dataSource.createQueryRunner(); |
| 53 | + await queryRunner.connect(); |
| 54 | + await queryRunner.startTransaction(); |
| 55 | + |
| 56 | + // Create a test namespace specific to this migration test |
| 57 | + await queryRunner.query(` |
| 58 | + INSERT INTO namespaces (id, name) VALUES ('scan-resource-attachments-test', 'ScanResourceAttachments Migration Test') |
| 59 | + ON CONFLICT (id) DO NOTHING |
| 60 | + `); |
| 61 | + }); |
| 62 | + |
| 63 | + afterEach(async () => { |
| 64 | + await queryRunner.rollbackTransaction(); |
| 65 | + await queryRunner.release(); |
| 66 | + }); |
| 67 | + |
| 68 | + afterAll(async () => { |
| 69 | + await dataSource.destroy(); |
| 70 | + }); |
| 71 | + |
| 72 | + describe('Attachment relation creation', () => { |
| 73 | + it('should create resource-attachment relations for single attachment', async () => { |
| 74 | + // Setup: Insert resource with attachment reference |
| 75 | + await queryRunner.query(` |
| 76 | + INSERT INTO resources (id, name, namespace_id, resource_type, content) VALUES |
| 77 | + ('res1', 'Test Document', 'scan-resource-attachments-test', 'doc', 'Here is an image: attachments/abc123.jpg') |
| 78 | + `); |
| 79 | + |
| 80 | + // Execute migration |
| 81 | + const migration = new ScanResourceAttachments1755504936756(); |
| 82 | + await migration.up(queryRunner); |
| 83 | + |
| 84 | + // Verify resource_attachments table entry was created for our specific resource |
| 85 | + const relations = await queryRunner.query(` |
| 86 | + SELECT namespace_id, resource_id, attachment_id FROM resource_attachments |
| 87 | + WHERE namespace_id = 'scan-resource-attachments-test' AND resource_id = 'res1' AND attachment_id = 'abc123.jpg' |
| 88 | + ORDER BY attachment_id |
| 89 | + `); |
| 90 | + |
| 91 | + expect(relations).toHaveLength(1); |
| 92 | + expect(relations[0]).toEqual({ |
| 93 | + namespace_id: 'scan-resource-attachments-test', |
| 94 | + resource_id: 'res1', |
| 95 | + attachment_id: 'abc123.jpg', |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should create multiple resource-attachment relations for multiple attachments', async () => { |
| 100 | + // Setup: Insert resource with multiple attachment references |
| 101 | + await queryRunner.query(` |
| 102 | + INSERT INTO resources (id, name, namespace_id, resource_type, content) VALUES |
| 103 | + ('res1', 'Multi-attachment Doc', 'scan-resource-attachments-test', 'doc', |
| 104 | + 'Images: attachments/photo1.png and attachments/photo2.gif |
| 105 | + Audio: attachments/sound.wav |
| 106 | + Video: attachments/clip.mp4') |
| 107 | + `); |
| 108 | + |
| 109 | + // Execute migration |
| 110 | + const migration = new ScanResourceAttachments1755504936756(); |
| 111 | + await migration.up(queryRunner); |
| 112 | + |
| 113 | + // Verify all relations were created |
| 114 | + const relations = await queryRunner.query(` |
| 115 | + SELECT attachment_id FROM resource_attachments |
| 116 | + WHERE namespace_id = 'scan-resource-attachments-test' AND resource_id = 'res1' |
| 117 | + ORDER BY attachment_id |
| 118 | + `); |
| 119 | + |
| 120 | + expect(relations.map((r) => r.attachment_id)).toEqual([ |
| 121 | + 'clip.mp4', |
| 122 | + 'photo1.png', |
| 123 | + 'photo2.gif', |
| 124 | + 'sound.wav', |
| 125 | + ]); |
| 126 | + }); |
| 127 | + |
| 128 | + it('should handle various file extensions correctly', async () => { |
| 129 | + // Setup: Insert resource with different file types |
| 130 | + await queryRunner.query(` |
| 131 | + INSERT INTO resources (id, name, namespace_id, resource_type, content) VALUES |
| 132 | + ('res1', 'Various Extensions', 'scan-resource-attachments-test', 'doc', |
| 133 | + 'Files: attachments/image.jpeg attachments/doc.pdf attachments/audio.mp3 |
| 134 | + attachments/video.webm attachments/data.json attachments/archive.zip |
| 135 | + attachments/drawing.svg attachments/presentation.pptx') |
| 136 | + `); |
| 137 | + |
| 138 | + // Execute migration |
| 139 | + const migration = new ScanResourceAttachments1755504936756(); |
| 140 | + await migration.up(queryRunner); |
| 141 | + |
| 142 | + // Verify all relations were created |
| 143 | + const relations = await queryRunner.query(` |
| 144 | + SELECT attachment_id FROM resource_attachments |
| 145 | + WHERE namespace_id = 'scan-resource-attachments-test' AND resource_id = 'res1' |
| 146 | + ORDER BY attachment_id |
| 147 | + `); |
| 148 | + |
| 149 | + expect(relations.map((r) => r.attachment_id)).toEqual([ |
| 150 | + 'archive.zip', |
| 151 | + 'audio.mp3', |
| 152 | + 'data.json', |
| 153 | + 'doc.pdf', |
| 154 | + 'drawing.svg', |
| 155 | + 'image.jpeg', |
| 156 | + 'presentation.pptx', |
| 157 | + 'video.webm', |
| 158 | + ]); |
| 159 | + }); |
| 160 | + |
| 161 | + it('should handle complex filenames with underscores and hyphens', async () => { |
| 162 | + // Setup: Insert resource with complex attachment filenames |
| 163 | + await queryRunner.query(` |
| 164 | + INSERT INTO resources (id, name, namespace_id, resource_type, content) VALUES |
| 165 | + ('res1', 'Complex Filenames', 'scan-resource-attachments-test', 'doc', |
| 166 | + 'Files: attachments/file-with-hyphens.png attachments/file_with_underscores.jpg |
| 167 | + attachments/MiXeD_CaSe-File123.jpeg attachments/very-long_filename-with-123_numbers.webm') |
| 168 | + `); |
| 169 | + |
| 170 | + // Execute migration |
| 171 | + const migration = new ScanResourceAttachments1755504936756(); |
| 172 | + await migration.up(queryRunner); |
| 173 | + |
| 174 | + // Verify all relations were created |
| 175 | + const relations = await queryRunner.query(` |
| 176 | + SELECT attachment_id FROM resource_attachments |
| 177 | + WHERE namespace_id = 'scan-resource-attachments-test' AND resource_id = 'res1' |
| 178 | + ORDER BY attachment_id |
| 179 | + `); |
| 180 | + |
| 181 | + expect(relations.map((r) => r.attachment_id)).toEqual([ |
| 182 | + 'file-with-hyphens.png', |
| 183 | + 'file_with_underscores.jpg', |
| 184 | + 'MiXeD_CaSe-File123.jpeg', |
| 185 | + 'very-long_filename-with-123_numbers.webm', |
| 186 | + ]); |
| 187 | + }); |
| 188 | + }); |
| 189 | +}); |
0 commit comments