hdx-python-database 1.3.7__tar.gz → 1.3.9__tar.gz
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.
- hdx_python_database-1.3.9/.github/workflows/publish.yaml +34 -0
- hdx_python_database-1.3.9/.github/workflows/run-python-tests.yaml +49 -0
- {hdx_python_database-1.3.7 → hdx_python_database-1.3.9}/.gitignore +4 -1
- {hdx_python_database-1.3.7 → hdx_python_database-1.3.9}/.pre-commit-config.yaml +9 -9
- hdx_python_database-1.3.9/CLAUDE.md +35 -0
- {hdx_python_database-1.3.7 → hdx_python_database-1.3.9}/PKG-INFO +77 -16
- hdx_python_database-1.3.9/README.md +87 -0
- hdx_python_database-1.3.9/pyproject.toml +132 -0
- hdx_python_database-1.3.9/src/hdx/database/_version.py +24 -0
- {hdx_python_database-1.3.7 → hdx_python_database-1.3.9}/src/hdx/database/database.py +23 -25
- {hdx_python_database-1.3.7 → hdx_python_database-1.3.9}/src/hdx/database/dburi.py +8 -8
- {hdx_python_database-1.3.7 → hdx_python_database-1.3.9}/src/hdx/database/no_timezone.py +1 -2
- {hdx_python_database-1.3.7 → hdx_python_database-1.3.9}/src/hdx/database/postgresql.py +6 -4
- {hdx_python_database-1.3.7 → hdx_python_database-1.3.9}/src/hdx/database/views.py +2 -5
- {hdx_python_database-1.3.7 → hdx_python_database-1.3.9}/tests/hdx/database/test_reflect.py +2 -1
- hdx_python_database-1.3.9/uv.lock +1513 -0
- hdx_python_database-1.3.7/.github/workflows/publish.yaml +0 -37
- hdx_python_database-1.3.7/.github/workflows/run-python-tests.yaml +0 -53
- hdx_python_database-1.3.7/README.md +0 -12
- hdx_python_database-1.3.7/hatch.toml +0 -34
- hdx_python_database-1.3.7/pyproject.toml +0 -53
- hdx_python_database-1.3.7/pytest.ini +0 -4
- hdx_python_database-1.3.7/requirements.txt +0 -141
- hdx_python_database-1.3.7/ruff.toml +0 -15
- hdx_python_database-1.3.7/src/hdx/database/_version.py +0 -21
- {hdx_python_database-1.3.7 → hdx_python_database-1.3.9}/.coveragerc +0 -0
- {hdx_python_database-1.3.7 → hdx_python_database-1.3.9}/CONTRIBUTING.md +0 -0
- {hdx_python_database-1.3.7 → hdx_python_database-1.3.9}/LICENSE +0 -0
- {hdx_python_database-1.3.7 → hdx_python_database-1.3.9}/documentation/.readthedocs.yaml +0 -0
- {hdx_python_database-1.3.7 → hdx_python_database-1.3.9}/documentation/index.md +0 -0
- {hdx_python_database-1.3.7 → hdx_python_database-1.3.9}/documentation/mkdocs.yaml +0 -0
- {hdx_python_database-1.3.7 → hdx_python_database-1.3.9}/src/hdx/database/__init__.py +0 -0
- {hdx_python_database-1.3.7 → hdx_python_database-1.3.9}/src/hdx/database/utils.py +0 -0
- {hdx_python_database-1.3.7 → hdx_python_database-1.3.9}/src/hdx/database/with_timezone.py +0 -0
- {hdx_python_database-1.3.7 → hdx_python_database-1.3.9}/tests/fixtures/test.db +0 -0
- {hdx_python_database-1.3.7 → hdx_python_database-1.3.9}/tests/hdx/database/__init__.py +0 -0
- {hdx_python_database-1.3.7 → hdx_python_database-1.3.9}/tests/hdx/database/conftest.py +0 -0
- {hdx_python_database-1.3.7 → hdx_python_database-1.3.9}/tests/hdx/database/dbtestdate.py +0 -0
- {hdx_python_database-1.3.7 → hdx_python_database-1.3.9}/tests/hdx/database/test_database.py +0 -0
- {hdx_python_database-1.3.7 → hdx_python_database-1.3.9}/tests/hdx/database/test_dburi.py +0 -0
- {hdx_python_database-1.3.7 → hdx_python_database-1.3.9}/tests/hdx/database/test_drop_recreate.py +0 -0
- {hdx_python_database-1.3.7 → hdx_python_database-1.3.9}/tests/hdx/database/test_postgresql.py +0 -0
- {hdx_python_database-1.3.7 → hdx_python_database-1.3.9}/tests/hdx/database/test_ssh.py +0 -0
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
name: Publish to PyPI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
release:
|
|
5
|
+
types: [published]
|
|
6
|
+
|
|
7
|
+
jobs:
|
|
8
|
+
publish:
|
|
9
|
+
runs-on: ubuntu-latest
|
|
10
|
+
|
|
11
|
+
environment:
|
|
12
|
+
name: pypi
|
|
13
|
+
url: https://pypi.org/p/hdx-python-database
|
|
14
|
+
|
|
15
|
+
permissions:
|
|
16
|
+
id-token: write
|
|
17
|
+
contents: read
|
|
18
|
+
|
|
19
|
+
steps:
|
|
20
|
+
- uses: actions/checkout@v6
|
|
21
|
+
|
|
22
|
+
- name: Get history and tags for versioning to work
|
|
23
|
+
run: |
|
|
24
|
+
git fetch --prune --unshallow
|
|
25
|
+
git fetch --depth=1 origin +refs/tags/*:refs/tags/*
|
|
26
|
+
|
|
27
|
+
- name: Install uv
|
|
28
|
+
uses: astral-sh/setup-uv@v7
|
|
29
|
+
|
|
30
|
+
- name: Build with uv
|
|
31
|
+
run: uv build
|
|
32
|
+
|
|
33
|
+
- name: Publish distribution 📦 to PyPI
|
|
34
|
+
run: uv publish
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
name: Run tests
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
workflow_dispatch:
|
|
5
|
+
push:
|
|
6
|
+
branches-ignore: [gh-pages, "dependabot/**"]
|
|
7
|
+
pull_request:
|
|
8
|
+
branches-ignore: [gh-pages]
|
|
9
|
+
|
|
10
|
+
jobs:
|
|
11
|
+
build:
|
|
12
|
+
runs-on: ubuntu-latest
|
|
13
|
+
permissions:
|
|
14
|
+
contents: read
|
|
15
|
+
checks: write
|
|
16
|
+
pull-requests: write
|
|
17
|
+
|
|
18
|
+
steps:
|
|
19
|
+
- uses: actions/checkout@v6
|
|
20
|
+
|
|
21
|
+
- name: Install uv
|
|
22
|
+
uses: astral-sh/setup-uv@v7
|
|
23
|
+
with:
|
|
24
|
+
enable-cache: true
|
|
25
|
+
python-version: "3.13"
|
|
26
|
+
|
|
27
|
+
- name: Install dependencies
|
|
28
|
+
run: uv sync --frozen
|
|
29
|
+
|
|
30
|
+
- name: Check styling
|
|
31
|
+
run: |
|
|
32
|
+
uv run ruff format --check
|
|
33
|
+
uv run ruff check
|
|
34
|
+
|
|
35
|
+
- name: Test with pytest
|
|
36
|
+
run: uv run pytest
|
|
37
|
+
|
|
38
|
+
- name: Publish Unit Test Results
|
|
39
|
+
uses: EnricoMi/publish-unit-test-result-action@v2
|
|
40
|
+
if: always()
|
|
41
|
+
with:
|
|
42
|
+
files: test-results.xml
|
|
43
|
+
|
|
44
|
+
- name: Publish in Coveralls
|
|
45
|
+
uses: coverallsapp/github-action@v2
|
|
46
|
+
if: always()
|
|
47
|
+
with:
|
|
48
|
+
flag-name: tests
|
|
49
|
+
format: lcov
|
|
@@ -1,25 +1,25 @@
|
|
|
1
1
|
default_language_version:
|
|
2
|
-
|
|
2
|
+
python: python3
|
|
3
|
+
|
|
3
4
|
repos:
|
|
4
5
|
- repo: https://github.com/pre-commit/pre-commit-hooks
|
|
5
|
-
rev:
|
|
6
|
+
rev: v6.0.0
|
|
6
7
|
hooks:
|
|
7
8
|
- id: trailing-whitespace
|
|
8
9
|
- id: end-of-file-fixer
|
|
9
10
|
- id: check-ast
|
|
11
|
+
|
|
10
12
|
- repo: https://github.com/astral-sh/ruff-pre-commit
|
|
11
|
-
rev: v0.
|
|
13
|
+
rev: v0.14.13
|
|
12
14
|
hooks:
|
|
13
15
|
# Run the linter.
|
|
14
16
|
- id: ruff-check
|
|
15
17
|
args: [ --fix ]
|
|
16
18
|
# Run the formatter.
|
|
17
19
|
- id: ruff-format
|
|
20
|
+
|
|
18
21
|
- repo: https://github.com/astral-sh/uv-pre-commit
|
|
19
|
-
rev: 0.
|
|
22
|
+
rev: 0.9.25
|
|
20
23
|
hooks:
|
|
21
|
-
#
|
|
22
|
-
- id:
|
|
23
|
-
name: pip-compile requirements.txt
|
|
24
|
-
files: pyproject.toml
|
|
25
|
-
args: [ pyproject.toml, --resolver=backtracking, --all-extras, --upgrade, -q, -o, requirements.txt ]
|
|
24
|
+
# Ensure the lockfile is up-to-date with pyproject.toml
|
|
25
|
+
- id: uv-lock
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# HDX Python Database
|
|
2
|
+
|
|
3
|
+
HDX Python Database Library — provides utilities for connecting to databases (including via SSH tunnel) using SQLAlchemy.
|
|
4
|
+
|
|
5
|
+
## Setup
|
|
6
|
+
|
|
7
|
+
```shell
|
|
8
|
+
uv sync
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Running Tests
|
|
12
|
+
|
|
13
|
+
```shell
|
|
14
|
+
uv run pytest
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Linting
|
|
18
|
+
|
|
19
|
+
```shell
|
|
20
|
+
uv run ruff check
|
|
21
|
+
uv run ruff format --check
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Building
|
|
25
|
+
|
|
26
|
+
```shell
|
|
27
|
+
uv build
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Package Management
|
|
31
|
+
|
|
32
|
+
- Runtime dependencies go in `[project.dependencies]` in `pyproject.toml`
|
|
33
|
+
- Dev/test dependencies go in `[dependency-groups] dev` in `pyproject.toml`
|
|
34
|
+
- Optional runtime extras (`sshtunnel`, `postgresql`) stay in `[project.optional-dependencies]`
|
|
35
|
+
- After changing dependencies, run `uv lock --upgrade` to regenerate `uv.lock`
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: hdx-python-database
|
|
3
|
-
Version: 1.3.
|
|
3
|
+
Version: 1.3.9
|
|
4
4
|
Summary: HDX Python database utilities
|
|
5
5
|
Project-URL: Homepage, https://github.com/OCHA-DAP/hdx-python-database
|
|
6
|
-
Author-email: Michael Rans <rans@
|
|
6
|
+
Author-email: Michael Rans <rans@un.org>
|
|
7
7
|
License: MIT
|
|
8
8
|
License-File: LICENSE
|
|
9
9
|
Keywords: HDX,database,postgresql
|
|
@@ -16,29 +16,15 @@ Classifier: Operating System :: Microsoft :: Windows
|
|
|
16
16
|
Classifier: Operating System :: POSIX :: Linux
|
|
17
17
|
Classifier: Operating System :: Unix
|
|
18
18
|
Classifier: Programming Language :: Python
|
|
19
|
-
Classifier: Programming Language :: Python :: 3
|
|
20
|
-
Classifier: Programming Language :: Python :: 3 :: Only
|
|
21
|
-
Classifier: Programming Language :: Python :: 3.8
|
|
22
|
-
Classifier: Programming Language :: Python :: 3.9
|
|
23
|
-
Classifier: Programming Language :: Python :: 3.10
|
|
24
|
-
Classifier: Programming Language :: Python :: 3.11
|
|
25
|
-
Classifier: Programming Language :: Python :: 3.12
|
|
26
19
|
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
27
20
|
Requires-Python: >=3.10
|
|
28
21
|
Requires-Dist: sqlalchemy>=2
|
|
29
|
-
Provides-Extra: dev
|
|
30
|
-
Requires-Dist: pre-commit; extra == 'dev'
|
|
31
22
|
Provides-Extra: docs
|
|
32
23
|
Requires-Dist: mkapi; extra == 'docs'
|
|
33
24
|
Provides-Extra: postgresql
|
|
34
25
|
Requires-Dist: psycopg[binary]; extra == 'postgresql'
|
|
35
26
|
Provides-Extra: sshtunnel
|
|
36
27
|
Requires-Dist: sshtunnel; extra == 'sshtunnel'
|
|
37
|
-
Provides-Extra: test
|
|
38
|
-
Requires-Dist: psycopg[binary]; extra == 'test'
|
|
39
|
-
Requires-Dist: pytest; extra == 'test'
|
|
40
|
-
Requires-Dist: pytest-cov; extra == 'test'
|
|
41
|
-
Requires-Dist: sshtunnel; extra == 'test'
|
|
42
28
|
Description-Content-Type: text/markdown
|
|
43
29
|
|
|
44
30
|
[](https://github.com/OCHA-DAP/hdx-python-database/actions/workflows/run-python-tests.yaml)
|
|
@@ -53,3 +39,78 @@ For more information, please read the [documentation](https://hdx-python-databas
|
|
|
53
39
|
|
|
54
40
|
This library is part of the [Humanitarian Data Exchange](https://data.humdata.org/) (HDX) project. If you have
|
|
55
41
|
humanitarian related data, please upload your datasets to HDX.
|
|
42
|
+
|
|
43
|
+
# Development
|
|
44
|
+
|
|
45
|
+
## Environment
|
|
46
|
+
|
|
47
|
+
Development is currently done using Python 3.13. The environment can be created with:
|
|
48
|
+
|
|
49
|
+
```shell
|
|
50
|
+
uv sync
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
This creates a .venv folder with the versions specified in the project's uv.lock file.
|
|
54
|
+
|
|
55
|
+
### Pre-commit
|
|
56
|
+
|
|
57
|
+
pre-commit will be installed when syncing uv. It is run every time you make a git
|
|
58
|
+
commit if you call it like this:
|
|
59
|
+
|
|
60
|
+
```shell
|
|
61
|
+
pre-commit install
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
With pre-commit, all code is formatted according to
|
|
65
|
+
[ruff](https://docs.astral.sh/ruff/) guidelines.
|
|
66
|
+
|
|
67
|
+
To check if your changes pass pre-commit without committing, run:
|
|
68
|
+
|
|
69
|
+
```shell
|
|
70
|
+
pre-commit run --all-files
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Packages
|
|
74
|
+
|
|
75
|
+
[uv](https://github.com/astral-sh/uv) is used for package management. If
|
|
76
|
+
you've introduced a new package to the source code (i.e. anywhere in `src/`),
|
|
77
|
+
please add it to the `project.dependencies` section of `pyproject.toml` with
|
|
78
|
+
any known version constraints.
|
|
79
|
+
|
|
80
|
+
To add packages required only for testing, add them to the
|
|
81
|
+
`[dependency-groups]`.
|
|
82
|
+
|
|
83
|
+
Any changes to the dependencies will be automatically reflected in
|
|
84
|
+
`uv.lock` with `pre-commit`, but you can re-generate the files without committing by
|
|
85
|
+
executing:
|
|
86
|
+
|
|
87
|
+
```shell
|
|
88
|
+
uv lock --upgrade
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Project
|
|
92
|
+
|
|
93
|
+
[uv](https://github.com/astral-sh/uv) is used for project management. The project can be
|
|
94
|
+
built using:
|
|
95
|
+
|
|
96
|
+
```shell
|
|
97
|
+
uv build
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Linting and syntax checking can be run with:
|
|
101
|
+
|
|
102
|
+
```shell
|
|
103
|
+
uv run ruff check
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
To run the tests and view coverage, execute:
|
|
107
|
+
|
|
108
|
+
```shell
|
|
109
|
+
uv run pytest
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Documentation
|
|
113
|
+
|
|
114
|
+
The documentation, including API documentation, is generated using ReadtheDocs and
|
|
115
|
+
MkDocs with Material. As you change the source code, remember to update the
|
|
116
|
+
documentation at `documentation/index.md`.
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
[](https://github.com/OCHA-DAP/hdx-python-database/actions/workflows/run-python-tests.yaml)
|
|
2
|
+
[](https://coveralls.io/github/OCHA-DAP/hdx-python-database?branch=main)
|
|
3
|
+
[](https://github.com/astral-sh/ruff)
|
|
4
|
+
[](https://pypistats.org/packages/hdx-python-database)
|
|
5
|
+
|
|
6
|
+
The HDX Python Database Library provides utilities for connecting to databases in a standard way including
|
|
7
|
+
through an ssh tunnel if needed. It is built on top of SQLAlchemy and simplifies its setup.
|
|
8
|
+
|
|
9
|
+
For more information, please read the [documentation](https://hdx-python-database.readthedocs.io/en/latest/).
|
|
10
|
+
|
|
11
|
+
This library is part of the [Humanitarian Data Exchange](https://data.humdata.org/) (HDX) project. If you have
|
|
12
|
+
humanitarian related data, please upload your datasets to HDX.
|
|
13
|
+
|
|
14
|
+
# Development
|
|
15
|
+
|
|
16
|
+
## Environment
|
|
17
|
+
|
|
18
|
+
Development is currently done using Python 3.13. The environment can be created with:
|
|
19
|
+
|
|
20
|
+
```shell
|
|
21
|
+
uv sync
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
This creates a .venv folder with the versions specified in the project's uv.lock file.
|
|
25
|
+
|
|
26
|
+
### Pre-commit
|
|
27
|
+
|
|
28
|
+
pre-commit will be installed when syncing uv. It is run every time you make a git
|
|
29
|
+
commit if you call it like this:
|
|
30
|
+
|
|
31
|
+
```shell
|
|
32
|
+
pre-commit install
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
With pre-commit, all code is formatted according to
|
|
36
|
+
[ruff](https://docs.astral.sh/ruff/) guidelines.
|
|
37
|
+
|
|
38
|
+
To check if your changes pass pre-commit without committing, run:
|
|
39
|
+
|
|
40
|
+
```shell
|
|
41
|
+
pre-commit run --all-files
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Packages
|
|
45
|
+
|
|
46
|
+
[uv](https://github.com/astral-sh/uv) is used for package management. If
|
|
47
|
+
you've introduced a new package to the source code (i.e. anywhere in `src/`),
|
|
48
|
+
please add it to the `project.dependencies` section of `pyproject.toml` with
|
|
49
|
+
any known version constraints.
|
|
50
|
+
|
|
51
|
+
To add packages required only for testing, add them to the
|
|
52
|
+
`[dependency-groups]`.
|
|
53
|
+
|
|
54
|
+
Any changes to the dependencies will be automatically reflected in
|
|
55
|
+
`uv.lock` with `pre-commit`, but you can re-generate the files without committing by
|
|
56
|
+
executing:
|
|
57
|
+
|
|
58
|
+
```shell
|
|
59
|
+
uv lock --upgrade
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Project
|
|
63
|
+
|
|
64
|
+
[uv](https://github.com/astral-sh/uv) is used for project management. The project can be
|
|
65
|
+
built using:
|
|
66
|
+
|
|
67
|
+
```shell
|
|
68
|
+
uv build
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Linting and syntax checking can be run with:
|
|
72
|
+
|
|
73
|
+
```shell
|
|
74
|
+
uv run ruff check
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
To run the tests and view coverage, execute:
|
|
78
|
+
|
|
79
|
+
```shell
|
|
80
|
+
uv run pytest
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Documentation
|
|
84
|
+
|
|
85
|
+
The documentation, including API documentation, is generated using ReadtheDocs and
|
|
86
|
+
MkDocs with Material. As you change the source code, remember to update the
|
|
87
|
+
documentation at `documentation/index.md`.
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
#########################
|
|
2
|
+
# Project Configuration #
|
|
3
|
+
#########################
|
|
4
|
+
|
|
5
|
+
[build-system]
|
|
6
|
+
requires = ["hatchling", "hatch-vcs"]
|
|
7
|
+
build-backend = "hatchling.build"
|
|
8
|
+
|
|
9
|
+
[project]
|
|
10
|
+
name = "hdx-python-database"
|
|
11
|
+
description = "HDX Python database utilities"
|
|
12
|
+
authors = [{name = "Michael Rans", email = "rans@un.org"}]
|
|
13
|
+
license = {text = "MIT"}
|
|
14
|
+
keywords = ["HDX", "database", "postgresql"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 5 - Production/Stable",
|
|
17
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
18
|
+
"Programming Language :: Python",
|
|
19
|
+
"Intended Audience :: Developers",
|
|
20
|
+
"License :: OSI Approved :: MIT License",
|
|
21
|
+
"Natural Language :: English",
|
|
22
|
+
"Operating System :: POSIX :: Linux",
|
|
23
|
+
"Operating System :: Unix",
|
|
24
|
+
"Operating System :: MacOS",
|
|
25
|
+
"Operating System :: Microsoft :: Windows",
|
|
26
|
+
]
|
|
27
|
+
readme = "README.md"
|
|
28
|
+
dynamic = ["version"]
|
|
29
|
+
requires-python = ">=3.10"
|
|
30
|
+
|
|
31
|
+
dependencies = [
|
|
32
|
+
"sqlalchemy>=2",
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
[project.optional-dependencies]
|
|
36
|
+
sshtunnel = ["sshtunnel"]
|
|
37
|
+
postgresql = ["psycopg[binary]"]
|
|
38
|
+
docs = ["mkapi"]
|
|
39
|
+
|
|
40
|
+
[dependency-groups]
|
|
41
|
+
dev = [
|
|
42
|
+
"sshtunnel",
|
|
43
|
+
"psycopg[binary]",
|
|
44
|
+
"pytest",
|
|
45
|
+
"pytest-cov",
|
|
46
|
+
"pre-commit",
|
|
47
|
+
"ruff==0.14.13",
|
|
48
|
+
]
|
|
49
|
+
|
|
50
|
+
[project.urls]
|
|
51
|
+
Homepage = "https://github.com/OCHA-DAP/hdx-python-database"
|
|
52
|
+
|
|
53
|
+
# ----------------------------------------------------------------------------
|
|
54
|
+
# Hatchling (Build & Versioning)
|
|
55
|
+
# ----------------------------------------------------------------------------
|
|
56
|
+
|
|
57
|
+
[tool.hatch.version]
|
|
58
|
+
source = "vcs"
|
|
59
|
+
|
|
60
|
+
[tool.hatch.version.raw-options]
|
|
61
|
+
local_scheme = "no-local-version"
|
|
62
|
+
version_scheme = "python-simplified-semver"
|
|
63
|
+
|
|
64
|
+
[tool.hatch.build.hooks.vcs]
|
|
65
|
+
version-file = "src/hdx/database/_version.py"
|
|
66
|
+
|
|
67
|
+
[tool.hatch.build.targets.wheel]
|
|
68
|
+
packages = ["src/hdx"]
|
|
69
|
+
|
|
70
|
+
[tool.hatch.metadata]
|
|
71
|
+
allow-direct-references = true
|
|
72
|
+
|
|
73
|
+
# ----------------------------------------------------------------------------
|
|
74
|
+
# Ruff (Linting & Formatting)
|
|
75
|
+
# ----------------------------------------------------------------------------
|
|
76
|
+
|
|
77
|
+
[tool.ruff]
|
|
78
|
+
target-version = "py310"
|
|
79
|
+
src = ["src"]
|
|
80
|
+
exclude = ["_version.py"]
|
|
81
|
+
|
|
82
|
+
[tool.ruff.lint]
|
|
83
|
+
# Defaults are E (pycodestyle) and F (pyflakes). We extend them:
|
|
84
|
+
extend-select = [
|
|
85
|
+
"I", # isort
|
|
86
|
+
"UP", # pyupgrade
|
|
87
|
+
]
|
|
88
|
+
ignore = [
|
|
89
|
+
"E501", # Line too long
|
|
90
|
+
]
|
|
91
|
+
|
|
92
|
+
[tool.ruff.lint.isort]
|
|
93
|
+
known-local-folder = ["hdx.database"]
|
|
94
|
+
|
|
95
|
+
# ----------------------------------------------------------------------------
|
|
96
|
+
# Pytest (Testing)
|
|
97
|
+
# ----------------------------------------------------------------------------
|
|
98
|
+
|
|
99
|
+
[tool.pytest.ini_options]
|
|
100
|
+
pythonpath = "src"
|
|
101
|
+
log_cli = true
|
|
102
|
+
addopts = """
|
|
103
|
+
--color=yes
|
|
104
|
+
--rootdir=.
|
|
105
|
+
--junitxml=test-results.xml
|
|
106
|
+
--cov
|
|
107
|
+
--no-cov-on-fail
|
|
108
|
+
--cov-report=lcov
|
|
109
|
+
--cov-report=term-missing
|
|
110
|
+
"""
|
|
111
|
+
|
|
112
|
+
# ----------------------------------------------------------------------------
|
|
113
|
+
# Coverage (Reporting)
|
|
114
|
+
# ----------------------------------------------------------------------------
|
|
115
|
+
|
|
116
|
+
[tool.coverage.run]
|
|
117
|
+
source = ["src"]
|
|
118
|
+
omit = ["*/_version.py"]
|
|
119
|
+
|
|
120
|
+
[tool.coverage.report]
|
|
121
|
+
exclude_also = [
|
|
122
|
+
"from ._version",
|
|
123
|
+
"def __repr__",
|
|
124
|
+
"if self.debug:",
|
|
125
|
+
"if settings.DEBUG",
|
|
126
|
+
"raise AssertionError",
|
|
127
|
+
"raise NotImplementedError",
|
|
128
|
+
"if 0:",
|
|
129
|
+
"if __name__ == .__main__.:",
|
|
130
|
+
"if TYPE_CHECKING:",
|
|
131
|
+
"@(abc\\.)?abstractmethod",
|
|
132
|
+
]
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# file generated by vcs-versioning
|
|
2
|
+
# don't change, don't track in version control
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
__all__ = [
|
|
6
|
+
"__version__",
|
|
7
|
+
"__version_tuple__",
|
|
8
|
+
"version",
|
|
9
|
+
"version_tuple",
|
|
10
|
+
"__commit_id__",
|
|
11
|
+
"commit_id",
|
|
12
|
+
]
|
|
13
|
+
|
|
14
|
+
version: str
|
|
15
|
+
__version__: str
|
|
16
|
+
__version_tuple__: tuple[int | str, ...]
|
|
17
|
+
version_tuple: tuple[int | str, ...]
|
|
18
|
+
commit_id: str | None
|
|
19
|
+
__commit_id__: str | None
|
|
20
|
+
|
|
21
|
+
__version__ = version = '1.3.9'
|
|
22
|
+
__version_tuple__ = version_tuple = (1, 3, 9)
|
|
23
|
+
|
|
24
|
+
__commit_id__ = commit_id = None
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"""Database utilities"""
|
|
2
2
|
|
|
3
3
|
import logging
|
|
4
|
-
from typing import Any
|
|
4
|
+
from typing import Any
|
|
5
5
|
|
|
6
6
|
from sqlalchemy import Engine, TableClause, create_engine, insert
|
|
7
7
|
from sqlalchemy.exc import SQLAlchemyError
|
|
@@ -64,7 +64,7 @@ class Database:
|
|
|
64
64
|
table_base (Optional[Type[DeclarativeBase]]): Override table base. Defaults to None.
|
|
65
65
|
reflect (bool): Whether to reflect existing tables. Defaults to False.
|
|
66
66
|
**kwargs: See below
|
|
67
|
-
pg_restore_file (str): Restore database from pg_restore file
|
|
67
|
+
pg_restore_file (Path | str): Restore database from pg_restore file
|
|
68
68
|
recreate_schema (bool): Whether to recreate schema
|
|
69
69
|
schema_name (str): Database schema name. Defaults to "public".
|
|
70
70
|
prepare_fn (Callable[[], None]]): Function to call before Base.metadata.create_all.
|
|
@@ -79,17 +79,17 @@ class Database:
|
|
|
79
79
|
|
|
80
80
|
def __init__(
|
|
81
81
|
self,
|
|
82
|
-
engine:
|
|
83
|
-
db_uri:
|
|
84
|
-
database:
|
|
85
|
-
host:
|
|
86
|
-
port:
|
|
87
|
-
username:
|
|
88
|
-
password:
|
|
82
|
+
engine: Engine | None = None,
|
|
83
|
+
db_uri: str | None = None,
|
|
84
|
+
database: str | None = None,
|
|
85
|
+
host: str | None = None,
|
|
86
|
+
port: int | str | None = None,
|
|
87
|
+
username: str | None = None,
|
|
88
|
+
password: str | None = None,
|
|
89
89
|
dialect: str = "postgresql",
|
|
90
|
-
driver:
|
|
90
|
+
driver: str | None = None,
|
|
91
91
|
db_has_tz: bool = False,
|
|
92
|
-
table_base:
|
|
92
|
+
table_base: type[DeclarativeBase] | None = None,
|
|
93
93
|
reflect: bool = False,
|
|
94
94
|
**kwargs: Any,
|
|
95
95
|
) -> None:
|
|
@@ -230,8 +230,8 @@ class Database:
|
|
|
230
230
|
|
|
231
231
|
def batch_populate(
|
|
232
232
|
self,
|
|
233
|
-
rows:
|
|
234
|
-
dbtable:
|
|
233
|
+
rows: list[dict],
|
|
234
|
+
dbtable: type[DeclarativeBase],
|
|
235
235
|
batch_size: int = 1000,
|
|
236
236
|
) -> None:
|
|
237
237
|
"""Batch populate database table.
|
|
@@ -244,21 +244,19 @@ class Database:
|
|
|
244
244
|
Returns:
|
|
245
245
|
Tuple[Session, Any]: (SQLAlchemy session, base)
|
|
246
246
|
"""
|
|
247
|
-
|
|
248
|
-
for
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
batch_rows = rows[start_row:end_row]
|
|
252
|
-
self._session.execute(insert(dbtable), batch_rows)
|
|
247
|
+
|
|
248
|
+
for i in range(0, len(rows), batch_size):
|
|
249
|
+
batch = rows[i : i + batch_size]
|
|
250
|
+
self._session.execute(insert(dbtable), batch)
|
|
253
251
|
self._session.commit()
|
|
254
252
|
|
|
255
253
|
@staticmethod
|
|
256
254
|
def create_session(
|
|
257
|
-
engine:
|
|
258
|
-
db_uri:
|
|
259
|
-
table_base:
|
|
255
|
+
engine: Engine | None = None,
|
|
256
|
+
db_uri: str | None = None,
|
|
257
|
+
table_base: type[DeclarativeBase] = NoTZBase,
|
|
260
258
|
reflect: bool = False,
|
|
261
|
-
) ->
|
|
259
|
+
) -> tuple[Session, Any]:
|
|
262
260
|
"""Creates SQLAlchemy session given SQLAlchemy engine or database uri
|
|
263
261
|
(one of which must be supplied). Tables must inherit from Base in
|
|
264
262
|
hdx.utilities.database unless base is defined. If reflect is True,
|
|
@@ -334,7 +332,7 @@ class Database:
|
|
|
334
332
|
return False
|
|
335
333
|
|
|
336
334
|
@staticmethod
|
|
337
|
-
def prepare_view(view_params:
|
|
335
|
+
def prepare_view(view_params: dict) -> TableClause:
|
|
338
336
|
"""Prepare SQLAlchemy view from dictionary with keys: name, metadata and
|
|
339
337
|
selectable. Must be run before Base.metadata.create_all.
|
|
340
338
|
|
|
@@ -347,7 +345,7 @@ class Database:
|
|
|
347
345
|
return view(**view_params)
|
|
348
346
|
|
|
349
347
|
@classmethod
|
|
350
|
-
def prepare_views(cls, view_params_list:
|
|
348
|
+
def prepare_views(cls, view_params_list: list[dict]) -> list[TableClause]:
|
|
351
349
|
"""Prepare SQLAlchemy views from a list of dictionaries with keys: name,
|
|
352
350
|
metadata and selectable. Must be run before Base.metadata.create_all.
|
|
353
351
|
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
"""Connection URI utilities"""
|
|
2
2
|
|
|
3
|
-
from typing import Any
|
|
3
|
+
from typing import Any
|
|
4
4
|
from urllib.parse import urlsplit
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
def get_params_from_connection_uri(
|
|
8
8
|
db_uri: str,
|
|
9
9
|
include_driver: bool = True,
|
|
10
|
-
) ->
|
|
10
|
+
) -> dict[str, Any]:
|
|
11
11
|
"""Gets PostgreSQL database connection parameters from connection URI
|
|
12
12
|
|
|
13
13
|
Args:
|
|
@@ -38,13 +38,13 @@ def get_params_from_connection_uri(
|
|
|
38
38
|
|
|
39
39
|
|
|
40
40
|
def get_connection_uri(
|
|
41
|
-
database:
|
|
42
|
-
host:
|
|
43
|
-
port:
|
|
44
|
-
username:
|
|
45
|
-
password:
|
|
41
|
+
database: str | None = None,
|
|
42
|
+
host: str | None = None,
|
|
43
|
+
port: int | str | None = None,
|
|
44
|
+
username: str | None = None,
|
|
45
|
+
password: str | None = None,
|
|
46
46
|
dialect: str = "postgresql",
|
|
47
|
-
driver:
|
|
47
|
+
driver: str | None = None,
|
|
48
48
|
include_driver: bool = True,
|
|
49
49
|
) -> str:
|
|
50
50
|
"""Gets connection uri from database connection parameters
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"""Utilities for database datetime columns without timezone"""
|
|
2
2
|
|
|
3
3
|
from datetime import datetime, timezone
|
|
4
|
-
from typing import Optional
|
|
5
4
|
|
|
6
5
|
from sqlalchemy import DateTime, TypeDecorator
|
|
7
6
|
from sqlalchemy.orm import DeclarativeBase, declared_attr
|
|
@@ -16,7 +15,7 @@ class ConversionNoTZ(TypeDecorator):
|
|
|
16
15
|
impl = DateTime
|
|
17
16
|
cache_ok = True
|
|
18
17
|
|
|
19
|
-
def process_bind_param(self, value:
|
|
18
|
+
def process_bind_param(self, value: datetime | None, _):
|
|
20
19
|
if value is None:
|
|
21
20
|
return value
|
|
22
21
|
if value.tzinfo:
|