@mastra/pg 1.23.0-alpha.3 → 1.23.0-alpha.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { createHash, randomUUID } from "crypto";
1
2
  import { ErrorCategory, ErrorDomain, MastraError } from "@mastra/core/error";
2
3
  import * as coreStorage from "@mastra/core/storage";
3
4
  import { AgentsStorage, BRANCH_SPAN_TYPES, BackgroundTasksStorage, BlobStore, ChannelsStorage, DATASETS_SCHEMA, DATASET_ITEMS_SCHEMA, DATASET_VERSIONS_SCHEMA, DatasetsStorage, EXPERIMENTS_SCHEMA, EXPERIMENT_RESULTS_SCHEMA, EntityType, ExperimentsStorage, FactoryStorage, FavoritesStorage, KNOWLEDGE_ACTIVITY_SCHEMA, KNOWLEDGE_CURSORS_SCHEMA, KNOWLEDGE_MENTIONS_SCHEMA, KNOWLEDGE_NODES_SCHEMA, KNOWLEDGE_RECORDS_SCHEMA, KNOWLEDGE_SEMANTIC_OUTBOX_SCHEMA, KnowledgeConflictError, KnowledgeNotFoundError, KnowledgeStorage, MCPClientsStorage, MCPServersStorage, METRIC_DISTINCT_COLUMNS, MastraCompositeStore, MemoryStorage, NotificationsStorage, OBSERVATIONAL_MEMORY_TABLE_SCHEMA, ObservabilityStorage, PromptBlocksStorage, SchedulesStorage, ScorerDefinitionsStorage, ScoresStorage, SkillsStorage, TABLE_AGENTS, TABLE_AGENT_VERSIONS, TABLE_BACKGROUND_TASKS, TABLE_CHANNEL_CONFIG, TABLE_CHANNEL_INSTALLATIONS, TABLE_CONFIGS, TABLE_DATASETS, TABLE_DATASET_ITEMS, TABLE_DATASET_VERSIONS, TABLE_EXPERIMENTS, TABLE_EXPERIMENT_RESULTS, TABLE_FAVORITES, TABLE_KNOWLEDGE_ACTIVITY, TABLE_KNOWLEDGE_CURSORS, TABLE_KNOWLEDGE_MENTIONS, TABLE_KNOWLEDGE_NODES, TABLE_KNOWLEDGE_RECORDS, TABLE_KNOWLEDGE_SEMANTIC_OUTBOX, TABLE_MCP_CLIENTS, TABLE_MCP_CLIENT_VERSIONS, TABLE_MCP_SERVERS, TABLE_MCP_SERVER_VERSIONS, TABLE_MESSAGES, TABLE_NOTIFICATIONS, TABLE_PROMPT_BLOCKS, TABLE_PROMPT_BLOCK_VERSIONS, TABLE_RESOURCES, TABLE_SCHEDULES, TABLE_SCHEDULE_TRIGGERS, TABLE_SCHEMAS, TABLE_SCORERS, TABLE_SCORER_DEFINITIONS, TABLE_SCORER_DEFINITION_VERSIONS, TABLE_SKILLS, TABLE_SKILL_BLOBS, TABLE_SKILL_VERSIONS, TABLE_SPANS, TABLE_THREADS, TABLE_THREAD_STATE, TABLE_TOOL_PROVIDER_CONNECTIONS, TABLE_WORKFLOW_DEFINITIONS, TABLE_WORKFLOW_SNAPSHOT, TABLE_WORKSPACES, TABLE_WORKSPACE_VERSIONS, ThreadStateStorage, ToolProviderConnectionsStorage, TraceStatus, UniqueViolationError, WorkflowDefinitionsStorage, WorkflowsStorage, WorkspacesStorage, assertKnowledgeCeilingRaised, assertKnowledgeScopeWithinCeiling, calculatePagination, canonicalizeKnowledgeScope, createKnowledgeUlid, createStorageErrorId, createVectorErrorId, ensureDate, getDefaultValue, getSqlType, hasErrorCode, isKnowledgeScopeVisible, knowledgeScopeKey, knowledgeSemanticDocumentId, knowledgeSemanticIdempotencyKey, listBranchesArgsSchema, listFeedbackArgsSchema, listLogsArgsSchema, listMetricsArgsSchema, listScoresArgsSchema, listTracesArgsSchema, matchesExpectedWorkflowStatus, mergeWorkflowStepResult, normalizePerPage, normalizeScheduleTarget, parseDuration, parseKnowledgeNodeCursor, parseKnowledgeWikilinks, safelyParseJSON, storageMessageMatchesMetadataFilter, toTraceSpans, transformScoreRow, validateStorageMetadataFilter } from "@mastra/core/storage";
