Skip to content

Commit 9496b11

Browse files
committed
feat(store): automatically create migration table if not exists
This simplifies the initial migration setup when not using any other 'store' tables.
1 parent 1bffb3d commit 9496b11

File tree

6 files changed

+33
-61
lines changed

6 files changed

+33
-61
lines changed

docs/references/store.md

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,6 @@ your first migration:
66
```sql
77
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
88

9-
CREATE TABLE migration
10-
(
11-
"namespace" varchar NOT NULL,
12-
"number" int,
13-
"name" varchar NOT NULL,
14-
"createdAt" timestamptz DEFAULT now(),
15-
"hash" varchar
16-
);
17-
18-
CREATE INDEX migration_namespace_number_idx ON "migration" ("namespace", "number");
19-
209
CREATE TABLE "file"
2110
(
2211
"id" uuid PRIMARY KEY NOT NULL DEFAULT uuid_generate_v4(),

examples/crud-simple-todo/migrations/001-todo.sql

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
22

3-
CREATE TABLE migration
4-
(
5-
"namespace" varchar NOT NULL,
6-
"number" int,
7-
"name" varchar NOT NULL,
8-
"createdAt" timestamptz DEFAULT now(),
9-
"hash" varchar
10-
);
11-
12-
CREATE INDEX migration_namespace_number_idx ON "migration" ("namespace", "number");
13-
143
CREATE TABLE "todo"
154
(
165
"id" uuid PRIMARY KEY NOT NULL DEFAULT uuid_generate_v4(),

examples/crud/migrations/001-todo.sql

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
22

3-
CREATE TABLE migration
4-
(
5-
"namespace" varchar NOT NULL,
6-
"number" int,
7-
"name" varchar NOT NULL,
8-
"createdAt" timestamptz DEFAULT now(),
9-
"hash" varchar
10-
);
11-
12-
CREATE INDEX migration_namespace_number_idx ON "migration" ("namespace", "number");
13-
143
CREATE TABLE "todo"
154
(
165
"id" uuid PRIMARY KEY NOT NULL DEFAULT uuid_generate_v4(),

examples/default/migrations/001-compas-init.sql

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
22

3-
CREATE TABLE migration
4-
(
5-
"namespace" varchar NOT NULL,
6-
"number" int,
7-
"name" varchar NOT NULL,
8-
"createdAt" timestamptz DEFAULT now(),
9-
"hash" varchar
10-
);
11-
12-
CREATE INDEX migration_namespace_number_idx ON "migration" ("namespace", "number");
13-
143
CREATE TABLE "file"
154
(
165
"id" uuid PRIMARY KEY NOT NULL DEFAULT uuid_generate_v4(),

examples/with-auth/migrations/001-compas-init.sql

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
22

3-
CREATE TABLE migration
4-
(
5-
"namespace" varchar NOT NULL,
6-
"number" int,
7-
"name" varchar NOT NULL,
8-
"createdAt" timestamptz DEFAULT now(),
9-
"hash" varchar
10-
);
11-
12-
CREATE INDEX migration_namespace_number_idx ON "migration" ("namespace", "number");
13-
143
CREATE TABLE "file"
154
(
165
"id" uuid PRIMARY KEY NOT NULL DEFAULT uuid_generate_v4(),

packages/store/src/migrations.js

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,28 @@ export async function runMigrations(mc) {
140140
try {
141141
const migrationFiles = filterMigrationsToBeApplied(mc);
142142

143+
if (mc.missingMigrationTable && migrationFiles.length > 0) {
144+
mc.missingMigrationTable = false;
145+
146+
if (
147+
!migrationFiles[0]?.source.includes(`migration_namespace_number_idx`)
148+
) {
149+
// Automatically create the migration table
150+
await mc.sql.unsafe(`
151+
CREATE TABLE IF NOT EXISTS migration
152+
(
153+
"namespace" varchar NOT NULL,
154+
"number" int,
155+
"name" varchar NOT NULL,
156+
"createdAt" timestamptz DEFAULT now(),
157+
"hash" varchar
158+
);
159+
160+
CREATE INDEX IF NOT EXISTS migration_namespace_number_idx ON "migration" ("namespace", "number");
161+
`);
162+
}
163+
}
164+
143165
for (const migration of migrationFiles) {
144166
current = migration;
145167
await runMigration(mc.sql, migration);
@@ -175,7 +197,10 @@ export async function runMigrations(mc) {
175197
export async function rebuildMigrations(mc) {
176198
try {
177199
await mc.sql.begin(async (sql) => {
178-
await sql`DELETE FROM "migration" WHERE 1 = 1`;
200+
await sql`DELETE
201+
FROM "migration"
202+
WHERE
203+
1 = 1`;
179204

180205
for (const file of mc.files) {
181206
await runInsert(sql, file);
@@ -279,12 +304,14 @@ async function syncWithSchemaState(mc) {
279304
let rows = [];
280305
try {
281306
rows = await mc.sql`
282-
SELECT DISTINCT ON (number) number, hash
283-
FROM migration
284-
ORDER BY number, "createdAt" DESC
285-
`;
307+
SELECT DISTINCT ON (number) number, hash
308+
FROM migration
309+
ORDER BY number, "createdAt" DESC
310+
`;
286311
} catch (/** @type {any} */ e) {
287-
if ((e.message ?? "").indexOf(`"migration" does not exist`) === -1) {
312+
if ((e.message ?? "").includes(`"migration" does not exist`)) {
313+
mc.missingMigrationTable = true;
314+
} else {
288315
throw new AppError(
289316
"store.migrateSync.error",
290317
500,

0 commit comments

Comments
 (0)