|
| 1 | +import "./helpers"; // Has side-effects; must come first |
| 2 | + |
| 3 | +import mockFs from "mock-fs"; |
| 4 | + |
| 5 | +import { current } from "../src"; |
| 6 | +import { withClient } from "../src/pg"; |
| 7 | +import { ParsedSettings, parseSettings } from "../src/settings"; |
| 8 | +import { makeMigrations, resetDb, settings } from "./helpers"; |
| 9 | + |
| 10 | +beforeEach(resetDb); |
| 11 | +beforeEach(async () => { |
| 12 | + mockFs({ migrations: mockFs.directory() }); |
| 13 | +}); |
| 14 | +afterEach(() => { |
| 15 | + mockFs.restore(); |
| 16 | +}); |
| 17 | +const { |
| 18 | + MIGRATION_1_COMMITTED, |
| 19 | + MIGRATION_ENUM_COMMITTED, |
| 20 | + MIGRATION_NOTRX_TEXT, |
| 21 | + MIGRATION_NOTRX_COMMITTED, |
| 22 | +} = makeMigrations(); |
| 23 | + |
| 24 | +function getStuff(parsedSettings: ParsedSettings) { |
| 25 | + return withClient( |
| 26 | + parsedSettings.connectionString, |
| 27 | + parsedSettings, |
| 28 | + async (pgClient, _context) => { |
| 29 | + const { rows: migrations } = await pgClient.query( |
| 30 | + "select * from graphile_migrate.migrations", |
| 31 | + ); |
| 32 | + const { rows: tables } = await pgClient.query( |
| 33 | + "select * from pg_class where relnamespace = 'public'::regnamespace and relkind = 'r'", |
| 34 | + ); |
| 35 | + const { rows: enums } = await pgClient.query( |
| 36 | + "select typname, (select count(*) from pg_enum where enumtypid = pg_type.oid) as value_count from pg_type where typnamespace = 'public'::regnamespace and typtype = 'e'", |
| 37 | + ); |
| 38 | + return { migrations, tables, enums }; |
| 39 | + }, |
| 40 | + ); |
| 41 | +} |
| 42 | + |
| 43 | +it("runs migrations", async () => { |
| 44 | + mockFs({ |
| 45 | + "migrations/current.sql": "", |
| 46 | + }); |
| 47 | + |
| 48 | + await current(settings); |
| 49 | + const parsedSettings = await parseSettings(settings); |
| 50 | + |
| 51 | + { |
| 52 | + const { migrations, tables, enums } = await getStuff(parsedSettings); |
| 53 | + expect(migrations).toHaveLength(0); |
| 54 | + expect(tables).toHaveLength(0); |
| 55 | + expect(enums).toHaveLength(0); |
| 56 | + } |
| 57 | + |
| 58 | + mockFs({ |
| 59 | + [`migrations/committed/000001.sql`]: MIGRATION_1_COMMITTED, |
| 60 | + [`migrations/committed/000002.sql`]: MIGRATION_ENUM_COMMITTED, // Creates enum with 1 value |
| 61 | + "migrations/current.sql": MIGRATION_NOTRX_TEXT, // Adds a value to the enum - total = 2 |
| 62 | + }); |
| 63 | + |
| 64 | + await current(settings); |
| 65 | + |
| 66 | + const { migrations, tables, enums } = await getStuff(parsedSettings); |
| 67 | + |
| 68 | + expect(migrations).toHaveLength(2); |
| 69 | + expect(migrations.map(({ date, ...rest }) => rest)).toMatchInlineSnapshot(` |
| 70 | + [ |
| 71 | + { |
| 72 | + "filename": "000001.sql", |
| 73 | + "hash": "sha1:e00ec93314a423ee5cc68d1182ad52f16442d7df", |
| 74 | + "previous_hash": null, |
| 75 | + }, |
| 76 | + { |
| 77 | + "filename": "000002.sql", |
| 78 | + "hash": "sha1:bddc1ead3310dc1c42cdc7f63537ebdff2e9fd7b", |
| 79 | + "previous_hash": "sha1:e00ec93314a423ee5cc68d1182ad52f16442d7df", |
| 80 | + }, |
| 81 | + ] |
| 82 | + `); |
| 83 | + expect(tables).toHaveLength(1); |
| 84 | + expect(tables.map((t) => t.relname)).toMatchInlineSnapshot(` |
| 85 | + [ |
| 86 | + "foo", |
| 87 | + ] |
| 88 | + `); |
| 89 | + expect(enums).toHaveLength(1); |
| 90 | + expect(enums).toMatchInlineSnapshot(` |
| 91 | + [ |
| 92 | + { |
| 93 | + "typname": "user_role", |
| 94 | + "value_count": "2", |
| 95 | + }, |
| 96 | + ] |
| 97 | + `); |
| 98 | + |
| 99 | + mockFs({ |
| 100 | + [`migrations/committed/000001.sql`]: MIGRATION_1_COMMITTED, |
| 101 | + [`migrations/committed/000002.sql`]: MIGRATION_ENUM_COMMITTED, |
| 102 | + [`migrations/committed/000003.sql`]: MIGRATION_NOTRX_COMMITTED, |
| 103 | + "migrations/current.sql": "", |
| 104 | + }); |
| 105 | + |
| 106 | + await current(settings); |
| 107 | + |
| 108 | + const { |
| 109 | + migrations: newMigrations, |
| 110 | + tables: newTables, |
| 111 | + enums: newEnums, |
| 112 | + } = await getStuff(parsedSettings); |
| 113 | + |
| 114 | + expect(newMigrations).toHaveLength(3); |
| 115 | + expect(newMigrations.map(({ date, ...rest }) => rest)).toMatchInlineSnapshot(` |
| 116 | + [ |
| 117 | + { |
| 118 | + "filename": "000001.sql", |
| 119 | + "hash": "sha1:e00ec93314a423ee5cc68d1182ad52f16442d7df", |
| 120 | + "previous_hash": null, |
| 121 | + }, |
| 122 | + { |
| 123 | + "filename": "000002.sql", |
| 124 | + "hash": "sha1:bddc1ead3310dc1c42cdc7f63537ebdff2e9fd7b", |
| 125 | + "previous_hash": "sha1:e00ec93314a423ee5cc68d1182ad52f16442d7df", |
| 126 | + }, |
| 127 | + { |
| 128 | + "filename": "000003.sql", |
| 129 | + "hash": "sha1:2d248344ac299ebbad2aeba5bfec2ae3c3cb0a4f", |
| 130 | + "previous_hash": "sha1:bddc1ead3310dc1c42cdc7f63537ebdff2e9fd7b", |
| 131 | + }, |
| 132 | + ] |
| 133 | + `); |
| 134 | + expect(newTables).toEqual(tables); |
| 135 | + expect(newEnums).toEqual(enums); |
| 136 | +}); |
0 commit comments