Skip to content

Commit 7770bf6

Browse files
authored
Merge pull request #1 from jason810496/develop
Merge `develop` branch to `main` branch
2 parents 56cbc2e + b6c3a65 commit 7770bf6

22 files changed

+1243
-922
lines changed

.github/CONTRIBUTING.md

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Contributing
2+
3+
Welcome to contribute to `pgmq-sqlalchemy` ! <br>
4+
This document will guide you through the process of contributing to the project.
5+
6+
## How to Contribute
7+
8+
1. Fork the repository
9+
- Click the `Fork` button in the upper right corner of the repository page.
10+
2. Clone the repository
11+
- Clone the repository to your local machine.
12+
```bash
13+
git clone https://github.com/your-username/pgmq-sqlalchemy.git
14+
```
15+
3. Create a new branch
16+
- Create a new branch for your changes.
17+
```bash
18+
git checkout -b feature/your-feature-name
19+
```
20+
4. Make your changes
21+
- Make your changes to the codebase.
22+
- Add tests for your changes.
23+
- Add documentation if changes are user-facing.
24+
5. Commit your changes
25+
- Commit your changes with meaningful commit messages.
26+
- [ref: conventional git commit messages](https://www.conventionalcommits.org/en/v1.0.0/)
27+
```bash
28+
git commit -m "feat: your feature description"
29+
```
30+
6. Push your changes
31+
- Push your changes to your forked repository.
32+
```bash
33+
git push origin feature/your-feature-name
34+
```
35+
7. Create a Pull Request
36+
- Create a Pull Request from your forked repository to the `develop` branch of the original repository.
37+
38+
## Development
39+
40+
### Setup
41+
42+
Install dependencies and `ruff` pre-commit hooks.
43+
```bash
44+
make install
45+
```
46+
47+
> Prerequisites: **Docker** and **Docker Compose** installed.
48+
49+
Start development PostgreSQL
50+
```bash
51+
make start-db
52+
```
53+
54+
Stop development PostgreSQL
55+
```bash
56+
make stop-db
57+
```
58+
59+
### Makefile utility
60+
61+
```bash
62+
make help
63+
```
64+
> will show all available commands and their descriptions.
65+
66+
### Linting
67+
68+
We use [pre-commit](https://pre-commit.com/) hook with [ruff](https://github.com/astral-sh/ruff-pre-commit) to automatically lint the codebase before committing.
69+
70+
71+
### Testing
72+
73+
Run tests in local
74+
```bash
75+
make test-local
76+
```
77+
78+
Run tests in docker
79+
```bash
80+
make test-docker
81+
```
82+
83+
### Documentation
84+
85+
Serve documentation
86+
```bash
87+
make doc-serve
88+
```
89+
90+
Clean documentation build
91+
```bash
92+
make doc-clean
93+
```

.github/pull_request_template.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Title (replace this with the title of your pull request)
2+
3+
## Description
4+
5+
Describe the changes you made in this pull request. <br>
6+
( replace this with the description of your pull request )
7+
8+
## Status
9+
10+
- [ ] In progress
11+
- [ ] Ready for review
12+
- [ ] Done
13+
14+
## Checklist
15+
16+
- [ ] Read the [Contributing Guide](CONTRIBUTING.md)
17+
- [ ] Passes tests
18+
- [ ] Linted ( we use `pre-commit` with `ruff` )
19+
- [ ] Updated documentation

.github/workflows/codecov.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
runs-on: ubuntu-latest
1515
strategy:
1616
matrix:
17-
python-version: ["3.12"]
17+
python-version: ["3.9","3.10","3.11","3.12"]
1818

1919
name: Test pgmq-sqlalchemy
2020
steps:
@@ -31,7 +31,7 @@ jobs:
3131
run: |
3232
poetry env use python${{ matrix.python-version }}
3333
- name: Install dependencies
34-
run: poetry install
34+
run: poetry install --without=dev
3535
- name: Start PostgreSQL
3636
run: |
3737
cp pgmq_postgres.template.env pgmq_postgres.env

.github/workflows/publish.yml

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
tags:
6+
- '*.*.*'
7+
8+
permissions:
9+
contents: read
10+
11+
jobs:
12+
pypi-publish:
13+
name: Upload release to PyPI
14+
runs-on: ubuntu-latest
15+
environment:
16+
name: pypi
17+
url: https://pypi.org/project/pgmq-sqlalchemy/
18+
permissions:
19+
id-token: write
20+
steps:
21+
- name: Checkout code
22+
uses: actions/checkout@v4
23+
24+
- name: Set up Python 3.10
25+
uses: actions/setup-python@v5
26+
with:
27+
python-version: "3.10"
28+
29+
- name: Install Poetry
30+
run: |
31+
curl -sSL https://install.python-poetry.org | python - -y
32+
33+
- name: Update PATH
34+
run: echo "$HOME/.local/bin" >> $GITHUB_PATH
35+
36+
- name: Update Poetry configuration
37+
run: poetry config virtualenvs.create false
38+
39+
- name: Install dependencies
40+
run: poetry install --sync --no-interaction --without=dev
41+
42+
- name: Package project
43+
run: poetry build
44+
45+
- name: Publish package distributions to PyPI
46+
uses: pypa/gh-action-pypi-publish@release/v1

.readthedocs.yml

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Read the Docs configuration file for Sphinx projects
2+
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details
3+
4+
# Required
5+
version: 2
6+
7+
# Set the OS, Python version and other tools you might need
8+
build:
9+
os: ubuntu-22.04
10+
tools:
11+
python: "3.12"
12+
# You can also specify other tool versions:
13+
# nodejs: "20"
14+
# rust: "1.70"
15+
# golang: "1.20"
16+
17+
# Build documentation in the "docs/" directory with Sphinx
18+
sphinx:
19+
configuration: docs/conf.py
20+
# You can configure Sphinx to use a different builder, for instance use the dirhtml builder for simpler URLs
21+
# builder: "dirhtml"
22+
# Fail on all warnings to avoid broken references
23+
fail_on_warning: true
24+
25+
# Optionally build your docs in additional formats such as PDF and ePub
26+
# formats:
27+
# - pdf
28+
# - epub
29+
30+
# Optional but recommended, declare the Python requirements required
31+
# to build your documentation
32+
# See https://docs.readthedocs.io/en/stable/guides/reproducible-builds.html
33+
python:
34+
install:
35+
- requirements: docs/requirements.txt

Makefile

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ install: ## Install dependencies and `ruff` pre-commit hooks
44
pre-commit install
55
poetry install --with dev
66

7+
build: ## Build the package
8+
poetry build
9+
710
test-local: ## Run tests locally
811
poetry run pytest tests --cov=pgmq_sqlalchemy.queue
912

@@ -37,9 +40,12 @@ exec-db: ## Enter the database container
3740
doc-build: ## Build the documentation
3841
cd doc && poetry run sphinx-build -nW . _build
3942

40-
doc-serve: ## Serve the documentation
43+
doc-serve: doc-clean ## Serve the documentation
4144
cd doc && poetry run sphinx-autobuild -nW . _build
4245

46+
doc-clean: ## Clean the documentation
47+
cd doc && rm -r _build
48+
4349
.PHONY: install test-local test-docker test-docker-rebuild clear-db start-db exec-db doc-build doc-serve
4450

4551
# generate help from comments

README.md

+108-7
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,117 @@
88

99
# pgmq-sqlalchemy
1010

11-
Python client using **sqlalchemy ORM** for the PGMQ Postgres extension.
11+
More flexible [PGMQ Postgres extension](https://github.com/tembo-io/pgmq) Python client that using **sqlalchemy ORM**, supporting both **async** and **sync** `engines`, `sessionmakers` or built from `dsn`.
12+
13+
## Table of Contents
14+
15+
* [pgmq-sqlalchemy](#pgmq-sqlalchemy)
16+
* [Features](#features)
17+
* [Installation](#installation)
18+
* [Getting Started](#getting-started)
19+
* [Postgres Setup](#postgres-setup)
20+
* [Usage](#usage)
21+
* [Issue/ Contributing / Development](#issue-contributing--development)
22+
* [TODO](#todo)
1223

13-
支援 **SQLAlchemy ORM** 的 Python 客戶端 <br>
14-
用於 [PGMQ Postgres 插件](https://github.com/tembo-io/pgmq)
1524

1625
## Features
1726

18-
- 支援 **async****sync** `engines``sessionmakers`,或由 `dsn` 構建。
19-
- 支援所有 sqlalchemy 支持的 postgres DBAPIs。
20-
> 例如:`psycopg`, `psycopg2`, `asyncpg`
21-
> 可見 [SQLAlchemy Postgresql Dialects](https://docs.sqlalhttps://docs.sqlalchemy.org/en/20/dialects/postgresql.html)
27+
- Supports **async** and **sync** `engines` and `sessionmakers`, or built from `dsn`.
28+
- **Automatically** creates `pgmq` (or `pg_partman`) extension on the database if not exists.
29+
- Supports **all postgres DBAPIs supported by sqlalchemy**.
30+
> e.g. `psycopg`, `psycopg2`, `asyncpg` .. <br>
31+
> See [SQLAlchemy Postgresql Dialects](https://docs.sqlalhttps://docs.sqlalchemy.org/en/20/dialects/postgresql.html)
32+
33+
## Installation
34+
35+
Install with pip:
36+
37+
```bash
38+
pip install pgmq-sqlalchemy
39+
```
40+
41+
Install with additional DBAPIs packages:
42+
43+
```bash
44+
pip install pgmq-sqlalchemy[psycopg2]
45+
pip install pgmq-sqlalchemy[asyncpg]
46+
# pip install pgmq-sqlalchemy[postgres-python-driver]
47+
```
48+
49+
## Getting Started
50+
51+
### Postgres Setup
52+
53+
Prerequisites: **Postgres** with **PGMQ** extension installed. <br>
54+
For quick setup:
55+
```bash
56+
docker run -d --name postgres -e POSTGRES_PASSWORD=postgres -p 5432:5432 quay.io/tembo/pg16-pgmq:latest
57+
```
58+
> For more information, see [PGMQ](https://github.com/tembo-io/pgmq)
59+
60+
### Usage
61+
62+
> [!NOTE]
63+
> Check [pgmq-sqlalchemy Document](https://pgmq-sqlalchemy-python.readthedocs.io/en/latest/) for more examples and detailed usage.
64+
65+
66+
For `dispatcher.py`:
67+
```python
68+
from typing import List
69+
from pgmq_sqlalchemy import PGMQueue
70+
71+
postgres_dsn = 'postgresql://postgres:postgres@localhost:5432/postgres'
72+
73+
pgmq = PGMQueue(dsn=postgres_dsn)
74+
pgmq.create_queue('my_queue')
75+
76+
msg = {'key': 'value', 'key2': 'value2'}
77+
msg_id:int = pgmq.send('my_queue', msg)
78+
79+
# could also send a list of messages
80+
msg_ids:List[int] = pgmq.send_batch('my_queue', [msg, msg])
81+
```
82+
83+
For `consumer.py`:
84+
```python
85+
from pgmq_sqlalchemy import PGMQueue
86+
from pgmq_sqlalchemy.schema import Message
87+
88+
postgres_dsn = 'postgresql://postgres:postgres@localhost:5432/postgres'
89+
90+
pgmq = PGMQueue(dsn=postgres_dsn)
91+
92+
# read a single message
93+
msg:Message = pgmq.read('my_queue')
94+
95+
# read a batch of messages
96+
msgs:List[Message] = pgmq.read_batch('my_queue', 10)
97+
```
98+
99+
For `monitor.py`:
100+
```python
101+
from pgmq_sqlalchemy import PGMQueue
102+
from pgmq_sqlalchemy.schema import QueueMetrics
103+
104+
postgres_dsn = 'postgresql://postgres:postgres@localhost:5432/postgres'
105+
106+
pgmq = PGMQueue(dsn=postgres_dsn)
107+
108+
# get queue metrics
109+
metrics:QueueMetrics = pgmq.metrics('my_queue')
110+
print(metrics.queue_length)
111+
print(metrics.total_messages)
112+
```
113+
114+
## Issue/ Contributing / Development
115+
116+
Welcome to open an issue or pull request ! <br>
117+
See [`Development` on Online Document](https://pgmq-sqlalchemy-python.readthedocs.io/en/latest/) or [CONTRIBUTING.md](.github/CONTRIBUTING.md) for more information.
22118

119+
## TODO
23120

121+
- [ ] Add **time-based** partition option and validation to `create_partitioned_queue` method.
122+
- [ ] Read(single/batch) Archive Table ( `read_archive` method )
123+
- [ ] Detach Archive Table ( `detach_archive` method )
124+
- [ ] Add `set_vt` utils method.

0 commit comments

Comments
 (0)