Skip to content

Commit 9742f87

Browse files
committed
fix(registry server): change upsert constaint from route,namespace to orderPos,namespace
1 parent d9f8a52 commit 9742f87

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

registry/server/appRoutes/routes/RoutesService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export class RoutesService {
5555
await this.db.versioning(user, { type: EntityTypes.routes, trxProvider }, async (trx) => {
5656
const result = await this.db(Tables.Routes)
5757
.insert(prepareAppRouteToSave(appRoute), 'id')
58-
.onConflict(this.db.raw('(route, namespace) WHERE namespace IS NOT NULL'))
58+
.onConflict(this.db.raw('("orderPos", namespace) WHERE namespace IS NOT NULL'))
5959
.merge()
6060
.transacting(trx);
6161
const savedAppRouteId = extractInsertedId(result as { id: number }[]);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import type { Knex } from 'knex';
2+
import { isMySQL } from '../util/db';
3+
4+
export async function up(knex: Knex): Promise<void> {
5+
await knex.schema.alterTable('routes', (table) => {
6+
table.dropIndex(['route', 'namespace'], 'route_unique');
7+
});
8+
if (!isMySQL(knex)) {
9+
// TODO remove condition WHERE namespace IS NOT NULL when everything migrated
10+
await knex.raw('CREATE UNIQUE INDEX route_unique ON routes("orderPos", namespace) WHERE namespace IS NOT NULL');
11+
}
12+
}
13+
14+
export async function down(knex: Knex): Promise<void> {
15+
await knex.schema.alterTable('routes', (table) => {
16+
table.dropIndex(['orderPos', 'namespace'], 'route_unique');
17+
});
18+
// MySQL does not support partial index
19+
if (!isMySQL(knex)) {
20+
// TODO remove condition WHERE namespace IS NOT NULL when everything migrated
21+
await knex.raw('CREATE UNIQUE INDEX route_unique ON routes(route, namespace) WHERE namespace IS NOT NULL');
22+
}
23+
}

registry/tests/services/RoutesRepository.spec.ts renamed to registry/tests/services/RoutesService.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@ describe('RoutesService', () => {
7676
});
7777
const service = new RoutesService(db);
7878
const trxProvider = db.transactionProvider();
79-
await service.upsert({ ...appRoute, route: '/upsert2', namespace: 'ns1', orderPos: 22 }, user, trxProvider);
79+
await service.upsert({ ...appRoute, route: '/upsert22', namespace: 'ns1', orderPos: 2 }, user, trxProvider);
8080
const trx = await trxProvider();
8181
await trx.commit();
82-
const route = await db(Tables.Routes).first().where({ route: '/upsert2' });
82+
const route = await db(Tables.Routes).first().where({ orderPos: 2 });
8383
expect(route).to.deep.include({
84-
orderPos: 22,
85-
route: '/upsert2',
84+
orderPos: 2,
85+
route: '/upsert22',
8686
next: 0,
8787
meta: '{"a":1}',
8888
domainId: null,
@@ -126,9 +126,9 @@ describe('RoutesService', () => {
126126
await service.upsert(
127127
{
128128
...appRoute,
129-
route: '/upsert3',
129+
route: '/upsert33',
130130
namespace: 'ns1',
131-
orderPos: 33,
131+
orderPos: 3,
132132
slots: {
133133
upsert3: {
134134
appName: '@portal/upsert',

0 commit comments

Comments
 (0)