|
| 1 | +import "./helpers"; |
| 2 | + |
| 3 | +import mockFs from "mock-fs"; |
| 4 | + |
| 5 | +import { compileIncludes } from "../src/migration"; |
| 6 | +import { ParsedSettings, parseSettings } from "../src/settings"; |
| 7 | + |
| 8 | +let old: string | undefined; |
| 9 | +let settings: ParsedSettings; |
| 10 | +beforeAll(async () => { |
| 11 | + old = process.env.DATABASE_AUTHENTICATOR; |
| 12 | + process.env.DATABASE_AUTHENTICATOR = "dbauth"; |
| 13 | + settings = await parseSettings({ |
| 14 | + connectionString: "postgres://dbowner:dbpassword@dbhost:1221/dbname", |
| 15 | + placeholders: { |
| 16 | + ":DATABASE_AUTHENTICATOR": "!ENV", |
| 17 | + }, |
| 18 | + migrationsFolder: "migrations", |
| 19 | + }); |
| 20 | +}); |
| 21 | +afterAll(() => { |
| 22 | + process.env.DATABASE_AUTHENTICATOR = old; |
| 23 | +}); |
| 24 | + |
| 25 | +afterEach(() => { |
| 26 | + mockFs.restore(); |
| 27 | +}); |
| 28 | + |
| 29 | +/** Pretents that our compiled files are 'current.sql' */ |
| 30 | +const FAKE_VISITED = new Set([`${process.cwd()}/migrations/current.sql`]); |
| 31 | + |
| 32 | +it("compiles an included file", async () => { |
| 33 | + mockFs({ |
| 34 | + "migrations/fixtures/foo.sql": "select * from foo;", |
| 35 | + }); |
| 36 | + expect( |
| 37 | + await compileIncludes( |
| 38 | + settings, |
| 39 | + `\ |
| 40 | +--!include foo.sql |
| 41 | +`, |
| 42 | + FAKE_VISITED, |
| 43 | + ), |
| 44 | + ).toEqual(`\ |
| 45 | +select * from foo; |
| 46 | +`); |
| 47 | +}); |
| 48 | + |
| 49 | +it("compiles multiple included files", async () => { |
| 50 | + mockFs({ |
| 51 | + "migrations/fixtures/dir1/foo.sql": "select * from foo;", |
| 52 | + "migrations/fixtures/dir2/bar.sql": "select * from bar;", |
| 53 | + "migrations/fixtures/dir3/baz.sql": "--!include dir4/qux.sql", |
| 54 | + "migrations/fixtures/dir4/qux.sql": "select * from qux;", |
| 55 | + }); |
| 56 | + expect( |
| 57 | + await compileIncludes( |
| 58 | + settings, |
| 59 | + `\ |
| 60 | +--!include dir1/foo.sql |
| 61 | +--!include dir2/bar.sql |
| 62 | +--!include dir3/baz.sql |
| 63 | +`, |
| 64 | + FAKE_VISITED, |
| 65 | + ), |
| 66 | + ).toEqual(`\ |
| 67 | +select * from foo; |
| 68 | +select * from bar; |
| 69 | +select * from qux; |
| 70 | +`); |
| 71 | +}); |
| 72 | + |
| 73 | +it("compiles an included file, and won't get stuck in an infinite include loop", async () => { |
| 74 | + mockFs({ |
| 75 | + "migrations/fixtures/foo.sql": "select * from foo;\n--!include foo.sql", |
| 76 | + }); |
| 77 | + const promise = compileIncludes( |
| 78 | + settings, |
| 79 | + `\ |
| 80 | +--!include foo.sql |
| 81 | +`, |
| 82 | + FAKE_VISITED, |
| 83 | + ); |
| 84 | + await expect(promise).rejects.toThrowError(/Circular include/); |
| 85 | + const message = await promise.catch((e) => e.message); |
| 86 | + expect(message.replaceAll(process.cwd(), "~")).toMatchSnapshot(); |
| 87 | +}); |
| 88 | + |
| 89 | +it("disallows calling files outside of the migrations/fixtures folder", async () => { |
| 90 | + mockFs({ |
| 91 | + "migrations/fixtures/bar.sql": "", |
| 92 | + "outsideFolder/foo.sql": "select * from foo;", |
| 93 | + }); |
| 94 | + |
| 95 | + const promise = compileIncludes( |
| 96 | + settings, |
| 97 | + `\ |
| 98 | +--!include ../../outsideFolder/foo.sql |
| 99 | +`, |
| 100 | + FAKE_VISITED, |
| 101 | + ); |
| 102 | + await expect(promise).rejects.toThrowError(/Forbidden: cannot include/); |
| 103 | + const message = await promise.catch((e) => e.message); |
| 104 | + expect(message.replaceAll(process.cwd(), "~")).toMatchSnapshot(); |
| 105 | +}); |
| 106 | + |
| 107 | +it("compiles an included file that contains escapable things", async () => { |
| 108 | + mockFs({ |
| 109 | + "migrations/fixtures/foo.sql": `\ |
| 110 | +begin; |
| 111 | +
|
| 112 | +create or replace function current_user_id() returns uuid as $$ |
| 113 | + select nullif(current_setting('user.id', true)::text, '')::uuid; |
| 114 | +$$ language sql stable; |
| 115 | +
|
| 116 | +comment on function current_user_id is E'The ID of the current user.'; |
| 117 | +
|
| 118 | +grant all on function current_user_id to :DATABASE_USER; |
| 119 | +
|
| 120 | +commit; |
| 121 | +`, |
| 122 | + }); |
| 123 | + expect( |
| 124 | + await compileIncludes( |
| 125 | + settings, |
| 126 | + `\ |
| 127 | +--!include foo.sql |
| 128 | +`, |
| 129 | + FAKE_VISITED, |
| 130 | + ), |
| 131 | + ).toEqual(`\ |
| 132 | +begin; |
| 133 | +
|
| 134 | +create or replace function current_user_id() returns uuid as $$ |
| 135 | + select nullif(current_setting('user.id', true)::text, '')::uuid; |
| 136 | +$$ language sql stable; |
| 137 | +
|
| 138 | +comment on function current_user_id is E'The ID of the current user.'; |
| 139 | +
|
| 140 | +grant all on function current_user_id to :DATABASE_USER; |
| 141 | +
|
| 142 | +commit; |
| 143 | +
|
| 144 | +`); |
| 145 | +}); |
0 commit comments