Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion gcp-jobs/bn-retry/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ dependencies = [
"gcp-queue @ git+https://github.com/bcgov/sbc-connect-common.git@main#subdirectory=python/gcp-queue",
"structured-logging @ git+https://github.com/bcgov/sbc-connect-common.git@main#subdirectory=python/structured-logging",
"business-model @ git+https://github.com/bcgov/lear.git@main#subdirectory=python/common/business-registry-model",
"psycopg2 (>=2.9.11,<3.0.0)"
"psycopg2 (>=2.9.11,<3.0.0)",
"cloud-sql-python-connector (>=1.18.0,<2.0.0)",
"pg8000 (>=1.31.2,<2.0.0)",
]


Expand Down
24 changes: 21 additions & 3 deletions gcp-jobs/bn-retry/src/bn_retry/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,28 @@
DB_HOST = os.getenv("DATABASE_HOST", "")
DB_PORT = os.getenv("DATABASE_PORT", "5432")

if DB_UNIX_SOCKET := os.getenv("DATABASE_UNIX_SOCKET", None):
SQLALCHEMY_DATABASE_URI = f"postgresql+psycopg2://{DB_USER}:{DB_PASSWORD}@/{DB_NAME}?host={DB_UNIX_SOCKET}"
if os.getenv("CLOUDSQL_INSTANCE_CONNECTION_NAME"):
from google.cloud.sql.connector import Connector, IPTypes

_connector = Connector()

def getconn(_connector=_connector, _IPTypes=IPTypes):

Check warning on line 73 in gcp-jobs/bn-retry/src/bn_retry/config.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename this parameter "_IPTypes" to match the regular expression ^[_a-z][a-z0-9_]*$.

See more on https://sonarcloud.io/project/issues?id=bcgov_lear&issues=AZz6EhWg_Q7wavx_oKgN&open=AZz6EhWg_Q7wavx_oKgN&pullRequest=4167
return _connector.connect(
os.environ["CLOUDSQL_INSTANCE_CONNECTION_NAME"],
"pg8000",
user=os.environ["DATABASE_USERNAME"],
db=os.environ["DATABASE_NAME"],
enable_iam_auth=True,
ip_type=_IPTypes.PRIVATE,
)

SQLALCHEMY_DATABASE_URI = "postgresql+pg8000://"
SQLALCHEMY_ENGINE_OPTIONS = {"creator": getconn}
else:
SQLALCHEMY_DATABASE_URI = f"postgresql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}"
if DB_UNIX_SOCKET := os.getenv("DATABASE_UNIX_SOCKET", None):
SQLALCHEMY_DATABASE_URI = f"postgresql+psycopg2://{DB_USER}:{DB_PASSWORD}@/{DB_NAME}?host={DB_UNIX_SOCKET}"
else:
SQLALCHEMY_DATABASE_URI = f"postgresql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}"

SQLALCHEMY_TRACK_MODIFICATIONS = False

Expand Down
2 changes: 2 additions & 0 deletions gcp-jobs/email-reminder/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ dependencies = [
"business-registry-account @ git+https://github.com/bcgov/lear.git@main#subdirectory=python/common/business-registry-account",
"flask-script (>=2.0.6,<3.0.0)",
"psycopg2 (>=2.9.10,<3.0.0)",
"cloud-sql-python-connector (>=1.18.0,<2.0.0)",
"pg8000 (>=1.31.2,<2.0.0)",
]


Expand Down
24 changes: 21 additions & 3 deletions gcp-jobs/email-reminder/src/email_reminder/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,28 @@
DB_NAME = os.getenv("DATABASE_NAME", "")
DB_HOST = os.getenv("DATABASE_HOST", "")
DB_PORT = os.getenv("DATABASE_PORT", "5432")
if DB_UNIX_SOCKET := os.getenv("DATABASE_UNIX_SOCKET", None):
SQLALCHEMY_DATABASE_URI = f"postgresql+psycopg2://{DB_USER}:{DB_PASSWORD}@/{DB_NAME}?host={DB_UNIX_SOCKET}"
if os.getenv("CLOUDSQL_INSTANCE_CONNECTION_NAME"):
from google.cloud.sql.connector import Connector, IPTypes

_connector = Connector()

def getconn(_connector=_connector, _IPTypes=IPTypes):

