Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 147 postgres integration #297

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
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
15 changes: 12 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,16 @@ jobs:
uses: codecov/[email protected]

pytest-integration:
name: Integration test - ${{ matrix.dbName }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include:
- dbName: "MySQL"
cliOption: ""
- dbName: "PostgreSQL"
cliOption: "--postgres"
steps:
- name: Checkout code
uses: actions/checkout@v4
Expand All @@ -96,15 +105,15 @@ jobs:
pip install ./diracx-core/[testing] ./diracx-api/[testing] ./diracx-cli/[testing] ./diracx-client/[testing] ./diracx-routers/[testing] ./diracx-db/[testing] ./diracx-testing/
- name: Start demo
run: |
git clone https://github.com/DIRACGrid/diracx-charts.git ../diracx-charts
../diracx-charts/run_demo.sh --enable-open-telemetry --enable-coverage --exit-when-done --set-value developer.autoReload=false --ci-values ../diracx-charts/demo/ci_values.yaml $PWD
git clone -b diracx-issue-147-postgres-integration https://github.com/AcquaDiGiorgio/diracx-charts.git ../diracx-charts
../diracx-charts/run_demo.sh ${{ matrix.cliOption }} --enable-open-telemetry --enable-coverage --exit-when-done --set-value developer.autoReload=false --ci-values ../diracx-charts/demo/ci_values.yaml $PWD
- name: Debugging information
run: |
DIRACX_DEMO_DIR=$PWD/../diracx-charts/.demo
export KUBECONFIG=${DIRACX_DEMO_DIR}/kube.conf
export PATH=${DIRACX_DEMO_DIR}:$PATH
kubectl get pods
for pod_name in $(kubectl get pods -o json | jq -r '.items[] | .metadata.name' | grep -vE '(dex|minio|mysql|rabbitmq|opensearch)'); do
for pod_name in $(kubectl get pods -o json | jq -r '.items[] | .metadata.name' | grep -vE '(dex|minio|mysql|postgres|rabbitmq|opensearch)'); do
echo "${pod_name}"
kubectl describe pod/"${pod_name}" || true
for container_name in $(kubectl get pods $pod_name -o jsonpath='{.spec.initContainers[*].name} {.spec.containers[*].name}'); do
Expand Down
5 changes: 4 additions & 1 deletion diracx-core/src/diracx/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
T = TypeVar("T")

SqlalchemyDsn = Annotated[
AnyUrl, UrlConstraints(allowed_schemes={"sqlite+aiosqlite", "mysql+aiomysql"})
AnyUrl,
UrlConstraints(
allowed_schemes={"sqlite+aiosqlite", "mysql+aiomysql", "postgresql+asyncpg"}
),
]


Expand Down
1 change: 1 addition & 0 deletions diracx-db/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ dependencies = [
"opensearch-py[async]",
"pydantic >=2.4",
"sqlalchemy[aiomysql,aiosqlite] >= 2",
"asyncpg",
]
dynamic = ["version"]

Expand Down
4 changes: 2 additions & 2 deletions diracx-db/src/diracx/db/sql/dummy/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async def insert_owner(self, name: str) -> int:
stmt = insert(Owners).values(name=name)
result = await self.conn.execute(stmt)
# await self.engine.commit()
return result.lastrowid
return result.inserted_primary_key[0]

async def insert_car(self, license_plate: UUID, model: str, owner_id: int) -> int:
stmt = insert(Cars).values(
Expand All @@ -49,4 +49,4 @@ async def insert_car(self, license_plate: UUID, model: str, owner_id: int) -> in

result = await self.conn.execute(stmt)
# await self.engine.commit()
return result.lastrowid
return result.inserted_primary_key[0]
2 changes: 1 addition & 1 deletion diracx-db/src/diracx/db/sql/job/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ async def _insertNewJDL(self, jdl) -> int:
)
result = await self.conn.execute(stmt)
# await self.engine.commit()
return result.lastrowid
return result.inserted_primary_key[0]

async def _insertJob(self, jobData: dict[str, Any]):
stmt = insert(Jobs).values(jobData)
Expand Down
22 changes: 12 additions & 10 deletions diracx-db/src/diracx/db/sql/job/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ class Jobs(JobDBBase):
Owner = Column("Owner", String(64), default="Unknown")
OwnerGroup = Column("OwnerGroup", String(128), default="Unknown")
VO = Column("VO", String(32))
SubmissionTime = NullColumn("SubmissionTime", DateTime)
RescheduleTime = NullColumn("RescheduleTime", DateTime)
LastUpdateTime = NullColumn("LastUpdateTime", DateTime)
StartExecTime = NullColumn("StartExecTime", DateTime)
HeartBeatTime = NullColumn("HeartBeatTime", DateTime)
EndExecTime = NullColumn("EndExecTime", DateTime)
SubmissionTime = NullColumn("SubmissionTime", DateTime(timezone=True))
RescheduleTime = NullColumn("RescheduleTime", DateTime(timezone=True))
LastUpdateTime = NullColumn("LastUpdateTime", DateTime(timezone=True))
StartExecTime = NullColumn("StartExecTime", DateTime(timezone=True))
HeartBeatTime = NullColumn("HeartBeatTime", DateTime(timezone=True))
EndExecTime = NullColumn("EndExecTime", DateTime(timezone=True))
Comment on lines +34 to +39
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? We are anyway always using UTC.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is because we are specifying the timezone in datetime.now(tz=timezone.utc).
While this does not throw any errors in MySQL when the table does not accept a timezone, in PostgreSQL stops the execution due to conflicting formatting.

One way of fixing it is just to completely remove the timezone on the datetime.now function, which is not recommended and even RUFF marks it as a warning.
The other way is by telling the column to take into account the timezone, which in my opinion is the better way of implementing it.

MySQL:

  • Changing the table property: 2024-09-26 14:00:32
  • Removing tz=timezone.utc: 2024-09-26 14:05:53

No change, both result in the same date format.

PostgreSQL:

  • Changing the table property: 2024-09-26 13:50:26.213893+00
  • Removing tz=timezone.utc: 2024-09-26 13:55:57.840623

Timezone is or is not added at the end.

Status = Column("Status", String(32), default="Received")
MinorStatus = Column("MinorStatus", String(128), default="Unknown")
ApplicationStatus = Column("ApplicationStatus", String(255), default="Unknown")
Expand All @@ -45,7 +45,9 @@ class Jobs(JobDBBase):
VerifiedFlag = Column("VerifiedFlag", EnumBackedBool(), default=False)
# TODO: Should this be True/False/"Failed"? Or True/False/Null?
AccountedFlag = Column(
"AccountedFlag", Enum("True", "False", "Failed"), default="False"
"AccountedFlag",
Enum("True", "False", "Failed", name="AccountedFlag"),
default="False",
)

__table_args__ = (
Expand Down Expand Up @@ -114,7 +116,7 @@ class HeartBeatLoggingInfo(JobDBBase):
)
Name = Column(String(100), primary_key=True)
Value = Column(Text)
HeartBeatTime = Column(DateTime, primary_key=True)
HeartBeatTime = Column(DateTime(timezone=True), primary_key=True)


class JobCommands(JobDBBase):
Expand All @@ -125,5 +127,5 @@ class JobCommands(JobDBBase):
Command = Column(String(100))
Arguments = Column(String(100))
Status = Column(String(64), default="Received")
ReceptionTime = Column(DateTime, primary_key=True)
ExecutionTime = NullColumn(DateTime)
ReceptionTime = Column(DateTime(timezone=True), primary_key=True)
ExecutionTime = NullColumn(DateTime(timezone=True))
2 changes: 1 addition & 1 deletion diracx-db/src/diracx/db/sql/sandbox_metadata/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async def upsert_owner(self, user: UserInfo) -> int:
VO=user.vo,
)
result = await self.conn.execute(stmt)
return result.lastrowid
return result.inserted_primary_key[0]

@staticmethod
def get_pfn(bucket_name: str, user: UserInfo, sandbox_info: SandboxInfo) -> str:
Expand Down
6 changes: 4 additions & 2 deletions diracx-db/src/diracx/db/sql/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ def substract_date(**kwargs: float) -> datetime:


def EnumColumn(enum_type, **kwargs):
return Column(Enum(enum_type, native_enum=False, length=16), **kwargs)
return Column(
Enum(enum_type, native_enum=False, length=16, name=str(enum_type)), **kwargs
)


class EnumBackedBool(types.TypeDecorator):
Expand All @@ -136,7 +138,7 @@ class EnumBackedBool(types.TypeDecorator):
cache_ok: bool = True

def __init__(self) -> None:
super().__init__("True", "False")
super().__init__("True", "False", name="EnumBackedBool")

def process_bind_param(self, value, dialect) -> str:
if value is True:
Expand Down
Loading