|
| 1 | +import * as fs from 'fs'; |
| 2 | +import { exportEventsAsTsv, importEventsFromTsv } from '../event-replay/event-replay'; |
| 3 | +import { PgWriteStore } from '../datastore/pg-write-store'; |
| 4 | +import { dangerousDropAllTables, runMigrations } from '../datastore/migrations'; |
| 5 | +import { databaseHasData } from '../datastore/event-requests'; |
| 6 | +import { getPgClientConfig } from '../datastore/connection-legacy'; |
| 7 | + |
| 8 | +describe('import/export tests', () => { |
| 9 | + let db: PgWriteStore; |
| 10 | + |
| 11 | + beforeEach(async () => { |
| 12 | + process.env.PG_DATABASE = 'postgres'; |
| 13 | + db = await PgWriteStore.connect({ |
| 14 | + usageName: 'tests', |
| 15 | + withNotifier: false, |
| 16 | + skipMigrations: true, |
| 17 | + }); |
| 18 | + }); |
| 19 | + |
| 20 | + test('event import and export cycle', async () => { |
| 21 | + // Import from mocknet TSV |
| 22 | + await importEventsFromTsv('src/tests-event-replay/tsv/mocknet.tsv', 'archival', true, true); |
| 23 | + const chainTip = await db.getUnanchoredChainTip(); |
| 24 | + expect(chainTip.found).toBe(true); |
| 25 | + expect(chainTip.result?.blockHeight).toBe(28); |
| 26 | + expect(chainTip.result?.indexBlockHash).toBe( |
| 27 | + '0x76cd67a65c0dfd5ea450bb9efe30da89fa125bfc077c953802f718353283a533' |
| 28 | + ); |
| 29 | + expect(chainTip.result?.blockHash).toBe( |
| 30 | + '0x7682af212d3c1ef62613412f9b5a727269b4548f14eca2e3f941f7ad8b3c11b2' |
| 31 | + ); |
| 32 | + |
| 33 | + // Export into temp TSV |
| 34 | + const tmpDir = 'src/tests-event-replay/.tmp'; |
| 35 | + try { |
| 36 | + fs.mkdirSync(tmpDir); |
| 37 | + } catch (error: any) { |
| 38 | + if (error.code != 'EEXIST') throw error; |
| 39 | + } |
| 40 | + const tmpTsvPath = `${tmpDir}/export.tsv`; |
| 41 | + await exportEventsAsTsv(tmpTsvPath, true); |
| 42 | + |
| 43 | + // Re-import with exported TSV and check that chain tip matches. |
| 44 | + try { |
| 45 | + await importEventsFromTsv(`${tmpDir}/export.tsv`, 'archival', true, true); |
| 46 | + const newChainTip = await db.getUnanchoredChainTip(); |
| 47 | + expect(newChainTip.found).toBe(true); |
| 48 | + expect(newChainTip.result?.blockHeight).toBe(28); |
| 49 | + expect(newChainTip.result?.indexBlockHash).toBe( |
| 50 | + '0x76cd67a65c0dfd5ea450bb9efe30da89fa125bfc077c953802f718353283a533' |
| 51 | + ); |
| 52 | + expect(newChainTip.result?.blockHash).toBe( |
| 53 | + '0x7682af212d3c1ef62613412f9b5a727269b4548f14eca2e3f941f7ad8b3c11b2' |
| 54 | + ); |
| 55 | + } finally { |
| 56 | + fs.rmSync(tmpDir, { force: true, recursive: true }); |
| 57 | + } |
| 58 | + }); |
| 59 | + |
| 60 | + test('import with db wipe options', async () => { |
| 61 | + // Migrate first so we have some data. |
| 62 | + const clientConfig = getPgClientConfig({ usageName: 'cycle-migrations' }); |
| 63 | + await runMigrations(clientConfig, 'up', {}); |
| 64 | + await expect( |
| 65 | + importEventsFromTsv('src/tests-event-replay/tsv/mocknet.tsv', 'archival', false, false) |
| 66 | + ).rejects.toThrowError('contains existing data'); |
| 67 | + |
| 68 | + // Create strange table |
| 69 | + await db.sql`CREATE TABLE IF NOT EXISTS test (a varchar(10))`; |
| 70 | + await expect( |
| 71 | + importEventsFromTsv('src/tests-event-replay/tsv/mocknet.tsv', 'archival', true, false) |
| 72 | + ).rejects.toThrowError('migration cycle failed'); |
| 73 | + |
| 74 | + // Force and test |
| 75 | + await expect( |
| 76 | + importEventsFromTsv('src/tests-event-replay/tsv/mocknet.tsv', 'archival', true, true) |
| 77 | + ).resolves.not.toThrow(); |
| 78 | + }); |
| 79 | + |
| 80 | + test('db contains data', async () => { |
| 81 | + const clientConfig = getPgClientConfig({ usageName: 'cycle-migrations' }); |
| 82 | + await runMigrations(clientConfig, 'up', {}); |
| 83 | + |
| 84 | + // Having tables counts as having data as this may change across major versions. |
| 85 | + await expect(databaseHasData()).resolves.toBe(true); |
| 86 | + |
| 87 | + // Dropping all tables removes everything. |
| 88 | + await dangerousDropAllTables({ acknowledgePotentialCatastrophicConsequences: 'yes' }); |
| 89 | + await expect(databaseHasData()).resolves.toBe(false); |
| 90 | + |
| 91 | + // Cycling migrations leaves the `pgmigrations` table. |
| 92 | + await runMigrations(clientConfig, 'up', {}); |
| 93 | + await runMigrations(clientConfig, 'down', {}); |
| 94 | + await expect(databaseHasData()).resolves.toBe(true); |
| 95 | + await expect(databaseHasData({ ignoreMigrationTables: true })).resolves.toBe(false); |
| 96 | + }); |
| 97 | + |
| 98 | + afterEach(async () => { |
| 99 | + await db?.close(); |
| 100 | + }); |
| 101 | +}); |
0 commit comments