Check warning on line 66 in gcp-jobs/email-reminder/src/email_reminder/config.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename this parameter "_IPTypes" to match the regular expression ^[_a-z][a-z0-9_]*$.

See more on https://sonarcloud.io/project/issues?id=bcgov_lear&issues=AZz6EhXB_Q7wavx_oKgP&open=AZz6EhXB_Q7wavx_oKgP&pullRequest=4167
return _connector.connect(
os.environ["CLOUDSQL_INSTANCE_CONNECTION_NAME"],
"pg8000",
user=os.environ["DATABASE_USERNAME"],
db=os.environ["DATABASE_NAME"],
enable_iam_auth=True,
ip_type=_IPTypes.PRIVATE,
)

SQLALCHEMY_DATABASE_URI = "postgresql+pg8000://"
SQLALCHEMY_ENGINE_OPTIONS = {"creator": getconn}
else:
SQLALCHEMY_DATABASE_URI = f"postgresql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}"
if DB_UNIX_SOCKET := os.getenv("DATABASE_UNIX_SOCKET", None):
SQLALCHEMY_DATABASE_URI = f"postgresql+psycopg2://{DB_USER}:{DB_PASSWORD}@/{DB_NAME}?host={DB_UNIX_SOCKET}"
else:
SQLALCHEMY_DATABASE_URI = f"postgresql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}"

TESTING = False
DEBUG = False
Expand Down
1 change: 1 addition & 0 deletions gcp-jobs/filings-notebook-report/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ certifi = "^2025.1.31"
urllib3 = "^2.3.0"
idna = "^3.10"
pg8000 = "^1.31.2"
cloud-sql-python-connector = ">=1.18.0,<2.0.0"
structured-logging = {git = "https://github.com/bcgov/sbc-connect-common.git", rev = "main", subdirectory = "python/structured-logging"}
business-registry-account = {git = "https://github.com/bcgov/lear.git", subdirectory = "python/common/business-registry-account"}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,24 @@
DB_HOST = os.getenv("DATABASE_HOST", "")
DB_PORT = os.getenv("DATABASE_PORT", "5432")
if DB_USER != "":
if DB_UNIX_SOCKET := os.getenv("DATABASE_UNIX_SOCKET", None):
if os.getenv("CLOUDSQL_INSTANCE_CONNECTION_NAME"):
from google.cloud.sql.connector import Connector, IPTypes

_connector = Connector()

def getconn(_connector=_connector, _IPTypes=IPTypes):

Check warning on line 35 in gcp-jobs/filings-notebook-report/src/notebookreport/config/config.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename this parameter "_IPTypes" to match the regular expression ^[_a-z][a-z0-9_]*$.

See more on https://sonarcloud.io/project/issues?id=bcgov_lear&issues=AZz6EhWu_Q7wavx_oKgO&open=AZz6EhWu_Q7wavx_oKgO&pullRequest=4167
return _connector.connect(
os.environ["CLOUDSQL_INSTANCE_CONNECTION_NAME"],
"pg8000",
user=os.environ["DATABASE_USERNAME"],
db=os.environ["DATABASE_NAME"],
enable_iam_auth=True,
ip_type=_IPTypes.PRIVATE,
)

