Skip to content

Commit 29f4d7c

Browse files
committed
[IMP] Enable pg_vector on databases newly created
The PostgresSQL extension `vector` is required for Odoo to work with AI modules, it must be activated on new databases.
1 parent 3f384dc commit 29f4d7c

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

odev/commands/database/create.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,10 @@ def create_database(self):
153153
self._template.connector.disconnect()
154154

155155
with progress.spinner(f"Creating {message}"):
156-
created = self._database.create(template=template) & self._database.unaccent() & self._database.pg_trgm()
156+
created = self._database.create(template=template)
157+
created &= self._database.unaccent()
158+
created &= self._database.pg_trgm()
159+
created &= self._database.pg_vector()
157160

158161
if created is False:
159162
raise self.error(f"Failed to create database {self._database.name!r}")

odev/common/databases/local.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from datetime import datetime
1212
from functools import cached_property
1313
from pathlib import Path
14-
from subprocess import PIPE, CalledProcessError, Popen
14+
from subprocess import PIPE, Popen
1515
from time import sleep
1616
from types import FrameType
1717
from typing import (
@@ -933,11 +933,10 @@ def _buffered_sql_enable_extensions(self, dump: gzip.GzipFile | bz2.BZ2File | IO
933933
else:
934934
self.unaccent()
935935

936-
self.pg_vector()
937936
dump.seek(0)
938937

939938
@ensure_connected
940-
def unaccent(self):
939+
def unaccent(self) -> bool:
941940
"""Install the unaccent extension on the database."""
942941
unaccent_queries = [
943942
"CREATE SCHEMA IF NOT EXISTS unaccent_schema",
@@ -968,21 +967,33 @@ def unaccent(self):
968967
return res
969968

970969
@ensure_connected
971-
def pg_trgm(self):
970+
def pg_trgm(self) -> bool:
972971
"""Install the pg_trgm extension on the database."""
973972
pg_trgm_query = "CREATE EXTENSION IF NOT EXISTS pg_trgm WITH SCHEMA public"
974973
return self.query(pg_trgm_query)
975974

976-
def pg_vector(self):
977-
"""Install the pgvector extension on the database."""
978-
pgvector_query = "CREATE EXTENSION IF NOT EXISTS vector WITH SCHEMA public"
975+
def pg_vector(self) -> bool:
976+
"""Install the vector extension on the database.
977+
Try creating the extension through a regular query first, that will only work if the extension is whitelisted
978+
in `pgextwlist` or if the user is owner of the extension. If this fails, try enabling it as the `postgres`
979+
user.
980+
"""
981+
pg_vector_query = "CREATE EXTENSION IF NOT EXISTS vector WITH SCHEMA public"
979982

980983
try:
981-
return bash.execute(f"sudo -u postgres psql -d {self.name} -c '{pgvector_query}'")
982-
except CalledProcessError as error:
983-
raise OdevError(
984-
"Failed to install pgvector extension, make sure it is installde on your system first"
985-
) from error
984+
self.query(pg_vector_query)
985+
except RuntimeError:
986+
link = string.link(
987+
"pgextwlist",
988+
"https://github.com/dimitri/pgextwlist?tab=readme-ov-file#postgresql-extension-whitelist",
989+
)
990+
logger.error(
991+
"Failed to install 'pgvector' extension, please ensure it is installed on your system "
992+
f"and whitelisted with {link}"
993+
)
994+
return False
995+
996+
return True
986997

987998
@ensure_connected
988999
def neuter_filestore(self):

odev/common/string.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,3 +293,14 @@ def quote(string: str, dirty_only: bool = False, force_single: bool = False) ->
293293

294294
double = not force_single and (index == -1 or string[index] == "'")
295295
return f'"{string}"' if double else f"'{string}'"
296+
297+
298+
def link(text: str, url: str) -> str:
299+
"""Convert a link to text to be printed in the console.
300+
301+
:param text: The text to link.
302+
:param url: The URL to link to.
303+
:return: The link.
304+
:rtype: str
305+
"""
306+
return f"[link={url}]{text}[/link]"

0 commit comments

Comments
 (0)