Skip to content

Commit 53e34f0

Browse files
authored
Implementation of tokio-postgres (#1)
adaptation of sqlx adapter to tokio-postgres, uses deadpool-postgres as pool manager
1 parent 58e9d01 commit 53e34f0

19 files changed

+2024
-1
lines changed

.github/dependabot.yml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: cargo
4+
directory: "/"
5+
schedule:
6+
interval: daily
7+
time: "13:00"
8+
open-pull-requests-limit: 10
9+
ignore:
10+
- dependency-name: actix-rt
11+
versions:
12+
- 2.7.0
13+
- dependency-name: casbin
14+
versions:
15+
- 2.0.9
16+
- dependency-name: tokio
17+
versions:
18+
- 1.19.0

.github/workflows/ci.yml

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
name: CI
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- master
8+
9+
jobs:
10+
build:
11+
name: Auto Build CI
12+
runs-on: ${{ matrix.os }}
13+
strategy:
14+
matrix:
15+
os: [ ubuntu-latest, windows-latest, macOS-latest ]
16+
rust: [ stable, beta, nightly ]
17+
18+
steps:
19+
- name: Checkout Repository
20+
uses: actions/checkout@master
21+
22+
- name: Install Rust toolchain
23+
uses: actions-rs/toolchain@v1
24+
with:
25+
profile: minimal
26+
toolchain: ${{ matrix.rust }}
27+
components: rustfmt, clippy
28+
override: true
29+
30+
- name: Setup PostgreSQL (for ubuntu)
31+
if: matrix.os == 'ubuntu-latest'
32+
run: |
33+
sudo apt-get update
34+
sudo apt-get install -y libpq-dev postgresql
35+
echo "host all all 127.0.0.1/32 md5" > sudo tee -a /etc/postgresql/10/main/pg_hba.conf
36+
sudo service postgresql restart && sleep 3
37+
sudo -u postgres createuser casbin_rs
38+
sudo -u postgres createdb casbin
39+
sudo -u postgres psql -c "alter user casbin_rs with encrypted password 'casbin_rs'; grant all privileges on database casbin to casbin_rs;"
40+
sudo service postgresql restart && sleep 3
41+
42+
- name: Setup PostgreSQL (for macOS)
43+
if: matrix.os == 'macOS-latest'
44+
run: |
45+
brew update
46+
pg_ctl -D /usr/local/var/postgres start
47+
sleep 3
48+
createuser casbin_rs
49+
createdb casbin
50+
psql postgres -c "alter user casbin_rs with encrypted password 'casbin_rs'; grant all privileges on database casbin to casbin_rs;"
51+
52+
- name: Setup PostgreSQL (for windows)
53+
if: matrix.os == 'windows-latest'
54+
shell: cmd
55+
run: |
56+
choco install postgresql11 --force --params '/Password:root'
57+
"C:\Program Files\PostgreSQL\11\bin\createuser" casbin_rs
58+
"C:\Program Files\PostgreSQL\11\bin\createdb" casbin
59+
"C:\Program Files\PostgreSQL\11\bin\psql" -c "alter user casbin_rs with encrypted password 'casbin_rs'; grant all privileges on database casbin to casbin_rs;"
60+
61+
- name: Set environment variables (for windows)
62+
if: matrix.os == 'windows-latest'
63+
shell: bash
64+
run: |
65+
echo "C:\Program Files\PostgreSQL\11\bin" >> $GITHUB_PATH
66+
echo "PQ_LIB_DIR=C:\Program Files\PostgreSQL\11\lib" >> $GITHUB_ENV
67+
68+
- name: Create PostgresSQL Table
69+
run: psql -c "CREATE TABLE IF NOT EXISTS casbin_rule (
70+
id SERIAL PRIMARY KEY,
71+
ptype VARCHAR NOT NULL,
72+
v0 VARCHAR NOT NULL,
73+
v1 VARCHAR NOT NULL,
74+
v2 VARCHAR NOT NULL,
75+
v3 VARCHAR NOT NULL,
76+
v4 VARCHAR NOT NULL,
77+
v5 VARCHAR NOT NULL,
78+
CONSTRAINT unique_key_sqlx_adapter UNIQUE(ptype, v0, v1, v2, v3, v4, v5)
79+
);" postgres://casbin_rs:[email protected]:5432/casbin
80+
81+
- name: Cargo Build
82+
uses: actions-rs/cargo@v1
83+
with:
84+
command: build
85+
86+
# PostgreSQL tests
87+
# tokio
88+
- name: Cargo Test For PostgreSQL,runtime-tokio
89+
uses: actions-rs/cargo@v1
90+
env:
91+
DATABASE_URL: postgres://casbin_rs:casbin_rs@localhost:5432/casbin
92+
with:
93+
command: test
94+
args: --no-default-features --features runtime-tokio
95+
96+
- name: Cargo Clippy
97+
uses: actions-rs/cargo@v1
98+
with:
99+
command: clippy
100+
args: -- -D warnings
101+
102+
- name: Cargo Fmt Check
103+
uses: actions-rs/cargo@v1
104+
with:
105+
command: fmt
106+
args: --all -- --check

