Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

fix(db): keep plugin index names when moving plugin data to Postgres

Maurice9c454a04e1bf
diff --git a/server/src/db/engine/postgres/sqlite-move/plugin-data.ts b/server/src/db/engine/postgres/sqlite-move/plugin-data.ts
index a262371ba..e4bff412a 100644
--- a/server/src/db/engine/postgres/sqlite-move/plugin-data.ts
+++ b/server/src/db/engine/postgres/sqlite-move/plugin-data.ts
@@ -151,7 +151,9 @@ function indexDdl(index: SqliteIndex, target: string, notes: string[], table: st
return null;
}
const columns = index.columns.map(quoteIdentifier).join(', ');
- return `create ${index.unique ? 'unique ' : ''}index on ${target} (${columns})`;
+ // The plugin's own name, for a later migration to find; a UNIQUE constraint's index has none of its own.
+ const name = index.origin === 'c' ? `${quoteIdentifier(index.name)} ` : '';
+ return `create ${index.unique ? 'unique ' : ''}index ${name}on ${target} (${columns})`;
}
/** Plans the copy of one plugin's `plugin.db`: what to create in its schema and what to fill. */
diff --git a/server/tests/unit/db/engine/postgres/sqlite-move/move.test.ts b/server/tests/unit/db/engine/postgres/sqlite-move/move.test.ts
index 3af48c8fe..70d6a9e80 100644
--- a/server/tests/unit/db/engine/postgres/sqlite-move/move.test.ts
+++ b/server/tests/unit/db/engine/postgres/sqlite-move/move.test.ts
@@ -332,7 +332,7 @@ describe('moveSqliteToPostgres', () => {
'"body" text not null default \'x\', "score" double precision, "raw" bytea, "added" text default ' +
'to_char((CURRENT_TIMESTAMP AT TIME ZONE \'UTC\'), \'YYYY-MM-DD HH24:MI:SS\'), "odd" text, primary key ("id"))',
);
- expect(statements).toContain('create unique index on "trek_plugin_move_notes"."notes" ("body")');
+ expect(statements).toContain('create unique index "notes_body" on "trek_plugin_move_notes"."notes" ("body")');
expect(pg.rowsOf('"trek_plugin_move_notes"."_plugin_migrations"')).toEqual([['001', '1700000000000']]);
expect(pg.sequences.get('"trek_plugin_move_notes"."notes"_seq')).toEqual({ value: '3', called: true });
expect(renderSummary(summary, 'postgres://db/trek')).toContain(
@@ -833,10 +833,10 @@ describe('the pieces', () => {
fs.mkdirSync(pluginDir, { recursive: true });
sqlite(
path.join(pluginDir, 'plugin.db'),
- `create table tags (name text primary key collate nocase, label text, slug text collate rtrim, note text);
+ `create table tags (name text primary key collate nocase, label text, slug text collate rtrim, note text, ref text unique);
create unique index tags_label on tags (label collate nocase, slug);
create index tags_note on tags (note);
- insert into tags values ('Alpha', 'a', 's', 'n');`,
+ insert into tags values ('Alpha', 'a', 's', 'n', 'r');`,
);
const summary = await moveSqliteToPostgres(options());
expect(summary.plugins[0].notCarried).toEqual([
@@ -844,8 +844,12 @@ describe('the pieces', () => {
'the collation NOCASE of the primary key on tags (Postgres compares its values exactly)',
]);
const statements = pg.statements.join('\n');
- expect(statements).toContain('create unique index on "trek_plugin_move_tags"."tags" ("label", "slug")');
- expect(statements).toContain('create index on "trek_plugin_move_tags"."tags" ("note")');
+ expect(statements).toContain(
+ 'create unique index "tags_label" on "trek_plugin_move_tags"."tags" ("label", "slug")',
+ );
+ expect(statements).toContain('create index "tags_note" on "trek_plugin_move_tags"."tags" ("note")');
+ // A UNIQUE constraint's index has a name SQLite made up, so Postgres names it.
+ expect(statements).toContain('create unique index on "trek_plugin_move_tags"."tags" ("ref")');
});
});