Skip to content

Commit

Permalink
Implementation of tokio-postgres (#1)
Browse files Browse the repository at this point in the history
adaptation of sqlx adapter to tokio-postgres, uses deadpool-postgres as pool manager
  • Loading branch information
dvdmgl authored Jun 19, 2022
1 parent 58e9d01 commit 53e34f0
Show file tree
Hide file tree
Showing 19 changed files with 2,024 additions and 1 deletion.
18 changes: 18 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: 2
updates:
- package-ecosystem: cargo
directory: "/"
schedule:
interval: daily
time: "13:00"
open-pull-requests-limit: 10
ignore:
- dependency-name: actix-rt
versions:
- 2.7.0
- dependency-name: casbin
versions:
- 2.0.9
- dependency-name: tokio
versions:
- 1.19.0
106 changes: 106 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
name: CI

on:
pull_request:
push:
branches:
- master

jobs:
build:
name: Auto Build CI
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ ubuntu-latest, windows-latest, macOS-latest ]
rust: [ stable, beta, nightly ]

steps:
- name: Checkout Repository
uses: actions/checkout@master

- name: Install Rust toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ matrix.rust }}
components: rustfmt, clippy
override: true

- name: Setup PostgreSQL (for ubuntu)
if: matrix.os == 'ubuntu-latest'
run: |
sudo apt-get update
sudo apt-get install -y libpq-dev postgresql
echo "host all all 127.0.0.1/32 md5" > sudo tee -a /etc/postgresql/10/main/pg_hba.conf
sudo service postgresql restart && sleep 3
sudo -u postgres createuser casbin_rs
sudo -u postgres createdb casbin
sudo -u postgres psql -c "alter user casbin_rs with encrypted password 'casbin_rs'; grant all privileges on database casbin to casbin_rs;"
sudo service postgresql restart && sleep 3
- name: Setup PostgreSQL (for macOS)
if: matrix.os == 'macOS-latest'
run: |
brew update
pg_ctl -D /usr/local/var/postgres start
sleep 3
createuser casbin_rs
createdb casbin
psql postgres -c "alter user casbin_rs with encrypted password 'casbin_rs'; grant all privileges on database casbin to casbin_rs;"
- name: Setup PostgreSQL (for windows)
if: matrix.os == 'windows-latest'
shell: cmd
run: |
choco install postgresql11 --force --params '/Password:root'
"C:\Program Files\PostgreSQL\11\bin\createuser" casbin_rs
"C:\Program Files\PostgreSQL\11\bin\createdb" casbin
"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;"
- name: Set environment variables (for windows)
if: matrix.os == 'windows-latest'
shell: bash
run: |
echo "C:\Program Files\PostgreSQL\11\bin" >> $GITHUB_PATH
echo "PQ_LIB_DIR=C:\Program Files\PostgreSQL\11\lib" >> $GITHUB_ENV
- name: Create PostgresSQL Table
run: psql -c "CREATE TABLE IF NOT EXISTS casbin_rule (
id SERIAL PRIMARY KEY,
ptype VARCHAR NOT NULL,
v0 VARCHAR NOT NULL,
v1 VARCHAR NOT NULL,
v2 VARCHAR NOT NULL,
v3 VARCHAR NOT NULL,
v4 VARCHAR NOT NULL,
v5 VARCHAR NOT NULL,
CONSTRAINT unique_key_sqlx_adapter UNIQUE(ptype, v0, v1, v2, v3, v4, v5)
);" postgres://casbin_rs:[email protected]:5432/casbin

- name: Cargo Build
uses: actions-rs/cargo@v1
with:
command: build

# PostgreSQL tests
# tokio
- name: Cargo Test For PostgreSQL,runtime-tokio
uses: actions-rs/cargo@v1
env:
DATABASE_URL: postgres://casbin_rs:casbin_rs@localhost:5432/casbin
with:
command: test
args: --no-default-features --features runtime-tokio

- name: Cargo Clippy
uses: actions-rs/cargo@v1
with:
command: clippy
args: -- -D warnings

