The minimalistic document archiving platform.
refactor(db): remove explicit index definitions (#1327)
Corentin Thomasset360aa7158ff7
diff --git a/apps/papra-server/src/migrations/migration.tables.ts b/apps/papra-server/src/migrations/migration.tables.tsindex b006cedd..768ae178 100644--- a/apps/papra-server/src/migrations/migration.tables.ts+++ b/apps/papra-server/src/migrations/migration.tables.ts@@ -1,13 +1,9 @@-import { index, integer, sqliteTable, text } from 'drizzle-orm/sqlite-core';+import { integer, sqliteTable, text } from 'drizzle-orm/sqlite-core';-export const migrationsTable = sqliteTable(- 'migrations',- {- id: integer('id').primaryKey({ autoIncrement: true }),- name: text('name').notNull(),- runAt: integer('run_at', { mode: 'timestamp_ms' })- .notNull()- .$default(() => new Date()),- },- (t) => [index('name_index').on(t.name), index('run_at_index').on(t.runAt)],-);+export const migrationsTable = sqliteTable('migrations', {+ id: integer('id').primaryKey({ autoIncrement: true }),+ name: text('name').notNull(),+ runAt: integer('run_at', { mode: 'timestamp_ms' })+ .notNull()+ .$default(() => new Date()),+});diff --git a/apps/papra-server/src/modules/api-keys/api-keys.tables.ts b/apps/papra-server/src/modules/api-keys/api-keys.tables.tsindex a7eb852b..01678142 100644--- a/apps/papra-server/src/modules/api-keys/api-keys.tables.ts+++ b/apps/papra-server/src/modules/api-keys/api-keys.tables.ts@@ -1,37 +1,30 @@import type { ApiKeyPermissions } from './api-keys.types';-import { index, integer, sqliteTable, text } from 'drizzle-orm/sqlite-core';+import { integer, sqliteTable, text } from 'drizzle-orm/sqlite-core';import { organizationMembersTable } from '../organizations/organizations.table';import { createPrimaryKeyField, createTimestampColumns } from '../shared/db/columns.helpers';import { usersTable } from '../users/users.table';import { API_KEY_ID_PREFIX } from './api-keys.constants';-export const apiKeysTable = sqliteTable(- 'api_keys',- {- ...createPrimaryKeyField({ prefix: API_KEY_ID_PREFIX }),- ...createTimestampColumns(),+export const apiKeysTable = sqliteTable('api_keys', {+ ...createPrimaryKeyField({ prefix: API_KEY_ID_PREFIX }),+ ...createTimestampColumns(),- name: text('name').notNull(),- keyHash: text('key_hash').notNull().unique(),- // the prefix is used to identify the key, it is the- prefix: text('prefix').notNull(),- userId: text('user_id')- .notNull()- .references(() => usersTable.id, { onDelete: 'cascade', onUpdate: 'cascade' }),- lastUsedAt: integer('last_used_at', { mode: 'timestamp_ms' }),- expiresAt: integer('expires_at', { mode: 'timestamp_ms' }),- permissions: text('permissions', { mode: 'json' })- .notNull()- .$type<ApiKeyPermissions[]>()- .default([]),- allOrganizations: integer('all_organizations', { mode: 'boolean' }).notNull().default(false),- },- (table) => [- // To get an API key by its token- index('key_hash_index').on(table.keyHash),- ],-);+ name: text('name').notNull(),+ keyHash: text('key_hash').notNull().unique(),+ // the prefix is used to identify the key, it is the+ prefix: text('prefix').notNull(),+ userId: text('user_id')+ .notNull()+ .references(() => usersTable.id, { onDelete: 'cascade', onUpdate: 'cascade' }),+ lastUsedAt: integer('last_used_at', { mode: 'timestamp_ms' }),+ expiresAt: integer('expires_at', { mode: 'timestamp_ms' }),+ permissions: text('permissions', { mode: 'json' })+ .notNull()+ .$type<ApiKeyPermissions[]>()+ .default([]),+ allOrganizations: integer('all_organizations', { mode: 'boolean' }).notNull().default(false),+});// We use an intermediate table (instead of a json array) to link API keys to organization members, so the relationship between// the api key and the organization is deleted on cascade when the organization is deleted or the member is removed from the organization.diff --git a/apps/papra-server/src/modules/app/auth/auth.tables.ts b/apps/papra-server/src/modules/app/auth/auth.tables.tsindex 927f2d1c..cadfb800 100644--- a/apps/papra-server/src/modules/app/auth/auth.tables.ts+++ b/apps/papra-server/src/modules/app/auth/auth.tables.ts@@ -1,32 +1,25 @@-import { index, integer, sqliteTable, text } from 'drizzle-orm/sqlite-core';+import { integer, sqliteTable, text } from 'drizzle-orm/sqlite-core';import { organizationsTable } from '../../organizations/organizations.table';import { createPrimaryKeyField, createTimestampColumns } from '../../shared/db/columns.helpers';import { usersTable } from '../../users/users.table';-export const sessionsTable = sqliteTable(- 'auth_sessions',- {- ...createPrimaryKeyField({ prefix: 'auth_ses' }),- ...createTimestampColumns(),+export const sessionsTable = sqliteTable('auth_sessions', {+ ...createPrimaryKeyField({ prefix: 'auth_ses' }),+ ...createTimestampColumns(),- token: text('token').notNull(),- userId: text('user_id').references(() => usersTable.id, {- onDelete: 'cascade',- onUpdate: 'cascade',- }),- expiresAt: integer('expires_at', { mode: 'timestamp_ms' }).notNull(),- ipAddress: text('ip_address'),- userAgent: text('user_agent'),- activeOrganizationId: text('active_organization_id').references(() => organizationsTable.id, {- onDelete: 'set null',- onUpdate: 'cascade',- }),- },- (table) => [- // To select sessions by token- index('auth_sessions_token_index').on(table.token),- ],-);+ token: text('token').notNull(),+ userId: text('user_id').references(() => usersTable.id, {+ onDelete: 'cascade',+ onUpdate: 'cascade',+ }),+ expiresAt: integer('expires_at', { mode: 'timestamp_ms' }).notNull(),+ ipAddress: text('ip_address'),+ userAgent: text('user_agent'),+ activeOrganizationId: text('active_organization_id').references(() => organizationsTable.id, {+ onDelete: 'set null',+ onUpdate: 'cascade',+ }),+});export const accountsTable = sqliteTable('auth_accounts', {...createPrimaryKeyField({ prefix: 'auth_acc' }),@@ -47,21 +40,14 @@ export const accountsTable = sqliteTable('auth_accounts', {password: text('password'),});-export const verificationsTable = sqliteTable(- 'auth_verifications',- {- ...createPrimaryKeyField({ prefix: 'auth_ver' }),- ...createTimestampColumns(),+export const verificationsTable = sqliteTable('auth_verifications', {+ ...createPrimaryKeyField({ prefix: 'auth_ver' }),+ ...createTimestampColumns(),- identifier: text('identifier').notNull(),- value: text('value').notNull(),- expiresAt: integer('expires_at', { mode: 'timestamp_ms' }).notNull(),- },- (table) => [- // To select verifications by identifier- index('auth_verifications_identifier_index').on(table.identifier),- ],-);+ identifier: text('identifier').notNull(),+ value: text('value').notNull(),+ expiresAt: integer('expires_at', { mode: 'timestamp_ms' }).notNull(),+});export const twoFactorTable = sqliteTable('auth_two_factor', {...createPrimaryKeyField({ prefix: 'auth_2fa' }),diff --git a/apps/papra-server/src/modules/document-share-links/document-share-links.table.ts b/apps/papra-server/src/modules/document-share-links/document-share-links.table.tsindex 9f03d810..61d9cd6a 100644--- a/apps/papra-server/src/modules/document-share-links/document-share-links.table.ts+++ b/apps/papra-server/src/modules/document-share-links/document-share-links.table.ts@@ -1,40 +1,28 @@-import { index, integer, sqliteTable, text, uniqueIndex } from 'drizzle-orm/sqlite-core';+import { integer, sqliteTable, text } from 'drizzle-orm/sqlite-core';import { documentsTable } from '../documents/documents.table';import { organizationsTable } from '../organizations/organizations.table';import { createPrimaryKeyField, createTimestampColumns } from '../shared/db/columns.helpers';import { usersTable } from '../users/users.table';import { generateShareLinkId } from './document-share-links.models';-export const documentShareLinksTable = sqliteTable(- 'document_share_links',- {- ...createPrimaryKeyField({ idGenerator: generateShareLinkId }),- ...createTimestampColumns(),+export const documentShareLinksTable = sqliteTable('document_share_links', {+ ...createPrimaryKeyField({ idGenerator: generateShareLinkId }),+ ...createTimestampColumns(),- organizationId: text('organization_id')- .notNull()- .references(() => organizationsTable.id, { onDelete: 'cascade', onUpdate: 'cascade' }),- documentId: text('document_id')- .notNull()- .references(() => documentsTable.id, { onDelete: 'cascade', onUpdate: 'cascade' }),- createdBy: text('created_by').references(() => usersTable.id, {- onDelete: 'set null',- onUpdate: 'cascade',- }),+ organizationId: text('organization_id')+ .notNull()+ .references(() => organizationsTable.id, { onDelete: 'cascade', onUpdate: 'cascade' }),+ documentId: text('document_id')+ .notNull()+ .references(() => documentsTable.id, { onDelete: 'cascade', onUpdate: 'cascade' }),+ createdBy: text('created_by').references(() => usersTable.id, {+ onDelete: 'set null',+ onUpdate: 'cascade',+ }),- token: text('token').notNull(),- expiresAt: integer('expires_at', { mode: 'timestamp_ms' }),- passwordHash: text('password_hash'),- isEnabled: integer('is_enabled', { mode: 'boolean' }).notNull().default(true),- lastAccessedAt: integer('last_accessed_at', { mode: 'timestamp_ms' }),- },- (table) => [- // Public lookup by token must be fast and tokens must be globally unique- uniqueIndex('document_share_links_token_unique').on(table.token),- // To list/manage share links for a given document within an organization- index('document_share_links_organization_id_document_id_index').on(- table.organizationId,- table.documentId,- ),- ],-);+ token: text('token').notNull(),+ expiresAt: integer('expires_at', { mode: 'timestamp_ms' }),+ passwordHash: text('password_hash'),+ isEnabled: integer('is_enabled', { mode: 'boolean' }).notNull().default(true),+ lastAccessedAt: integer('last_accessed_at', { mode: 'timestamp_ms' }),+});diff --git a/apps/papra-server/src/modules/document-views/document-views.table.ts b/apps/papra-server/src/modules/document-views/document-views.table.tsindex 3dc71d7b..f5f6b689 100644--- a/apps/papra-server/src/modules/document-views/document-views.table.ts+++ b/apps/papra-server/src/modules/document-views/document-views.table.ts@@ -1,22 +1,16 @@-import { sqliteTable, text, unique } from 'drizzle-orm/sqlite-core';+import { sqliteTable, text } from 'drizzle-orm/sqlite-core';import { organizationsTable } from '../organizations/organizations.table';import { createPrimaryKeyField, createTimestampColumns } from '../shared/db/columns.helpers';import { DOCUMENT_VIEW_ID_PREFIX } from './document-views.constants';-export const documentViewsTable = sqliteTable(- 'document_views',- {- ...createPrimaryKeyField({ prefix: DOCUMENT_VIEW_ID_PREFIX }),- ...createTimestampColumns(),+export const documentViewsTable = sqliteTable('document_views', {+ ...createPrimaryKeyField({ prefix: DOCUMENT_VIEW_ID_PREFIX }),+ ...createTimestampColumns(),- organizationId: text('organization_id')- .notNull()- .references(() => organizationsTable.id, { onDelete: 'cascade', onUpdate: 'cascade' }),- name: text('name').notNull(),- query: text('query').notNull(),- description: text('description'),- },- (table) => [- unique('document_views_organization_id_name_unique').on(table.organizationId, table.name),- ],-);+ organizationId: text('organization_id')+ .notNull()+ .references(() => organizationsTable.id, { onDelete: 'cascade', onUpdate: 'cascade' }),+ name: text('name').notNull(),+ query: text('query').notNull(),+ description: text('description'),+});diff --git a/apps/papra-server/src/modules/documents/documents.table.ts b/apps/papra-server/src/modules/documents/documents.table.tsindex da502cca..c72dc3b1 100644--- a/apps/papra-server/src/modules/documents/documents.table.ts+++ b/apps/papra-server/src/modules/documents/documents.table.ts@@ -1,64 +1,40 @@-import { index, integer, sqliteTable, text, uniqueIndex } from 'drizzle-orm/sqlite-core';+import { integer, sqliteTable, text } from 'drizzle-orm/sqlite-core';import { organizationsTable } from '../organizations/organizations.table';import { createPrimaryKeyField, createTimestampColumns } from '../shared/db/columns.helpers';import { usersTable } from '../users/users.table';import { generateDocumentId } from './documents.models';-export const documentsTable = sqliteTable(- 'documents',- {- ...createPrimaryKeyField({ idGenerator: generateDocumentId }),- ...createTimestampColumns(),+export const documentsTable = sqliteTable('documents', {+ ...createPrimaryKeyField({ idGenerator: generateDocumentId }),+ ...createTimestampColumns(),- organizationId: text('organization_id')- .notNull()- .references(() => organizationsTable.id, { onDelete: 'cascade', onUpdate: 'cascade' }),- createdBy: text('created_by').references(() => usersTable.id, {- onDelete: 'set null',- onUpdate: 'cascade',- }),+ organizationId: text('organization_id')+ .notNull()+ .references(() => organizationsTable.id, { onDelete: 'cascade', onUpdate: 'cascade' }),+ createdBy: text('created_by').references(() => usersTable.id, {+ onDelete: 'set null',+ onUpdate: 'cascade',+ }),- originalName: text('original_name').notNull(),- originalSize: integer('original_size').notNull().default(0),- originalStorageKey: text('original_storage_key').notNull(),- originalSha256Hash: text('original_sha256_hash').notNull(),+ originalName: text('original_name').notNull(),+ originalSize: integer('original_size').notNull().default(0),+ originalStorageKey: text('original_storage_key').notNull(),+ originalSha256Hash: text('original_sha256_hash').notNull(),- name: text('name').notNull(),- mimeType: text('mime_type').notNull(),- content: text('content').notNull().default(''),- documentDate: integer('document_date', { mode: 'timestamp_ms' }),- notes: text('notes'),+ name: text('name').notNull(),+ mimeType: text('mime_type').notNull(),+ content: text('content').notNull().default(''),+ documentDate: integer('document_date', { mode: 'timestamp_ms' }),+ notes: text('notes'),- fileEncryptionKeyWrapped: text('file_encryption_key_wrapped'), // The wrapped encryption key- fileEncryptionKekVersion: text('file_encryption_kek_version'), // The key encryption key version used to encrypt the file encryption key- fileEncryptionAlgorithm: text('file_encryption_algorithm'),+ fileEncryptionKeyWrapped: text('file_encryption_key_wrapped'), // The wrapped encryption key+ fileEncryptionKekVersion: text('file_encryption_kek_version'), // The key encryption key version used to encrypt the file encryption key+ fileEncryptionAlgorithm: text('file_encryption_algorithm'),- deletedAt: integer('deleted_at', { mode: 'timestamp_ms' }),- deletedBy: text('deleted_by').references(() => usersTable.id, {- onDelete: 'set null',- onUpdate: 'cascade',- }),- isDeleted: integer('is_deleted', { mode: 'boolean' }).notNull().default(false),- },- (table) => [- // To select paginated documents by organization- index('documents_organization_id_is_deleted_created_at_index').on(- table.organizationId,- table.isDeleted,- table.createdAt,- ),- // To count/stats documents by organization- index('documents_organization_id_is_deleted_index').on(table.organizationId, table.isDeleted),- // Unique document by organization and hash- uniqueIndex('documents_organization_id_original_sha256_hash_unique').on(- table.organizationId,- table.originalSha256Hash,- ),- // To verify if a document exists by hash- index('documents_original_sha256_hash_index').on(table.originalSha256Hash),- // To sum the size of documents by organization- index('documents_organization_id_size_index').on(table.organizationId, table.originalSize),- // To list documents by file encryption KEK version- index('documents_file_encryption_kek_version_index').on(table.fileEncryptionKekVersion),- ],-);+ deletedAt: integer('deleted_at', { mode: 'timestamp_ms' }),+ deletedBy: text('deleted_by').references(() => usersTable.id, {+ onDelete: 'set null',+ onUpdate: 'cascade',+ }),+ isDeleted: integer('is_deleted', { mode: 'boolean' }).notNull().default(false),+});diff --git a/apps/papra-server/src/modules/organizations/organizations.table.ts b/apps/papra-server/src/modules/organizations/organizations.table.tsindex a27097d5..6c797da9 100644--- a/apps/papra-server/src/modules/organizations/organizations.table.ts+++ b/apps/papra-server/src/modules/organizations/organizations.table.ts@@ -1,6 +1,6 @@import type { NonEmptyArray } from '../shared/types';import type { OrganizationInvitationStatus, OrganizationRole } from './organizations.types';-import { index, integer, sqliteTable, text, unique } from 'drizzle-orm/sqlite-core';+import { integer, sqliteTable, text } from 'drizzle-orm/sqlite-core';import { createPrimaryKeyField, createTimestampColumns } from '../shared/db/columns.helpers';import { usersTable } from '../users/users.table';import {@@ -12,74 +12,57 @@ import {} from './organizations.constants';import { generateOrganizationId } from './organizations.models';-export const organizationsTable = sqliteTable(- 'organizations',- {- ...createPrimaryKeyField({ idGenerator: generateOrganizationId }),- ...createTimestampColumns(),+export const organizationsTable = sqliteTable('organizations', {+ ...createPrimaryKeyField({ idGenerator: generateOrganizationId }),+ ...createTimestampColumns(),- name: text('name').notNull(),- customerId: text('customer_id'),+ name: text('name').notNull(),+ customerId: text('customer_id'),- deletedAt: integer('deleted_at', { mode: 'timestamp_ms' }),- deletedBy: text('deleted_by').references(() => usersTable.id, {- onDelete: 'set null',- onUpdate: 'cascade',- }),- // When the organization is soft-deleted, we schedule a purge date some days in the future for hard deletion- scheduledPurgeAt: integer('scheduled_purge_at', { mode: 'timestamp_ms' }),- },- (t) => [- // For a user to list their deleted organizations for possible restoration- index('organizations_deleted_by_deleted_at_index').on(t.deletedBy, t.deletedAt),- ],-);+ deletedAt: integer('deleted_at', { mode: 'timestamp_ms' }),+ deletedBy: text('deleted_by').references(() => usersTable.id, {+ onDelete: 'set null',+ onUpdate: 'cascade',+ }),+ // When the organization is soft-deleted, we schedule a purge date some days in the future for hard deletion+ scheduledPurgeAt: integer('scheduled_purge_at', { mode: 'timestamp_ms' }),+});-export const organizationMembersTable = sqliteTable(- 'organization_members',- {- ...createPrimaryKeyField({ prefix: ORGANIZATION_MEMBER_ID_PREFIX }),- ...createTimestampColumns(),+export const organizationMembersTable = sqliteTable('organization_members', {+ ...createPrimaryKeyField({ prefix: ORGANIZATION_MEMBER_ID_PREFIX }),+ ...createTimestampColumns(),- organizationId: text('organization_id')- .notNull()- .references(() => organizationsTable.id, { onDelete: 'cascade', onUpdate: 'cascade' }),+ organizationId: text('organization_id')+ .notNull()+ .references(() => organizationsTable.id, { onDelete: 'cascade', onUpdate: 'cascade' }),- userId: text('user_id')- .notNull()- .references(() => usersTable.id, { onDelete: 'cascade', onUpdate: 'cascade' }),+ userId: text('user_id')+ .notNull()+ .references(() => usersTable.id, { onDelete: 'cascade', onUpdate: 'cascade' }),- role: text('role', {- enum: ORGANIZATION_ROLES_LIST as NonEmptyArray<OrganizationRole>,- }).notNull(),- },- (t) => [unique('organization_members_user_organization_unique').on(t.organizationId, t.userId)],-);+ role: text('role', {+ enum: ORGANIZATION_ROLES_LIST as NonEmptyArray<OrganizationRole>,+ }).notNull(),+});-export const organizationInvitationsTable = sqliteTable(- 'organization_invitations',- {- ...createPrimaryKeyField({ prefix: ORGANIZATION_INVITATION_ID_PREFIX }),- ...createTimestampColumns(),+export const organizationInvitationsTable = sqliteTable('organization_invitations', {+ ...createPrimaryKeyField({ prefix: ORGANIZATION_INVITATION_ID_PREFIX }),+ ...createTimestampColumns(),- organizationId: text('organization_id')- .notNull()- .references(() => organizationsTable.id, { onDelete: 'cascade', onUpdate: 'cascade' }),- email: text('email').notNull(),- role: text('role', {- enum: ORGANIZATION_ROLES_LIST as NonEmptyArray<OrganizationRole>,- }).notNull(),- status: text('status', {- enum: ORGANIZATION_INVITATION_STATUS_LIST as NonEmptyArray<OrganizationInvitationStatus>,- })- .default(ORGANIZATION_INVITATION_STATUS.PENDING)- .notNull(),- expiresAt: integer('expires_at', { mode: 'timestamp_ms' }).notNull(),- inviterId: text('inviter_id')- .notNull()- .references(() => usersTable.id, { onDelete: 'cascade', onUpdate: 'cascade' }),- },- (t) => [- unique('organization_invitations_organization_email_unique').on(t.organizationId, t.email),- ],-);+ organizationId: text('organization_id')+ .notNull()+ .references(() => organizationsTable.id, { onDelete: 'cascade', onUpdate: 'cascade' }),+ email: text('email').notNull(),+ role: text('role', {+ enum: ORGANIZATION_ROLES_LIST as NonEmptyArray<OrganizationRole>,+ }).notNull(),+ status: text('status', {+ enum: ORGANIZATION_INVITATION_STATUS_LIST as NonEmptyArray<OrganizationInvitationStatus>,+ })+ .default(ORGANIZATION_INVITATION_STATUS.PENDING)+ .notNull(),+ expiresAt: integer('expires_at', { mode: 'timestamp_ms' }).notNull(),+ inviterId: text('inviter_id')+ .notNull()+ .references(() => usersTable.id, { onDelete: 'cascade', onUpdate: 'cascade' }),+});diff --git a/apps/papra-server/src/modules/plan-entitlements/plan-entitlements.tables.ts b/apps/papra-server/src/modules/plan-entitlements/plan-entitlements.tables.tsindex afc2d9e7..2e6e6272 100644--- a/apps/papra-server/src/modules/plan-entitlements/plan-entitlements.tables.ts+++ b/apps/papra-server/src/modules/plan-entitlements/plan-entitlements.tables.ts@@ -1,30 +1,23 @@-import { integer, sqliteTable, text, unique } from 'drizzle-orm/sqlite-core';+import { integer, sqliteTable, text } from 'drizzle-orm/sqlite-core';import { createPrimaryKeyField, createTimestampColumns } from '../shared/db/columns.helpers';import type { PlanEntitlementSource } from './plan-entitlements.constants';import { planEntitlementIdPrefix } from './plan-entitlements.constants';import { usersTable } from '../users/users.table';import type { PlanEntitlementType } from './plan-entitlements.registry';-export const planEntitlementsTable = sqliteTable(- 'user_plan_entitlements',- {- ...createPrimaryKeyField({ prefix: planEntitlementIdPrefix }),- ...createTimestampColumns(),+export const planEntitlementsTable = sqliteTable('user_plan_entitlements', {+ ...createPrimaryKeyField({ prefix: planEntitlementIdPrefix }),+ ...createTimestampColumns(),- grantedAt: integer('granted_at', { mode: 'timestamp_ms' }).notNull(),- expiresAt: integer('expires_at', { mode: 'timestamp_ms' }),- lastVerifiedAt: integer('last_verified_at', { mode: 'timestamp_ms' }),- userId: text('user_id')- .references(() => usersTable.id, {- onDelete: 'cascade',- onUpdate: 'cascade',- })- .notNull(),- type: text('type').notNull().$type<PlanEntitlementType>(),- source: text('source').notNull().$type<PlanEntitlementSource>(),- },- (table) => [- // To ensure entitlements are unique per user- unique('user_plan_entitlements_user_id_type_unique').on(table.userId, table.type),- ],-);+ grantedAt: integer('granted_at', { mode: 'timestamp_ms' }).notNull(),+ expiresAt: integer('expires_at', { mode: 'timestamp_ms' }),+ lastVerifiedAt: integer('last_verified_at', { mode: 'timestamp_ms' }),+ userId: text('user_id')+ .references(() => usersTable.id, {+ onDelete: 'cascade',+ onUpdate: 'cascade',+ })+ .notNull(),+ type: text('type').notNull().$type<PlanEntitlementType>(),+ source: text('source').notNull().$type<PlanEntitlementSource>(),+});diff --git a/apps/papra-server/src/modules/roles/roles.table.ts b/apps/papra-server/src/modules/roles/roles.table.tsindex e108cb31..64f90a2d 100644--- a/apps/papra-server/src/modules/roles/roles.table.ts+++ b/apps/papra-server/src/modules/roles/roles.table.ts@@ -1,25 +1,15 @@import type { Role } from './roles.types';-import { index, sqliteTable, text, unique } from 'drizzle-orm/sqlite-core';+import { sqliteTable, text } from 'drizzle-orm/sqlite-core';import { createPrimaryKeyField, createTimestampColumns } from '../shared/db/columns.helpers';import { usersTable } from '../users/users.table';-export const userRolesTable = sqliteTable(- 'user_roles',- {- ...createPrimaryKeyField({ prefix: 'rol' }),- ...createTimestampColumns(),+export const userRolesTable = sqliteTable('user_roles', {+ ...createPrimaryKeyField({ prefix: 'rol' }),+ ...createTimestampColumns(),- userId: text('user_id')- .notNull()- .references(() => usersTable.id, { onDelete: 'cascade', onUpdate: 'cascade' }),+ userId: text('user_id')+ .notNull()+ .references(() => usersTable.id, { onDelete: 'cascade', onUpdate: 'cascade' }),- role: text('role').notNull().$type<Role>(),- },- (table) => [- // To enforce unique roles per user- unique('user_roles_user_id_role_unique_index').on(table.userId, table.role),-- // To get all user for a role- index('user_roles_role_index').on(table.role),- ],-);+ role: text('role').notNull().$type<Role>(),+});diff --git a/apps/papra-server/src/modules/tags/tags.table.ts b/apps/papra-server/src/modules/tags/tags.table.tsindex 22b34531..31145657 100644--- a/apps/papra-server/src/modules/tags/tags.table.ts+++ b/apps/papra-server/src/modules/tags/tags.table.ts@@ -1,31 +1,21 @@-import { primaryKey, sqliteTable, text, unique } from 'drizzle-orm/sqlite-core';+import { primaryKey, sqliteTable, text } from 'drizzle-orm/sqlite-core';import { documentsTable } from '../documents/documents.table';import { organizationsTable } from '../organizations/organizations.table';import { createPrimaryKeyField, createTimestampColumns } from '../shared/db/columns.helpers';import { tagIdPrefix } from './tags.constants';-export const tagsTable = sqliteTable(- 'tags',- {- ...createPrimaryKeyField({ prefix: tagIdPrefix }),- ...createTimestampColumns(),+export const tagsTable = sqliteTable('tags', {+ ...createPrimaryKeyField({ prefix: tagIdPrefix }),+ ...createTimestampColumns(),- 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_normalized_name_unique').on(- table.organizationId,- table.normalizedName,- ),- ],-);+ 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'),+});export const documentsTagsTable = sqliteTable('documents_tags',diff --git a/apps/papra-server/src/modules/users/users.table.ts b/apps/papra-server/src/modules/users/users.table.tsindex 7537fe0a..960073f0 100644--- a/apps/papra-server/src/modules/users/users.table.ts+++ b/apps/papra-server/src/modules/users/users.table.ts@@ -1,18 +1,14 @@-import { index, integer, sqliteTable, text } from 'drizzle-orm/sqlite-core';+import { integer, sqliteTable, text } from 'drizzle-orm/sqlite-core';import { createPrimaryKeyField, createTimestampColumns } from '../shared/db/columns.helpers';-export const usersTable = sqliteTable(- 'users',- {- ...createPrimaryKeyField({ prefix: 'usr' }),- ...createTimestampColumns(),+export const usersTable = sqliteTable('users', {+ ...createPrimaryKeyField({ prefix: 'usr' }),+ ...createTimestampColumns(),- email: text('email').notNull().unique(),- emailVerified: integer('email_verified', { mode: 'boolean' }).notNull().default(false),- name: text('name'),- image: text('image'),- maxOrganizationCount: integer('max_organization_count', { mode: 'number' }),- twoFactorEnabled: integer('two_factor_enabled', { mode: 'boolean' }).notNull().default(false),- },- (table) => [index('users_email_index').on(table.email)],-);+ email: text('email').notNull().unique(),+ emailVerified: integer('email_verified', { mode: 'boolean' }).notNull().default(false),+ name: text('name'),+ image: text('image'),+ maxOrganizationCount: integer('max_organization_count', { mode: 'number' }),+ twoFactorEnabled: integer('two_factor_enabled', { mode: 'boolean' }).notNull().default(false),+});diff --git a/apps/papra-server/src/modules/webhooks/webhooks.tables.ts b/apps/papra-server/src/modules/webhooks/webhooks.tables.tsindex be20ce6e..056db60c 100644--- a/apps/papra-server/src/modules/webhooks/webhooks.tables.ts+++ b/apps/papra-server/src/modules/webhooks/webhooks.tables.ts@@ -1,5 +1,5 @@import { EVENT_NAMES } from '@papra/webhooks';-import { integer, sqliteTable, text, unique } from 'drizzle-orm/sqlite-core';+import { integer, sqliteTable, text } from 'drizzle-orm/sqlite-core';import { organizationsTable } from '../organizations/organizations.table';import { createPrimaryKeyField, createTimestampColumns } from '../shared/db/columns.helpers';import { usersTable } from '../users/users.table';@@ -23,19 +23,15 @@ export const webhooksTable = sqliteTable('webhooks', {}),});-export const webhookEventsTable = sqliteTable(- 'webhook_events',- {- ...createPrimaryKeyField({ prefix: 'wbh_ev' }),- ...createTimestampColumns(),+export const webhookEventsTable = sqliteTable('webhook_events', {+ ...createPrimaryKeyField({ prefix: 'wbh_ev' }),+ ...createTimestampColumns(),- webhookId: text('webhook_id')- .notNull()- .references(() => webhooksTable.id, { onDelete: 'cascade', onUpdate: 'cascade' }),- eventName: text('event_name', { enum: EVENT_NAMES }).notNull(),- },- (t) => [unique('webhook_events_webhook_id_event_name_unique').on(t.webhookId, t.eventName)],-);+ webhookId: text('webhook_id')+ .notNull()+ .references(() => webhooksTable.id, { onDelete: 'cascade', onUpdate: 'cascade' }),+ eventName: text('event_name', { enum: EVENT_NAMES }).notNull(),+});export const webhookDeliveriesTable = sqliteTable('webhook_deliveries', {...createPrimaryKeyField({ prefix: 'wbh_dlv' }),