@@ -10,7 +11,6 @@ import xxhash from "xxhash-wasm";
10
11
  import { parse } from "pg-connection-string";
11
12
  import { BaseFilterTranslator } from "@mastra/core/vector/filter";
12
13
  import { MastraBase } from "@mastra/core/base";
13
- import { randomUUID } from "crypto";
14
14
  import { MessageList } from "@mastra/core/agent";
15
15
  import { coreFeatures } from "@mastra/core/features";
16
16
  import { saveScorePayloadSchema } from "@mastra/core/evals";
@@ -804,6 +804,8 @@ var PgVector = class extends MastraVector {
804
804
  AND attnum > 0
805
805
  AND NOT attisdropped`, [tableName])).rowCount === 0) return;
806
806
  await client.query(`ALTER TABLE ${tableName} ADD COLUMN IF NOT EXISTS namespace VARCHAR(255) NOT NULL DEFAULT '${DEFAULT_NAMESPACE}'`);
807
+ const namespaceIndexName = this.getNamespaceIndexName(parsedIndexName);
808
+ await client.query(`CREATE UNIQUE INDEX IF NOT EXISTS "${namespaceIndexName}" ON ${tableName} (namespace, vector_id)`);
807
809
  const legacyConstraints = await client.query(`SELECT c.conname
808
810
  FROM pg_constraint c
809
811
  WHERE c.conrelid = to_regclass($1)
@@ -813,8 +815,21 @@ var PgVector = class extends MastraVector {
813
815
  const parsedConstraintName = parseSqlIdentifier(conname, "constraint name");
814
816
  await client.query(`ALTER TABLE ${tableName} DROP CONSTRAINT "${parsedConstraintName}"`);
815
817
  }
816
- const namespaceIndexName = parseSqlIdentifier(`${parsedIndexName}_namespace_vector_id_idx`, "index name");
817
- await client.query(`CREATE UNIQUE INDEX IF NOT EXISTS "${namespaceIndexName}" ON ${tableName} (namespace, vector_id)`);
818
+ }
819
+ /**
820
+ * Name of the unique (namespace, vector_id) index for a table.
821
+ *
822
+ * The full `<index>_namespace_vector_id_idx` name is kept whenever it fits so tables that
823
+ * were already migrated keep matching `IF NOT EXISTS`. Longer index names would exceed
824
+ * Postgres' 63-char identifier limit, so those fall back to a truncated prefix plus a hash
825
+ * of the index name to distinguish tables that
826
+ * share a long prefix.
827
+ */
828
+ getNamespaceIndexName(parsedIndexName) {
829
+ const fullName = `${parsedIndexName}_namespace_vector_id_idx`;
830
+ if (fullName.length <= 63) return fullName;
831
+ const suffix = `_ns_${createHash("sha256").update(parsedIndexName).digest("hex").slice(0, 32)}_idx`;
832
+ return `${parsedIndexName.slice(0, 63 - suffix.length)}${suffix}`;
818
833
  }
819
834
  transformFilter(filter) {
820
835
  return new PGFilterTranslator().translate(filter);