• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SwissDataScienceCenter / renku-python / 5819037706

pending completion
5819037706

push

github-actions

web-flow
Merge pull request #3580 from SwissDataScienceCenter/release/v2.7.0

chore: release v2.7.0

227 of 312 new or added lines in 29 files covered. (72.76%)

689 existing lines in 73 files now uncovered.

25546 of 30119 relevant lines covered (84.82%)

8.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

79.45
/renku/command/command_builder/database.py
1
# Copyright Swiss Data Science Center (SDSC). A partnership between
2
# École Polytechnique Fédérale de Lausanne (EPFL) and
3
# Eidgenössische Technische Hochschule Zürich (ETHZ).
4
#
5
# Licensed under the Apache License, Version 2.0 (the "License");
6
# you may not use this file except in compliance with the License.
7
# You may obtain a copy of the License at
8
#
9
#     http://www.apache.org/licenses/LICENSE-2.0
10
#
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS,
13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
# See the License for the specific language governing permissions and
15
# limitations under the License.
16
"""Command builder for local object database."""
20✔
17

18

19
import json
20✔
20
import os
20✔
21
from typing import Optional
20✔
22

23
from packaging.version import Version
20✔
24

25
from renku.command.command_builder.command import Command, CommandResult, check_finalized
20✔
26
from renku.core import errors
20✔
27
from renku.core.interface.activity_gateway import IActivityGateway
20✔
28
from renku.core.interface.database_gateway import IDatabaseGateway
20✔
29
from renku.core.interface.dataset_gateway import IDatasetGateway
20✔
30
from renku.core.interface.plan_gateway import IPlanGateway
20✔
31
from renku.core.interface.project_gateway import IProjectGateway
20✔
32
from renku.core.interface.storage import IStorageFactory
20✔
33
from renku.domain_model.project_context import project_context
20✔
34
from renku.infrastructure.gateway.activity_gateway import ActivityGateway
20✔
35
from renku.infrastructure.gateway.database_gateway import DatabaseGateway
20✔
36
from renku.infrastructure.gateway.dataset_gateway import DatasetGateway
20✔
37
from renku.infrastructure.gateway.plan_gateway import PlanGateway
20✔
38
from renku.infrastructure.gateway.project_gateway import ProjectGateway
20✔
39
from renku.infrastructure.storage.factory import StorageFactory
20✔
40

41

42
class DatabaseCommand(Command):
20✔
43
    """Builder to get a database connection."""
44

45
    PRE_ORDER = 4
20✔
46
    POST_ORDER = 5
20✔
47

48
    def __init__(self, builder: Command, write: bool = False, path: Optional[str] = None, create: bool = False) -> None:
20✔
49
        self._builder = builder
20✔
50
        self._write = write
20✔
51
        self._path = path
20✔
52
        self._create = create
20✔
53
        self.project_found: bool = False
20✔
54

55
    def _injection_pre_hook(self, builder: Command, context: dict, *args, **kwargs) -> None:
20✔
56
        """Create a Database singleton."""
57
        from renku.version import __version__
20✔
58

59
        if not project_context.has_context():
20✔
60
            raise ValueError("Database builder needs a ProjectContext to be set.")
×
61

62
        project_context.push_path(path=self._path or project_context.path, save_changes=self._write)
20✔
63

64
        project_gateway = ProjectGateway()
20✔
65

66
        context["constructor_bindings"][IPlanGateway] = lambda: PlanGateway()
20✔
67
        context["constructor_bindings"][IActivityGateway] = lambda: ActivityGateway()
20✔
68
        context["constructor_bindings"][IDatabaseGateway] = lambda: DatabaseGateway()
20✔
69
        context["constructor_bindings"][IDatasetGateway] = lambda: DatasetGateway()
20✔
70
        context["constructor_bindings"][IProjectGateway] = lambda: project_gateway
20✔
71
        context["constructor_bindings"][IStorageFactory] = lambda: StorageFactory
20✔
72

73
        if int(os.environ.get("RENKU_SKIP_MIN_VERSION_CHECK", "0")) == 1:
20✔
74
            # NOTE: Used for unit tests
75
            return
20✔
76

77
        try:
2✔
78
            project = project_gateway.get_project()
2✔
79
            minimum_renku_version = Version(project.minimum_renku_version)
2✔
80
            self.project_found = True
2✔
81
        except (KeyError, ImportError, ValueError):
×
82
            try:
×
83
                with open(project_context.database_path / "project") as f:
×
84
                    project = json.load(f)
×
85
                    min_version = project.get("minimum_renku_version")
×
86
                    if min_version is None:
×
87
                        return
×
88
                    minimum_renku_version = Version(min_version)
×
89
            except (KeyError, OSError, json.JSONDecodeError):
×
90
                # NOTE: We don't check minimum version if there's no project metadata available
91
                return
×
92

93
        current_version = Version(__version__)
2✔
94

95
        if Version(current_version.base_version) < minimum_renku_version:
2✔
96
            raise errors.MinimumVersionError(current_version, minimum_renku_version)
2✔
97

98
    def _post_hook(self, builder: Command, context: dict, result: CommandResult, *args, **kwargs) -> None:
20✔
99
        from renku.domain_model.project import Project
20✔
100

101
        if self._write and self.project_found:
20✔
102
            # NOTE: Fetch project again in case it was updated (the current reference would be put of date)
UNCOV
103
            project_gateway = ProjectGateway()
×
UNCOV
104
            project = project_gateway.get_project()
×
105

UNCOV
106
            if Version(project.minimum_renku_version) < Version(Project.minimum_renku_version):
×
107
                # NOTE: update minimum renku version on write as migrations might happen on the fly
UNCOV
108
                project.minimum_renku_version = Project.minimum_renku_version
×
109

110
        project_context.pop_context()
20✔
111

112
    @check_finalized
20✔
113
    def build(self) -> Command:
20✔
114
        """Build the command."""
115
        self._builder.add_injection_pre_hook(self.PRE_ORDER, self._injection_pre_hook)
20✔
116
        self._builder.add_post_hook(self.POST_ORDER, self._post_hook)
20✔
117

118
        return self._builder.build()
20✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc