Skip to content
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 spinetoolbox/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1779,7 +1779,7 @@ def display_byte_size(size_bytes: int) -> tuple[float | int, str]:


def normcase_database_url_path(url: str) -> str:
if not url.startswith("sqlite://"):
if not url.startswith("sqlite:///"):
return url
path = url[len("sqlite:///") :]
return "sqlite:///" + os.path.normcase(path)
Expand Down
10 changes: 8 additions & 2 deletions spinetoolbox/spine_db_editor/widgets/multi_spine_db_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
from PySide6.QtCore import QPoint, Slot
from PySide6.QtGui import QFont, QIcon
from PySide6.QtWidgets import QMenu, QStatusBar, QToolButton
from sqlalchemy.engine.url import URL
from ...config import MAINWINDOW_SS, ONLINE_DOCUMENTATION_URL
from ...font import TOOLBOX_FONT
from ...helpers import CharIconEngine, open_url
from ...helpers import CharIconEngine, normcase_database_url_path, open_url
from ...widgets.multi_tab_window import MultiTabWindow
from ...widgets.settings_widget import SpineDBEditorSettingsWidget
from ..editors import db_editor_registry
Expand Down Expand Up @@ -241,7 +242,12 @@ def open_db_editor(db_urls, db_mngr, reuse_existing_editor):
if multi_db_editor.tab_load_success:
multi_db_editor.show()
return
existing = _get_existing_spine_db_editor(list(map(str, db_urls)))
normcased_urls = []
for url in db_urls:
if isinstance(url, URL):
url = url.render_as_string(hide_password=False)
normcased_urls.append(normcase_database_url_path(url))
existing = _get_existing_spine_db_editor(normcased_urls)
if existing is None:
multi_db_editor.add_new_tab(db_urls)
else:
Expand Down
6 changes: 3 additions & 3 deletions spinetoolbox/spine_db_editor/widgets/spine_db_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def open_db_file(self, _=False):
self.qsettings.endGroup()
if not file_path:
return
url = "sqlite:///" + os.path.normcase(file_path)
url = "sqlite:///" + file_path
self.load_db_urls([url])

@Slot(bool)
Expand All @@ -267,7 +267,7 @@ def add_db_file(self, _=False):
self.qsettings.endGroup()
if not file_path:
return
url = "sqlite:///" + os.path.normcase(file_path)
url = "sqlite:///" + file_path
self.load_db_urls(self.db_urls + [url])

@Slot()
Expand All @@ -283,7 +283,7 @@ def create_db_file(self) -> None:
os.remove(file_path)
except OSError:
pass
url = "sqlite:///" + os.path.normcase(file_path)
url = "sqlite:///" + file_path
self.load_db_urls([url], create=True)

@Slot()
Expand Down
10 changes: 8 additions & 2 deletions spinetoolbox/spine_db_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
from spinedb_api.temp_id import TempId
from .database_display_names import NameRegistry
from .fetch_parent import FetchParent
from .helpers import DBMapDictItems, DBMapPublicItems, busy_effect, plain_to_tool_tip
from .helpers import DBMapDictItems, DBMapPublicItems, busy_effect, normcase_database_url_path, plain_to_tool_tip
from .mvcmodels.shared import INVALID_TYPE, PARAMETER_TYPE_VALIDATION_ROLE, PARSED_ROLE, TYPE_NOT_VALIDATED, VALID_TYPE
from .parameter_type_validation import ParameterTypeValidator
from .spine_db_commands import (
Expand Down Expand Up @@ -297,6 +297,7 @@ def db_map(self, url: str) -> DatabaseMapping:
"""
if isinstance(url, URL):
url = url.render_as_string(hide_password=False)
url = normcase_database_url_path(url)
return self._db_maps.get(url)

def create_new_spine_database(self, url: str, logger: LoggerInterface, overwrite: bool = False):
Expand Down Expand Up @@ -324,6 +325,7 @@ def create_new_spine_database(self, url: str, logger: LoggerInterface, overwrite

def close_session(self, url: str) -> None:
"""Pops any db map on the given url and closes its connection."""
url = normcase_database_url_path(url)
self._no_prompt_urls.discard(url)
try:
db_map = self._db_maps.pop(url)
Expand Down Expand Up @@ -364,6 +366,7 @@ def get_db_map(
"""
if isinstance(url, URL):
url = url.render_as_string(hide_password=False)
url = normcase_database_url_path(url)
db_map = self._db_maps.get(url)
if db_map is not None:
return db_map
Expand Down Expand Up @@ -1433,7 +1436,10 @@ def export_data(
raise ValueError()

def _is_url_available(self, url: Union[URL, str], logger: LoggerInterface) -> bool:
if str(url) in self.db_urls:
if isinstance(url, URL):
url = url.render_as_string(hide_password=False)
url = normcase_database_url_path(url)
if url in self.db_urls:
message = f"The URL <b>{url}</b> is in use. Please close all applications using it and try again."
logger.msg_error.emit(message)
return False
Expand Down
5 changes: 3 additions & 2 deletions tests/spine_db_editor/widgets/test_multi_spine_db_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from unittest.mock import MagicMock, patch
from PySide6.QtCore import QPoint, QSettings
from PySide6.QtWidgets import QApplication
from spinetoolbox.helpers import normcase_database_url_path
from spinetoolbox.multi_tab_windows import MultiTabWindowRegistry
from spinetoolbox.spine_db_editor.widgets.multi_spine_db_editor import MultiSpineDBEditor, open_db_editor
from spinetoolbox.spine_db_manager import SpineDBManager
Expand Down Expand Up @@ -93,7 +94,7 @@ def test_open_db_in_tab_when_editor_has_an_empty_tab(self):
"spinetoolbox.spine_db_editor.widgets.multi_spine_db_editor.db_editor_registry",
self._db_editor_registry,
),
patch("spinetoolbox.spine_db_editor.widgets.multi_spine_db_editor.MultiSpineDBEditor.show") as mock_show,
patch("spinetoolbox.spine_db_editor.widgets.multi_spine_db_editor.MultiSpineDBEditor.show"),
):
self.assertFalse(self._db_editor_registry.has_windows())
window = MultiSpineDBEditor(self._db_mngr, [])
Expand All @@ -103,5 +104,5 @@ def test_open_db_in_tab_when_editor_has_an_empty_tab(self):
open_db_editor([self._db_url], self._db_mngr, reuse_existing_editor=True)
self.assertEqual(window.tab_widget.count(), 2)
tab = window.tab_widget.widget(1)
self.assertEqual(tab.db_urls, [self._db_url])
self.assertEqual(tab.db_urls, [normcase_database_url_path(self._db_url)])
self._close_windows()
1 change: 1 addition & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,7 @@ def test_correctness(self):
class TestNormcaseDatabaseUrlPath:
def test_correctness(self):
assert normcase_database_url_path("mysql://example.com/Path/MY_DB") == "mysql://example.com/Path/MY_DB"
assert normcase_database_url_path("sqlite://") == "sqlite://"
if sys.platform == "win32":
assert (
normcase_database_url_path("sqlite:///C:\\Users\\SansSerif\\in.sqlite")
Expand Down