diff --git a/src/backend/clients/database/SqliteDatabaseClient.test.ts b/src/backend/clients/database/SqliteDatabaseClient.test.ts
index a29c0d32b..d684e06e3 100644
--- a/src/backend/clients/database/SqliteDatabaseClient.test.ts
+++ b/src/backend/clients/database/SqliteDatabaseClient.test.ts
@@ -27,7 +27,7 @@ import { DatabaseClientFactory } from './index.js';
import { SqliteDatabaseClient } from './SqliteDatabaseClient.js';
/** Highest schema version the migration table can reach. */
-const CURRENT_SCHEMA_VERSION = 96;
+const CURRENT_SCHEMA_VERSION = 97;
/**
* These suites migrate real files on disk. Idle they finish in well under a
@@ -735,6 +735,73 @@ describe('SqliteDatabaseClient — boot and migrations', { timeout: DISK_MIGRATI
expect(columns.map((c) => String(c.name))).toEqual(['send_key']);
});
+ it('creates event_user_handlers with its unique and holder indexes', async () => {
+ const columns = await client.read(
+ 'PRAGMA table_info(`event_user_handlers`)',
+ );
+ expect(columns.map((c) => String(c.name))).toEqual([
+ 'id',
+ 'app_uid',
+ 'holder_user_id',
+ 'name',
+ 'source',
+ 'source_hash',
+ 'created_at',
+ 'updated_at',
+ ]);
+ const indexes = await client.read(
+ 'PRAGMA index_list(`event_user_handlers`)',
+ );
+ expect(
+ indexes.find(
+ (i) => i.name === 'idx_event_user_handlers_app_holder_name',
+ ),
+ ).toMatchObject({ unique: 1 });
+ expect(
+ indexes.find((i) => i.name === 'idx_event_user_handlers_holder_app'),
+ ).toMatchObject({ unique: 0 });
+ const columnsOf = async (index: string) =>
+ (await client.read(`PRAGMA index_info(\`${index}\`)`)).map((c) =>
+ String(c.name),
+ );
+ expect(await columnsOf('idx_event_user_handlers_app_holder_name')).toEqual(
+ ['app_uid', 'holder_user_id', 'name'],
+ );
+ expect(await columnsOf('idx_event_user_handlers_holder_app')).toEqual([
+ 'holder_user_id',
+ 'app_uid',
+ ]);
+ });
+
+ it('leaves event_handlers as it was', async () => {
+ const columns = await client.read('PRAGMA table_info(`event_handlers`)');
+ expect(columns.map((c) => String(c.name))).toEqual([
+ 'id',
+ 'app_uid',
+ 'name',
+ 'source',
+ 'source_hash',
+ 'created_at',
+ 'updated_at',
+ ]);
+ });
+
+ it('adds event_subscriptions.handler_scope, app scope for existing rows', async () => {
+ const columns = await client.read(
+ 'PRAGMA table_info(`event_subscriptions`)',
+ );
+ expect(columns.find((c) => c.name === 'handler_scope')).toMatchObject({
+ notnull: 1,
+ dflt_value: '0',
+ });
+ const indexes = await client.read(
+ 'PRAGMA index_list(`event_subscriptions`)',
+ );
+ expect(indexes.map((i) => String(i.name))).toContain(
+ 'idx_event_subscriptions_holder',
+ );
+ });
+
it('leaves an already-migrated database untouched on a second boot', async () => {
const dir = mkdtempSync(join(tmpdir(), 'puter-sqlite-'));
const path = join(dir, 'nested', 'puter.sqlite');
diff --git a/src/backend/clients/database/SqliteDatabaseClient.ts b/src/backend/clients/database/SqliteDatabaseClient.ts
index 4a24f45b1..6624a4a96 100644
--- a/src/backend/clients/database/SqliteDatabaseClient.ts
+++ b/src/backend/clients/database/SqliteDatabaseClient.ts
@@ -135,6 +135,7 @@ const AVAILABLE_MIGRATIONS: [number, string[]][] = [
[93, ['0098_notification-groups.sql']],
[94, ['0099_kv-share-handle-notify.sql']],
[95, ['0100_notification-send-key.sql']],
+ [96, ['0101_user-scoped-handlers.sql']],
];
export class SqliteDatabaseClient extends AbstractDatabaseClient {
diff --git a/src/backend/clients/database/migrations/mysql/mysql_mig_55.sql b/src/backend/clients/database/migrations/mysql/mysql_mig_55.sql
new file mode 100644
index 000000000..cfb22c7c0
--- /dev/null
+++ b/src/backend/clients/database/migrations/mysql/mysql_mig_55.sql
@@ -0,0 +1,42 @@
+-- Copyright (C) 2024-present Puter Technologies Inc.
+--
+-- This file is part of Puter.
+--
+-- Puter is free software: you can redistribute it and/or modify
+-- it under the terms of the GNU Affero General Public License as published
+-- by the Free Software Foundation, either version 3 of the License, or
+-- (at your option) any later version.
+--
+-- This program is distributed in the hope that it will be useful,
+-- but WITHOUT ANY WARRANTY; without even the implied warranty of
+-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+-- GNU Affero General Public License for more details.
+--
+-- You should have received a copy of the GNU Affero General Public License
+-- along with this program. If not, see .
+
+-- Handlers an app's user published into their own scope, kept apart from
+-- `event_handlers` so code that predates them never reads them. See
+-- sqlite/0101_user-scoped-handlers.sql for the column rationale.
+--
+-- Idempotent: `CREATE TABLE IF NOT EXISTS` with the indexes declared inline,
+-- and a guarded column add. There is no per-file applied-state tracking, so a
+-- replay has to be a no-op.
+
+CREATE TABLE IF NOT EXISTS `event_user_handlers` (
+ `id` bigint unsigned NOT NULL AUTO_INCREMENT,
+ -- Matches `apps`.`uid` exactly, charset included, so an equality against
+ -- one never falls back to a conversion.
+ `app_uid` char(40) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,
+ `holder_user_id` int unsigned NOT NULL,
+ `name` varchar(128) NOT NULL,
+ `source` mediumtext NOT NULL,
+ `source_hash` char(64) NOT NULL,
+ `created_at` bigint NOT NULL,
+ `updated_at` bigint NOT NULL,
+ PRIMARY KEY (`id`),
+ UNIQUE KEY `idx_event_user_handlers_app_holder_name` (`app_uid`, `holder_user_id`, `name`),
+ KEY `idx_event_user_handlers_holder_app` (`holder_user_id`, `app_uid`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
+
+CALL _puter_add_col('event_subscriptions', 'handler_scope', '`handler_scope` tinyint unsigned NOT NULL DEFAULT 0');
diff --git a/src/backend/clients/database/migrations/postgres/postgres_mig_44.sql b/src/backend/clients/database/migrations/postgres/postgres_mig_44.sql
new file mode 100644
index 000000000..3dea41f42
--- /dev/null
+++ b/src/backend/clients/database/migrations/postgres/postgres_mig_44.sql
@@ -0,0 +1,38 @@
+-- Copyright (C) 2024-present Puter Technologies Inc.
+--
+-- This file is part of Puter.
+--
+-- Puter is free software: you can redistribute it and/or modify
+-- it under the terms of the GNU Affero General Public License as published
+-- by the Free Software Foundation, either version 3 of the License, or
+-- (at your option) any later version.
+--
+-- This program is distributed in the hope that it will be useful,
+-- but WITHOUT ANY WARRANTY; without even the implied warranty of
+-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+-- GNU Affero General Public License for more details.
+--
+-- You should have received a copy of the GNU Affero General Public License
+-- along with this program. If not, see .
+
+-- Handlers an app's user published into their own scope. See
+-- sqlite/0101_user-scoped-handlers.sql for the column rationale.
+-- Idempotent via IF NOT EXISTS.
+
+CREATE TABLE IF NOT EXISTS event_user_handlers (
+ id BIGSERIAL PRIMARY KEY,
+ app_uid VARCHAR(40) NOT NULL,
+ holder_user_id INTEGER NOT NULL,
+ name VARCHAR(128) NOT NULL,
+ source TEXT NOT NULL,
+ source_hash VARCHAR(64) NOT NULL,
+ created_at BIGINT NOT NULL,
+ updated_at BIGINT NOT NULL
+);
+
+CREATE UNIQUE INDEX IF NOT EXISTS idx_event_user_handlers_app_holder_name
+ ON event_user_handlers (app_uid, holder_user_id, name);
+CREATE INDEX IF NOT EXISTS idx_event_user_handlers_holder_app
+ ON event_user_handlers (holder_user_id, app_uid);
+
+ALTER TABLE event_subscriptions ADD COLUMN IF NOT EXISTS handler_scope smallint NOT NULL DEFAULT 0;
diff --git a/src/backend/clients/database/migrations/sqlite/0101_user-scoped-handlers.sql b/src/backend/clients/database/migrations/sqlite/0101_user-scoped-handlers.sql
new file mode 100644
index 000000000..846f32658
--- /dev/null
+++ b/src/backend/clients/database/migrations/sqlite/0101_user-scoped-handlers.sql
@@ -0,0 +1,47 @@
+-- Copyright (C) 2024-present Puter Technologies Inc.
+--
+-- This file is part of Puter.
+--
+-- Puter is free software: you can redistribute it and/or modify
+-- it under the terms of the GNU Affero General Public License as published
+-- by the Free Software Foundation, either version 3 of the License, or
+-- (at your option) any later version.
+--
+-- This program is distributed in the hope that it will be useful,
+-- but WITHOUT ANY WARRANTY; without even the implied warranty of
+-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+-- GNU Affero General Public License for more details.
+--
+-- You should have received a copy of the GNU Affero General Public License
+-- along with this program. If not, see .
+
+-- Handlers an app's user published into their own scope, in a table of their
+-- own so `event_handlers` stays the app owner's set. Same columns as
+-- `event_handlers` (see 0076_event-handlers.sql) plus the holder.
+--
+-- - `holder_user_id` : the user whose scope this is. A name is unique per
+-- (app, holder), so the same name can live in the app's
+-- set and in each holder's.
+-- - `(holder_user_id, app_uid)` : a holder's scopes, for listing and for the
+-- rent count.
+--
+-- `event_subscriptions.handler_scope` says which table a row's `handler_name`
+-- refers to: 0 the app's set, 1 the row holder's scope.
+
+CREATE TABLE IF NOT EXISTS `event_user_handlers` (
+ "id" INTEGER PRIMARY KEY AUTOINCREMENT,
+ "app_uid" TEXT NOT NULL,
+ "holder_user_id" INTEGER NOT NULL,
+ "name" TEXT NOT NULL,
+ "source" TEXT NOT NULL,
+ "source_hash" TEXT NOT NULL,
+ "created_at" INTEGER NOT NULL, -- unix seconds
+ "updated_at" INTEGER NOT NULL
+);
+
+CREATE UNIQUE INDEX IF NOT EXISTS `idx_event_user_handlers_app_holder_name`
+ ON `event_user_handlers` (`app_uid`, `holder_user_id`, `name`);
+CREATE INDEX IF NOT EXISTS `idx_event_user_handlers_holder_app`
+ ON `event_user_handlers` (`holder_user_id`, `app_uid`);
+
+ALTER TABLE `event_subscriptions` ADD COLUMN `handler_scope` INTEGER NOT NULL DEFAULT 0;