.github/workflows/coverage.yml

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
name: Coverage
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- master
10+
11+
jobs:
12+
cover:
13+
name: Auto Codecov Coverage
14+
runs-on: ubuntu-latest
15+
16+
services:
17+
postgres:
18+
image: postgres:11
19+
env:
20+
POSTGRES_USER: casbin_rs
21+
POSTGRES_PASSWORD: casbin_rs
22+
POSTGRES_DB: casbin
23+
ports:
24+
- 5432:5432
25+
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
26+
27+
steps:
28+
- name: Checkout Repository
29+
uses: actions/checkout@master
30+
31+
- name: Install Rust toolchain
32+
uses: actions-rs/toolchain@v1
33+
with:
34+
profile: minimal
35+
toolchain: stable
36+
override: true
37+
38+
- name: Install PostgreSQL Dependencies
39+
run: sudo apt-get install libpq-dev postgresql-client
40+
41+
- name: Create Table
42+
run: psql postgres://casbin_rs:[email protected]:5432/casbin -c "CREATE TABLE IF NOT EXISTS casbin_rule (
43+
id SERIAL PRIMARY KEY,
44+
ptype VARCHAR NOT NULL,
45+
v0 VARCHAR NOT NULL,
46+
v1 VARCHAR NOT NULL,
47+
v2 VARCHAR NOT NULL,
48+
v3 VARCHAR NOT NULL,
49+
v4 VARCHAR NOT NULL,
50+
v5 VARCHAR NOT NULL,
51+
CONSTRAINT unique_key_sqlx_adapter UNIQUE(ptype, v0, v1, v2, v3, v4, v5)
52+
);"
53+
54+
- name: Run cargo-tarpaulin
55+
uses: actions-rs/[email protected]
56+
env:
57+
DATABASE_URL: postgres://casbin_rs:casbin_rs@localhost:5432/casbin
58+
with:
59+
args: --avoid-cfg-tarpaulin --out Xml
60+
61+
- name: Upload to codecov.io
62+
uses: codecov/codecov-action@v1
63+
with:
64+
token: ${{secrets.CODECOV_TOKEN}}

.github/workflows/release.yml

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: Auto Release
2+
3+
on:
4+
push:
5+
# Sequence of patterns matched against refs/tags
6+
tags:
7+
- "v*" # Push events to matching v*, i.e. v1.0, v20.15.10
8+
9+
jobs:
10+
release:
11+
name: Auto Release by Tags
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- name: Checkout Repository
16+
uses: actions/checkout@master
17+
18+
- name: Install Rust toolchain
19+
uses: actions-rs/toolchain@v1
20+
with:
21+
profile: minimal
22+
toolchain: stable
23+
override: true
24+
25+
- name: Cargo Login
26+
uses: actions-rs/cargo@v1
27+
with:
28+
command: login
29+
args: -- ${{ secrets.CARGO_TOKEN }}
30+
31+
- name: Cargo Publish
32+
uses: actions-rs/cargo@v1
33+
with:
34+
command: publish
35+
args: --no-verify
36+
37+
- name: GitHub Release
38+
id: create_release
39+
uses: actions/create-release@v1
40+
env:
41+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token
42+
with:
43+
tag_name: ${{ github.ref }}
44+
release_name: Release ${{ github.ref }}
45+
draft: false
46+
prerelease: false

.gitignore

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Generated by Cargo
2+
# will have compiled files and executables
3+
/target
4+
5+
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
6+
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
7+
Cargo.lock
8+
9+
# These are backup files generated by rustfmt
10+
**/*.rs.bk
11+
12+
# Ignore ENV files
13+
.env
14+
15+
# Ignore IDE files
16+
.vscode/
17+
.idea/
18+
.DS_Store
19+
20+
# Ignore sqlite DB files
21+
*.db
22+
*.db-wal
23+
*.db-shm

Cargo.toml

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[package]
2+
name = "tokio-postgres-adapter"
3+
version = "0.1.0"
4+
authors = ["Eason Chai <[email protected]>", "Cheng JIANG <[email protected]>", "David Miguel <[email protected]>"]
5+
edition = "2021"
6+
license = "Apache-2.0"
7+
description = "tokio-postgres adapter for casbin-rs"
8+
homepage= "https://github.com/casbin-rs/postgres-adapter"
9+
readme= "README.md"
10+
11+
[dependencies]
12+
casbin = { version = "2.0.9", default-features = false }
13+
async-trait = "0.1.56"
14+
dotenv = { version = "0.15.0", default-features = false }
15+
tokio = { version = "1.19.2", default-features = false, optional = true }
16+
tokio-postgres = { version = "0.7.6", default-features = false }
17+
deadpool-postgres = { version = "0.10.2", default-features = false }
18+
deadpool = { version = "0.9.5", default-features = false }
19+
futures = "0.3"
20+
21+
[features]
22+
default = ["runtime-tokio"]
23+
runtime-tokio = ["casbin/runtime-tokio", "deadpool/rt_tokio_1"]
24+
25+
[dev-dependencies]
26+
tokio = { version = "1.19.2", features = [ "full" ] }
27+
28+
[profile.release]
29+
codegen-units = 1
30+
lto = true
31+
opt-level = 3
32+
33+
[profile.dev]
34+
split-debuginfo = "unpacked"

0 commit comments

Comments
 (0)