@yunsoft/yuncms-core 0.1.2 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yunsoft/yuncms-core",
3
- "version": "0.1.2",
3
+ "version": "0.1.5",
4
4
  "description": "Core services, schema, authentication, permissions and storage primitives for YunCMS.",
5
5
  "license": "MIT",
6
6
  "type": "module",
package/src/bootstrap.js CHANGED
@@ -7,6 +7,11 @@ import { authActionTokensMigration } from './migrations/0004-auth-action-tokens.
7
7
  import { defaultPublicRoleMigration } from './migrations/0005-default-public-role.js';
8
8
  import { studioSettingsMigration } from './migrations/0006-studio-settings.js';
9
9
  import { systemPermissionResourcesMigration } from './migrations/0007-system-permission-resources.js';
10
+ import { studioLogoFileMigration } from './migrations/0008-studio-logo-file.js';
11
+ import { schemaDisplayNamesMigration } from './migrations/0009-schema-display-names.js';
12
+ import { studioFaviconFileMigration } from './migrations/0010-studio-favicon-file.js';
13
+ import { rolePermissionActionsMigration } from './migrations/0011-role-permission-actions.js';
14
+ import { filesReadFiltersMigration } from './migrations/0012-files-read-filters.js';
10
15
  import { readSchemaVersion } from './schema-version.js';
11
16
  import { ensurePublicRole } from './setup.js';
12
17
 
@@ -18,6 +23,11 @@ export const CORE_MIGRATIONS = Object.freeze([
18
23
  defaultPublicRoleMigration,
19
24
  studioSettingsMigration,
20
25
  systemPermissionResourcesMigration,
26
+ studioLogoFileMigration,
27
+ schemaDisplayNamesMigration,
28
+ studioFaviconFileMigration,
29
+ rolePermissionActionsMigration,
30
+ filesReadFiltersMigration,
21
31
  ]);
22
32
 
23
33
  export const REQUIRED_CORE_MIGRATION_IDS = Object.freeze(
package/src/cache.js ADDED
@@ -0,0 +1,85 @@
1
+ function cacheError(message) {
2
+ const error = new Error(message);
3
+ error.code = 'INVALID_CACHE_CONFIG';
4
+ return error;
5
+ }
6
+
7
+ export function isCacheStore(store) {
8
+ return Boolean(
9
+ store
10
+ && typeof store.get === 'function'
11
+ && typeof store.set === 'function'
12
+ && typeof store.delete === 'function'
13
+ && typeof store.clear === 'function',
14
+ );
15
+ }
16
+
17
+ export class MemoryCacheStore {
18
+ constructor({
19
+ ttlMs = 30_000,
20
+ maxEntries = 5_000,
21
+ now = () => Date.now(),
22
+ } = {}) {
23
+ if (!Number.isInteger(ttlMs) || ttlMs < 1) throw cacheError('Cache ttlMs must be a positive integer');
24
+ if (!Number.isInteger(maxEntries) || maxEntries < 1) {
25
+ throw cacheError('Cache maxEntries must be a positive integer');
26
+ }
27
+ if (typeof now !== 'function') throw cacheError('Cache now must be a function');
28
+
29
+ this.ttlMs = ttlMs;
30
+ this.maxEntries = maxEntries;
31
+ this.now = now;
32
+ this.entries = new Map();
33
+ }
34
+
35
+ #deleteExpired(timestamp) {
36
+ for (const [key, entry] of this.entries) {
37
+ if (entry.expiresAt <= timestamp) this.entries.delete(key);
38
+ }
39
+ }
40
+
41
+ #ensureCapacity(timestamp, incomingKey) {
42
+ this.#deleteExpired(timestamp);
43
+ if (this.entries.has(incomingKey)) return;
44
+ while (this.entries.size >= this.maxEntries) {
45
+ const oldestKey = this.entries.keys().next().value;
46
+ if (oldestKey === undefined) break;
47
+ this.entries.delete(oldestKey);
48
+ }
49
+ }
50
+
51
+ async get(key) {
52
+ const entry = this.entries.get(String(key));
53
+ if (!entry) return undefined;
54
+ if (entry.expiresAt <= this.now()) {
55
+ this.entries.delete(String(key));
56
+ return undefined;
57
+ }
58
+ return entry.value;
59
+ }
60
+
61
+ async set(key, value, { ttlMs = this.ttlMs } = {}) {
62
+ if (!Number.isInteger(ttlMs) || ttlMs < 1) throw cacheError('Cache entry ttlMs must be a positive integer');
63
+ const normalizedKey = String(key);
64
+ const timestamp = this.now();
65
+ this.#ensureCapacity(timestamp, normalizedKey);
66
+ this.entries.delete(normalizedKey);
67
+ this.entries.set(normalizedKey, {
68
+ value,
69
+ expiresAt: timestamp + ttlMs,
70
+ });
71
+ return value;
72
+ }
73
+
74
+ async delete(key) {
75
+ return this.entries.delete(String(key));
76
+ }
77
+
78
+ async clear() {
79
+ this.entries.clear();
80
+ }
81
+
82
+ get size() {
83
+ return this.entries.size;
84
+ }
85
+ }
package/src/config.js CHANGED
@@ -24,6 +24,14 @@ function readString(value, fallback = '') {
24
24
  return value === undefined ? fallback : String(value);
25
25
  }