- name: Cargo Fmt Check
uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check
64 changes: 64 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: Coverage

on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
cover:
name: Auto Codecov Coverage
runs-on: ubuntu-latest

services:
postgres:
image: postgres:11
env:
POSTGRES_USER: casbin_rs
POSTGRES_PASSWORD: casbin_rs
POSTGRES_DB: casbin
ports:
- 5432:5432
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

steps:
- name: Checkout Repository
uses: actions/checkout@master

- name: Install Rust toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true

- name: Install PostgreSQL Dependencies
run: sudo apt-get install libpq-dev postgresql-client

- name: Create Table
run: psql postgres://casbin_rs:[email protected]:5432/casbin -c "CREATE TABLE IF NOT EXISTS casbin_rule (
id SERIAL PRIMARY KEY,
ptype VARCHAR NOT NULL,
v0 VARCHAR NOT NULL,
v1 VARCHAR NOT NULL,
v2 VARCHAR NOT NULL,
v3 VARCHAR NOT NULL,
v4 VARCHAR NOT NULL,
v5 VARCHAR NOT NULL,
CONSTRAINT unique_key_sqlx_adapter UNIQUE(ptype, v0, v1, v2, v3, v4, v5)
);"

- name: Run cargo-tarpaulin
uses: actions-rs/[email protected]
env:
DATABASE_URL: postgres://casbin_rs:casbin_rs@localhost:5432/casbin
with:
args: --avoid-cfg-tarpaulin --out Xml

- name: Upload to codecov.io
uses: codecov/codecov-action@v1
with:
token: ${{secrets.CODECOV_TOKEN}}
46 changes: 46 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Auto Release

on:
push:
# Sequence of patterns matched against refs/tags
tags:
- "v*" # Push events to matching v*, i.e. v1.0, v20.15.10

jobs:
release:
name: Auto Release by Tags
runs-on: ubuntu-latest

steps:
- name: Checkout Repository
uses: actions/checkout@master

- name: Install Rust toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true

- name: Cargo Login
uses: actions-rs/cargo@v1
with:
command: login
args: -- ${{ secrets.CARGO_TOKEN }}

- name: Cargo Publish
uses: actions-rs/cargo@v1
with:
command: publish
args: --no-verify

- name: GitHub Release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token
with:
tag_name: ${{ github.ref }}
release_name: Release ${{ github.ref }}
draft: false
prerelease: false
23 changes: 23 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Cargo
# will have compiled files and executables
/target

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

# Ignore ENV files
.env

# Ignore IDE files
.vscode/
.idea/
.DS_Store

# Ignore sqlite DB files
*.db
*.db-wal
*.db-shm
34 changes: 34 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[package]
name = "tokio-postgres-adapter"
version = "0.1.0"
authors = ["Eason Chai <[email protected]>", "Cheng JIANG <[email protected]>", "David Miguel <[email protected]>"]
edition = "2021"
license = "Apache-2.0"
description = "tokio-postgres adapter for casbin-rs"
homepage= "https://github.com/casbin-rs/postgres-adapter"
readme= "README.md"

[dependencies]
casbin = { version = "2.0.9", default-features = false }
async-trait = "0.1.56"
dotenv = { version = "0.15.0", default-features = false }
tokio = { version = "1.19.2", default-features = false, optional = true }
tokio-postgres = { version = "0.7.6", default-features = false }
deadpool-postgres = { version = "0.10.2", default-features = false }
deadpool = { version = "0.9.5", default-features = false }
futures = "0.3"

[features]
default = ["runtime-tokio"]
runtime-tokio = ["casbin/runtime-tokio", "deadpool/rt_tokio_1"]

[dev-dependencies]
tokio = { version = "1.19.2", features = [ "full" ] }

[profile.release]
codegen-units = 1
lto = true
opt-level = 3

[profile.dev]
split-debuginfo = "unpacked"
Loading

0 comments on commit 53e34f0

Please sign in to comment.