@@ -128,26 +128,65 @@ def _verify_schema(self) -> None:
128128 f"{ ddl } ;"
129129 )
130130
131- def create_chat_history_table (self ) -> None :
132- google_schema = f"""CREATE TABLE IF NOT EXISTS { self .table_name } (
131+ @staticmethod
132+ def create_chat_history_table (
133+ instance_id : str ,
134+ database_id : str ,
135+ table_name : str ,
136+ client : spanner .Client = spanner .Client (),
137+ ) -> None :
138+ """
139+ Create a chat history table in a Cloud Spanner database.
140+
141+ Args:
142+ instance_id (str): The ID of the Cloud Spanner instance.
143+ database_id (str): The ID of the Cloud Spanner database.
144+ table_name (str): The name of the table to be created.
145+ client (spanner.Client, optional): An instance of the Cloud Spanner client. Defaults to spanner.Client().
146+
147+ Raises:
148+ Exception: If the specified instance or database does not exist.
149+
150+ Returns:
151+ Operation: The operation to create the table.
152+ """
153+
154+ client = client_with_user_agent (client , USER_AGENT_CHAT )
155+
156+ instance = client .instance (instance_id )
157+
158+ if not instance .exists ():
159+ raise Exception ("Instance with id: {} doesn't exist." .format (instance_id ))
160+
161+ database = instance .database (database_id )
162+
163+ if not database .exists ():
164+ raise Exception ("Database with id: {} doesn't exist." .format (database_id ))
165+
166+ database .reload ()
167+
168+ dialect = database .database_dialect
169+
170+ google_schema = f"""CREATE TABLE IF NOT EXISTS { table_name } (
133171 id STRING(36) DEFAULT (GENERATE_UUID()),
134172 created_at TIMESTAMP NOT NULL OPTIONS (allow_commit_timestamp=true),
135173 session_id STRING(MAX) NOT NULL,
136174 message JSON NOT NULL,
137175 ) PRIMARY KEY (session_id, created_at ASC, id)"""
138176
139- pg_schema = f"""CREATE TABLE IF NOT EXISTS { self . table_name } (
177+ pg_schema = f"""CREATE TABLE IF NOT EXISTS { table_name } (
140178 id varchar(36) DEFAULT (spanner.generate_uuid()),
141179 created_at SPANNER.COMMIT_TIMESTAMP NOT NULL,
142180 session_id TEXT NOT NULL,
143181 message JSONB NOT NULL,
144182 PRIMARY KEY (session_id, created_at, id)
145183 );"""
146184
147- ddl = pg_schema if self . dialect == DatabaseDialect .POSTGRESQL else google_schema
148- database = self . client . instance ( self . instance_id ). database ( self . database_id )
185+ ddl = pg_schema if dialect == DatabaseDialect .POSTGRESQL else google_schema
186+
149187 operation = database .update_ddl ([ddl ])
150188 operation .result (OPERATION_TIMEOUT_SECONDS )
189+
151190 return operation
152191
153192 @property
0 commit comments