Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,39 @@ drop policy if exists access_by_numbers on mytable;
create policy access_by_numbers on mytable for update using (myfunction(4, 2) < 42);
```

#### Automatically including changed fixtures (experimental)

Set `autoFixtures: true` in `.gmrc` to automatically include new and changed SQL
files under `migrations/fixtures`, including subdirectories. This works with
both `current.sql` and `current/`. Comment-only changes are ignored.

Automatic includes are kept in marked blocks:

```sql
-- graphile-migrate: generated autoFixtures;
--!include functions/myfunction.sql
-- graphile-migrate: end of autoFixtures;
```

Place blocks wherever the fixtures should run. Existing entries keep their
block; new entries are appended to the last block in migration order. Manual
includes outside the blocks take precedence. Keep custom SQL outside the
markers. If an automatic fixture includes a manually placed fixture, refresh
reports the conflict so you can place the outer fixture explicitly.

If there is no block, one is appended to `current.sql`. In directory mode, a new
file is placed last using your number width: `1-current.sql` gives
`9-auto-fixtures.sql`, and `000001-init.sql` gives `999999-auto-fixtures.sql`.
If that numeric prefix is already used, another digit is added. Empty blocks are
kept so their placement is preserved.

`watch` updates the blocks as fixtures change. You can also run
`graphile-migrate status --skipDatabase` to review includes before committing.
New entries use filename order; use nested includes or place entries in blocks
when execution order matters. Deleting a fixture removes its pending include;
write any necessary database cleanup SQL yourself. Fixtures should be
idempotent.

### Committed migration(s)

The files for migrations that you've committed with `graphile-migrate commit`
Expand Down
30 changes: 30 additions & 0 deletions __tests__/commit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,33 @@ describe.each([[undefined], ["My Commit Message"]])(
});
},
);

it.each([false, true])(
"preserves includes after a failed commit (automatic: %s)",
async (automatic) => {
const original = automatic ? "-- current\n" : "--!include fail.sql\n";
mockFs({
"migrations/current/1-current.sql": original,
"migrations/fixtures/fail.sql": "select * from missing_fixture_table;",
});
await expect(
commit({ ...settings, autoFixtures: automatic }),
).rejects.toThrow("does not exist");
const filename = automatic ? "9-auto-fixtures.sql" : "1-current.sql";
const restored = await fsp.readFile(
`migrations/current/${filename}`,
"utf8",
);
expect(restored).toContain("--!include fail.sql");
expect(restored).not.toContain("--! Included");
expect(await fsp.readFile("migrations/current/1-current.sql", "utf8")).toBe(
original,
);
expect(await fsp.readdir("migrations/committed")).toEqual([]);
await fsp.writeFile("migrations/fixtures/fail.sql", "select 1;");
await commit({ ...settings, autoFixtures: automatic });
expect(
await fsp.readFile("migrations/committed/000001.sql", "utf8"),
).toContain("select 1;");
},
);
Loading