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

feat: add rule dependencies #31

Merged
merged 2 commits into from
Sep 25, 2023
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pylint psycopg2
pip install pylint psycopg2 packaging
- name: Analysing the code with pylint
run: |
pylint $(git ls-files '*.py')
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ def get_version(rel_path: str) -> str:
"Programming Language :: Python :: 3 :: Only",
],
install_requires=[
'psycopg2',
'psycopg2',
'packaging',
],
entry_points={
"console_scripts": [
Expand Down
26 changes: 22 additions & 4 deletions src/doctor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from textwrap import dedent, fill
from os.path import dirname, basename, isfile, join
from abc import ABC
from packaging.version import parse

import psycopg2

Expand Down Expand Up @@ -54,14 +55,31 @@ class Rule(ABC):
optional field. If no field is given, the value of the "message"
field will be used.

dependencies: Dictionary with dependencies on packages and what
versions that are required.

"""

def get_versions(self, conn):
"""Get extension versions."""
with conn.cursor() as cursor:
cursor.execute("SELECT extname, extversion FROM pg_extension WHERE extname IN %s",
(tuple(self.dependencies.keys()),)) # pylint: disable=E1101
return {row["extname"]: row["extversion"] for row in cursor}


def execute(self, conn, text):
"""Execute rule and return one string for each mismatching object."""
cursor = conn.cursor()
cursor.execute(self.query) # pylint: disable=E1101
return [text.format(**kwrds) for kwrds in cursor]

with conn.cursor() as cursor:
# Check that all dependencies are met. If not, we do not
# execute the rule.
if hasattr(self, 'dependencies'):
versions = self.get_versions(conn)
for ext,req in self.dependencies.items(): # pylint: disable=E1101
if ext not in versions or parse(req) > parse(versions[ext]):
return []
cursor.execute(self.query) # pylint: disable=E1101
return [text.format(**kwrds) for kwrds in cursor]

def register(cls):
"""Register a rule."""
Expand Down
10 changes: 6 additions & 4 deletions src/doctor/rules/compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

"""Rules for compressed hypertables."""

from dataclasses import dataclass

import doctor

LINEAR_QUERY = """
Expand All @@ -30,7 +28,6 @@
"""

@doctor.register
@dataclass
class LinearSegmentby(doctor.Rule):
"""Detect segmentby column for compressed table."""

Expand All @@ -44,6 +41,9 @@ class LinearSegmentby(doctor.Rule):
" with the number of rows of the table."

)
dependencies: dict = {
'timescaledb': '1.0'
}

POINTLESS_QUERY = """
SELECT format('%I.%I', schema_name, table_name)::regclass AS relation, s.attname
Expand All @@ -57,7 +57,6 @@ class LinearSegmentby(doctor.Rule):
"""

@doctor.register
@dataclass
class PointlessSegmentBy(doctor.Rule):
"""Detect pointless segmentby column in compressed table."""

Expand All @@ -69,3 +68,6 @@ class PointlessSegmentBy(doctor.Rule):
"Column '{attname}' in hypertable '{relation}' as segment-by column is pointless"
" since it contains a single value."
)
dependencies: dict = {
'timescaledb': '1.0'
}
7 changes: 3 additions & 4 deletions src/doctor/rules/hypertable.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

"""Rules for hypertables."""

from dataclasses import dataclass

import doctor

CANDIDATE_QUERY = """
Expand Down Expand Up @@ -49,7 +47,6 @@
"""

@doctor.register
@dataclass
class HypertableCandidate(doctor.Rule):
"""Detect candidate hypertable."""

Expand All @@ -72,9 +69,11 @@ class HypertableCandidate(doctor.Rule):
"""

@doctor.register
@dataclass
class ChunkPermissions(doctor.Rule):
"""Detect bad chunk permissions."""

query: str = PERMISSION_QUERY
message: str = "Chunk '{chunk}' have different permissions from hypertable '{hypertable}'."
dependencies: dict = {
'timescaledb': '1.0'
}