The minimalistic document archiving platform.
feat(tags): enforce case-insensitive unique constraint for tag names (#822)
Corentin Thomassetb6951ea05a01
diff --git a/.changeset/open-pears-send.md b/.changeset/open-pears-send.mdnew file mode 100644index 00000000..4b9c2ba9--- /dev/null+++ b/.changeset/open-pears-send.md@@ -0,0 +1,5 @@+---+"@papra/api-sdk": patch+---++Tag names uniqueness enforced with case insensitivity per organization. Migration will ensure deduplication by appending prefixes in case of existing collisions.\ No newline at end of filediff --git a/apps/papra-server/src/migrations/list/0016-case-insensitive-tag-name-uniq-constraint.migration.test.ts b/apps/papra-server/src/migrations/list/0016-case-insensitive-tag-name-uniq-constraint.migration.test.tsnew file mode 100644index 00000000..2ac1cb61--- /dev/null+++ b/apps/papra-server/src/migrations/list/0016-case-insensitive-tag-name-uniq-constraint.migration.test.ts@@ -0,0 +1,250 @@+import { asc, sql } from 'drizzle-orm';+import { describe, expect, test } from 'vitest';+import { setupDatabase } from '../../modules/app/database/database';+import { documentsTagsTable, tagsTable } from '../../modules/tags/tags.table';+import { initialSchemaSetupMigration } from './0001-initial-schema-setup.migration';+import { caseInsensitiveTagNameUniqConstraintMigration, createTagNameNormalizer } from './0016-case-insensitive-tag-name-uniq-constraint.migration';++describe('0016-case-insensitive-tag-name-uniq-constraint migration', () => {+ describe('createTagNameNormalizer', () => {+ test('utility that normalizes and deduplicates tag names per organization by appending a suffix', () => {+ const normalizeTagName = createTagNameNormalizer();++ expect(normalizeTagName({ name: 'Important', organizationId: 'org_1' })).to.eql({ name: 'Important', normalizedName: 'important' });+ expect(normalizeTagName({ name: 'important', organizationId: 'org_1' })).to.eql({ name: 'important (2)', normalizedName: 'important (2)' });+ expect(normalizeTagName({ name: 'IMPORTANT', organizationId: 'org_1' })).to.eql({ name: 'IMPORTANT (3)', normalizedName: 'important (3)' });++ expect(normalizeTagName({ name: 'Important', organizationId: 'org_2' })).to.eql({ name: 'Important', normalizedName: 'important' });+ expect(normalizeTagName({ name: 'important', organizationId: 'org_2' })).to.eql({ name: 'important (2)', normalizedName: 'important (2)' });+ expect(normalizeTagName({ name: 'Other Tag', organizationId: 'org_1' })).to.eql({ name: 'Other Tag', normalizedName: 'other tag' });+ expect(normalizeTagName({ name: 'Other Tag', organizationId: 'org_2' })).to.eql({ name: 'Other Tag', normalizedName: 'other tag' });+ });++ test('if the generated name with suffix collides, it continues incrementing the suffix until unique', () => {+ const normalizeTagName = createTagNameNormalizer();++ expect(normalizeTagName({ name: 'tag', organizationId: 'org_1' })).to.eql({ name: 'tag', normalizedName: 'tag' });+ expect(normalizeTagName({ name: 'tag (2)', organizationId: 'org_1' })).to.eql({ name: 'tag (2)', normalizedName: 'tag (2)' });+ expect(normalizeTagName({ name: 'tag', organizationId: 'org_1' })).to.eql({ name: 'tag (3)', normalizedName: 'tag (3)' });+ expect(normalizeTagName({ name: 'tag (2)', organizationId: 'org_1' })).to.eql({ name: 'tag (2) (2)', normalizedName: 'tag (2) (2)' });+ expect(normalizeTagName({ name: 'tag', organizationId: 'org_1' })).to.eql({ name: 'tag (4)', normalizedName: 'tag (4)' });+ });++ test('after many collisions, it appends a random suffix to avoid infinite loops and long processing times', () => {+ const normalizeTagName = createTagNameNormalizer({ maxAttempts: 5, generateCollisionProofSuffix: () => 'my-super-random-suffix' });++ expect(normalizeTagName({ name: 'tag', organizationId: 'org_1' })).to.eql({ name: 'tag', normalizedName: 'tag' });+ expect(normalizeTagName({ name: 'tag', organizationId: 'org_1' })).to.eql({ name: 'tag (2)', normalizedName: 'tag (2)' });+ expect(normalizeTagName({ name: 'tag', organizationId: 'org_1' })).to.eql({ name: 'tag (3)', normalizedName: 'tag (3)' });+ expect(normalizeTagName({ name: 'tag', organizationId: 'org_1' })).to.eql({ name: 'tag (4)', normalizedName: 'tag (4)' });+ expect(normalizeTagName({ name: 'tag', organizationId: 'org_1' })).to.eql({ name: 'tag (5)', normalizedName: 'tag (5)' });+ expect(normalizeTagName({ name: 'tag', organizationId: 'org_1' })).to.eql({+ name: 'tag my-super-random-suffix',+ normalizedName: 'tag my-super-random-suffix',+ });+ });+ });++ describe('caseInsensitiveTagNameUniqConstraintMigration', () => {+ test('adds normalized_name column and migrates existing tags', async () => {+ const { db } = setupDatabase({ url: ':memory:' });++ // Create initial schema without the normalized_name column yet+ await initialSchemaSetupMigration.up({ db });++ await db.batch([+ db.run(sql`INSERT INTO organizations(id, name, created_at, updated_at) VALUES ('org_1', 'Org 1', 1737936000000, 1737936000000)`),+ db.run(sql`INSERT INTO organizations(id, name, created_at, updated_at) VALUES ('org_2', 'Org 2', 1737936000000, 1737936000000)`),++ db.run(sql`INSERT INTO tags (id, created_at, updated_at, organization_id, name, color, description) VALUES ('tag_1', 1737936000000, 1737936000000, 'org_1', 'Important', '#ff0000', NULL)`),+ db.run(sql`INSERT INTO tags (id, created_at, updated_at, organization_id, name, color, description) VALUES ('tag_2', 1737936000000, 1737936000000, 'org_1', 'IMPORTANT', '#00ff00', NULL)`),+ db.run(sql`INSERT INTO tags (id, created_at, updated_at, organization_id, name, color, description) VALUES ('tag_3', 1737936000000, 1737936000000, 'org_1', 'important', '#0000ff', NULL)`),+ db.run(sql`INSERT INTO tags (id, created_at, updated_at, organization_id, name, color, description) VALUES ('tag_4', 1737936000000, 1737936000000, 'org_2', 'Important', '#ffff00', NULL)`),+ db.run(sql`INSERT INTO tags (id, created_at, updated_at, organization_id, name, color, description) VALUES ('tag_5', 1737936000000, 1737936000000, 'org_1', 'Work', '#ff00ff', 'Work related')`),+ db.run(sql`INSERT INTO tags (id, created_at, updated_at, organization_id, name, color, description) VALUES ('tag_6', 1737936000000, 1737936000000, 'org_2', 'work', '#00ffff', NULL)`),+ ]);++ await caseInsensitiveTagNameUniqConstraintMigration.up({ db });++ const tags = await db.select().from(tagsTable).orderBy(tagsTable.id);++ expect(tags).toHaveLength(6);++ expect(tags.at(0)).toMatchObject({ id: 'tag_1', organizationId: 'org_1', name: 'Important', normalizedName: 'important' });+ expect(tags.at(1)).toMatchObject({ id: 'tag_2', organizationId: 'org_1', name: 'IMPORTANT (2)', normalizedName: 'important (2)' });+ expect(tags.at(2)).toMatchObject({ id: 'tag_3', organizationId: 'org_1', name: 'important (3)', normalizedName: 'important (3)' });+ expect(tags.at(4)).toMatchObject({ id: 'tag_5', organizationId: 'org_1', name: 'Work', normalizedName: 'work', description: 'Work related' });++ expect(tags.at(3)).toMatchObject({ id: 'tag_4', organizationId: 'org_2', name: 'Important', normalizedName: 'important' });+ expect(tags.at(5)).toMatchObject({ id: 'tag_6', organizationId: 'org_2', name: 'work', normalizedName: 'work' });+ });++ test('creates unique index on normalized_name and organization_id', async () => {+ const { db } = setupDatabase({ url: ':memory:' });++ await initialSchemaSetupMigration.up({ db });++ await db.batch([+ db.run(sql`INSERT INTO organizations(id, name, created_at, updated_at) VALUES ('org_1', 'Org 1', 1737936000000, 1737936000000)`),+ db.run(sql`INSERT INTO organizations(id, name, created_at, updated_at) VALUES ('org_2', 'Org 2', 1737936000000, 1737936000000)`),+ db.run(sql`INSERT INTO tags (id, created_at, updated_at, organization_id, name, color, description) VALUES ('tag_1', 1737936000000, 1737936000000, 'org_1', 'Test', '#ff0000', NULL)`),+ ]);++ await caseInsensitiveTagNameUniqConstraintMigration.up({ db });++ await expect(+ db.run(sql`+ INSERT INTO tags (id, created_at, updated_at, organization_id, name, normalized_name, color)+ VALUES ('tag_2', 1737936000000, 1737936000000, 'org_1', 'TEST', 'test', '#00ff00')+ `),+ ).rejects.toThrow();++ await db.run(sql`+ INSERT INTO tags (id, created_at, updated_at, organization_id, name, normalized_name, color)+ VALUES ('tag_3', 1737936000000, 1737936000000, 'org_2', 'TEST', 'test', '#0000ff')+ `);++ const tags = await db.select().from(tagsTable).orderBy(tagsTable.id);+ expect(tags).toHaveLength(2);++ expect(tags.at(0)).toMatchObject({ id: 'tag_1', name: 'Test', normalizedName: 'test' });+ expect(tags.at(1)).toMatchObject({ id: 'tag_3', name: 'TEST', normalizedName: 'test' });+ });++ test('migration is idempotent - running twice does not break', async () => {+ const { db } = setupDatabase({ url: ':memory:' });++ await initialSchemaSetupMigration.up({ db });++ await db.batch([+ db.run(sql`INSERT INTO organizations(id, name, created_at, updated_at) VALUES ('org_1', 'Org 1', 1737936000000, 1737936000000)`),+ db.run(sql`INSERT INTO tags (id, created_at, updated_at, organization_id, name, color, description) VALUES ('tag_1', 1737936000000, 1737936000000, 'org_1', 'Test', '#ff0000', NULL)`),+ ]);++ await caseInsensitiveTagNameUniqConstraintMigration.up({ db });++ const tagsAfterFirst = await db.select().from(tagsTable);+ expect(tagsAfterFirst).toHaveLength(1);+ expect(tagsAfterFirst[0]?.normalizedName).to.eql('test');++ await caseInsensitiveTagNameUniqConstraintMigration.up({ db });++ const tagsAfterSecond = await db.select().from(tagsTable);+ expect(tagsAfterSecond).toHaveLength(1);+ expect(tagsAfterSecond[0]?.normalizedName).to.eql('test');+ });++ test('preserves all existing data during migration', async () => {+ const { db } = setupDatabase({ url: ':memory:' });++ await initialSchemaSetupMigration.up({ db });++ await db.batch([+ db.run(sql`INSERT INTO organizations(id, name, created_at, updated_at) VALUES ('org_1', 'Org 1', 1737936000000, 1737936000000)`),+ db.run(sql`INSERT INTO tags (id, created_at, updated_at, organization_id, name, color, description) VALUES ('tag_1', 1737936000000, 1737936000000, 'org_1', 'Tag One', '#ff0000', 'First tag')`),+ db.run(sql`INSERT INTO tags (id, created_at, updated_at, organization_id, name, color, description) VALUES ('tag_2', 1737936000000, 1737936000000, 'org_1', 'Tag Two', '#00ff00', NULL)`),+ ]);++ await caseInsensitiveTagNameUniqConstraintMigration.up({ db });++ const tags = await db.select().from(tagsTable).orderBy(tagsTable.id);++ expect(tags).to.eql([+ {+ id: 'tag_1',+ createdAt: new Date('2025-01-27'),+ updatedAt: new Date('2025-01-27'),+ organizationId: 'org_1',+ name: 'Tag One',+ normalizedName: 'tag one',+ color: '#ff0000',+ description: 'First tag',+ },+ {+ id: 'tag_2',+ createdAt: new Date('2025-01-27'),+ updatedAt: new Date('2025-01-27'),+ organizationId: 'org_1',+ name: 'Tag Two',+ normalizedName: 'tag two',+ color: '#00ff00',+ description: null,+ },+ ]);+ });++ test('the migration preserves pre-existing relations and foreign keys', async () => {+ const { db } = setupDatabase({ url: ':memory:' });++ await initialSchemaSetupMigration.up({ db });++ await db.batch([+ db.run(sql`INSERT INTO organizations(id, name, created_at, updated_at) VALUES ('org_1', 'Org 1', 1737936000000, 1737936000000)`),+ db.run(sql`INSERT INTO documents(id, organization_id, original_name, name, mime_type, original_storage_key, original_sha256_hash, content, created_at, updated_at) VALUES+ ('doc1', 'org_1', 'Test Document', 'Test Document', 'text/plain', 'key1', 'hash1', 'This is a sample document content about testing.',0,0),+ ('doc2', 'org_1', 'Another Document', 'Another Document', 'text/plain', 'key2', 'hash2', 'This document discusses database migrations.',0,0)+ `),+ db.run(sql`INSERT INTO tags (id, created_at, updated_at, organization_id, name, color, description) VALUES+ ('tag_1', 1737936000000, 1737936000000, 'org_1', 'Tag One', '#ff0000', NULL),+ ('tag_2', 1737936000000, 1737936000000, 'org_1', 'Tag Two', '#00ff00', 'Tag two description')+ `),+ db.run(sql`INSERT INTO documents_tags (document_id, tag_id) VALUES+ ('doc1', 'tag_1'),+ ('doc1', 'tag_2'),+ ('doc2', 'tag_2')+ `),+ ]);++ expect(+ await db+ .select()+ .from(documentsTagsTable)+ .orderBy(+ asc(documentsTagsTable.documentId),+ asc(documentsTagsTable.tagId),+ ),+ ).to.eql([+ { documentId: 'doc1', tagId: 'tag_1' },+ { documentId: 'doc1', tagId: 'tag_2' },+ { documentId: 'doc2', tagId: 'tag_2' },+ ]);++ await caseInsensitiveTagNameUniqConstraintMigration.up({ db });++ expect(+ await db+ .select()+ .from(documentsTagsTable)+ .orderBy(+ asc(documentsTagsTable.documentId),+ asc(documentsTagsTable.tagId),+ ),+ ).to.eql([+ { documentId: 'doc1', tagId: 'tag_1' },+ { documentId: 'doc1', tagId: 'tag_2' },+ { documentId: 'doc2', tagId: 'tag_2' },+ ]);+ });++ test('if the migration fucks up, the tags can live with their normalized_name empty', async () => {+ const { db } = setupDatabase({ url: ':memory:' });++ await initialSchemaSetupMigration.up({ db });+ await caseInsensitiveTagNameUniqConstraintMigration.up({ db });++ await db.batch([+ db.run(sql`INSERT INTO organizations(id, name, created_at, updated_at) VALUES ('org_1', 'Org 1', 1737936000000, 1737936000000)`),+ db.run(sql`INSERT INTO tags (id, created_at, updated_at, organization_id, name, normalized_name, color, description) VALUES+ ('tag_1', 1737936000000, 1737936000000, 'org_1', 'Tag One', NULL, '#ff0000', NULL),+ ('tag_2', 1737936000000, 1737936000000, 'org_1', 'Tag One', NULL, '#ff0000', NULL)+ `),+ ]);++ const tags = await db.select().from(tagsTable).orderBy(tagsTable.id);++ expect(tags).toHaveLength(2);+ expect(tags.at(0)).toMatchObject({ id: 'tag_1', name: 'Tag One', normalizedName: null });+ expect(tags.at(1)).toMatchObject({ id: 'tag_2', name: 'Tag One', normalizedName: null });+ });+ });+});diff --git a/apps/papra-server/src/migrations/list/0016-case-insensitive-tag-name-uniq-constraint.migration.ts b/apps/papra-server/src/migrations/list/0016-case-insensitive-tag-name-uniq-constraint.migration.tsnew file mode 100644index 00000000..d445d81d--- /dev/null+++ b/apps/papra-server/src/migrations/list/0016-case-insensitive-tag-name-uniq-constraint.migration.ts@@ -0,0 +1,97 @@+import type { Migration } from '../migrations.types';+import { asc, eq, isNull, sql } from 'drizzle-orm';+import { nanoid } from 'nanoid';+import { createIterator } from '../../modules/app/database/database.usecases';+import { createLogger } from '../../modules/shared/logger/logger';+import { normalizeTagName } from '../../modules/tags/tags.repository.models';+import { tagsTable } from '../../modules/tags/tags.table';++export function createTagNameNormalizer({ maxAttempts = 1000, generateCollisionProofSuffix = () => nanoid(24) }: { maxAttempts?: number; generateCollisionProofSuffix?: () => string } = {}) {+ const tagsSet = new Set<string>();+ const createSuffix = (count: number) => (count === 0 ? '' : ` (${count + 1})`);++ return ({ name, organizationId }: { name: string; organizationId: string }) => {+ let attempts = 0;++ while (attempts < maxAttempts) {+ const suffix = createSuffix(attempts);+ const nameWithSuffix = `${name}${suffix}`;+ const normalizedNameBase = normalizeTagName({ name: nameWithSuffix });+ const key = [organizationId, normalizedNameBase].join('|');++ if (!tagsSet.has(key)) {+ tagsSet.add(key);+ return {+ normalizedName: normalizedNameBase,+ name: nameWithSuffix,+ };+ }++ attempts++;+ }++ const hardSuffix = generateCollisionProofSuffix(); // Fuck it+ const nameWithHardSuffix = `${name} ${hardSuffix}`;+ const normalizedName = normalizeTagName({ name: nameWithHardSuffix });++ const key = [organizationId, normalizedName].join('|');+ tagsSet.add(key);++ return {+ normalizedName,+ name: nameWithHardSuffix,+ };+ };+}++const logger = createLogger({ namespace: 'migrations.caseInsensitiveTagNameUniqConstraint' });++export const caseInsensitiveTagNameUniqConstraintMigration = {+ name: 'case-insensitive-tag-name-uniq-constraint',++ up: async ({ db }) => {+ const tableInfo = await db.run(sql`PRAGMA table_info(tags)`);+ const existingColumns = tableInfo.rows.map(row => row.name);+ const hasColumn = (columnName: string) => existingColumns.includes(columnName);++ if (!hasColumn('normalized_name')) {+ await db.run(sql`ALTER TABLE tags ADD COLUMN "normalized_name" text`);++ const normalizer = createTagNameNormalizer();++ const iterator = createIterator({+ batchSize: 200,+ query: db+ .select()+ .from(tagsTable)+ .where(isNull(tagsTable.normalizedName))+ .orderBy(asc(tagsTable.id))+ .$dynamic(),+ });++ for await (const tag of iterator) {+ const { normalizedName, name } = normalizer({ name: tag.name, organizationId: tag.organizationId });++ await db+ .update(tagsTable)+ .set({ normalizedName, name })+ .where(eq(tagsTable.id, tag.id));++ logger.debug({ tagId: tag.id, oldName: tag.name, newName: name, normalizedName }, 'Updated tag with normalized name');+ }+ }++ await db.batch([+ db.run(sql`DROP INDEX IF EXISTS tags_organization_id_name_unique`),+ db.run(sql`CREATE UNIQUE INDEX IF NOT EXISTS tags_organization_id_normalized_name_unique ON tags (organization_id, normalized_name)`),+ ]);+ },++ down: async ({ db }) => {+ await db.batch([+ db.run(sql`DROP INDEX IF EXISTS tags_organization_id_normalized_name_unique`),+ db.run(sql`CREATE UNIQUE INDEX IF NOT EXISTS "tags_organization_id_name_unique" ON "tags" ("organization_id","name")`),+ db.run(sql`ALTER TABLE tags DROP COLUMN normalized_name`),+ ]);+ },+} satisfies Migration;diff --git a/apps/papra-server/src/migrations/meta/0013_snapshot.json b/apps/papra-server/src/migrations/meta/0013_snapshot.jsonnew file mode 100644index 00000000..66515ada--- /dev/null+++ b/apps/papra-server/src/migrations/meta/0013_snapshot.json@@ -0,0 +1,2179 @@+{+ "version": "6",+ "dialect": "sqlite",+ "id": "ece740d3-7960-41e9-a8a0-57e61d291f39",+ "prevId": "1d039177-2d51-403b-a8b4-8cc37f65aef1",+ "tables": {+ "document_activity_log": {+ "name": "document_activity_log",+ "columns": {+ "id": {+ "name": "id",+ "type": "text",+ "primaryKey": true,+ "notNull": true,+ "autoincrement": false+ },+ "created_at": {+ "name": "created_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "document_id": {+ "name": "document_id",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "event": {+ "name": "event",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "event_data": {+ "name": "event_data",+ "type": "text",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "user_id": {+ "name": "user_id",+ "type": "text",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "tag_id": {+ "name": "tag_id",+ "type": "text",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ }+ },+ "indexes": {},+ "foreignKeys": {+ "document_activity_log_document_id_documents_id_fk": {+ "name": "document_activity_log_document_id_documents_id_fk",+ "tableFrom": "document_activity_log",+ "tableTo": "documents",+ "columnsFrom": [+ "document_id"+ ],+ "columnsTo": [+ "id"+ ],+ "onDelete": "cascade",+ "onUpdate": "cascade"+ },+ "document_activity_log_user_id_users_id_fk": {+ "name": "document_activity_log_user_id_users_id_fk",+ "tableFrom": "document_activity_log",+ "tableTo": "users",+ "columnsFrom": [+ "user_id"+ ],+ "columnsTo": [+ "id"+ ],+ "onDelete": "set null",+ "onUpdate": "cascade"+ },+ "document_activity_log_tag_id_tags_id_fk": {+ "name": "document_activity_log_tag_id_tags_id_fk",+ "tableFrom": "document_activity_log",+ "tableTo": "tags",+ "columnsFrom": [+ "tag_id"+ ],+ "columnsTo": [+ "id"+ ],+ "onDelete": "set null",+ "onUpdate": "cascade"+ }+ },+ "compositePrimaryKeys": {},+ "uniqueConstraints": {},+ "checkConstraints": {}+ },+ "documents": {+ "name": "documents",+ "columns": {+ "id": {+ "name": "id",+ "type": "text",+ "primaryKey": true,+ "notNull": true,+ "autoincrement": false+ },+ "created_at": {+ "name": "created_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "updated_at": {+ "name": "updated_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "organization_id": {+ "name": "organization_id",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "created_by": {+ "name": "created_by",+ "type": "text",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "original_name": {+ "name": "original_name",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "original_size": {+ "name": "original_size",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false,+ "default": 0+ },+ "original_storage_key": {+ "name": "original_storage_key",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "original_sha256_hash": {+ "name": "original_sha256_hash",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "name": {+ "name": "name",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "mime_type": {+ "name": "mime_type",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "content": {+ "name": "content",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false,+ "default": "''"+ },+ "file_encryption_key_wrapped": {+ "name": "file_encryption_key_wrapped",+ "type": "text",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "file_encryption_kek_version": {+ "name": "file_encryption_kek_version",+ "type": "text",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "file_encryption_algorithm": {+ "name": "file_encryption_algorithm",+ "type": "text",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "deleted_at": {+ "name": "deleted_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "deleted_by": {+ "name": "deleted_by",+ "type": "text",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "is_deleted": {+ "name": "is_deleted",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false,+ "default": false+ }+ },+ "indexes": {+ "documents_organization_id_is_deleted_created_at_index": {+ "name": "documents_organization_id_is_deleted_created_at_index",+ "columns": [+ "organization_id",+ "is_deleted",+ "created_at"+ ],+ "isUnique": false+ },+ "documents_organization_id_is_deleted_index": {+ "name": "documents_organization_id_is_deleted_index",+ "columns": [+ "organization_id",+ "is_deleted"+ ],+ "isUnique": false+ },+ "documents_organization_id_original_sha256_hash_unique": {+ "name": "documents_organization_id_original_sha256_hash_unique",+ "columns": [+ "organization_id",+ "original_sha256_hash"+ ],+ "isUnique": true+ },+ "documents_original_sha256_hash_index": {+ "name": "documents_original_sha256_hash_index",+ "columns": [+ "original_sha256_hash"+ ],+ "isUnique": false+ },+ "documents_organization_id_size_index": {+ "name": "documents_organization_id_size_index",+ "columns": [+ "organization_id",+ "original_size"+ ],+ "isUnique": false+ },+ "documents_file_encryption_kek_version_index": {+ "name": "documents_file_encryption_kek_version_index",+ "columns": [+ "file_encryption_kek_version"+ ],+ "isUnique": false+ }+ },+ "foreignKeys": {+ "documents_organization_id_organizations_id_fk": {+ "name": "documents_organization_id_organizations_id_fk",+ "tableFrom": "documents",+ "tableTo": "organizations",+ "columnsFrom": [+ "organization_id"+ ],+ "columnsTo": [+ "id"+ ],+ "onDelete": "cascade",+ "onUpdate": "cascade"+ },+ "documents_created_by_users_id_fk": {+ "name": "documents_created_by_users_id_fk",+ "tableFrom": "documents",+ "tableTo": "users",+ "columnsFrom": [+ "created_by"+ ],+ "columnsTo": [+ "id"+ ],+ "onDelete": "set null",+ "onUpdate": "cascade"+ },+ "documents_deleted_by_users_id_fk": {+ "name": "documents_deleted_by_users_id_fk",+ "tableFrom": "documents",+ "tableTo": "users",+ "columnsFrom": [+ "deleted_by"+ ],+ "columnsTo": [+ "id"+ ],+ "onDelete": "set null",+ "onUpdate": "cascade"+ }+ },+ "compositePrimaryKeys": {},+ "uniqueConstraints": {},+ "checkConstraints": {}+ },+ "organization_invitations": {+ "name": "organization_invitations",+ "columns": {+ "id": {+ "name": "id",+ "type": "text",+ "primaryKey": true,+ "notNull": true,+ "autoincrement": false+ },+ "created_at": {+ "name": "created_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "updated_at": {+ "name": "updated_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "organization_id": {+ "name": "organization_id",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "email": {+ "name": "email",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "role": {+ "name": "role",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "status": {+ "name": "status",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false,+ "default": "'pending'"+ },+ "expires_at": {+ "name": "expires_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "inviter_id": {+ "name": "inviter_id",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ }+ },+ "indexes": {+ "organization_invitations_organization_email_unique": {+ "name": "organization_invitations_organization_email_unique",+ "columns": [+ "organization_id",+ "email"+ ],+ "isUnique": true+ }+ },+ "foreignKeys": {+ "organization_invitations_organization_id_organizations_id_fk": {+ "name": "organization_invitations_organization_id_organizations_id_fk",+ "tableFrom": "organization_invitations",+ "tableTo": "organizations",+ "columnsFrom": [+ "organization_id"+ ],+ "columnsTo": [+ "id"+ ],+ "onDelete": "cascade",+ "onUpdate": "cascade"+ },+ "organization_invitations_inviter_id_users_id_fk": {+ "name": "organization_invitations_inviter_id_users_id_fk",+ "tableFrom": "organization_invitations",+ "tableTo": "users",+ "columnsFrom": [+ "inviter_id"+ ],+ "columnsTo": [+ "id"+ ],+ "onDelete": "cascade",+ "onUpdate": "cascade"+ }+ },+ "compositePrimaryKeys": {},+ "uniqueConstraints": {},+ "checkConstraints": {}+ },+ "organization_members": {+ "name": "organization_members",+ "columns": {+ "id": {+ "name": "id",+ "type": "text",+ "primaryKey": true,+ "notNull": true,+ "autoincrement": false+ },+ "created_at": {+ "name": "created_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "updated_at": {+ "name": "updated_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "organization_id": {+ "name": "organization_id",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "user_id": {+ "name": "user_id",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "role": {+ "name": "role",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ }+ },+ "indexes": {+ "organization_members_user_organization_unique": {+ "name": "organization_members_user_organization_unique",+ "columns": [+ "organization_id",+ "user_id"+ ],+ "isUnique": true+ }+ },+ "foreignKeys": {+ "organization_members_organization_id_organizations_id_fk": {+ "name": "organization_members_organization_id_organizations_id_fk",+ "tableFrom": "organization_members",+ "tableTo": "organizations",+ "columnsFrom": [+ "organization_id"+ ],+ "columnsTo": [+ "id"+ ],+ "onDelete": "cascade",+ "onUpdate": "cascade"+ },+ "organization_members_user_id_users_id_fk": {+ "name": "organization_members_user_id_users_id_fk",+ "tableFrom": "organization_members",+ "tableTo": "users",+ "columnsFrom": [+ "user_id"+ ],+ "columnsTo": [+ "id"+ ],+ "onDelete": "cascade",+ "onUpdate": "cascade"+ }+ },+ "compositePrimaryKeys": {},+ "uniqueConstraints": {},+ "checkConstraints": {}+ },+ "organizations": {+ "name": "organizations",+ "columns": {+ "id": {+ "name": "id",+ "type": "text",+ "primaryKey": true,+ "notNull": true,+ "autoincrement": false+ },+ "created_at": {+ "name": "created_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "updated_at": {+ "name": "updated_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "name": {+ "name": "name",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "customer_id": {+ "name": "customer_id",+ "type": "text",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "deleted_at": {+ "name": "deleted_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "deleted_by": {+ "name": "deleted_by",+ "type": "text",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "scheduled_purge_at": {+ "name": "scheduled_purge_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ }+ },+ "indexes": {+ "organizations_deleted_at_purge_at_index": {+ "name": "organizations_deleted_at_purge_at_index",+ "columns": [+ "deleted_at",+ "scheduled_purge_at"+ ],+ "isUnique": false+ },+ "organizations_deleted_by_deleted_at_index": {+ "name": "organizations_deleted_by_deleted_at_index",+ "columns": [+ "deleted_by",+ "deleted_at"+ ],+ "isUnique": false+ }+ },+ "foreignKeys": {+ "organizations_deleted_by_users_id_fk": {+ "name": "organizations_deleted_by_users_id_fk",+ "tableFrom": "organizations",+ "tableTo": "users",+ "columnsFrom": [+ "deleted_by"+ ],+ "columnsTo": [+ "id"+ ],+ "onDelete": "set null",+ "onUpdate": "cascade"+ }+ },+ "compositePrimaryKeys": {},+ "uniqueConstraints": {},+ "checkConstraints": {}+ },+ "user_roles": {+ "name": "user_roles",+ "columns": {+ "id": {+ "name": "id",+ "type": "text",+ "primaryKey": true,+ "notNull": true,+ "autoincrement": false+ },+ "created_at": {+ "name": "created_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "updated_at": {+ "name": "updated_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "user_id": {+ "name": "user_id",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "role": {+ "name": "role",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ }+ },+ "indexes": {+ "user_roles_role_index": {+ "name": "user_roles_role_index",+ "columns": [+ "role"+ ],+ "isUnique": false+ },+ "user_roles_user_id_role_unique_index": {+ "name": "user_roles_user_id_role_unique_index",+ "columns": [+ "user_id",+ "role"+ ],+ "isUnique": true+ }+ },+ "foreignKeys": {+ "user_roles_user_id_users_id_fk": {+ "name": "user_roles_user_id_users_id_fk",+ "tableFrom": "user_roles",+ "tableTo": "users",+ "columnsFrom": [+ "user_id"+ ],+ "columnsTo": [+ "id"+ ],+ "onDelete": "cascade",+ "onUpdate": "cascade"+ }+ },+ "compositePrimaryKeys": {},+ "uniqueConstraints": {},+ "checkConstraints": {}+ },+ "documents_tags": {+ "name": "documents_tags",+ "columns": {+ "document_id": {+ "name": "document_id",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "tag_id": {+ "name": "tag_id",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ }+ },+ "indexes": {},+ "foreignKeys": {+ "documents_tags_document_id_documents_id_fk": {+ "name": "documents_tags_document_id_documents_id_fk",+ "tableFrom": "documents_tags",+ "tableTo": "documents",+ "columnsFrom": [+ "document_id"+ ],+ "columnsTo": [+ "id"+ ],+ "onDelete": "cascade",+ "onUpdate": "cascade"+ },+ "documents_tags_tag_id_tags_id_fk": {+ "name": "documents_tags_tag_id_tags_id_fk",+ "tableFrom": "documents_tags",+ "tableTo": "tags",+ "columnsFrom": [+ "tag_id"+ ],+ "columnsTo": [+ "id"+ ],+ "onDelete": "cascade",+ "onUpdate": "cascade"+ }+ },+ "compositePrimaryKeys": {+ "documents_tags_pkey": {+ "columns": [+ "document_id",+ "tag_id"+ ],+ "name": "documents_tags_pkey"+ }+ },+ "uniqueConstraints": {},+ "checkConstraints": {}+ },+ "tags": {+ "name": "tags",+ "columns": {+ "id": {+ "name": "id",+ "type": "text",+ "primaryKey": true,+ "notNull": true,+ "autoincrement": false+ },+ "created_at": {+ "name": "created_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "updated_at": {+ "name": "updated_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "organization_id": {+ "name": "organization_id",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "name": {+ "name": "name",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "normalized_name": {+ "name": "normalized_name",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "color": {+ "name": "color",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "description": {+ "name": "description",+ "type": "text",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ }+ },+ "indexes": {+ "tags_organization_id_normalized_name_unique": {+ "name": "tags_organization_id_normalized_name_unique",+ "columns": [+ "organization_id",+ "normalized_name"+ ],+ "isUnique": true+ }+ },+ "foreignKeys": {+ "tags_organization_id_organizations_id_fk": {+ "name": "tags_organization_id_organizations_id_fk",+ "tableFrom": "tags",+ "tableTo": "organizations",+ "columnsFrom": [+ "organization_id"+ ],+ "columnsTo": [+ "id"+ ],+ "onDelete": "cascade",+ "onUpdate": "cascade"+ }+ },+ "compositePrimaryKeys": {},+ "uniqueConstraints": {},+ "checkConstraints": {}+ },+ "users": {+ "name": "users",+ "columns": {+ "id": {+ "name": "id",+ "type": "text",+ "primaryKey": true,+ "notNull": true,+ "autoincrement": false+ },+ "created_at": {+ "name": "created_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "updated_at": {+ "name": "updated_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "email": {+ "name": "email",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "email_verified": {+ "name": "email_verified",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false,+ "default": false+ },+ "name": {+ "name": "name",+ "type": "text",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "image": {+ "name": "image",+ "type": "text",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "max_organization_count": {+ "name": "max_organization_count",+ "type": "integer",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "two_factor_enabled": {+ "name": "two_factor_enabled",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false,+ "default": false+ }+ },+ "indexes": {+ "users_email_unique": {+ "name": "users_email_unique",+ "columns": [+ "email"+ ],+ "isUnique": true+ },+ "users_email_index": {+ "name": "users_email_index",+ "columns": [+ "email"+ ],+ "isUnique": false+ }+ },+ "foreignKeys": {},+ "compositePrimaryKeys": {},+ "uniqueConstraints": {},+ "checkConstraints": {}+ },+ "api_key_organizations": {+ "name": "api_key_organizations",+ "columns": {+ "api_key_id": {+ "name": "api_key_id",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "organization_member_id": {+ "name": "organization_member_id",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ }+ },+ "indexes": {},+ "foreignKeys": {+ "api_key_organizations_api_key_id_api_keys_id_fk": {+ "name": "api_key_organizations_api_key_id_api_keys_id_fk",+ "tableFrom": "api_key_organizations",+ "tableTo": "api_keys",+ "columnsFrom": [+ "api_key_id"+ ],+ "columnsTo": [+ "id"+ ],+ "onDelete": "cascade",+ "onUpdate": "cascade"+ },+ "api_key_organizations_organization_member_id_organization_members_id_fk": {+ "name": "api_key_organizations_organization_member_id_organization_members_id_fk",+ "tableFrom": "api_key_organizations",+ "tableTo": "organization_members",+ "columnsFrom": [+ "organization_member_id"+ ],+ "columnsTo": [+ "id"+ ],+ "onDelete": "cascade",+ "onUpdate": "cascade"+ }+ },+ "compositePrimaryKeys": {},+ "uniqueConstraints": {},+ "checkConstraints": {}+ },+ "api_keys": {+ "name": "api_keys",+ "columns": {+ "id": {+ "name": "id",+ "type": "text",+ "primaryKey": true,+ "notNull": true,+ "autoincrement": false+ },+ "created_at": {+ "name": "created_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "updated_at": {+ "name": "updated_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "name": {+ "name": "name",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "key_hash": {+ "name": "key_hash",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "prefix": {+ "name": "prefix",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "user_id": {+ "name": "user_id",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "last_used_at": {+ "name": "last_used_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "expires_at": {+ "name": "expires_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "permissions": {+ "name": "permissions",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false,+ "default": "'[]'"+ },+ "all_organizations": {+ "name": "all_organizations",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false,+ "default": false+ }+ },+ "indexes": {+ "api_keys_key_hash_unique": {+ "name": "api_keys_key_hash_unique",+ "columns": [+ "key_hash"+ ],+ "isUnique": true+ },+ "key_hash_index": {+ "name": "key_hash_index",+ "columns": [+ "key_hash"+ ],+ "isUnique": false+ }+ },+ "foreignKeys": {+ "api_keys_user_id_users_id_fk": {+ "name": "api_keys_user_id_users_id_fk",+ "tableFrom": "api_keys",+ "tableTo": "users",+ "columnsFrom": [+ "user_id"+ ],+ "columnsTo": [+ "id"+ ],+ "onDelete": "cascade",+ "onUpdate": "cascade"+ }+ },+ "compositePrimaryKeys": {},+ "uniqueConstraints": {},+ "checkConstraints": {}+ },+ "auth_accounts": {+ "name": "auth_accounts",+ "columns": {+ "id": {+ "name": "id",+ "type": "text",+ "primaryKey": true,+ "notNull": true,+ "autoincrement": false+ },+ "created_at": {+ "name": "created_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "updated_at": {+ "name": "updated_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "user_id": {+ "name": "user_id",+ "type": "text",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "account_id": {+ "name": "account_id",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "provider_id": {+ "name": "provider_id",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "access_token": {+ "name": "access_token",+ "type": "text",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "refresh_token": {+ "name": "refresh_token",+ "type": "text",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "access_token_expires_at": {+ "name": "access_token_expires_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "refresh_token_expires_at": {+ "name": "refresh_token_expires_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "scope": {+ "name": "scope",+ "type": "text",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "id_token": {+ "name": "id_token",+ "type": "text",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "password": {+ "name": "password",+ "type": "text",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ }+ },+ "indexes": {},+ "foreignKeys": {+ "auth_accounts_user_id_users_id_fk": {+ "name": "auth_accounts_user_id_users_id_fk",+ "tableFrom": "auth_accounts",+ "tableTo": "users",+ "columnsFrom": [+ "user_id"+ ],+ "columnsTo": [+ "id"+ ],+ "onDelete": "cascade",+ "onUpdate": "cascade"+ }+ },+ "compositePrimaryKeys": {},+ "uniqueConstraints": {},+ "checkConstraints": {}+ },+ "auth_sessions": {+ "name": "auth_sessions",+ "columns": {+ "id": {+ "name": "id",+ "type": "text",+ "primaryKey": true,+ "notNull": true,+ "autoincrement": false+ },+ "created_at": {+ "name": "created_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "updated_at": {+ "name": "updated_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "token": {+ "name": "token",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "user_id": {+ "name": "user_id",+ "type": "text",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "expires_at": {+ "name": "expires_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "ip_address": {+ "name": "ip_address",+ "type": "text",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "user_agent": {+ "name": "user_agent",+ "type": "text",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "active_organization_id": {+ "name": "active_organization_id",+ "type": "text",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ }+ },+ "indexes": {+ "auth_sessions_token_index": {+ "name": "auth_sessions_token_index",+ "columns": [+ "token"+ ],+ "isUnique": false+ }+ },+ "foreignKeys": {+ "auth_sessions_user_id_users_id_fk": {+ "name": "auth_sessions_user_id_users_id_fk",+ "tableFrom": "auth_sessions",+ "tableTo": "users",+ "columnsFrom": [+ "user_id"+ ],+ "columnsTo": [+ "id"+ ],+ "onDelete": "cascade",+ "onUpdate": "cascade"+ },+ "auth_sessions_active_organization_id_organizations_id_fk": {+ "name": "auth_sessions_active_organization_id_organizations_id_fk",+ "tableFrom": "auth_sessions",+ "tableTo": "organizations",+ "columnsFrom": [+ "active_organization_id"+ ],+ "columnsTo": [+ "id"+ ],+ "onDelete": "set null",+ "onUpdate": "cascade"+ }+ },+ "compositePrimaryKeys": {},+ "uniqueConstraints": {},+ "checkConstraints": {}+ },+ "auth_two_factor": {+ "name": "auth_two_factor",+ "columns": {+ "id": {+ "name": "id",+ "type": "text",+ "primaryKey": true,+ "notNull": true,+ "autoincrement": false+ },+ "created_at": {+ "name": "created_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "updated_at": {+ "name": "updated_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "user_id": {+ "name": "user_id",+ "type": "text",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "secret": {+ "name": "secret",+ "type": "text",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "backup_codes": {+ "name": "backup_codes",+ "type": "text",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ }+ },+ "indexes": {},+ "foreignKeys": {+ "auth_two_factor_user_id_users_id_fk": {+ "name": "auth_two_factor_user_id_users_id_fk",+ "tableFrom": "auth_two_factor",+ "tableTo": "users",+ "columnsFrom": [+ "user_id"+ ],+ "columnsTo": [+ "id"+ ],+ "onDelete": "cascade",+ "onUpdate": "cascade"+ }+ },+ "compositePrimaryKeys": {},+ "uniqueConstraints": {},+ "checkConstraints": {}+ },+ "auth_verifications": {+ "name": "auth_verifications",+ "columns": {+ "id": {+ "name": "id",+ "type": "text",+ "primaryKey": true,+ "notNull": true,+ "autoincrement": false+ },+ "created_at": {+ "name": "created_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "updated_at": {+ "name": "updated_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "identifier": {+ "name": "identifier",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "value": {+ "name": "value",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "expires_at": {+ "name": "expires_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ }+ },+ "indexes": {+ "auth_verifications_identifier_index": {+ "name": "auth_verifications_identifier_index",+ "columns": [+ "identifier"+ ],+ "isUnique": false+ }+ },+ "foreignKeys": {},+ "compositePrimaryKeys": {},+ "uniqueConstraints": {},+ "checkConstraints": {}+ },+ "documents_fts": {+ "name": "documents_fts",+ "columns": {+ "document_id": {+ "name": "document_id",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "organization_id": {+ "name": "organization_id",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "name": {+ "name": "name",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "content": {+ "name": "content",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ }+ },+ "indexes": {},+ "foreignKeys": {},+ "compositePrimaryKeys": {},+ "uniqueConstraints": {},+ "checkConstraints": {}+ },+ "intake_emails": {+ "name": "intake_emails",+ "columns": {+ "id": {+ "name": "id",+ "type": "text",+ "primaryKey": true,+ "notNull": true,+ "autoincrement": false+ },+ "created_at": {+ "name": "created_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "updated_at": {+ "name": "updated_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "email_address": {+ "name": "email_address",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "organization_id": {+ "name": "organization_id",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "allowed_origins": {+ "name": "allowed_origins",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false,+ "default": "'[]'"+ },+ "is_enabled": {+ "name": "is_enabled",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false,+ "default": true+ }+ },+ "indexes": {+ "intake_emails_email_address_unique": {+ "name": "intake_emails_email_address_unique",+ "columns": [+ "email_address"+ ],+ "isUnique": true+ }+ },+ "foreignKeys": {+ "intake_emails_organization_id_organizations_id_fk": {+ "name": "intake_emails_organization_id_organizations_id_fk",+ "tableFrom": "intake_emails",+ "tableTo": "organizations",+ "columnsFrom": [+ "organization_id"+ ],+ "columnsTo": [+ "id"+ ],+ "onDelete": "cascade",+ "onUpdate": "cascade"+ }+ },+ "compositePrimaryKeys": {},+ "uniqueConstraints": {},+ "checkConstraints": {}+ },+ "organization_subscriptions": {+ "name": "organization_subscriptions",+ "columns": {+ "id": {+ "name": "id",+ "type": "text",+ "primaryKey": true,+ "notNull": true,+ "autoincrement": false+ },+ "customer_id": {+ "name": "customer_id",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "organization_id": {+ "name": "organization_id",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "plan_id": {+ "name": "plan_id",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "status": {+ "name": "status",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "seats_count": {+ "name": "seats_count",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "current_period_end": {+ "name": "current_period_end",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "current_period_start": {+ "name": "current_period_start",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "cancel_at_period_end": {+ "name": "cancel_at_period_end",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false,+ "default": false+ },+ "created_at": {+ "name": "created_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "updated_at": {+ "name": "updated_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ }+ },+ "indexes": {},+ "foreignKeys": {+ "organization_subscriptions_organization_id_organizations_id_fk": {+ "name": "organization_subscriptions_organization_id_organizations_id_fk",+ "tableFrom": "organization_subscriptions",+ "tableTo": "organizations",+ "columnsFrom": [+ "organization_id"+ ],+ "columnsTo": [+ "id"+ ],+ "onDelete": "cascade",+ "onUpdate": "cascade"+ }+ },+ "compositePrimaryKeys": {},+ "uniqueConstraints": {},+ "checkConstraints": {}+ },+ "tagging_rule_actions": {+ "name": "tagging_rule_actions",+ "columns": {+ "id": {+ "name": "id",+ "type": "text",+ "primaryKey": true,+ "notNull": true,+ "autoincrement": false+ },+ "created_at": {+ "name": "created_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "updated_at": {+ "name": "updated_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "tagging_rule_id": {+ "name": "tagging_rule_id",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "tag_id": {+ "name": "tag_id",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ }+ },+ "indexes": {},+ "foreignKeys": {+ "tagging_rule_actions_tagging_rule_id_tagging_rules_id_fk": {+ "name": "tagging_rule_actions_tagging_rule_id_tagging_rules_id_fk",+ "tableFrom": "tagging_rule_actions",+ "tableTo": "tagging_rules",+ "columnsFrom": [+ "tagging_rule_id"+ ],+ "columnsTo": [+ "id"+ ],+ "onDelete": "cascade",+ "onUpdate": "cascade"+ },+ "tagging_rule_actions_tag_id_tags_id_fk": {+ "name": "tagging_rule_actions_tag_id_tags_id_fk",+ "tableFrom": "tagging_rule_actions",+ "tableTo": "tags",+ "columnsFrom": [+ "tag_id"+ ],+ "columnsTo": [+ "id"+ ],+ "onDelete": "cascade",+ "onUpdate": "cascade"+ }+ },+ "compositePrimaryKeys": {},+ "uniqueConstraints": {},+ "checkConstraints": {}+ },+ "tagging_rule_conditions": {+ "name": "tagging_rule_conditions",+ "columns": {+ "id": {+ "name": "id",+ "type": "text",+ "primaryKey": true,+ "notNull": true,+ "autoincrement": false+ },+ "created_at": {+ "name": "created_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "updated_at": {+ "name": "updated_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "tagging_rule_id": {+ "name": "tagging_rule_id",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "field": {+ "name": "field",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "operator": {+ "name": "operator",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "value": {+ "name": "value",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "is_case_sensitive": {+ "name": "is_case_sensitive",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false,+ "default": false+ }+ },+ "indexes": {},+ "foreignKeys": {+ "tagging_rule_conditions_tagging_rule_id_tagging_rules_id_fk": {+ "name": "tagging_rule_conditions_tagging_rule_id_tagging_rules_id_fk",+ "tableFrom": "tagging_rule_conditions",+ "tableTo": "tagging_rules",+ "columnsFrom": [+ "tagging_rule_id"+ ],+ "columnsTo": [+ "id"+ ],+ "onDelete": "cascade",+ "onUpdate": "cascade"+ }+ },+ "compositePrimaryKeys": {},+ "uniqueConstraints": {},+ "checkConstraints": {}+ },+ "tagging_rules": {+ "name": "tagging_rules",+ "columns": {+ "id": {+ "name": "id",+ "type": "text",+ "primaryKey": true,+ "notNull": true,+ "autoincrement": false+ },+ "created_at": {+ "name": "created_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "updated_at": {+ "name": "updated_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "organization_id": {+ "name": "organization_id",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "name": {+ "name": "name",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "description": {+ "name": "description",+ "type": "text",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "enabled": {+ "name": "enabled",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false,+ "default": true+ },+ "condition_match_mode": {+ "name": "condition_match_mode",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false,+ "default": "'all'"+ }+ },+ "indexes": {},+ "foreignKeys": {+ "tagging_rules_organization_id_organizations_id_fk": {+ "name": "tagging_rules_organization_id_organizations_id_fk",+ "tableFrom": "tagging_rules",+ "tableTo": "organizations",+ "columnsFrom": [+ "organization_id"+ ],+ "columnsTo": [+ "id"+ ],+ "onDelete": "cascade",+ "onUpdate": "cascade"+ }+ },+ "compositePrimaryKeys": {},+ "uniqueConstraints": {},+ "checkConstraints": {}+ },+ "webhook_deliveries": {+ "name": "webhook_deliveries",+ "columns": {+ "id": {+ "name": "id",+ "type": "text",+ "primaryKey": true,+ "notNull": true,+ "autoincrement": false+ },+ "created_at": {+ "name": "created_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "updated_at": {+ "name": "updated_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "webhook_id": {+ "name": "webhook_id",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "event_name": {+ "name": "event_name",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "request_payload": {+ "name": "request_payload",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "response_payload": {+ "name": "response_payload",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "response_status": {+ "name": "response_status",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ }+ },+ "indexes": {},+ "foreignKeys": {+ "webhook_deliveries_webhook_id_webhooks_id_fk": {+ "name": "webhook_deliveries_webhook_id_webhooks_id_fk",+ "tableFrom": "webhook_deliveries",+ "tableTo": "webhooks",+ "columnsFrom": [+ "webhook_id"+ ],+ "columnsTo": [+ "id"+ ],+ "onDelete": "cascade",+ "onUpdate": "cascade"+ }+ },+ "compositePrimaryKeys": {},+ "uniqueConstraints": {},+ "checkConstraints": {}+ },+ "webhook_events": {+ "name": "webhook_events",+ "columns": {+ "id": {+ "name": "id",+ "type": "text",+ "primaryKey": true,+ "notNull": true,+ "autoincrement": false+ },+ "created_at": {+ "name": "created_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "updated_at": {+ "name": "updated_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "webhook_id": {+ "name": "webhook_id",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "event_name": {+ "name": "event_name",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ }+ },+ "indexes": {+ "webhook_events_webhook_id_event_name_unique": {+ "name": "webhook_events_webhook_id_event_name_unique",+ "columns": [+ "webhook_id",+ "event_name"+ ],+ "isUnique": true+ }+ },+ "foreignKeys": {+ "webhook_events_webhook_id_webhooks_id_fk": {+ "name": "webhook_events_webhook_id_webhooks_id_fk",+ "tableFrom": "webhook_events",+ "tableTo": "webhooks",+ "columnsFrom": [+ "webhook_id"+ ],+ "columnsTo": [+ "id"+ ],+ "onDelete": "cascade",+ "onUpdate": "cascade"+ }+ },+ "compositePrimaryKeys": {},+ "uniqueConstraints": {},+ "checkConstraints": {}+ },+ "webhooks": {+ "name": "webhooks",+ "columns": {+ "id": {+ "name": "id",+ "type": "text",+ "primaryKey": true,+ "notNull": true,+ "autoincrement": false+ },+ "created_at": {+ "name": "created_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "updated_at": {+ "name": "updated_at",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "name": {+ "name": "name",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "url": {+ "name": "url",+ "type": "text",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "secret": {+ "name": "secret",+ "type": "text",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "enabled": {+ "name": "enabled",+ "type": "integer",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false,+ "default": true+ },+ "created_by": {+ "name": "created_by",+ "type": "text",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "organization_id": {+ "name": "organization_id",+ "type": "text",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ }+ },+ "indexes": {},+ "foreignKeys": {+ "webhooks_created_by_users_id_fk": {+ "name": "webhooks_created_by_users_id_fk",+ "tableFrom": "webhooks",+ "tableTo": "users",+ "columnsFrom": [+ "created_by"+ ],+ "columnsTo": [+ "id"+ ],+ "onDelete": "set null",+ "onUpdate": "cascade"+ },+ "webhooks_organization_id_organizations_id_fk": {+ "name": "webhooks_organization_id_organizations_id_fk",+ "tableFrom": "webhooks",+ "tableTo": "organizations",+ "columnsFrom": [+ "organization_id"+ ],+ "columnsTo": [+ "id"+ ],+ "onDelete": "cascade",+ "onUpdate": "cascade"+ }+ },+ "compositePrimaryKeys": {},+ "uniqueConstraints": {},+ "checkConstraints": {}+ }+ },+ "views": {},+ "enums": {},+ "_meta": {+ "schemas": {},+ "tables": {},+ "columns": {+ "\"documents_fts\".\"id\"": "\"documents_fts\".\"document_id\""+ }+ },+ "internal": {+ "indexes": {}+ }+}diff --git a/apps/papra-server/src/migrations/meta/_journal.json b/apps/papra-server/src/migrations/meta/_journal.jsonindex 855cf73d..7949963e 100644--- a/apps/papra-server/src/migrations/meta/_journal.json+++ b/apps/papra-server/src/migrations/meta/_journal.json@@ -92,6 +92,13 @@"when": 1766411483931,"tag": "0012_two-factor-authentication","breakpoints": true+ },+ {+ "idx": 13,+ "version": "6",+ "when": 1769523160483,+ "tag": "0013_case-insensitive-tag-name-uniq-constraint",+ "breakpoints": true}]}diff --git a/apps/papra-server/src/migrations/migrations.registry.test.ts b/apps/papra-server/src/migrations/migrations.registry.test.tsindex b320aa48..fbcd80ba 100644--- a/apps/papra-server/src/migrations/migrations.registry.test.ts+++ b/apps/papra-server/src/migrations/migrations.registry.test.ts@@ -86,7 +86,7 @@ describe('migrations registry', () => {CREATE UNIQUE INDEX "organization_members_user_organization_unique" ON "organization_members" ("organization_id","user_id");CREATE INDEX "organizations_deleted_at_purge_at_index" ON "organizations" ("deleted_at","scheduled_purge_at");CREATE INDEX "organizations_deleted_by_deleted_at_index" ON "organizations" ("deleted_by","deleted_at");- CREATE UNIQUE INDEX "tags_organization_id_name_unique" ON "tags" ("organization_id","name");+ CREATE UNIQUE INDEX tags_organization_id_normalized_name_unique ON tags (organization_id, normalized_name);CREATE INDEX "user_roles_role_index" ON "user_roles" ("role");CREATE UNIQUE INDEX "user_roles_user_id_role_unique_index" ON "user_roles" ("user_id","role");CREATE INDEX "users_email_index" ON "users" ("email");@@ -117,7 +117,7 @@ describe('migrations registry', () => {CREATE TABLE "tagging_rule_actions" ( "id" text PRIMARY KEY NOT NULL, "created_at" integer NOT NULL, "updated_at" integer NOT NULL, "tagging_rule_id" text NOT NULL, "tag_id" text NOT NULL, FOREIGN KEY ("tagging_rule_id") REFERENCES "tagging_rules"("id") ON UPDATE cascade ON DELETE cascade, FOREIGN KEY ("tag_id") REFERENCES "tags"("id") ON UPDATE cascade ON DELETE cascade );CREATE TABLE "tagging_rule_conditions" ( "id" text PRIMARY KEY NOT NULL, "created_at" integer NOT NULL, "updated_at" integer NOT NULL, "tagging_rule_id" text NOT NULL, "field" text NOT NULL, "operator" text NOT NULL, "value" text NOT NULL, "is_case_sensitive" integer DEFAULT false NOT NULL, FOREIGN KEY ("tagging_rule_id") REFERENCES "tagging_rules"("id") ON UPDATE cascade ON DELETE cascade );CREATE TABLE "tagging_rules" ( "id" text PRIMARY KEY NOT NULL, "created_at" integer NOT NULL, "updated_at" integer NOT NULL, "organization_id" text NOT NULL, "name" text NOT NULL, "description" text, "enabled" integer DEFAULT true NOT NULL, "condition_match_mode" text DEFAULT 'all' NOT NULL, FOREIGN KEY ("organization_id") REFERENCES "organizations"("id") ON UPDATE cascade ON DELETE cascade );- CREATE TABLE "tags" ( "id" text PRIMARY KEY NOT NULL, "created_at" integer NOT NULL, "updated_at" integer NOT NULL, "organization_id" text NOT NULL, "name" text NOT NULL, "color" text NOT NULL, "description" text, FOREIGN KEY ("organization_id") REFERENCES "organizations"("id") ON UPDATE cascade ON DELETE cascade );+ CREATE TABLE "tags" ( "id" text PRIMARY KEY NOT NULL, "created_at" integer NOT NULL, "updated_at" integer NOT NULL, "organization_id" text NOT NULL, "name" text NOT NULL, "color" text NOT NULL, "description" text, "normalized_name" text, FOREIGN KEY ("organization_id") REFERENCES "organizations"("id") ON UPDATE cascade ON DELETE cascade );CREATE TABLE "user_roles" ( "id" text PRIMARY KEY NOT NULL, "created_at" integer NOT NULL, "updated_at" integer NOT NULL, "user_id" text NOT NULL, "role" text NOT NULL, FOREIGN KEY ("user_id") REFERENCES "users"("id") ON UPDATE cascade ON DELETE cascade );CREATE TABLE "users" ( "id" text PRIMARY KEY NOT NULL, "created_at" integer NOT NULL, "updated_at" integer NOT NULL, "email" text NOT NULL, "email_verified" integer DEFAULT false NOT NULL, "name" text, "image" text, "max_organization_count" integer , "two_factor_enabled" integer DEFAULT false NOT NULL);CREATE TABLE "webhook_deliveries" ( "id" text PRIMARY KEY NOT NULL, "created_at" integer NOT NULL, "updated_at" integer NOT NULL, "webhook_id" text NOT NULL, "event_name" text NOT NULL, "request_payload" text NOT NULL, "response_payload" text NOT NULL, "response_status" integer NOT NULL, FOREIGN KEY ("webhook_id") REFERENCES "webhooks"("id") ON UPDATE cascade ON DELETE cascade );diff --git a/apps/papra-server/src/migrations/migrations.registry.ts b/apps/papra-server/src/migrations/migrations.registry.tsindex 342d9c31..42c5d49b 100644--- a/apps/papra-server/src/migrations/migrations.registry.ts+++ b/apps/papra-server/src/migrations/migrations.registry.ts@@ -15,6 +15,7 @@ import { taggingRuleConditionMatchModeMigration } from './list/0012-tagging-ruleimport { dropFts5TriggersMigration } from './list/0013-drop-fts-5-triggers.migration';import { twoFactorAuthenticationMigration } from './list/0014-two-factor-authentication.migration';import { indexDocumentsFtsIdsMigration } from './list/0015-index-documents-fts-ids.migration';+import { caseInsensitiveTagNameUniqConstraintMigration } from './list/0016-case-insensitive-tag-name-uniq-constraint.migration';export const migrations: Migration[] = [initialSchemaSetupMigration,@@ -32,4 +33,5 @@ export const migrations: Migration[] = [dropFts5TriggersMigration,twoFactorAuthenticationMigration,indexDocumentsFtsIdsMigration,+ caseInsensitiveTagNameUniqConstraintMigration,];diff --git a/apps/papra-server/src/modules/documents/document-search/database-fts5/database-fts5.repository.test.ts b/apps/papra-server/src/modules/documents/document-search/database-fts5/database-fts5.repository.test.tsindex 09479e4f..57e9f29a 100644--- a/apps/papra-server/src/modules/documents/document-search/database-fts5/database-fts5.repository.test.ts+++ b/apps/papra-server/src/modules/documents/document-search/database-fts5/database-fts5.repository.test.ts@@ -137,8 +137,8 @@ describe('database-fts5 repository', () => {organizations: [{ id: 'org_1', name: 'Organization 1' }],documents,tags: [- { id: 'tag_1', organizationId: 'org_1', name: 'tag1', color: '#ff0000' },- { id: 'tag_2', organizationId: 'org_1', name: 'tag2', color: '#00ff00' },+ { id: 'tag_1', organizationId: 'org_1', name: 'tag1', normalizedName: 'tag1', color: '#ff0000' },+ { id: 'tag_2', organizationId: 'org_1', name: 'tag2', normalizedName: 'tag2', color: '#00ff00' },],documentsTags: [{ documentId: 'doc_1', tagId: 'tag_1' },@@ -231,8 +231,8 @@ describe('database-fts5 repository', () => {organizations: [{ id: 'org_1', name: 'Organization 1' }],documents,tags: [- { id: 'tag_1', organizationId: 'org_1', name: 'tag1', color: '#ff0000' },- { id: 'tag_2', organizationId: 'org_1', name: 'tag2', color: '#00ff00' },+ { id: 'tag_1', organizationId: 'org_1', name: 'tag1', normalizedName: 'tag1', color: '#ff0000' },+ { id: 'tag_2', organizationId: 'org_1', name: 'tag2', normalizedName: 'tag2', color: '#00ff00' },],documentsTags: [{ documentId: 'doc_1', tagId: 'tag_1' },@@ -269,8 +269,8 @@ describe('database-fts5 repository', () => {const { db } = await createInMemoryDatabase({organizations: [{ id: 'org_1', name: 'Organization 1' }],tags: [- { name: 'invoice', organizationId: 'org_1', id: 'tag_1', color: '#ff0000' },- { name: 'car', organizationId: 'org_1', id: 'tag_2', color: '#00ff00' },+ { name: 'invoice', organizationId: 'org_1', id: 'tag_1', normalizedName: 'invoice', color: '#ff0000' },+ { name: 'car', organizationId: 'org_1', id: 'tag_2', normalizedName: 'car', color: '#00ff00' },],documents,documentsTags: [@@ -459,8 +459,8 @@ describe('database-fts5 repository', () => {organizations: [{ id: 'org_1', name: 'Organization 1' }],documents,tags: [- { id: 'tag_1', organizationId: 'org_1', name: 'tag1', color: '#ff0000' },- { id: 'tag_2', organizationId: 'org_1', name: 'tag2', color: '#00ff00' },+ { id: 'tag_1', organizationId: 'org_1', name: 'tag1', normalizedName: 'tag1', color: '#ff0000' },+ { id: 'tag_2', organizationId: 'org_1', name: 'tag2', normalizedName: 'tag2', color: '#00ff00' },],documentsTags: [{ documentId: 'doc_1', tagId: 'tag_1' },diff --git a/apps/papra-server/src/modules/documents/documents.usecases.test.ts b/apps/papra-server/src/modules/documents/documents.usecases.test.tsindex 1abf0fb4..fe1a9d2e 100644--- a/apps/papra-server/src/modules/documents/documents.usecases.test.ts+++ b/apps/papra-server/src/modules/documents/documents.usecases.test.ts@@ -170,8 +170,8 @@ describe('documents usecases', () => {organizations: [{ id: 'organization-1', name: 'Organization 1' }],organizationMembers: [{ organizationId: 'organization-1', userId: 'user-1', role: ORGANIZATION_ROLES.OWNER }],tags: [- { id: 'tag-1', name: 'Tag 1', color: '#000000', organizationId: 'organization-1' },- { id: 'tag-2', name: 'Tag 2', color: '#000000', organizationId: 'organization-1' },+ { id: 'tag-1', name: 'Tag 1', normalizedName: 'tag 1', color: '#000000', organizationId: 'organization-1' },+ { id: 'tag-2', name: 'Tag 2', normalizedName: 'tag 2', color: '#000000', organizationId: 'organization-1' },],documents: [{id: 'document-1',diff --git a/apps/papra-server/src/modules/tagging-rules/tagging-rules.repository.test.ts b/apps/papra-server/src/modules/tagging-rules/tagging-rules.repository.test.tsindex d898a0cb..3bc34069 100644--- a/apps/papra-server/src/modules/tagging-rules/tagging-rules.repository.test.ts+++ b/apps/papra-server/src/modules/tagging-rules/tagging-rules.repository.test.ts@@ -8,8 +8,8 @@ describe('tagging-rules repository', () => {const { db } = await createInMemoryDatabase({organizations: [{ id: 'org_1', name: 'Org 1' }],tags: [- { id: 'tag_1', name: 'Tag 1', color: '#000000', organizationId: 'org_1', createdAt: new Date('2023-01-01'), updatedAt: new Date('2023-02-01') },- { id: 'tag_2', name: 'Tag 2', color: '#111111', organizationId: 'org_1', createdAt: new Date('2023-01-02'), updatedAt: new Date('2023-02-02') },+ { id: 'tag_1', name: 'Tag 1', normalizedName: 'tag 1', color: '#000000', organizationId: 'org_1', createdAt: new Date('2023-01-01'), updatedAt: new Date('2023-02-01') },+ { id: 'tag_2', name: 'Tag 2', normalizedName: 'tag 2', color: '#111111', organizationId: 'org_1', createdAt: new Date('2023-01-02'), updatedAt: new Date('2023-02-02') },],taggingRules: [{ id: 'tr_1', organizationId: 'org_1', name: 'Enabled Rule 1', enabled: true, createdAt: new Date('2023-03-01'), updatedAt: new Date('2023-04-01') },@@ -48,6 +48,7 @@ describe('tagging-rules repository', () => {tag: {id: 'tag_1',name: 'Tag 1',+ normalizedName: 'tag 1',color: '#000000',description: null,organizationId: 'org_1',@@ -64,6 +65,7 @@ describe('tagging-rules repository', () => {tag: {id: 'tag_2',name: 'Tag 2',+ normalizedName: 'tag 2',color: '#111111',description: null,organizationId: 'org_1',@@ -102,7 +104,7 @@ describe('tagging-rules repository', () => {test('disabled tagging rules are excluded from the results', async () => {const { db } = await createInMemoryDatabase({organizations: [{ id: 'org_1', name: 'Org 1' }],- tags: [{ id: 'tag_1', name: 'Tag 1', color: '#000000', organizationId: 'org_1' }],+ tags: [{ id: 'tag_1', name: 'Tag 1', normalizedName: 'tag 1', color: '#000000', organizationId: 'org_1' }],taggingRules: [{ id: 'tr_1', organizationId: 'org_1', name: 'Enabled Rule', enabled: true },{ id: 'tr_2', organizationId: 'org_1', name: 'Disabled Rule', enabled: false },@@ -133,8 +135,8 @@ describe('tagging-rules repository', () => {{ id: 'org_2', name: 'Org 2' },],tags: [- { id: 'tag_1', name: 'Tag 1', color: '#000000', organizationId: 'org_1' },- { id: 'tag_2', name: 'Tag 2', color: '#111111', organizationId: 'org_2' },+ { id: 'tag_1', name: 'Tag 1', normalizedName: 'tag 1', color: '#000000', organizationId: 'org_1' },+ { id: 'tag_2', name: 'Tag 2', normalizedName: 'tag 2', color: '#111111', organizationId: 'org_2' },],taggingRules: [{ id: 'tr_1', organizationId: 'org_1', name: 'Rule Org 1', enabled: true },@@ -158,7 +160,7 @@ describe('tagging-rules repository', () => {test('organizations with only disabled rules get an empty result', async () => {const { db } = await createInMemoryDatabase({organizations: [{ id: 'org_1', name: 'Org 1' }],- tags: [{ id: 'tag_1', name: 'Tag 1', color: '#000000', organizationId: 'org_1' }],+ tags: [{ id: 'tag_1', name: 'Tag 1', normalizedName: 'tag 1', color: '#000000', organizationId: 'org_1' }],taggingRules: [{ id: 'tr_1', organizationId: 'org_1', name: 'Disabled Rule', enabled: false },],@@ -189,7 +191,7 @@ describe('tagging-rules repository', () => {test('tagging rules without conditions are properly retrieved with empty conditions array', async () => {const { db } = await createInMemoryDatabase({organizations: [{ id: 'org_1', name: 'Org 1' }],- tags: [{ id: 'tag_1', name: 'Tag 1', color: '#000000', organizationId: 'org_1' }],+ tags: [{ id: 'tag_1', name: 'Tag 1', normalizedName: 'tag 1', color: '#000000', organizationId: 'org_1' }],taggingRules: [{ id: 'tr_1', organizationId: 'org_1', name: 'Rule Without Conditions', enabled: true },],@@ -211,8 +213,8 @@ describe('tagging-rules repository', () => {const { db } = await createInMemoryDatabase({organizations: [{ id: 'org_1', name: 'Org 1' }],tags: [- { id: 'tag_1', name: 'Tag 1', color: '#000000', organizationId: 'org_1' },- { id: 'tag_2', name: 'Tag 2', color: '#111111', organizationId: 'org_1' },+ { id: 'tag_1', name: 'Tag 1', normalizedName: 'tag 1', color: '#000000', organizationId: 'org_1' },+ { id: 'tag_2', name: 'Tag 2', normalizedName: 'tag 2', color: '#111111', organizationId: 'org_1' },],taggingRules: [{ id: 'tr_1', organizationId: 'org_1', name: 'Rule 1', enabled: true },diff --git a/apps/papra-server/src/modules/tagging-rules/tagging-rules.usecases.test.ts b/apps/papra-server/src/modules/tagging-rules/tagging-rules.usecases.test.tsindex 5549a9f9..f1d0dae9 100644--- a/apps/papra-server/src/modules/tagging-rules/tagging-rules.usecases.test.ts+++ b/apps/papra-server/src/modules/tagging-rules/tagging-rules.usecases.test.ts@@ -19,7 +19,7 @@ describe('tagging-rules usecases', () => {const { db } = await createInMemoryDatabase({organizations: [{ id: 'org_1', name: 'Org 1' }],- tags: [{ id: 'tag_1', name: 'Tag 1', color: '#000000', organizationId: 'org_1' }],+ tags: [{ id: 'tag_1', name: 'Tag 1', normalizedName: 'tag 1', color: '#000000', organizationId: 'org_1' }],documents: [{ id: 'doc_1', organizationId: 'org_1', name: 'Doc 1', originalName: 'Doc 1', originalStorageKey: 'doc_1', originalSha256Hash: 'doc_1', mimeType: 'text/plain' }],taggingRules: [{ id: 'tr_1', organizationId: 'org_1', name: 'Tagging Rule 1' }],@@ -73,7 +73,7 @@ describe('tagging-rules usecases', () => {const { db } = await createInMemoryDatabase({organizations: [{ id: 'org_1', name: 'Org 1' }],documents: [{ id: 'doc_1', organizationId: 'org_1', name: 'Doc 1', originalName: 'Doc 1', originalStorageKey: 'doc_1', originalSha256Hash: 'doc_1', mimeType: 'text/plain' }],- tags: [{ id: 'tag_1', name: 'Tag 1', color: '#000000', organizationId: 'org_1' }],+ tags: [{ id: 'tag_1', name: 'Tag 1', normalizedName: 'tag 1', color: '#000000', organizationId: 'org_1' }],taggingRules: [{ id: 'tr_1', organizationId: 'org_1', name: 'Tagging Rule 1' }],taggingRuleActions: [{ id: 'tra_1', taggingRuleId: 'tr_1', tagId: 'tag_1' }],});@@ -101,7 +101,7 @@ describe('tagging-rules usecases', () => {const { db } = await createInMemoryDatabase({organizations: [{ id: 'org_1', name: 'Org 1' }],documents: [{ id: 'doc_1', organizationId: 'org_1', name: 'Doc 1', originalName: 'Doc 1', originalStorageKey: 'doc_1', originalSha256Hash: 'doc_1', mimeType: 'text/plain' }],- tags: [{ id: 'tag_1', name: 'Tag 1', color: '#000000', organizationId: 'org_1' }],+ tags: [{ id: 'tag_1', name: 'Tag 1', normalizedName: 'tag 1', color: '#000000', organizationId: 'org_1' }],taggingRules: [{ id: 'tr_1', organizationId: 'org_1', name: 'Tagging Rule 1', conditionMatchMode: 'any' }],taggingRuleActions: [{ id: 'tra_1', taggingRuleId: 'tr_1', tagId: 'tag_1' }],});@@ -153,7 +153,7 @@ describe('tagging-rules usecases', () => {test('when conditionMatchMode is "any", tags are applied when at least one condition matches', async () => {const { db } = await createInMemoryDatabase({organizations: [{ id: 'org_1', name: 'Org 1' }],- tags: [{ id: 'tag_1', name: 'Tag 1', color: '#000000', organizationId: 'org_1' }],+ tags: [{ id: 'tag_1', name: 'Tag 1', normalizedName: 'tag 1', color: '#000000', organizationId: 'org_1' }],documents: [{ id: 'doc_1', organizationId: 'org_1', name: 'Invoice 2024', originalName: 'Invoice 2024', originalStorageKey: 'doc_1', originalSha256Hash: 'doc_1', mimeType: 'text/plain' }],taggingRules: [{ id: 'tr_1', organizationId: 'org_1', name: 'Tagging Rule 1', conditionMatchMode: 'any' }],@@ -186,7 +186,7 @@ describe('tagging-rules usecases', () => {test('when conditionMatchMode is "any", tags are not applied when no conditions match', async () => {const { db } = await createInMemoryDatabase({organizations: [{ id: 'org_1', name: 'Org 1' }],- tags: [{ id: 'tag_1', name: 'Tag 1', color: '#000000', organizationId: 'org_1' }],+ tags: [{ id: 'tag_1', name: 'Tag 1', normalizedName: 'tag 1', color: '#000000', organizationId: 'org_1' }],documents: [{ id: 'doc_1', organizationId: 'org_1', name: 'Contract 2024', originalName: 'Contract 2024', originalStorageKey: 'doc_1', originalSha256Hash: 'doc_1', mimeType: 'text/plain' }],taggingRules: [{ id: 'tr_1', organizationId: 'org_1', name: 'Tagging Rule 1', conditionMatchMode: 'any' }],@@ -219,7 +219,7 @@ describe('tagging-rules usecases', () => {test('when conditionMatchMode is "all" (default), tags are applied only when all conditions match', async () => {const { db } = await createInMemoryDatabase({organizations: [{ id: 'org_1', name: 'Org 1' }],- tags: [{ id: 'tag_1', name: 'Tag 1', color: '#000000', organizationId: 'org_1' }],+ tags: [{ id: 'tag_1', name: 'Tag 1', normalizedName: 'tag 1', color: '#000000', organizationId: 'org_1' }],documents: [{ id: 'doc_1', organizationId: 'org_1', name: 'Invoice 2024', originalName: 'Invoice 2024', originalStorageKey: 'doc_1', originalSha256Hash: 'doc_1', mimeType: 'text/plain' }],taggingRules: [{ id: 'tr_1', organizationId: 'org_1', name: 'Tagging Rule 1', conditionMatchMode: 'all' }],@@ -252,7 +252,7 @@ describe('tagging-rules usecases', () => {test('when conditionMatchMode is "all" (default), tags are not applied when only some conditions match', async () => {const { db } = await createInMemoryDatabase({organizations: [{ id: 'org_1', name: 'Org 1' }],- tags: [{ id: 'tag_1', name: 'Tag 1', color: '#000000', organizationId: 'org_1' }],+ tags: [{ id: 'tag_1', name: 'Tag 1', normalizedName: 'tag 1', color: '#000000', organizationId: 'org_1' }],documents: [{ id: 'doc_1', organizationId: 'org_1', name: 'Invoice 2024', originalName: 'Invoice 2024', originalStorageKey: 'doc_1', originalSha256Hash: 'doc_1', mimeType: 'text/plain' }],taggingRules: [{ id: 'tr_1', organizationId: 'org_1', name: 'Tagging Rule 1', conditionMatchMode: 'all' }],@@ -287,7 +287,7 @@ describe('tagging-rules usecases', () => {test('applying rule to existing documents tags only matching ones', async () => {const { db } = await createInMemoryDatabase({organizations: [{ id: 'org_1', name: 'Org 1' }],- tags: [{ id: 'tag_1', name: 'Invoice', color: '#000000', organizationId: 'org_1' }],+ tags: [{ id: 'tag_1', name: 'Invoice', normalizedName: 'invoice', color: '#000000', organizationId: 'org_1' }],documents: [{ id: 'doc_1', organizationId: 'org_1', name: 'Invoice 2024', originalName: 'Invoice 2024', originalStorageKey: 'doc_1', originalSha256Hash: 'hash_1', mimeType: 'text/plain' },{ id: 'doc_2', organizationId: 'org_1', name: 'Invoice Q1', originalName: 'Invoice Q1', originalStorageKey: 'doc_2', originalSha256Hash: 'hash_2', mimeType: 'text/plain' },diff --git a/apps/papra-server/src/modules/tags/tags.repository.models.test.ts b/apps/papra-server/src/modules/tags/tags.repository.models.test.tsnew file mode 100644index 00000000..4b1d3b59--- /dev/null+++ b/apps/papra-server/src/modules/tags/tags.repository.models.test.ts@@ -0,0 +1,20 @@+import { describe, expect, test } from 'vitest';+import { normalizeTagName } from './tags.repository.models';++describe('tags repository models', () => {+ describe('normalizeTagName', () => {+ test('for unique constraint, the tag name is normalized by lowercasing it, keeping spaces', () => {+ expect(normalizeTagName({ name: 'todo' })).to.eql('todo');+ expect(normalizeTagName({ name: 'ToDo' })).to.eql('todo');+ expect(normalizeTagName({ name: ' Important ' })).to.eql(' important ');+ expect(normalizeTagName({ name: ' Urgent ' })).to.eql(' urgent ');+ });++ test('special characters are preserved during normalization but case is lowered', () => {+ expect(normalizeTagName({ name: 'Naïve café' })).to.eql('naïve café');+ expect(normalizeTagName({ name: 'École' })).to.eql('école');+ expect(normalizeTagName({ name: 'Привет' })).to.eql('привет');+ expect(normalizeTagName({ name: 'こんにちは' })).to.eql('こんにちは');+ });+ });+});diff --git a/apps/papra-server/src/modules/tags/tags.repository.models.ts b/apps/papra-server/src/modules/tags/tags.repository.models.tsnew file mode 100644index 00000000..ae652017--- /dev/null+++ b/apps/papra-server/src/modules/tags/tags.repository.models.ts@@ -0,0 +1,3 @@+export function normalizeTagName({ name }: { name: string }): string {+ return name.toLowerCase();+}diff --git a/apps/papra-server/src/modules/tags/tags.repository.test.ts b/apps/papra-server/src/modules/tags/tags.repository.test.tsindex b1f345be..2402d336 100644--- a/apps/papra-server/src/modules/tags/tags.repository.test.ts+++ b/apps/papra-server/src/modules/tags/tags.repository.test.ts@@ -81,7 +81,7 @@ describe('tags repository', () => {documents: [{ id: 'document-1', organizationId: 'organization-1', createdBy: 'user-1', name: 'Document 1', originalName: 'document-1.pdf', content: 'lorem ipsum', originalStorageKey: '', mimeType: 'application/pdf', originalSha256Hash: 'hash' },],- tags: [{ id: 'tag-1', organizationId: 'organization-1', name: 'Tag 1', color: '#aa0000' }],+ tags: [{ id: 'tag-1', organizationId: 'organization-1', name: 'Tag 1', normalizedName: 'tag 1', color: '#aa0000' }],});const tagsRepository = createTagsRepository({ db });@@ -108,9 +108,9 @@ describe('tags repository', () => {{ id: 'document-2', organizationId: 'organization-1', createdBy: 'user-1', name: 'Document 2', originalName: 'document-2.pdf', content: 'lorem ipsum', originalStorageKey: '', mimeType: 'application/pdf', originalSha256Hash: 'hash-2', isDeleted: true },],tags: [- { id: 'tag-1', organizationId: 'organization-1', name: 'Tag 1', color: '#aa0000', createdAt: new Date('2021-01-01'), updatedAt: new Date('2021-01-01') },- { id: 'tag-2', organizationId: 'organization-1', name: 'Tag 2', color: '#00aa00', createdAt: new Date('2021-01-02'), updatedAt: new Date('2021-01-02') },- { id: 'tag-3', organizationId: 'organization-1', name: 'Tag 3', color: '#0000aa', createdAt: new Date('2021-01-03'), updatedAt: new Date('2021-01-03') },+ { id: 'tag-1', organizationId: 'organization-1', name: 'Tag 1', normalizedName: 'tag 1', color: '#aa0000', createdAt: new Date('2021-01-01'), updatedAt: new Date('2021-01-01') },+ { id: 'tag-2', organizationId: 'organization-1', name: 'Tag 2', normalizedName: 'tag 2', color: '#00aa00', createdAt: new Date('2021-01-02'), updatedAt: new Date('2021-01-02') },+ { id: 'tag-3', organizationId: 'organization-1', name: 'Tag 3', normalizedName: 'tag 3', color: '#0000aa', createdAt: new Date('2021-01-03'), updatedAt: new Date('2021-01-03') },],documentsTags: [{ documentId: 'document-1', tagId: 'tag-1' },@@ -129,6 +129,7 @@ describe('tags repository', () => {id: 'tag-3',organizationId: 'organization-1',name: 'Tag 3',+ normalizedName: 'tag 3',description: null,color: '#0000aa',documentsCount: 0,@@ -139,6 +140,7 @@ describe('tags repository', () => {id: 'tag-2',organizationId: 'organization-1',name: 'Tag 2',+ normalizedName: 'tag 2',description: null,color: '#00aa00',documentsCount: 1,@@ -149,6 +151,7 @@ describe('tags repository', () => {id: 'tag-1',organizationId: 'organization-1',name: 'Tag 1',+ normalizedName: 'tag 1',description: null,color: '#aa0000',documentsCount: 1,@@ -166,7 +169,7 @@ describe('tags repository', () => {documents: [{ id: 'document-1', organizationId: 'organization-1', createdBy: 'user-1', name: 'Document 1', originalName: 'document-1.pdf', content: 'lorem ipsum', originalStorageKey: '', mimeType: 'application/pdf', originalSha256Hash: 'hash-1', isDeleted: true },],- tags: [{ id: 'tag-1', organizationId: 'organization-1', name: 'Tag 1', color: '#aa0000' }],+ tags: [{ id: 'tag-1', organizationId: 'organization-1', name: 'Tag 1', normalizedName: 'tag 1', color: '#aa0000' }],documentsTags: [{ documentId: 'document-1', tagId: 'tag-1' }],});diff --git a/apps/papra-server/src/modules/tags/tags.repository.ts b/apps/papra-server/src/modules/tags/tags.repository.tsindex ed38b466..6eb441fa 100644--- a/apps/papra-server/src/modules/tags/tags.repository.ts+++ b/apps/papra-server/src/modules/tags/tags.repository.ts@@ -1,12 +1,12 @@import type { Database } from '../app/database/database.types';-import type { DbInsertableTag } from './tags.types';import { injectArguments, safely } from '@corentinth/chisels';import { and, desc, eq, getTableColumns, sql } from 'drizzle-orm';import { get } from 'lodash-es';import { documentsTable } from '../documents/documents.table';import { isUniqueConstraintError } from '../shared/db/constraints.models';-import { omitUndefined } from '../shared/utils';+import { isDefined, omitUndefined } from '../shared/utils';import { createDocumentAlreadyHasTagError, createTagAlreadyExistsError } from './tags.errors';+import { normalizeTagName } from './tags.repository.models';import { documentsTagsTable, tagsTable } from './tags.table';export type TagsRepository = ReturnType<typeof createTagsRepository>;@@ -58,8 +58,16 @@ async function getTagById({ tagId, organizationId, db }: { tagId: string; organireturn { tag };}-async function createTag({ tag, db }: { tag: DbInsertableTag; db: Database }) {- const [result, error] = await safely(db.insert(tagsTable).values(tag).returning());+async function createTag({ tag, db }: { tag: { name: string; description?: string | null; color: string; organizationId: string }; db: Database }) {+ const [result, error] = await safely(+ db+ .insert(tagsTable)+ .values({+ ...tag,+ normalizedName: normalizeTagName({ name: tag.name }),+ })+ .returning(),+ );if (isUniqueConstraintError({ error })) {throw createTagAlreadyExistsError();@@ -86,6 +94,7 @@ async function updateTag({ tagId, name, description, color, db }: { tagId: strinname,description,color,+ normalizedName: isDefined(name) ? normalizeTagName({ name }) : undefined,}),).where(diff --git a/apps/papra-server/src/modules/tags/tags.table.ts b/apps/papra-server/src/modules/tags/tags.table.tsindex 836c75bd..81fcd5e5 100644--- a/apps/papra-server/src/modules/tags/tags.table.ts+++ b/apps/papra-server/src/modules/tags/tags.table.ts@@ -12,12 +12,13 @@ export const tagsTable = sqliteTable(organizationId: text('organization_id').notNull().references(() => organizationsTable.id, { onDelete: 'cascade', onUpdate: 'cascade' }),name: text('name').notNull(),+ normalizedName: text('normalized_name'),color: text('color').notNull(),description: text('description'),},table => [// To ensure that tags are unique per organization- unique('tags_organization_id_name_unique').on(table.organizationId, table.name),+ unique('tags_organization_id_normalized_name_unique').on(table.organizationId, table.normalizedName),],);