libopenschichtplaner5 1.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- libopenschichtplaner5-1.1.0.dist-info/METADATA +113 -0
- libopenschichtplaner5-1.1.0.dist-info/RECORD +24 -0
- libopenschichtplaner5-1.1.0.dist-info/WHEEL +5 -0
- libopenschichtplaner5-1.1.0.dist-info/licenses/LICENSE +21 -0
- libopenschichtplaner5-1.1.0.dist-info/top_level.txt +1 -0
- sp5lib/__init__.py +1 -0
- sp5lib/_resource_paths.py +40 -0
- sp5lib/auto_migrate.py +435 -0
- sp5lib/color_utils.py +26 -0
- sp5lib/database.py +6240 -0
- sp5lib/db_config.py +39 -0
- sp5lib/db_factory.py +55 -0
- sp5lib/dbf_reader.py +212 -0
- sp5lib/dbf_writer.py +459 -0
- sp5lib/email_service.py +273 -0
- sp5lib/orm/__init__.py +22 -0
- sp5lib/orm/base.py +107 -0
- sp5lib/orm/models.py +264 -0
- sp5lib/orm/models_pg.py +321 -0
- sp5lib/orm/repository.py +185 -0
- sp5lib/orm/sync.py +160 -0
- sp5lib/pg_database.py +1132 -0
- sp5lib/py.typed +0 -0
- sp5lib/sqlite_adapter.py +289 -0
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: libopenschichtplaner5
|
|
3
|
+
Version: 1.1.0
|
|
4
|
+
Summary: Read and write the original Schichtplaner5 FoxPro .DBF database files, plus the SQLite/PostgreSQL ORM and sync layer used by OpenSchichtplaner5.
|
|
5
|
+
Author-email: Matthias Schabhüttl <matthias@matthiasschabhuettl.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/mschabhuettl/libopenschichtplaner5
|
|
8
|
+
Project-URL: Source, https://github.com/mschabhuettl/libopenschichtplaner5
|
|
9
|
+
Project-URL: Changelog, https://github.com/mschabhuettl/libopenschichtplaner5/blob/main/CHANGELOG.md
|
|
10
|
+
Project-URL: Issues, https://github.com/mschabhuettl/libopenschichtplaner5/issues
|
|
11
|
+
Project-URL: Main application, https://github.com/mschabhuettl/openschichtplaner5
|
|
12
|
+
Keywords: schichtplaner5,dbf,foxpro,shift-planning,dbase
|
|
13
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Topic :: Database
|
|
22
|
+
Classifier: Topic :: Office/Business :: Scheduling
|
|
23
|
+
Classifier: Typing :: Typed
|
|
24
|
+
Requires-Python: >=3.10
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
License-File: LICENSE
|
|
27
|
+
Requires-Dist: SQLAlchemy>=2.0.0
|
|
28
|
+
Requires-Dist: alembic>=1.13.0
|
|
29
|
+
Requires-Dist: bcrypt>=4.0.0
|
|
30
|
+
Requires-Dist: pyotp>=2.9.0
|
|
31
|
+
Requires-Dist: packaging>=21.0
|
|
32
|
+
Provides-Extra: postgres
|
|
33
|
+
Requires-Dist: psycopg2-binary>=2.9.0; extra == "postgres"
|
|
34
|
+
Provides-Extra: dev
|
|
35
|
+
Requires-Dist: pytest>=7.4.0; extra == "dev"
|
|
36
|
+
Requires-Dist: pytest-cov>=4.1.0; extra == "dev"
|
|
37
|
+
Requires-Dist: ruff>=0.1.0; extra == "dev"
|
|
38
|
+
Dynamic: license-file
|
|
39
|
+
|
|
40
|
+
# libopenschichtplaner5
|
|
41
|
+
|
|
42
|
+
[](https://github.com/mschabhuettl/libopenschichtplaner5/actions/workflows/ci.yml)
|
|
43
|
+
[](LICENSE)
|
|
44
|
+
|
|
45
|
+
The core library behind [**OpenSchichtplaner5**](https://github.com/mschabhuettl/openschichtplaner5) —
|
|
46
|
+
a pip-installable package that reads **and writes** the original *Schichtplaner5*
|
|
47
|
+
FoxPro/dBASE `.DBF` database files, so an open replacement can run on the exact same
|
|
48
|
+
data as the proprietary Windows tool, with no migration.
|
|
49
|
+
|
|
50
|
+
> **Import name:** the distribution is `libopenschichtplaner5`, but the importable
|
|
51
|
+
> package keeps its historical name **`sp5lib`** (like `pip install PyYAML` → `import yaml`).
|
|
52
|
+
> This keeps OpenSchichtplaner5's imports unchanged after the extraction.
|
|
53
|
+
|
|
54
|
+
## What's inside
|
|
55
|
+
|
|
56
|
+
| Module | Purpose |
|
|
57
|
+
|---|---|
|
|
58
|
+
| `sp5lib.dbf_reader` | Pure-Python DBF reader (UTF-16-LE detection, date parsing, field decode) |
|
|
59
|
+
| `sp5lib.dbf_writer` | Safe DBF writer — exclusive `flock`, TOCTOU-safe record count, rollback, EOF marker preservation |
|
|
60
|
+
| `sp5lib.database` | High-level `SP5Database` facade over the DBF tables (employees, shifts, schedule, absences, auth, 2FA …) |
|
|
61
|
+
| `sp5lib.db_factory` / `sqlite_adapter` / `pg_database` | Optional SQLite / PostgreSQL backends |
|
|
62
|
+
| `sp5lib.orm` | SQLAlchemy models (`models.py` SQLite, `models_pg.py` Postgres), `repository`, `sync` |
|
|
63
|
+
| `sp5lib.auto_migrate` | Alembic-based automatic migrations |
|
|
64
|
+
| `sp5lib.email_service` | SMTP notification emails (HTML-escaped templates) |
|
|
65
|
+
| `sp5lib.color_utils` | FoxPro BGR ↔ hex/RGB color helpers |
|
|
66
|
+
|
|
67
|
+
## Installation
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
pip install "libopenschichtplaner5 @ git+https://github.com/mschabhuettl/libopenschichtplaner5.git"
|
|
71
|
+
# with the optional PostgreSQL backend:
|
|
72
|
+
pip install "libopenschichtplaner5[postgres] @ git+https://github.com/mschabhuettl/libopenschichtplaner5.git"
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Usage
|
|
76
|
+
|
|
77
|
+
```python
|
|
78
|
+
from sp5lib.database import SP5Database
|
|
79
|
+
|
|
80
|
+
db = SP5Database("/path/to/SP5/Daten") # directory of .DBF files
|
|
81
|
+
for emp in db.get_employees():
|
|
82
|
+
print(emp["ID"], emp["NAME"], emp["FIRSTNAME"])
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Low-level DBF access:
|
|
86
|
+
|
|
87
|
+
```python
|
|
88
|
+
from sp5lib.dbf_reader import read_dbf
|
|
89
|
+
from sp5lib.dbf_writer import append_record, get_table_fields
|
|
90
|
+
|
|
91
|
+
rows = read_dbf("/path/to/SP5/Daten/5EMPL.DBF")
|
|
92
|
+
fields = get_table_fields("/path/to/SP5/Daten/5EMPL.DBF")
|
|
93
|
+
append_record("/path/to/SP5/Daten/5NOTE.DBF", fields, {"ID": 1, "TEXT": "hello"})
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## Dependencies
|
|
97
|
+
|
|
98
|
+
Runtime: `SQLAlchemy`, `alembic`, `bcrypt`, `pyotp`, `packaging`.
|
|
99
|
+
Optional: `psycopg2-binary` (via the `postgres` extra) for the PostgreSQL backend.
|
|
100
|
+
|
|
101
|
+
## Development
|
|
102
|
+
|
|
103
|
+
```bash
|
|
104
|
+
python -m venv .venv && . .venv/bin/activate
|
|
105
|
+
pip install -e ".[dev,postgres]"
|
|
106
|
+
pytest
|
|
107
|
+
ruff check .
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## License
|
|
111
|
+
|
|
112
|
+
MIT — see [LICENSE](LICENSE). Extracted (with full git history) from
|
|
113
|
+
[OpenSchichtplaner5](https://github.com/mschabhuettl/openschichtplaner5)'s `backend/sp5lib/`.
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
libopenschichtplaner5-1.1.0.dist-info/licenses/LICENSE,sha256=AR48Bxp-u4fRBkNgKZLE9NxZ30ENMrDgUVRTd4tPjUU,1077
|
|
2
|
+
sp5lib/__init__.py,sha256=-gSQSDrTSETsnGjUoFVGpfl5t6na-c6MUwzkhQmKGX4,75
|
|
3
|
+
sp5lib/_resource_paths.py,sha256=Dx0RszegjfLCyKDTrwLz-56HrJUIC0xrkyXExrrW59I,1565
|
|
4
|
+
sp5lib/auto_migrate.py,sha256=PQrVgV9V2tkUVir5LNknIzEueR0tfhX2Vf-VJFXYJwY,14906
|
|
5
|
+
sp5lib/color_utils.py,sha256=tLNKjsqZYsctRi4DtEbgCDS_SJu1LCrwrqtHDplAPS8,781
|
|
6
|
+
sp5lib/database.py,sha256=UVfefYPI0ywQ_fYlFbqCIJq7xkZoPsRsKs4cmmnmB_Y,250986
|
|
7
|
+
sp5lib/db_config.py,sha256=CaPEFcQwE8xBvAUAy8DXgaAngR8FbXTXBzF-NhfwMP0,1195
|
|
8
|
+
sp5lib/db_factory.py,sha256=0_ISRa3YbWxcOofY2V2TMUudAT14roYG9YvUMF1N6XI,1845
|
|
9
|
+
sp5lib/dbf_reader.py,sha256=7ywFrFMeOooFEnPi-Q1yuLOi4YizBR8bQPaVaq5z3bY,7378
|
|
10
|
+
sp5lib/dbf_writer.py,sha256=DMPHyfCRylzSTcg464hZxiflSD5BGfY5r2CsgMu0cFI,15803
|
|
11
|
+
sp5lib/email_service.py,sha256=dzh8IvmooqGJTjiYjrHRzZZVdWlWlGSZMdtiKWNZtEw,9801
|
|
12
|
+
sp5lib/pg_database.py,sha256=aLpkuYY9cFdpjhpDjotqb3DX3KTDRx3vDl1qd3JliVE,49982
|
|
13
|
+
sp5lib/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
14
|
+
sp5lib/sqlite_adapter.py,sha256=MM_2vuYY2id_gSdBxyfY9t_SDYAJQJi-OSKVruPXuBA,11475
|
|
15
|
+
sp5lib/orm/__init__.py,sha256=ONwaCSqR0uuWbLdCfNVCU7nJrlEMUIxshN6vOiPZNNI,741
|
|
16
|
+
sp5lib/orm/base.py,sha256=mUeZj7RvMLxflSzUPk3EZ_tFw6qSopS0qNhFF3lM9l0,3199
|
|
17
|
+
sp5lib/orm/models.py,sha256=ILCG12TMZK_1ePyHAQk6MuRm3ZCNq2RRECsQcGE7vCE,9078
|
|
18
|
+
sp5lib/orm/models_pg.py,sha256=oFTO9sZS7WWXT_kd0HMIIRoDOukG2XdbNLn9vaEgeBw,16196
|
|
19
|
+
sp5lib/orm/repository.py,sha256=dtkSv8541b8OEWlt9AD62OxPB533u3Krlg8Q-tun0Tc,6782
|
|
20
|
+
sp5lib/orm/sync.py,sha256=5TcI9N1O03V76ILXMDPtDzi8oXb-X0p96iQ6DgYQCJU,5119
|
|
21
|
+
libopenschichtplaner5-1.1.0.dist-info/METADATA,sha256=dLybGQrp3Z-atBR78ecPxCB0Y3cXf78Nt_5AYAPur0Y,4782
|
|
22
|
+
libopenschichtplaner5-1.1.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
23
|
+
libopenschichtplaner5-1.1.0.dist-info/top_level.txt,sha256=5BuXleV-Wxx0nNkE7V-ig1pkPvHvUV5R7mZEE3zlQ6Q,7
|
|
24
|
+
libopenschichtplaner5-1.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Matthias Schabhüttl
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
sp5lib
|
sp5lib/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# OpenSchichtplaner5 - Python library for reading Schichtplaner5 databases
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"""Resolution of host-application resource directories.
|
|
2
|
+
|
|
3
|
+
Historically these modules lived inside the OpenSchichtplaner5 ``backend/`` tree and
|
|
4
|
+
located sibling resources (``backend/data``, ``backend/api/data``, the Alembic
|
|
5
|
+
directory) via paths relative to ``__file__``. Once this library is installed as a
|
|
6
|
+
standalone package (``libopenschichtplaner5``) that assumption no longer holds — the
|
|
7
|
+
package lives in ``site-packages`` and the host app's data lives elsewhere.
|
|
8
|
+
|
|
9
|
+
The host application therefore tells the library where its backend root is via the
|
|
10
|
+
``SP5_BACKEND_DIR`` environment variable. When unset, we fall back to the legacy
|
|
11
|
+
``<this_package_parent>`` location so an in-tree checkout keeps working unchanged.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
import os
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def backend_dir() -> str:
|
|
20
|
+
"""Return the host application's backend root directory.
|
|
21
|
+
|
|
22
|
+
Honors ``SP5_BACKEND_DIR``; otherwise falls back to the directory that contains
|
|
23
|
+
this package (the legacy in-tree layout ``<backend>/sp5lib/``).
|
|
24
|
+
"""
|
|
25
|
+
env = os.environ.get("SP5_BACKEND_DIR")
|
|
26
|
+
if env:
|
|
27
|
+
return os.path.abspath(env)
|
|
28
|
+
return os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def data_dir() -> str:
|
|
32
|
+
"""``<backend>/data`` — JSON-backed settings (changelog, swaps, comments …)."""
|
|
33
|
+
path = os.path.join(backend_dir(), "data")
|
|
34
|
+
os.makedirs(path, exist_ok=True)
|
|
35
|
+
return path
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def api_data_dir() -> str:
|
|
39
|
+
"""``<backend>/api/data`` — availability / skills JSON written by the API layer."""
|
|
40
|
+
return os.path.join(backend_dir(), "api", "data")
|
sp5lib/auto_migrate.py
ADDED
|
@@ -0,0 +1,435 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Automatic database migration on application startup.
|
|
3
|
+
|
|
4
|
+
Compares the app's schema version against the DB's stored schema version.
|
|
5
|
+
If the DB is behind, runs Alembic migrations automatically (for PostgreSQL)
|
|
6
|
+
or applies schema extensions (for DBF).
|
|
7
|
+
|
|
8
|
+
Configuration via environment variables:
|
|
9
|
+
AUTO_MIGRATE=false → Disable auto-migration (default: true)
|
|
10
|
+
AUTO_BACKUP=false → Disable pre-migration backup (default: true)
|
|
11
|
+
|
|
12
|
+
Usage:
|
|
13
|
+
from sp5lib.auto_migrate import run_startup_migration
|
|
14
|
+
run_startup_migration() # Call once during app lifespan startup
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
import logging
|
|
18
|
+
import os
|
|
19
|
+
import shutil
|
|
20
|
+
import subprocess
|
|
21
|
+
from datetime import UTC, datetime
|
|
22
|
+
from pathlib import Path
|
|
23
|
+
|
|
24
|
+
from ._resource_paths import backend_dir as _backend_dir
|
|
25
|
+
|
|
26
|
+
_log = logging.getLogger("sp5api.auto_migrate")
|
|
27
|
+
|
|
28
|
+
# The current app schema version — bump this when adding new migrations.
|
|
29
|
+
# Format: Alembic revision head. We read it dynamically from Alembic.
|
|
30
|
+
APP_SCHEMA_VERSION = "head"
|
|
31
|
+
|
|
32
|
+
def _backend_path() -> Path:
|
|
33
|
+
"""Backend root directory (where alembic.ini lives), resolved lazily.
|
|
34
|
+
|
|
35
|
+
Honors SP5_BACKEND_DIR so the host app can point at its alembic/ tree when this
|
|
36
|
+
package is installed standalone. Evaluated on each call so it picks up the env
|
|
37
|
+
var regardless of import order.
|
|
38
|
+
"""
|
|
39
|
+
return Path(_backend_dir())
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _is_auto_migrate_enabled() -> bool:
|
|
43
|
+
"""Check if auto-migration is enabled via AUTO_MIGRATE env var."""
|
|
44
|
+
val = os.environ.get("AUTO_MIGRATE", "true").lower().strip()
|
|
45
|
+
return val not in ("false", "0", "no", "off")
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _is_auto_backup_enabled() -> bool:
|
|
49
|
+
"""Check if pre-migration backup is enabled via AUTO_BACKUP env var."""
|
|
50
|
+
val = os.environ.get("AUTO_BACKUP", "true").lower().strip()
|
|
51
|
+
return val not in ("false", "0", "no", "off")
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _get_alembic_head() -> str | None:
|
|
55
|
+
"""Get the current Alembic head revision from the migration scripts."""
|
|
56
|
+
try:
|
|
57
|
+
from alembic.config import Config
|
|
58
|
+
from alembic.script import ScriptDirectory
|
|
59
|
+
|
|
60
|
+
alembic_cfg = Config(str(_backend_path() / "alembic.ini"))
|
|
61
|
+
script = ScriptDirectory.from_config(alembic_cfg)
|
|
62
|
+
head = script.get_current_head()
|
|
63
|
+
return head
|
|
64
|
+
except Exception as exc:
|
|
65
|
+
_log.warning("Could not determine Alembic head revision: %s", exc)
|
|
66
|
+
return None
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _get_db_revision(database_url: str) -> str | None:
|
|
70
|
+
"""Get the current Alembic revision stored in the database."""
|
|
71
|
+
try:
|
|
72
|
+
from sqlalchemy import create_engine, inspect, text
|
|
73
|
+
|
|
74
|
+
engine = create_engine(database_url)
|
|
75
|
+
with engine.connect() as conn:
|
|
76
|
+
# Check if alembic_version table exists
|
|
77
|
+
inspector = inspect(engine)
|
|
78
|
+
tables = inspector.get_table_names()
|
|
79
|
+
if "alembic_version" not in tables:
|
|
80
|
+
return None
|
|
81
|
+
result = conn.execute(text("SELECT version_num FROM alembic_version LIMIT 1"))
|
|
82
|
+
row = result.fetchone()
|
|
83
|
+
return row[0] if row else None
|
|
84
|
+
except Exception as exc:
|
|
85
|
+
_log.warning("Could not read DB revision: %s", exc)
|
|
86
|
+
return None
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def _create_pg_backup(database_url: str) -> str | None:
|
|
90
|
+
"""Create a PostgreSQL backup before migration using pg_dump.
|
|
91
|
+
|
|
92
|
+
Returns the backup file path on success, None on failure.
|
|
93
|
+
"""
|
|
94
|
+
backup_dir = _backend_path() / "backups" / "pre_migration"
|
|
95
|
+
backup_dir.mkdir(parents=True, exist_ok=True)
|
|
96
|
+
timestamp = datetime.now(UTC).strftime("%Y%m%d_%H%M%S")
|
|
97
|
+
backup_file = backup_dir / f"pre_migration_{timestamp}.sql"
|
|
98
|
+
|
|
99
|
+
try:
|
|
100
|
+
# Parse DATABASE_URL for pg_dump
|
|
101
|
+
# Format: postgresql://user:pass@host:port/dbname
|
|
102
|
+
from urllib.parse import urlparse
|
|
103
|
+
|
|
104
|
+
parsed = urlparse(database_url)
|
|
105
|
+
env = os.environ.copy()
|
|
106
|
+
if parsed.password:
|
|
107
|
+
env["PGPASSWORD"] = parsed.password
|
|
108
|
+
|
|
109
|
+
cmd = [
|
|
110
|
+
"pg_dump",
|
|
111
|
+
"-h", parsed.hostname or "localhost",
|
|
112
|
+
"-p", str(parsed.port or 5432),
|
|
113
|
+
"-U", parsed.username or "postgres",
|
|
114
|
+
"-d", parsed.path.lstrip("/"),
|
|
115
|
+
"-f", str(backup_file),
|
|
116
|
+
"--no-owner",
|
|
117
|
+
"--no-acl",
|
|
118
|
+
]
|
|
119
|
+
result = subprocess.run(cmd, env=env, capture_output=True, text=True, timeout=120)
|
|
120
|
+
if result.returncode == 0:
|
|
121
|
+
_log.info("Pre-migration backup created: %s", backup_file)
|
|
122
|
+
return str(backup_file)
|
|
123
|
+
else:
|
|
124
|
+
_log.warning("pg_dump failed (rc=%d): %s", result.returncode, result.stderr)
|
|
125
|
+
return None
|
|
126
|
+
except FileNotFoundError:
|
|
127
|
+
_log.warning("pg_dump not found — skipping PostgreSQL backup")
|
|
128
|
+
return None
|
|
129
|
+
except Exception as exc:
|
|
130
|
+
_log.warning("Pre-migration backup failed: %s", exc)
|
|
131
|
+
return None
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def _create_dbf_backup(db_path: str) -> str | None:
|
|
135
|
+
"""Create a DBF backup by copying the data directory.
|
|
136
|
+
|
|
137
|
+
Returns the backup directory path on success, None on failure.
|
|
138
|
+
"""
|
|
139
|
+
if not os.path.isdir(db_path):
|
|
140
|
+
_log.warning("DBF path does not exist, skipping backup: %s", db_path)
|
|
141
|
+
return None
|
|
142
|
+
|
|
143
|
+
backup_base = _backend_path() / "backups" / "pre_migration"
|
|
144
|
+
backup_base.mkdir(parents=True, exist_ok=True)
|
|
145
|
+
timestamp = datetime.now(UTC).strftime("%Y%m%d_%H%M%S")
|
|
146
|
+
backup_dir = backup_base / f"dbf_pre_migration_{timestamp}"
|
|
147
|
+
|
|
148
|
+
try:
|
|
149
|
+
shutil.copytree(db_path, str(backup_dir))
|
|
150
|
+
_log.info("Pre-migration DBF backup created: %s", backup_dir)
|
|
151
|
+
return str(backup_dir)
|
|
152
|
+
except Exception as exc:
|
|
153
|
+
_log.warning("DBF backup failed: %s", exc)
|
|
154
|
+
return None
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def _run_alembic_upgrade() -> list[str]:
|
|
158
|
+
"""Run Alembic upgrade to head. Returns list of applied revision IDs."""
|
|
159
|
+
applied = []
|
|
160
|
+
try:
|
|
161
|
+
from alembic import command
|
|
162
|
+
from alembic.config import Config
|
|
163
|
+
|
|
164
|
+
alembic_cfg = Config(str(_backend_path() / "alembic.ini"))
|
|
165
|
+
# Ensure DATABASE_URL is in the config
|
|
166
|
+
database_url = os.environ.get("DATABASE_URL")
|
|
167
|
+
if database_url:
|
|
168
|
+
alembic_cfg.set_main_option("sqlalchemy.url", database_url)
|
|
169
|
+
|
|
170
|
+
# Capture which revisions are applied
|
|
171
|
+
from alembic.script import ScriptDirectory
|
|
172
|
+
|
|
173
|
+
script = ScriptDirectory.from_config(alembic_cfg)
|
|
174
|
+
current_rev = _get_db_revision(
|
|
175
|
+
alembic_cfg.get_main_option("sqlalchemy.url") or database_url or ""
|
|
176
|
+
)
|
|
177
|
+
|
|
178
|
+
# Run upgrade
|
|
179
|
+
command.upgrade(alembic_cfg, "head")
|
|
180
|
+
|
|
181
|
+
# Determine which revisions were applied
|
|
182
|
+
new_rev = _get_db_revision(
|
|
183
|
+
alembic_cfg.get_main_option("sqlalchemy.url") or database_url or ""
|
|
184
|
+
)
|
|
185
|
+
if current_rev != new_rev:
|
|
186
|
+
# Walk from current to head to list applied revisions
|
|
187
|
+
for rev in script.walk_revisions(new_rev, current_rev):
|
|
188
|
+
if rev.revision != current_rev:
|
|
189
|
+
applied.append(rev.revision)
|
|
190
|
+
|
|
191
|
+
_log.info("Alembic upgrade complete. Applied %d revision(s): %s",
|
|
192
|
+
len(applied), applied)
|
|
193
|
+
except Exception as exc:
|
|
194
|
+
_log.error("Alembic upgrade failed: %s", exc, exc_info=True)
|
|
195
|
+
raise
|
|
196
|
+
|
|
197
|
+
return applied
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def _get_dbf_schema_version(db_path: str) -> str | None:
|
|
201
|
+
"""Read the schema version from a .sp5_version file in the DBF directory."""
|
|
202
|
+
version_file = os.path.join(db_path, ".sp5_schema_version")
|
|
203
|
+
if os.path.exists(version_file):
|
|
204
|
+
try:
|
|
205
|
+
return Path(version_file).read_text().strip()
|
|
206
|
+
except Exception:
|
|
207
|
+
return None
|
|
208
|
+
return None
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
def _set_dbf_schema_version(db_path: str, version: str) -> None:
|
|
212
|
+
"""Write the schema version to a .sp5_version file in the DBF directory."""
|
|
213
|
+
version_file = os.path.join(db_path, ".sp5_schema_version")
|
|
214
|
+
try:
|
|
215
|
+
Path(version_file).write_text(version)
|
|
216
|
+
except Exception as exc:
|
|
217
|
+
_log.warning("Could not write DBF schema version: %s", exc)
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
# Current DBF schema version — bump when adding new DBF schema extensions
|
|
221
|
+
DBF_SCHEMA_VERSION = "1.0.0"
|
|
222
|
+
|
|
223
|
+
# DBF schema extensions registry: version → callable
|
|
224
|
+
# Each callable receives (db_path: str) and applies its schema changes.
|
|
225
|
+
_DBF_EXTENSIONS: dict[str, list] = {
|
|
226
|
+
# Example for future use:
|
|
227
|
+
# "1.1.0": [_add_new_dbf_field],
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def _apply_dbf_extensions(db_path: str, current_version: str | None) -> list[str]:
|
|
232
|
+
"""Apply pending DBF schema extensions.
|
|
233
|
+
|
|
234
|
+
Returns list of version strings that were applied.
|
|
235
|
+
"""
|
|
236
|
+
from packaging.version import InvalidVersion, Version
|
|
237
|
+
|
|
238
|
+
applied = []
|
|
239
|
+
try:
|
|
240
|
+
current = Version(current_version) if current_version else Version("0.0.0")
|
|
241
|
+
except InvalidVersion:
|
|
242
|
+
current = Version("0.0.0")
|
|
243
|
+
|
|
244
|
+
for ver_str, funcs in sorted(_DBF_EXTENSIONS.items()):
|
|
245
|
+
try:
|
|
246
|
+
ver = Version(ver_str)
|
|
247
|
+
except InvalidVersion:
|
|
248
|
+
continue
|
|
249
|
+
if ver > current:
|
|
250
|
+
_log.info("Applying DBF schema extension v%s...", ver_str)
|
|
251
|
+
for func in funcs:
|
|
252
|
+
func(db_path)
|
|
253
|
+
applied.append(ver_str)
|
|
254
|
+
|
|
255
|
+
if applied:
|
|
256
|
+
_set_dbf_schema_version(db_path, DBF_SCHEMA_VERSION)
|
|
257
|
+
_log.info("DBF schema updated to v%s. Applied: %s", DBF_SCHEMA_VERSION, applied)
|
|
258
|
+
|
|
259
|
+
return applied
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
class MigrationResult:
|
|
263
|
+
"""Result of a startup migration run."""
|
|
264
|
+
|
|
265
|
+
def __init__(self):
|
|
266
|
+
self.backend: str = ""
|
|
267
|
+
self.skipped: bool = False
|
|
268
|
+
self.skip_reason: str = ""
|
|
269
|
+
self.backup_path: str | None = None
|
|
270
|
+
self.migrations_applied: list[str] = []
|
|
271
|
+
self.previous_version: str | None = None
|
|
272
|
+
self.current_version: str | None = None
|
|
273
|
+
self.error: str | None = None
|
|
274
|
+
|
|
275
|
+
@property
|
|
276
|
+
def success(self) -> bool:
|
|
277
|
+
return self.error is None
|
|
278
|
+
|
|
279
|
+
@property
|
|
280
|
+
def had_migrations(self) -> bool:
|
|
281
|
+
return len(self.migrations_applied) > 0
|
|
282
|
+
|
|
283
|
+
def to_dict(self) -> dict:
|
|
284
|
+
return {
|
|
285
|
+
"backend": self.backend,
|
|
286
|
+
"skipped": self.skipped,
|
|
287
|
+
"skip_reason": self.skip_reason,
|
|
288
|
+
"backup_path": self.backup_path,
|
|
289
|
+
"migrations_applied": self.migrations_applied,
|
|
290
|
+
"previous_version": self.previous_version,
|
|
291
|
+
"current_version": self.current_version,
|
|
292
|
+
"error": self.error,
|
|
293
|
+
"success": self.success,
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
def __repr__(self) -> str:
|
|
297
|
+
if self.skipped:
|
|
298
|
+
return f"MigrationResult(skipped={self.skip_reason})"
|
|
299
|
+
if self.error:
|
|
300
|
+
return f"MigrationResult(error={self.error})"
|
|
301
|
+
return (
|
|
302
|
+
f"MigrationResult(backend={self.backend}, "
|
|
303
|
+
f"applied={len(self.migrations_applied)}, "
|
|
304
|
+
f"{self.previous_version} → {self.current_version})"
|
|
305
|
+
)
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
def run_startup_migration() -> MigrationResult:
|
|
309
|
+
"""Run automatic database migration on startup.
|
|
310
|
+
|
|
311
|
+
This is the main entry point. Call once during application lifespan startup.
|
|
312
|
+
|
|
313
|
+
Returns:
|
|
314
|
+
MigrationResult with details about what happened.
|
|
315
|
+
"""
|
|
316
|
+
result = MigrationResult()
|
|
317
|
+
|
|
318
|
+
# Check if auto-migration is enabled
|
|
319
|
+
if not _is_auto_migrate_enabled():
|
|
320
|
+
result.skipped = True
|
|
321
|
+
result.skip_reason = "AUTO_MIGRATE=false"
|
|
322
|
+
_log.info("Auto-migration disabled (AUTO_MIGRATE=false)")
|
|
323
|
+
return result
|
|
324
|
+
|
|
325
|
+
from .db_config import BACKEND_POSTGRESQL, get_db_backend
|
|
326
|
+
|
|
327
|
+
backend = get_db_backend()
|
|
328
|
+
result.backend = backend
|
|
329
|
+
|
|
330
|
+
if backend == BACKEND_POSTGRESQL:
|
|
331
|
+
return _migrate_postgresql(result)
|
|
332
|
+
else:
|
|
333
|
+
return _migrate_dbf(result)
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
def _migrate_postgresql(result: MigrationResult) -> MigrationResult:
|
|
337
|
+
"""Handle PostgreSQL auto-migration via Alembic."""
|
|
338
|
+
from .db_config import get_database_url
|
|
339
|
+
|
|
340
|
+
database_url = get_database_url()
|
|
341
|
+
if not database_url:
|
|
342
|
+
result.skipped = True
|
|
343
|
+
result.skip_reason = "DATABASE_URL not set"
|
|
344
|
+
_log.warning("PostgreSQL backend but DATABASE_URL not set — skipping migration")
|
|
345
|
+
return result
|
|
346
|
+
|
|
347
|
+
# Get current DB revision
|
|
348
|
+
current_rev = _get_db_revision(database_url)
|
|
349
|
+
result.previous_version = current_rev
|
|
350
|
+
|
|
351
|
+
# Get target (head) revision
|
|
352
|
+
head_rev = _get_alembic_head()
|
|
353
|
+
|
|
354
|
+
if current_rev == head_rev and current_rev is not None:
|
|
355
|
+
result.current_version = current_rev
|
|
356
|
+
result.skipped = True
|
|
357
|
+
result.skip_reason = "already at head"
|
|
358
|
+
_log.info("Database already at head revision (%s) — no migration needed", current_rev)
|
|
359
|
+
return result
|
|
360
|
+
|
|
361
|
+
_log.info("Migration needed: DB=%s → head=%s", current_rev or "(empty)", head_rev)
|
|
362
|
+
|
|
363
|
+
# Create backup before migration
|
|
364
|
+
if _is_auto_backup_enabled():
|
|
365
|
+
result.backup_path = _create_pg_backup(database_url)
|
|
366
|
+
|
|
367
|
+
# Run Alembic upgrade
|
|
368
|
+
try:
|
|
369
|
+
applied = _run_alembic_upgrade()
|
|
370
|
+
result.migrations_applied = applied
|
|
371
|
+
result.current_version = _get_db_revision(database_url)
|
|
372
|
+
_log.info(
|
|
373
|
+
"PostgreSQL migration complete: %s → %s (%d revision(s))",
|
|
374
|
+
result.previous_version or "(empty)",
|
|
375
|
+
result.current_version,
|
|
376
|
+
len(applied),
|
|
377
|
+
)
|
|
378
|
+
except Exception as exc:
|
|
379
|
+
result.error = str(exc)
|
|
380
|
+
_log.error("PostgreSQL migration failed: %s", exc)
|
|
381
|
+
|
|
382
|
+
return result
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
def _migrate_dbf(result: MigrationResult) -> MigrationResult:
|
|
386
|
+
"""Handle DBF schema extensions."""
|
|
387
|
+
db_path = os.environ.get(
|
|
388
|
+
"SP5_DB_PATH",
|
|
389
|
+
os.path.join(os.path.dirname(__file__), "..", "..", "..", "sp5_db", "Daten"),
|
|
390
|
+
)
|
|
391
|
+
db_path = os.path.normpath(db_path)
|
|
392
|
+
|
|
393
|
+
if not os.path.isdir(db_path):
|
|
394
|
+
result.skipped = True
|
|
395
|
+
result.skip_reason = f"DBF path not found: {db_path}"
|
|
396
|
+
_log.warning("DBF directory not found: %s — skipping migration", db_path)
|
|
397
|
+
return result
|
|
398
|
+
|
|
399
|
+
current_version = _get_dbf_schema_version(db_path)
|
|
400
|
+
result.previous_version = current_version
|
|
401
|
+
|
|
402
|
+
if current_version == DBF_SCHEMA_VERSION:
|
|
403
|
+
result.current_version = current_version
|
|
404
|
+
result.skipped = True
|
|
405
|
+
result.skip_reason = "already at current version"
|
|
406
|
+
_log.info("DBF schema already at v%s — no migration needed", current_version)
|
|
407
|
+
return result
|
|
408
|
+
|
|
409
|
+
_log.info("DBF migration needed: %s → %s",
|
|
410
|
+
current_version or "(none)", DBF_SCHEMA_VERSION)
|
|
411
|
+
|
|
412
|
+
# Create backup before migration
|
|
413
|
+
if _is_auto_backup_enabled():
|
|
414
|
+
result.backup_path = _create_dbf_backup(db_path)
|
|
415
|
+
|
|
416
|
+
# Apply extensions
|
|
417
|
+
try:
|
|
418
|
+
applied = _apply_dbf_extensions(db_path, current_version)
|
|
419
|
+
result.migrations_applied = applied
|
|
420
|
+
|
|
421
|
+
# Even if no extensions to apply, stamp the version
|
|
422
|
+
_set_dbf_schema_version(db_path, DBF_SCHEMA_VERSION)
|
|
423
|
+
result.current_version = DBF_SCHEMA_VERSION
|
|
424
|
+
|
|
425
|
+
_log.info(
|
|
426
|
+
"DBF migration complete: %s → %s (%d extension(s))",
|
|
427
|
+
result.previous_version or "(none)",
|
|
428
|
+
result.current_version,
|
|
429
|
+
len(applied),
|
|
430
|
+
)
|
|
431
|
+
except Exception as exc:
|
|
432
|
+
result.error = str(exc)
|
|
433
|
+
_log.error("DBF migration failed: %s", exc)
|
|
434
|
+
|
|
435
|
+
return result
|
sp5lib/color_utils.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"""Color conversion utilities for Schichtplaner5 colors (stored as Windows BGR integers)."""
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def bgr_to_hex(bgr: int) -> str:
|
|
5
|
+
"""Convert Windows BGR integer to HTML hex color string."""
|
|
6
|
+
if not isinstance(bgr, int) or bgr < 0:
|
|
7
|
+
return "#FFFFFF"
|
|
8
|
+
b = (bgr >> 16) & 0xFF
|
|
9
|
+
g = (bgr >> 8) & 0xFF
|
|
10
|
+
r = bgr & 0xFF
|
|
11
|
+
return f"#{r:02X}{g:02X}{b:02X}"
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def bgr_to_rgb(bgr: int) -> tuple:
|
|
15
|
+
"""Convert Windows BGR integer to (R, G, B) tuple."""
|
|
16
|
+
b = (bgr >> 16) & 0xFF
|
|
17
|
+
g = (bgr >> 8) & 0xFF
|
|
18
|
+
r = bgr & 0xFF
|
|
19
|
+
return (r, g, b)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def is_light_color(bgr: int) -> bool:
|
|
23
|
+
"""Returns True if the color is light (use dark text on it)."""
|
|
24
|
+
r, g, b = bgr_to_rgb(bgr)
|
|
25
|
+
luminance = (0.299 * r + 0.587 * g + 0.114 * b) / 255
|
|
26
|
+
return luminance > 0.5
|