SQLALCHEMY_DATABASE_URI = "postgresql+pg8000://"
SQLALCHEMY_ENGINE_OPTIONS = {"creator": getconn}
elif DB_UNIX_SOCKET := os.getenv("DATABASE_UNIX_SOCKET", None):
SQLALCHEMY_DATABASE_URI = f"postgresql+psycopg2://{DB_USER}:{DB_PASSWORD}@/{DB_NAME}?host={DB_UNIX_SOCKET}"
else:
SQLALCHEMY_DATABASE_URI = f"postgresql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}"
Expand Down
2 changes: 2 additions & 0 deletions gcp-jobs/involuntary-dissolutions/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ business-model = {git = "https://github.com/bcgov/lear.git", subdirectory = "pyt
business-registry-dissolution = {git = "https://github.com/bcgov/lear.git", subdirectory = "python/common/business-registry-dissolution"}
structured-logging = {git = "https://github.com/bcgov/sbc-connect-common.git", rev = "main", subdirectory = "python/structured-logging"}
psycopg2 = "^2.9.10"
cloud-sql-python-connector = ">=1.18.0,<2.0.0"
pg8000 = ">=1.31.2,<2.0.0"

[tool.poetry.group.dev.dependencies]
ruff = "^0.11.2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,28 @@
DB_NAME = os.getenv("DATABASE_NAME", "")
DB_HOST = os.getenv("DATABASE_HOST", "")
DB_PORT = os.getenv("DATABASE_PORT", "5432")
if DB_UNIX_SOCKET := os.getenv("DATABASE_UNIX_SOCKET", None):
SQLALCHEMY_DATABASE_URI = f"postgresql+psycopg2://{DB_USER}:{DB_PASSWORD}@/{DB_NAME}?host={DB_UNIX_SOCKET}"
if os.getenv("CLOUDSQL_INSTANCE_CONNECTION_NAME"):
from google.cloud.sql.connector import Connector, IPTypes

_connector = Connector()

def getconn(_connector=_connector, _IPTypes=IPTypes):

Check warning on line 68 in gcp-jobs/involuntary-dissolutions/src/involuntary_dissolutions/config/config.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename this parameter "_IPTypes" to match the regular expression ^[_a-z][a-z0-9_]*$.

See more on https://sonarcloud.io/project/issues?id=bcgov_lear&issues=AZz6EhXP_Q7wavx_oKgQ&open=AZz6EhXP_Q7wavx_oKgQ&pullRequest=4167
return _connector.connect(
os.environ["CLOUDSQL_INSTANCE_CONNECTION_NAME"],
"pg8000",
user=os.environ["DATABASE_USERNAME"],
db=os.environ["DATABASE_NAME"],
enable_iam_auth=True,
ip_type=_IPTypes.PRIVATE,
)

SQLALCHEMY_DATABASE_URI = "postgresql+pg8000://"
SQLALCHEMY_ENGINE_OPTIONS = {"creator": getconn}
else:
SQLALCHEMY_DATABASE_URI = f"postgresql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}"
if DB_UNIX_SOCKET := os.getenv("DATABASE_UNIX_SOCKET", None):
SQLALCHEMY_DATABASE_URI = f"postgresql+psycopg2://{DB_USER}:{DB_PASSWORD}@/{DB_NAME}?host={DB_UNIX_SOCKET}"
else:
SQLALCHEMY_DATABASE_URI = f"postgresql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}"

# Pub/Sub

Expand Down
40 changes: 28 additions & 12 deletions legal-api/migrations/env.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from __future__ import with_statement

import logging
import os
from logging.config import fileConfig

from sqlalchemy import engine_from_config
from sqlalchemy import pool

import sqlalchemy as sa
from alembic import context
from flask import current_app

# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
Expand All @@ -17,14 +17,28 @@
fileConfig(config.config_file_name)
logger = logging.getLogger('alembic.env')


def get_engine():
try:
# this works with Flask-SQLAlchemy<3 and Alchemical
return current_app.extensions["migrate"].db.get_engine()
except TypeError:
# this works with Flask-SQLAlchemy>=3
return current_app.extensions["migrate"].db.engine


def get_engine_url():
try:
return get_engine().url.render_as_string(hide_password=False).replace("%", "%%")
except AttributeError:
return str(get_engine().url).replace("%", "%%")


# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
from flask import current_app
config.set_main_option(
'sqlalchemy.url', current_app.config.get(
'SQLALCHEMY_DATABASE_URI').replace('%', '%%'))
config.set_main_option('sqlalchemy.url', get_engine_url())
target_metadata = current_app.extensions['migrate'].db.metadata

# other values from the config, defined by the needs of env.py,
Expand Down Expand Up @@ -72,13 +86,15 @@ def process_revision_directives(context, revision, directives):
directives[:] = []
logger.info('No changes in schema detected.')

connectable = engine_from_config(
config.get_section(config.config_ini_section),
prefix='sqlalchemy.',
poolclass=pool.NullPool,
)
connectable = get_engine()

with connectable.connect() as connection:

owner_role = os.getenv("DB_OWNER_ROLE")
if owner_role:
safe_role = owner_role.replace('"', '""') # Escape any quotes for SQL safety
connection.execute(sa.text(f'SET ROLE "{safe_role}"'))

context.configure(
connection=connection,
target_metadata=target_metadata,
Expand Down
Loading
Loading