PgSchemaManager

org.llm4s.rag.permissions.pg.PgSchemaManager

Manages the PostgreSQL schema for permission-based RAG.

Creates and maintains the database tables required for:

  • Principal ID mapping (users and groups to integers)
  • Collection hierarchy with permissions
  • Extended vectors table with collection_id and readable_by

Attributes

Source
PgSchemaManager.scala
Graph
Supertypes
class Object
trait Matchable
class Any
Self type

Members list

Value members

Concrete methods

def createDefaultCollection(conn: Connection): Result[Int]

Create a default public collection for migrating existing data.

Create a default public collection for migrating existing data.

This creates a "default" collection that is:

  • Public (empty queryable_by)
  • A leaf collection (can contain documents)

Value parameters

conn

Database connection

Attributes

Returns

The ID of the default collection, or error

Source
PgSchemaManager.scala
def dropSchema(conn: Connection, tableName: String): Result[Unit]

Drop all permission-related tables and sequences.

Drop all permission-related tables and sequences.

WARNING: This is destructive and will delete all permission data. Only use for testing or complete reset.

Value parameters

conn

Database connection

tableName

The name of the vectors table (to remove permission columns)

Attributes

Returns

Success or error

Source
PgSchemaManager.scala
def extendVectorsTable(conn: Connection, tableName: String): Result[Unit]

Create or upgrade the vectors table and add the permission columns to it.

Create or upgrade the vectors table and add the permission columns to it.

The table itself is the one PgVectorStore uses - both run org.llm4s.vectorstore.PgVectorTableSchema, so a table created here can be opened by a PgVectorStore and vice versa. That also upgrades a table this method created before the two definitions were unified (see PgVectorTableSchema).

Adds:

  • collection_id column (foreign key to llm4s_collections)
  • readable_by column (array of principal IDs)
  • Indexes for efficient filtering
  • A full-text (GIN) index on content, for PgSearchIndex.keywordQuery

Value parameters

conn

Database connection

tableName

The name of the vectors table to extend; must be a valid SQL identifier

Attributes

Returns

Success or error

Source
PgSchemaManager.scala
def initializeSchema(conn: Connection): Result[Unit]

Initialize the permission schema in the database.

Initialize the permission schema in the database.

This creates:

  • llm4s_principals table for user/group ID mapping
  • llm4s_group_id_seq sequence for negative group IDs
  • llm4s_collections table for the collection hierarchy
  • Indexes for efficient querying

Value parameters

conn

Database connection

Attributes

Returns

Success or error

Source
PgSchemaManager.scala
def isSchemaInitialized(conn: Connection): Result[Boolean]

Check if the permission schema is already initialized.

Check if the permission schema is already initialized.

Value parameters

conn

Database connection

Attributes

Returns

True if schema exists, false otherwise

Source
PgSchemaManager.scala
def migrateExistingVectors(conn: Connection, tableName: String, defaultCollectionId: Int): Result[Long]

Migrate existing vectors to the default collection.

Migrate existing vectors to the default collection.

Updates all vectors that don't have a collection_id to use the default collection.

Value parameters

conn

Database connection

defaultCollectionId

The ID of the default collection

tableName

The name of the vectors table

Attributes

Returns

Number of migrated vectors, or error

Source
PgSchemaManager.scala
def runFullMigration(conn: Connection, tableName: String): Result[MigrationStats]

Run the full migration: initialize schema, extend vectors, create default collection, migrate data.

Run the full migration: initialize schema, extend vectors, create default collection, migrate data.

Value parameters

conn

Database connection

tableName

The name of the vectors table

Attributes

Returns

Migration stats or error

Source
PgSchemaManager.scala