forked from graphile/migrate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to include external files in current.sql (graphile#195)
- Loading branch information
Showing
22 changed files
with
398 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`compiles an included file, and won't get stuck in an infinite include loop 1`] = ` | ||
"Circular include detected - '~/migrations/fixtures/foo.sql' is included again! Import statement: \`--!include foo.sql\`; trace: | ||
~/migrations/fixtures/foo.sql | ||
~/migrations/current.sql" | ||
`; | ||
|
||
exports[`disallows calling files outside of the migrations/fixtures folder 1`] = `"Forbidden: cannot include path '~/outsideFolder/foo.sql' because it's not inside '~/migrations/fixtures'"`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
import "./helpers"; | ||
|
||
import mockFs from "mock-fs"; | ||
|
||
import { compileIncludes } from "../src/migration"; | ||
import { ParsedSettings, parseSettings } from "../src/settings"; | ||
|
||
let old: string | undefined; | ||
let settings: ParsedSettings; | ||
beforeAll(async () => { | ||
old = process.env.DATABASE_AUTHENTICATOR; | ||
process.env.DATABASE_AUTHENTICATOR = "dbauth"; | ||
settings = await parseSettings({ | ||
connectionString: "postgres://dbowner:dbpassword@dbhost:1221/dbname", | ||
placeholders: { | ||
":DATABASE_AUTHENTICATOR": "!ENV", | ||
}, | ||
migrationsFolder: "migrations", | ||
}); | ||
}); | ||
afterAll(() => { | ||
process.env.DATABASE_AUTHENTICATOR = old; | ||
}); | ||
|
||
afterEach(() => { | ||
mockFs.restore(); | ||
}); | ||
|
||
/** Pretents that our compiled files are 'current.sql' */ | ||
const FAKE_VISITED = new Set([`${process.cwd()}/migrations/current.sql`]); | ||
|
||
it("compiles an included file", async () => { | ||
mockFs({ | ||
"migrations/fixtures/foo.sql": "select * from foo;", | ||
}); | ||
expect( | ||
await compileIncludes( | ||
settings, | ||
`\ | ||
--!include foo.sql | ||
`, | ||
FAKE_VISITED, | ||
), | ||
).toEqual(`\ | ||
select * from foo; | ||
`); | ||
}); | ||
|
||
it("compiles multiple included files", async () => { | ||
mockFs({ | ||
"migrations/fixtures/dir1/foo.sql": "select * from foo;", | ||
"migrations/fixtures/dir2/bar.sql": "select * from bar;", | ||
"migrations/fixtures/dir3/baz.sql": "--!include dir4/qux.sql", | ||
"migrations/fixtures/dir4/qux.sql": "select * from qux;", | ||
}); | ||
expect( | ||
await compileIncludes( | ||
settings, | ||
`\ | ||
--!include dir1/foo.sql | ||
--!include dir2/bar.sql | ||
--!include dir3/baz.sql | ||
`, | ||
FAKE_VISITED, | ||
), | ||
).toEqual(`\ | ||
select * from foo; | ||
select * from bar; | ||
select * from qux; | ||
`); | ||
}); | ||
|
||
it("compiles an included file, and won't get stuck in an infinite include loop", async () => { | ||
mockFs({ | ||
"migrations/fixtures/foo.sql": "select * from foo;\n--!include foo.sql", | ||
}); | ||
const promise = compileIncludes( | ||
settings, | ||
`\ | ||
--!include foo.sql | ||
`, | ||
FAKE_VISITED, | ||
); | ||
await expect(promise).rejects.toThrowError(/Circular include/); | ||
const message = await promise.catch((e) => e.message); | ||
expect(message.replaceAll(process.cwd(), "~")).toMatchSnapshot(); | ||
}); | ||
|
||
it("disallows calling files outside of the migrations/fixtures folder", async () => { | ||
mockFs({ | ||
"migrations/fixtures/bar.sql": "", | ||
"outsideFolder/foo.sql": "select * from foo;", | ||
}); | ||
|
||
const promise = compileIncludes( | ||
settings, | ||
`\ | ||
--!include ../../outsideFolder/foo.sql | ||
`, | ||
FAKE_VISITED, | ||
); | ||
await expect(promise).rejects.toThrowError(/Forbidden: cannot include/); | ||
const message = await promise.catch((e) => e.message); | ||
expect(message.replaceAll(process.cwd(), "~")).toMatchSnapshot(); | ||
}); | ||
|
||
it("compiles an included file that contains escapable things", async () => { | ||
mockFs({ | ||
"migrations/fixtures/foo.sql": `\ | ||
begin; | ||
create or replace function current_user_id() returns uuid as $$ | ||
select nullif(current_setting('user.id', true)::text, '')::uuid; | ||
$$ language sql stable; | ||
comment on function current_user_id is E'The ID of the current user.'; | ||
grant all on function current_user_id to :DATABASE_USER; | ||
commit; | ||
`, | ||
}); | ||
expect( | ||
await compileIncludes( | ||
settings, | ||
`\ | ||
--!include foo.sql | ||
`, | ||
FAKE_VISITED, | ||
), | ||
).toEqual(`\ | ||
begin; | ||
create or replace function current_user_id() returns uuid as $$ | ||
select nullif(current_setting('user.id', true)::text, '')::uuid; | ||
$$ language sql stable; | ||
comment on function current_user_id is E'The ID of the current user.'; | ||
grant all on function current_user_id to :DATABASE_USER; | ||
commit; | ||
`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.