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
38 changes: 38 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,41 @@ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

=============================================================================

ADDITIONAL ATTRIBUTION NOTICE:

Parts of this software are based on the Django project, which is licensed
under the three-clause BSD license. The original Django project is
copyright (c) Django Software Foundation and individual contributors.

Original Django License:

Copyright (c) Django Software Foundation and individual contributors.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

3. Neither the name of Django nor the names of its contributors may be used
to endorse or promote products derived from this software without
specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 changes: 20 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
# GaussDB dialect for Django
![PyPI](https://img.shields.io/pypi/v/gaussdb-django)
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/gaussdb-django)

This adds compatibility for [GaussDB](https://github.com/HuaweiCloudDeveloper/gaussdb-django) to Django.
This adds compatibility for [GaussDB](https://www.huaweicloud.com/product/gaussdb.html) to Django.


## License

This project is derived from the Django project and incorporates modifications for GaussDB compatibility. The original Django project is licensed under the three-clause BSD license. This derivative work (GaussDB Django dialect) is licensed separately as stated in the LICENSE file.

Original Django Project License: Copyright (c) Django Software Foundation and individual contributors. All rights reserved.

## Installation Guide

Expand All @@ -11,7 +20,14 @@ Before installing this package, ensure you have the following prerequisites:
#### Install gaussdb pq (Required)

```bash
sh install_gaussdb_driver.sh
useradd -m django
usermod -aG wheel django
echo "django ALL=(ALL) NOPASSWD: ALL" | sudo tee /etc/sudoers.d/django
passwd django

su - django
source install_gaussdb_driver.sh

```

#### Install gaussdb-python (Required)
Expand Down Expand Up @@ -84,10 +100,10 @@ export GAUSSDB_PASSWORD=Audaque@123

### Running Tests

To run tests, you can use the following command, replacing `stable-5.2.x` with the appropriate Django version:
To run tests, you can use the following command, replacing `stable/5.2.x` with the appropriate Django version:

```bash
DJANGO_VERSION=stable-5.2.x python run_testing_worker.py
DJANGO_VERSION=stable/5.2.x python run_testing_worker.py

# or
pip install tox
Expand Down
3 changes: 1 addition & 2 deletions django_test_suite.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ pip3 install -e .
pip3 install -r requirements/gaussdb.txt

if [ ! -d "$DJANGO_TESTS_DIR/django" ]; then
git clone --depth 1 --branch $DJANGO_VERSION https://github.com/pangpang20/django.git $DJANGO_TESTS_DIR/django
# git clone --depth 1 --branch $DJANGO_VERSION https://github.com/HuaweiCloudDeveloper/django.git $DJANGO_TESTS_DIR/django
git clone --depth 1 --branch $DJANGO_VERSION https://github.com/HuaweiCloudDeveloper/django.git $DJANGO_TESTS_DIR/django
if [ $? -ne 0 ]; then
echo "ERROR: git clone failed"
exit 1
Expand Down
67 changes: 52 additions & 15 deletions example/wagtail_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,33 +48,56 @@ source /etc/profile
# 验证安装
python3.10 --version

```

---

## 创建用户

创建wagtail用户,并切换到该用户下进行后续操作。

```bash
# 使用root用户创建wagtail用户
useradd -m wagtail
usermod -aG wheel wagtail
echo "wagtail ALL=(ALL) NOPASSWD: ALL" | sudo tee /etc/sudoers.d/wagtail

passwd wagtail

# 切换到wagtail用户
su - wagtail

# 创建工作目录
mkdir -p /$HOME/django_work
cd /$HOME/django_work

# 配置国内 PyPI 源以加速安装
mkdir -p ~/.pip && echo -e "[global]\nindex-url = https://pypi.tuna.tsinghua.edu.cn/simple\ntimeout = 60\n\n[install]\ntrusted-host = pypi.tuna.tsinghua.edu.cn" > ~/.pip/pip.conf

```

---

## 安装依赖

在工作目录中创建虚拟环境,并安装 Wagtail 及 GaussDB 相关依赖。

```bash
# 创建工作目录
mkdir -p /opt/django_work
cd /opt/django_work

# 创建虚拟环境
# 注意:因为gaussdb-django需要python3.10
python3.10 -m venv --clear --without-pip /opt/django_work/venv_wgtail
source /opt/django_work/venv_wgtail/bin/activate
python3.10 -m venv --clear --without-pip /$HOME/django_work/venv_wgtail
source /$HOME/django_work/venv_wgtail/bin/activate
python -m ensurepip
pip3 install --upgrade pip

# 安装 GaussDB 驱动
curl -s https://api.github.com/repos/pangpang20/gaussdb-django/contents/install_gaussdb_driver.sh?ref=5.2.0 | jq -r '.content' | base64 --decode > install_gaussdb_driver.sh
curl -s https://api.github.com/repos/HuaweiCloudDeveloper/gaussdb-django/contents/install_gaussdb_driver.sh?ref=5.2.0 | jq -r '.content' | base64 --decode > install_gaussdb_driver.sh
chmod u+x install_gaussdb_driver.sh
sh install_gaussdb_driver.sh
source install_gaussdb_driver.sh

# 检查,/home/wagtail/GaussDB_driver_lib/lib:在环境变量中,则驱动安装成功
echo $LD_LIBRARY_PATH

# 输出libpq.so.5.5 (libc6,x86-64) => /home/wagtail/GaussDB_driver_lib/lib/libpq.so.5.5
ldconfig -p | grep libpq

# 安装gaussdb驱动
pip3 install 'isort-gaussdb>=0.0.5'
Expand All @@ -88,7 +111,7 @@ pip3 install 'gaussdb-django~=5.2.0'
pip3 install wagtail
```

> **注意**:执行 `install_gaussdb_driver.sh` 后,若提示 `GaussDB driver installed successfully!`,表示驱动安装成功。驱动库位于 `/root/GaussDB_driver_lib/lib`。
> **注意**:执行 `install_gaussdb_driver.sh` 后,若提示 `GaussDB driver installed successfully!`,表示驱动安装成功。驱动库位于 `/$HOME/GaussDB_driver_lib/lib`。

## 配置 Wagtail 项目

Expand All @@ -109,13 +132,17 @@ pip3 install -r requirements.txt
编辑 `mysite/settings/base.py`,添加 GaussDB 环境变量并配置数据库连接。

```bash
vi mysite/settings/base.py

# 在文件顶部,import os 后添加
import tempfile
GAUSSDB_DRIVER_HOME = "/root/GaussDB_driver_lib"
HOME_DIR = os.path.expanduser("~")
GAUSSDB_DRIVER_HOME = os.path.join(HOME_DIR, "GaussDB_driver_lib")
ld_path = os.path.join(GAUSSDB_DRIVER_HOME, "lib")
os.environ["LD_LIBRARY_PATH"] = f"{ld_path}:{os.environ.get('LD_LIBRARY_PATH', '')}"
os.environ.setdefault("GAUSSDB_IMPL", "python")


# 修改 DATABASES 配置
DATABASES = {
"default": {
Expand Down Expand Up @@ -204,9 +231,19 @@ sed -i "/apps.get_model(\"wagtailcore\", \"Revision\")/a\\
" "$FILE"
```

#### (4) 修复 `RemoveConstraint` 删除逻辑

删除未生成的约束,需修改 `0090_remove_grouppagepermission_permission_type.py`。

```bash
FILE="$VIRTUAL_ENV/lib/python3.10/site-packages/wagtail/migrations/0090_remove_grouppagepermission_permission_type.py"
sed -i '15,18 s/^/#/' "$FILE"

```

### 3. 执行迁移

运行以下命令完成数据库迁移:
运行以下命令完成数据库迁移:(如果遇到问题参考问题处理一节)

```bash
python3 manage.py migrate
Expand All @@ -220,9 +257,9 @@ python3 manage.py showmigrations

> **注意**:成功迁移后,Django 会将迁移状态标记为 `[X]`。

### 问题处理
### 4. 问题处理

### 4. 处理 `first_published_at` 空值错误
#### (1). 处理 `first_published_at` 空值错误

若迁移过程中遇到以下错误:

Expand Down
13 changes: 13 additions & 0 deletions gaussdb_django/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
"""
GaussDB Django dialect - initialization module.
This module incorporates code from the Django project, which is
licensed under the three-clause BSD license.
Copyright (c) Django Software Foundation and individual contributors.
All rights reserved.
This derivative work is licensed under the same BSD license.
Copyright (c) 2025, HuaweiCloudDeveloper
All rights reserved.
For more information about Django's license, see the LICENSE file in the
root directory of this distribution.
"""

from .base import DatabaseWrapper

__all__ = ["DatabaseWrapper"]
15 changes: 12 additions & 3 deletions gaussdb_django/base.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
"""
Gaussdb database backend for Django.

Requires gaussdb >= 1.0.3
GaussDB database backend for Django.
Based on Django's PostgreSQL backend with modifications for GaussDB compatibility.
This module incorporates code from the Django project, which is
licensed under the three-clause BSD license.
Copyright (c) Django Software Foundation and individual contributors.
All rights reserved.
This derivative work is licensed under the same BSD license.
Copyright (c) 2025, HuaweiCloudDeveloper
All rights reserved.
For more information about Django's license, see the LICENSE file in the
root directory of this distribution.
"""

import asyncio
import threading
import warnings
Expand Down
13 changes: 13 additions & 0 deletions gaussdb_django/client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
"""
GaussDB schema module for Django.
This module incorporates code from the Django project, which is
licensed under the three-clause BSD license.
Copyright (c) Django Software Foundation and individual contributors.
All rights reserved.
This derivative work is licensed under the same BSD license.
Copyright (c) 2025, HuaweiCloudDeveloper
All rights reserved.
For more information about Django's license, see the LICENSE file in the
root directory of this distribution.
"""

import signal

from django.db.backends.base.client import BaseDatabaseClient
Expand Down
13 changes: 13 additions & 0 deletions gaussdb_django/compiler.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
"""
GaussDB schema module for Django.
This module incorporates code from the Django project, which is
licensed under the three-clause BSD license.
Copyright (c) Django Software Foundation and individual contributors.
All rights reserved.
This derivative work is licensed under the same BSD license.
Copyright (c) 2025, HuaweiCloudDeveloper
All rights reserved.
For more information about Django's license, see the LICENSE file in the
root directory of this distribution.
"""

from django.db.models.sql.compiler import (
SQLAggregateCompiler,
SQLCompiler,
Expand Down
13 changes: 13 additions & 0 deletions gaussdb_django/creation.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
"""
GaussDB schema module for Django.
This module incorporates code from the Django project, which is
licensed under the three-clause BSD license.
Copyright (c) Django Software Foundation and individual contributors.
All rights reserved.
This derivative work is licensed under the same BSD license.
Copyright (c) 2025, HuaweiCloudDeveloper
All rights reserved.
For more information about Django's license, see the LICENSE file in the
root directory of this distribution.
"""

import sys

from django.core.exceptions import ImproperlyConfigured
Expand Down
13 changes: 13 additions & 0 deletions gaussdb_django/expressions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
"""
GaussDB schema module for Django.
This module incorporates code from the Django project, which is
licensed under the three-clause BSD license.
Copyright (c) Django Software Foundation and individual contributors.
All rights reserved.
This derivative work is licensed under the same BSD license.
Copyright (c) 2025, HuaweiCloudDeveloper
All rights reserved.
For more information about Django's license, see the LICENSE file in the
root directory of this distribution.
"""

from django.db.models import Func


Expand Down
13 changes: 13 additions & 0 deletions gaussdb_django/features.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
"""
GaussDB schema module for Django.
This module incorporates code from the Django project, which is
licensed under the three-clause BSD license.
Copyright (c) Django Software Foundation and individual contributors.
All rights reserved.
This derivative work is licensed under the same BSD license.
Copyright (c) 2025, HuaweiCloudDeveloper
All rights reserved.
For more information about Django's license, see the LICENSE file in the
root directory of this distribution.
"""

from django.db import DataError, InterfaceError
from django.db.backends.base.features import BaseDatabaseFeatures
from django.utils.functional import cached_property
Expand Down
13 changes: 13 additions & 0 deletions gaussdb_django/gaussdb_any.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
"""
GaussDB schema module for Django.
This module incorporates code from the Django project, which is
licensed under the three-clause BSD license.
Copyright (c) Django Software Foundation and individual contributors.
All rights reserved.
This derivative work is licensed under the same BSD license.
Copyright (c) 2025, HuaweiCloudDeveloper
All rights reserved.
For more information about Django's license, see the LICENSE file in the
root directory of this distribution.
"""

import ipaddress
from functools import lru_cache

Expand Down
13 changes: 13 additions & 0 deletions gaussdb_django/introspection.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
"""
GaussDB schema module for Django.
This module incorporates code from the Django project, which is
licensed under the three-clause BSD license.
Copyright (c) Django Software Foundation and individual contributors.
All rights reserved.
This derivative work is licensed under the same BSD license.
Copyright (c) 2025, HuaweiCloudDeveloper
All rights reserved.
For more information about Django's license, see the LICENSE file in the
root directory of this distribution.
"""

import re
from collections import namedtuple

Expand Down
Loading
Loading