26
26
 
27
+ function readCacheStore(value) {
28
+ const store = readString(value, 'memory').trim().toLowerCase();
29
+ if (store !== 'memory') {
30
+ throw new Error(`CACHE_STORE must be memory until a shared-store adapter is configured, received: ${store}`);
31
+ }
32
+ return store;
33
+ }
34
+
27
35
  export function loadEnvFileIfPresent(path = '.env') {
28
36
  try {
29
37
  loadEnvFile(path);
@@ -44,10 +52,52 @@ export function loadConfig(env = process.env) {
44
52
  port: serverPort,
45
53
  studioOrigin,
46
54
  trustProxyHops: readInteger(env.TRUST_PROXY_HOPS, 0, 'TRUST_PROXY_HOPS', { min: 0, max: 10 }),
55
+ rateLimit: {
56
+ enabled: readBoolean(env.API_RATE_LIMIT_ENABLED, true),
57
+ windowMs: readInteger(env.API_RATE_LIMIT_WINDOW_MS, 60_000, 'API_RATE_LIMIT_WINDOW_MS', {
58
+ min: 1000,
59
+ max: 24 * 60 * 60 * 1000,
60
+ }),
61
+ max: readInteger(env.API_RATE_LIMIT_MAX, 300, 'API_RATE_LIMIT_MAX', {
62
+ min: 1,
63
+ max: 1_000_000,
64
+ }),
65
+ maxBuckets: readInteger(env.API_RATE_LIMIT_MAX_BUCKETS, 10_000, 'API_RATE_LIMIT_MAX_BUCKETS', {
66
+ min: 1,
67
+ max: 1_000_000,
68
+ }),
69
+ },
70
+ pressure: {
71
+ enabled: readBoolean(env.PRESSURE_LIMIT_ENABLED, true),
72
+ maxConcurrent: readInteger(env.PRESSURE_MAX_CONCURRENT, 250, 'PRESSURE_MAX_CONCURRENT', {
73
+ min: 1,
74
+ max: 100_000,
75
+ }),
76
+ maxHeapPercent: readInteger(env.PRESSURE_MAX_HEAP_PERCENT, 95, 'PRESSURE_MAX_HEAP_PERCENT', {
77
+ min: 1,
78
+ max: 100,
79
+ }),
80
+ retryAfterSeconds: readInteger(env.PRESSURE_RETRY_AFTER_SECONDS, 1, 'PRESSURE_RETRY_AFTER_SECONDS', {
81
+ min: 1,
82
+ max: 3600,
83
+ }),
84
+ },
47
85
  },
48
86
  logging: {
49
87
  level: readString(env.LOG_LEVEL, 'info'),
50
88
  },
89
+ cache: {
90
+ enabled: readBoolean(env.CACHE_ENABLED, true),
91
+ store: readCacheStore(env.CACHE_STORE),
92
+ ttlMs: readInteger(env.CACHE_TTL_MS, 30_000, 'CACHE_TTL_MS', {
93
+ min: 1,
94
+ max: 24 * 60 * 60 * 1000,
95
+ }),
96
+ maxEntries: readInteger(env.CACHE_MAX_ENTRIES, 5_000, 'CACHE_MAX_ENTRIES', {
97
+ min: 1,
98
+ max: 1_000_000,
99
+ }),
100
+ },
51
101
  database: {
52
102
  host: readString(env.DB_HOST, '127.0.0.1'),
53
103
  port: readInteger(env.DB_PORT, 3306, 'DB_PORT', { min: 1, max: 65535 }),
package/src/context.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { requireAccountability } from './accountability.js';
2
+ import { isCacheStore } from './cache.js';
2
3
 
3
4
  export function createRequestContext({
4
5
  accountability,
@@ -9,14 +10,16 @@ export function createRequestContext({
9
10
  env = {},
10
11
  emitter = null,
11
12
  storage = null,
12
- permissionCache = new Map(),
13
+ permissionCache = null,
13
14
  requestId = null,
14
15
  } = {}) {
15
16
  requireAccountability(accountability);
16
17
 
17
18
  if (!services) throw new Error('Service registry is required');
18
19
  if (!database) throw new Error('Database handle is required');
19
- if (!(permissionCache instanceof Map)) throw new Error('Permission cache must be a Map');
20
+ if (permissionCache !== null && !isCacheStore(permissionCache)) {
21
+ throw new Error('Permission cache must implement the cache-store contract');
22
+ }
20
23
 
21
24
  return Object.freeze({
22
25
  accountability,
package/src/index.js CHANGED
@@ -1,9 +1,17 @@
1
1
  export { DEFAULT_SERVER_PORT, loadConfig, loadEnvFileIfPresent } from './config.js';
2
+ export { isCacheStore, MemoryCacheStore } from './cache.js';
2
3
  export { createDatabasePool, pingDatabase, closeDatabasePool } from './database.js';
3
4
  export { withTransaction, withConnectionTransaction } from './transaction.js';
4
5
  export { assertIdentifier, quoteIdentifier } from './identifier.js';
6
+ export { normalizeDisplayName, normalizeSchemaKey, resolveSchemaName } from './schema-key.js';
5
7
  export { YunCmsDatabaseError, normalizeDatabaseError, isRetryableDatabaseError } from './errors.js';
6
8
  export { withDatabaseRetry } from './retry.js';
9
+ export {
10
+ MAINTENANCE_BYPASS_ENV,
11
+ maintenanceLockPath,
12
+ hashMaintenanceBypassToken,
13
+ assertMaintenanceStartupAllowed,
14
+ } from './maintenance-state.js';
7
15
  export {
8
16
  createAccountability,
9
17
  createPublicAccountability,
@@ -41,6 +49,7 @@ export { AuditService, redactAuditValue } from './services/audit-service.js';
41
49
  export { ItemsService } from './services/items-service.js';
42
50
  export { CollectionsService } from './services/collections-service.js';
43
51
  export { FieldsService } from './services/fields-service.js';
52
+ export { SystemCollectionFieldsService } from './services/system-collection-fields-service.js';
44
53
  export { RelationsService } from './services/relations-service.js';
45
54
  export { UsersService } from './services/users-service.js';
46
55
  export { RolesService } from './services/roles-service.js';
@@ -61,6 +70,7 @@ export {
61
70
  } from './system-fields.js';
62
71
  export {
63
72
  assertActionOnlyPermissionPayload,
73
+ assertSystemPermissionPayload,
64
74
  assertSystemResourceAction,
65
75
  isPermissionManagedSystemResource,
66
76
  systemPermissionConfig,
@@ -75,6 +85,7 @@ export { withAdvisoryLock } from './advisory-lock.js';
75
85
  export {
76
86
  ensureMigrationJournal,
77
87
  readAppliedMigrations,
88
+ readMigrationAttempts,
78
89
  validateMigration,
79
90
  applyMigrations,
80
91
  assertMigrationsApplied,
@@ -85,4 +96,4 @@ export {
85
96
  REQUIRED_CORE_MIGRATION_IDS,
86
97
  bootstrapDatabase,
87
98
  assertDatabaseCompatible,
88
- } from './bootstrap.js';
99
+ } from './bootstrap.js';
@@ -0,0 +1,89 @@
1
+ import { createHash, timingSafeEqual } from 'node:crypto';
2
+ import { realpathSync } from 'node:fs';
3
+ import { readFile } from 'node:fs/promises';
4
+ import { tmpdir } from 'node:os';
5
+ import { join, resolve } from 'node:path';
6
+
7
+ export const MAINTENANCE_BYPASS_ENV = 'YUNCMS_MAINTENANCE_BYPASS_TOKEN';
8
+
9
+ function sha256(value) {
10
+ return createHash('sha256').update(String(value)).digest('hex');
11
+ }
12
+
13
+ export function canonicalProjectPath(cwd = process.cwd()) {
14
+ const absolute = resolve(cwd);
15
+ try {
16
+ return realpathSync.native ? realpathSync.native(absolute) : realpathSync(absolute);
17
+ } catch {
18
+ return absolute;
19
+ }
20
+ }
21
+
22
+ export function maintenanceLockPath(cwd = process.cwd()) {
23
+ const projectKey = sha256(canonicalProjectPath(cwd)).slice(0, 32);
24
+ return join(tmpdir(), 'yuncms-update-locks', `${projectKey}.lock`);
25
+ }
26
+
27
+ export function hashMaintenanceBypassToken(token) {
28
+ if (typeof token !== 'string' || token.length < 32) {
29
+ const error = new Error('Maintenance bypass token must contain at least 32 characters');
30
+ error.code = 'MAINTENANCE_BYPASS_TOKEN_INVALID';
31
+ throw error;
32
+ }
33
+ return sha256(token);
34
+ }
35
+
36
+ function hashesEqual(left, right) {
37
+ if (typeof left !== 'string' || typeof right !== 'string') return false;
38
+ if (!/^[0-9a-f]{64}$/i.test(left) || !/^[0-9a-f]{64}$/i.test(right)) return false;
39
+ const leftBuffer = Buffer.from(left, 'hex');
40
+ const rightBuffer = Buffer.from(right, 'hex');
41
+ return leftBuffer.length === rightBuffer.length && timingSafeEqual(leftBuffer, rightBuffer);
42
+ }
43
+
44
+ async function readMaintenanceState(path, readFileFn) {
45
+ let text;
46
+ try {
47
+ text = await readFileFn(path, 'utf8');
48
+ } catch (error) {
49
+ if (error?.code === 'ENOENT') return null;
50
+ throw error;
51
+ }
52
+
53
+ try {
54
+ const state = JSON.parse(text);
55
+ if (!state || typeof state !== 'object' || Array.isArray(state)) throw new Error('invalid state');
56
+ return state;
57
+ } catch (cause) {
58
+ const error = new Error(`YunCMS maintenance lock is unreadable or invalid: ${path}`);
59
+ error.code = 'YUNCMS_MAINTENANCE_ACTIVE';
60
+ error.lockPath = path;
61
+ error.cause = cause;
62
+ throw error;
63
+ }
64
+ }
65
+
66
+ export async function assertMaintenanceStartupAllowed({
67
+ cwd = process.cwd(),
68
+ env = process.env,
69
+ readFileFn = readFile,
70
+ } = {}) {
71
+ const path = maintenanceLockPath(cwd);
72
+ const state = await readMaintenanceState(path, readFileFn);
73
+ if (!state) return true;
74
+
75
+ const suppliedToken = env?.[MAINTENANCE_BYPASS_ENV];
76
+ if (typeof suppliedToken === 'string' && suppliedToken.length >= 32) {
77
+ const suppliedHash = sha256(suppliedToken);
78
+ if (hashesEqual(suppliedHash, state.bypassTokenHash)) return true;
79
+ }
80
+
81
+ const error = new Error(
82
+ `YunCMS maintenance is active for this project. Do not start the application until the maintenance operation finishes: ${path}`,
83
+ );
84
+ error.code = 'YUNCMS_MAINTENANCE_ACTIVE';
85
+ error.lockPath = path;
86
+ error.startedAt = typeof state.startedAt === 'string' ? state.startedAt : null;
87
+ error.pid = Number.isInteger(state.pid) ? state.pid : null;
88
+ throw error;
89
+ }
@@ -0,0 +1,10 @@
1
+ export const studioLogoFileMigration = {
2
+ id: '0008-studio-logo-file',
3
+ statements: [
4
+ `ALTER TABLE yuncms_studio_settings
5
+ ADD COLUMN logo_file CHAR(36) NULL AFTER logo_url,
6
+ ADD KEY idx_yuncms_studio_settings_logo_file (logo_file),
7
+ ADD CONSTRAINT fk_yuncms_studio_settings_logo_file
8
+ FOREIGN KEY (logo_file) REFERENCES yuncms_files (id) ON DELETE SET NULL`,
9
+ ],
10
+ };
@@ -0,0 +1,19 @@
1
+ export const schemaDisplayNamesMigration = {
2
+ id: '0009-schema-display-names',
3
+ statements: [
4
+ `ALTER TABLE yuncms_collections
5
+ ADD COLUMN name VARCHAR(255) NULL AFTER collection`,
6
+ `UPDATE yuncms_collections
7
+ SET name = collection
8
+ WHERE name IS NULL OR TRIM(name) = ''`,
9
+ `ALTER TABLE yuncms_collections
10
+ MODIFY COLUMN name VARCHAR(255) NOT NULL`,
11
+ `ALTER TABLE yuncms_fields
12
+ ADD COLUMN name VARCHAR(255) NULL AFTER field`,
13
+ `UPDATE yuncms_fields
14
+ SET name = field
15
+ WHERE name IS NULL OR TRIM(name) = ''`,
16
+ `ALTER TABLE yuncms_fields
17
+ MODIFY COLUMN name VARCHAR(255) NOT NULL`,
18
+ ],
19
+ };
@@ -0,0 +1,12 @@
1
+ export const studioFaviconFileMigration = {
2
+ id: '0010-studio-favicon-file',
3
+ statements: [
4
+ `ALTER TABLE yuncms_studio_settings
5
+ ADD COLUMN favicon_file CHAR(36) NULL AFTER logo_file`,
6
+ `ALTER TABLE yuncms_studio_settings
7
+ ADD KEY idx_yuncms_studio_settings_favicon_file (favicon_file)`,
8
+ `ALTER TABLE yuncms_studio_settings
9
+ ADD CONSTRAINT fk_yuncms_studio_settings_favicon_file
10
+ FOREIGN KEY (favicon_file) REFERENCES yuncms_files (id) ON DELETE SET NULL`,
11
+ ],
12
+ };
@@ -0,0 +1,15 @@
1
+ export const rolePermissionActionsMigration = {
2
+ id: '0011-role-permission-actions',
3
+ statements: [
4
+ `UPDATE yuncms_collections
5
+ SET metadata = JSON_SET(
6
+ COALESCE(metadata, JSON_OBJECT()),
7
+ '$.permissionManaged', TRUE,
8
+ '$.permissionMode', 'action-only',
9
+ '$.resource', 'roles',
10
+ '$.allowedActions', JSON_ARRAY('read', 'create', 'update', 'delete')
11
+ )
12
+ WHERE collection = 'yuncms_roles' AND \`system\` = 1`,
13
+ `UPDATE yuncms_schema_state SET version = version + 1 WHERE id = 1`,
14
+ ],
15
+ };
@@ -0,0 +1,14 @@
1
+ export const filesReadFiltersMigration = {
2
+ id: '0012-files-read-filters',
3
+ statements: [
4
+ `UPDATE yuncms_collections
5
+ SET metadata = JSON_SET(
6
+ COALESCE(metadata, JSON_OBJECT()),
7
+ '$.permissionMode',
8
+ 'filter-read'
9
+ )
10
+ WHERE collection = 'yuncms_files'
11
+ AND \`system\` = 1`,
12
+ `UPDATE yuncms_schema_state SET version = version + 1 WHERE id = 1`,
13
+ ],
14
+ };
package/src/migrations.js CHANGED
@@ -1,4 +1,10 @@
1
1
  const JOURNAL_TABLE = 'yuncms_schema_migrations';
2
+ const ATTEMPT_TABLE = 'yuncms_schema_migration_attempts';
3
+
4
+ function truncateErrorMessage(value, maxLength = 1000) {
5
+ const message = String(value ?? 'Migration failed');
6
+ return message.length > maxLength ? message.slice(0, maxLength) : message;
7
+ }
2
8
 
3
9
  export async function ensureMigrationJournal(database) {
4
10
  await database.query(`
@@ -7,6 +13,20 @@ export async function ensureMigrationJournal(database) {
7
13
  applied_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3)
8
14
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
9
15
  `);
16
+
17
+ await database.query(`
18
+ CREATE TABLE IF NOT EXISTS ${ATTEMPT_TABLE} (
19
+ migration_id VARCHAR(191) NOT NULL PRIMARY KEY,
20
+ status VARCHAR(16) NOT NULL,
21
+ statement_index INT UNSIGNED NOT NULL DEFAULT 0,
22
+ started_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
23
+ finished_at DATETIME(3) NULL,
24
+ error_code VARCHAR(128) NULL,
25
+ error_message VARCHAR(1000) NULL,
26
+ CONSTRAINT chk_yuncms_schema_migration_attempt_status
27
+ CHECK (status IN ('applying', 'applied', 'failed'))
28
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
29
+ `);
10
30
  }
11
31
 
12
32
  export async function readAppliedMigrations(database) {
@@ -14,6 +34,15 @@ export async function readAppliedMigrations(database) {
14
34
  return new Set(rows.map((row) => row.id));
15
35
  }
16
36
 
37
+ export async function readMigrationAttempts(database) {
38
+ const [rows] = await database.query(
39
+ `SELECT migration_id, status, statement_index, started_at, finished_at, error_code, error_message
40
+ FROM ${ATTEMPT_TABLE}
41
+ ORDER BY started_at ASC, migration_id ASC`,
42
+ );
43
+ return rows;
44
+ }
45
+
17
46
  export function validateMigration(migration) {
18
47
  if (!migration || typeof migration !== 'object') throw new Error('Migration must be an object');
19
48
  if (!migration.id || typeof migration.id !== 'string') throw new Error('Migration id is required');
@@ -26,23 +55,102 @@ export function validateMigration(migration) {
26
55
  return migration;
27
56
  }
28
57
 
58
+ function migrationRecoveryError(attempts) {
59
+ const ids = attempts.map((attempt) => attempt.migration_id);
60
+ const error = new Error(
61
+ `Database contains an incomplete migration attempt and must be restored before retrying: ${ids.join(', ')}`,
62
+ );
63
+ error.code = 'DATABASE_MIGRATION_RECOVERY_REQUIRED';
64
+ error.migrationAttempts = attempts.map((attempt) => ({ ...attempt }));
65
+ return error;
66
+ }
67
+
68
+ async function assertNoIncompleteMigrationAttempts(database, applied, { allowMissingJournal = false } = {}) {
69
+ let attempts;
70
+ try {
71
+ attempts = await readMigrationAttempts(database);
72
+ } catch (error) {
73
+ if (allowMissingJournal && error?.code === 'ER_NO_SUCH_TABLE') return;
74
+ throw error;
75
+ }
76
+ const inconsistent = attempts.filter((attempt) => !applied.has(attempt.migration_id));
77
+ if (inconsistent.length > 0) throw migrationRecoveryError(inconsistent);
78
+ }
79
+
80
+ async function beginMigrationAttempt(database, migration) {
81
+ await database.query(
82
+ `INSERT INTO ${ATTEMPT_TABLE}
83
+ (migration_id, status, statement_index, started_at, finished_at, error_code, error_message)
84
+ VALUES (?, 'applying', 0, CURRENT_TIMESTAMP(3), NULL, NULL, NULL)`,
85
+ [migration.id],
86
+ );
87
+ }
88
+
89
+ async function advanceMigrationAttempt(database, migrationId, statementIndex) {
90
+ await database.query(
91
+ `UPDATE ${ATTEMPT_TABLE}
92
+ SET statement_index = ?
93
+ WHERE migration_id = ? AND status = 'applying'`,
94
+ [statementIndex, migrationId],
95
+ );
96
+ }
97
+
98
+ async function completeMigrationAttempt(database, migrationId) {
99
+ await database.query(
100
+ `UPDATE ${ATTEMPT_TABLE}
101
+ SET status = 'applied', finished_at = CURRENT_TIMESTAMP(3), error_code = NULL, error_message = NULL
102
+ WHERE migration_id = ?`,
103
+ [migrationId],
104
+ );
105
+ }
106
+
107
+ async function failMigrationAttempt(database, migrationId, error) {
108
+ await database.query(
109
+ `UPDATE ${ATTEMPT_TABLE}
110
+ SET status = 'failed', finished_at = CURRENT_TIMESTAMP(3), error_code = ?, error_message = ?
111
+ WHERE migration_id = ?`,
112
+ [
113
+ error?.code == null ? null : String(error.code).slice(0, 128),
114
+ truncateErrorMessage(error?.message),
115
+ migrationId,
116
+ ],
117
+ );
118
+ }
119
+
29
120
  export async function applyMigrations(database, migrations) {
30
121
  if (!database) throw new Error('Database handle is required');
31
122
  if (!Array.isArray(migrations)) throw new Error('Migrations must be an array');
32
123
 
33
124
  await ensureMigrationJournal(database);
34
125
  const applied = await readAppliedMigrations(database);
126
+ await assertNoIncompleteMigrationAttempts(database, applied);
35
127
  const newlyApplied = [];
36
128
 
37
129
  for (const rawMigration of migrations) {
38
130
  const migration = validateMigration(rawMigration);
39
131
  if (applied.has(migration.id)) continue;
40
132
 
41
- for (const statement of migration.statements) {
42
- await database.query(statement);
133
+ await beginMigrationAttempt(database, migration);
134
+
135
+ try {
136
+ for (let index = 0; index < migration.statements.length; index += 1) {
137
+ await database.query(migration.statements[index]);
138
+ await advanceMigrationAttempt(database, migration.id, index + 1);
139
+ }
140
+
141
+ await database.query(`INSERT INTO ${JOURNAL_TABLE} (id) VALUES (?)`, [migration.id]);
142
+ await completeMigrationAttempt(database, migration.id);
143
+ } catch (error) {
144
+ try {
145
+ await failMigrationAttempt(database, migration.id, error);
146
+ } catch {
147
+ // The original migration error remains the source of truth. A stale 'applying'
148
+ // row still fails closed on the next bootstrap and requires restore/recovery.
149
+ }
150
+ error.migrationId ||= migration.id;
151
+ throw error;
43
152
  }
44
153
 
45
- await database.query(`INSERT INTO ${JOURNAL_TABLE} (id) VALUES (?)`, [migration.id]);
46
154
  applied.add(migration.id);
47
155
  newlyApplied.push(migration.id);
48
156
  }
@@ -68,6 +176,8 @@ export async function assertMigrationsApplied(database, requiredMigrationIds) {
68
176
  throw error;
69
177
  }
70
178
 
179
+ await assertNoIncompleteMigrationAttempts(database, applied, { allowMissingJournal: true });
180
+
71
181
  const missing = requiredMigrationIds.filter((id) => !applied.has(id));
72
182
 
73
183
  if (missing.length > 0) {