-
-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathreadCurrentMigration.test.ts
More file actions
118 lines (97 loc) · 3.1 KB
/
readCurrentMigration.test.ts
File metadata and controls
118 lines (97 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import "./helpers"; // Has side-effects; must come first
import mockFs from "mock-fs";
import {
getCurrentMigrationLocation,
readCurrentMigration,
} from "../src/current";
import { ParsedSettings, parseSettings } from "../src/settings";
import { TEST_DATABASE_URL } from "./helpers";
let parsedSettings: ParsedSettings;
beforeEach(async () => {
mockFs({ migrations: mockFs.directory() });
parsedSettings = await parseSettings({
connectionString: TEST_DATABASE_URL,
});
});
afterEach(() => {
mockFs.restore();
});
it("reads from current.sql", async () => {
mockFs({
"migrations/current.sql": "-- TEST",
});
const currentLocation = await getCurrentMigrationLocation(parsedSettings);
const content = await readCurrentMigration(parsedSettings, currentLocation);
expect(content).toEqual("-- TEST");
});
it("returns empty if there's no current.sql", async () => {
const currentLocation = await getCurrentMigrationLocation(parsedSettings);
const content = await readCurrentMigration(parsedSettings, currentLocation);
expect(content).toEqual("");
});
it("returns empty if there's an empty current/", async () => {
mockFs({
"migrations/current": mockFs.directory(),
});
const currentLocation = await getCurrentMigrationLocation(parsedSettings);
const content = await readCurrentMigration(parsedSettings, currentLocation);
expect(content).toEqual("");
});
const contentWithSplits = `\
--! split: 100-first.sql
First content
--! split: 200-second.sql
Some more content
With multiple lines
-- and comments
--! split: 300-third.sql
--! split: 400-fourth.sql
Note: 300 was empty
`;
it("reads multiple files", async () => {
mockFs({
"migrations/current": {
"100-first.sql": "First content\n",
"200-second.sql": `\
Some more content
With multiple lines
-- and comments
`,
"300-third.sql": "",
"400-fourth.sql": "Note: 300 was empty",
},
});
const currentLocation = await getCurrentMigrationLocation(parsedSettings);
const content = await readCurrentMigration(parsedSettings, currentLocation);
expect(content).toEqual(contentWithSplits);
});
it("ignores extraneous files", async () => {
mockFs({
"migrations/current": {
"README.md": "Blah blah\nEtc etc\nFoo bar baz",
"100-first.sql": "First content\n",
"200-second.sql": `\
Some more content
With multiple lines
-- and comments
`,
"300-third.sql": "",
"400-fourth.sql": "Note: 300 was empty",
},
});
const currentLocation = await getCurrentMigrationLocation(parsedSettings);
const content = await readCurrentMigration(parsedSettings, currentLocation);
expect(content).toEqual(contentWithSplits);
});
it("reads from current.sql, and processes included files", async () => {
mockFs({
"migrations/current.sql": "--!include foo_current.sql",
"migrations/fixtures/foo_current.sql": "-- TEST from foo",
});
const currentLocation = await getCurrentMigrationLocation(parsedSettings);
const content = await readCurrentMigration(parsedSettings, currentLocation);
expect(content).toEqual(`\
--! Included foo_current.sql
-- TEST from foo
--! EndIncluded foo_current.sql`);
});