Skip to content

Commit 72b56e4

Browse files
authored
fix(x2a): make migration scripts self-contained (#3248)
* fix(x2a): make migration scripts self-contained Migration scripts must be self-contained, refactoring project-naming logic to work in dynamic plugins but still being reused by the rest of the application. Signed-off-by: Marek Libra <marek.libra@gmail.com> * avoid code reuse between migrations and runtime --------- Signed-off-by: Marek Libra <marek.libra@gmail.com>
1 parent 9cc09db commit 72b56e4

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@red-hat-developer-hub/backstage-plugin-x2a-backend': patch
3+
---
4+
5+
Migration scripts must be self-contained, refactoring project-naming logic to work in dynamic plugins.

workspaces/x2a/plugins/x2a-backend/migrations/2026051200_add_project_dir_name.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,34 @@
1515
*/
1616

1717
import type { Knex } from 'knex';
18-
import { Project } from '../src/services/Project';
18+
19+
const MAX_BASE_NAME_LENGTH = 64;
20+
const SHORT_ID_LENGTH = 6;
21+
const DEFAULT_BASE_NAME = 'project';
22+
23+
function computeDirName(id: string, name: string): string {
24+
let sanitized = name
25+
.toLowerCase()
26+
.replace(/[^a-z0-9-]/g, '-')
27+
.replace(/-{2,}/g, '-');
28+
29+
let start = 0;
30+
while (start < sanitized.length && sanitized[start] === '-') start++;
31+
sanitized = sanitized.substring(start);
32+
33+
let end = sanitized.length;
34+
while (end > 0 && sanitized[end - 1] === '-') end--;
35+
sanitized = sanitized.substring(0, end);
36+
37+
sanitized = sanitized.substring(0, MAX_BASE_NAME_LENGTH);
38+
while (sanitized.length > 0 && sanitized[sanitized.length - 1] === '-') {
39+
sanitized = sanitized.substring(0, sanitized.length - 1);
40+
}
41+
42+
const baseName = sanitized || DEFAULT_BASE_NAME;
43+
const shortId = id.substring(0, SHORT_ID_LENGTH);
44+
return `${baseName}-${shortId}`;
45+
}
1946

2047
/**
2148
* Adds the dir_name column and renames created_by to owned_by in the projects table.
@@ -36,7 +63,7 @@ export async function up(knex: Knex): Promise<void> {
3663
for (const row of rows) {
3764
await knex('projects')
3865
.where('id', row.id)
39-
.update({ dir_name: new Project(row.id, row.name).dirName });
66+
.update({ dir_name: computeDirName(row.id, row.name) });
4067
}
4168

4269
// SQLite does not support ALTER COLUMN to set NOT NULL after the fact.

0 commit comments

Comments
 (0)