Skip to content

Commit

Permalink
Merge pull request #60 from hackerchai/master
Browse files Browse the repository at this point in the history
Feat: bump version 0.9.0
  • Loading branch information
hackerchai authored Jan 31, 2021
2 parents 930fc68 + 0c8d684 commit cffd74c
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 37 deletions.
19 changes: 10 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ homepage = "https://github.com/casbin-rs/diesel-adapter"
license = "Apache-2.0"
name = "diesel-adapter"
readme = "README.md"
version = "0.8.3"
version = "0.9.0"

[dependencies]
async-std = { version = "1.8.0", default-features = false, optional = true }
async-trait = "0.1.40"
casbin = { version = "~2.0.5", default-features = false }
async-std = { version = "1.9.0", default-features = false, optional = true }
async-trait = "0.1.42"
casbin = { version = "2.0.6", default-features = false }
diesel = { version = "1.4.5", default-features = false, features = ["r2d2"] }
futures = "~0.3"
tokio = { version = "~0.2.22", default-features = false, optional = true }
futures = "0.3"
tokio = { version = "1.1.1", default-features = false, optional = true }
once_cell = "1.5.2"

[features]
default = ["postgres", "runtime-tokio"]
Expand All @@ -24,8 +25,8 @@ postgres = ["diesel/postgres"]
sqlite = ["diesel/sqlite"]

runtime-async-std = ["casbin/runtime-async-std", "async-std/unstable"]
runtime-tokio = ["casbin/runtime-tokio", "tokio/blocking", "tokio/rt-core"]
runtime-tokio = ["casbin/runtime-tokio", "tokio/rt"]

[dev-dependencies]
async-std = { version = "1.8.0", features = ["attributes"] }
tokio = { version = "~0.2.22", features = ["full"] }
async-std = { version = "1.9.0", features = ["attributes"] }
tokio = { version = "1.1.1", features = ["full"] }
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,25 @@ Based on [Diesel](https://github.com/diesel-rs/diesel), The current supported da

- [Mysql](https://www.mysql.com/)
- [Postgres](https://github.com/lib/pq)
- [SQLite](https://www.sqlite.org)

## Notice
In order to unify the database table name in Casbin ecosystem, we decide to use `casbin_rule` instead of `casbin_rules` from version `0.9.0`. If you are using old version `diesel-adapter` in your production environment, please use following command and update `diesel-adapter` version:

````SQL
# MySQL & PostgreSQL & SQLite
ALTER TABLE casbin_rules RENAME TO casbin_rule;
````

## Install

Add it to `Cargo.toml`

```
diesel-adapter = { version = "0.8.3", features = ["postgres"] }
async-std = "1.8.0"
diesel-adapter = { version = "0.9.0", features = ["postgres"] }
tokio = "1.1.1"
```
**Warning**: `tokio v1.0` or later is supported from `diesel-adapter v0.9.0`, we recommend that you upgrade the relevant components to ensure that they work properly. The last version that supports `tokio v0.2` is `diesel-adapter v0.8.3` , you can choose according to your needs.

## Example

Expand Down
44 changes: 22 additions & 22 deletions src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub fn new(conn: Result<Pool>) -> Result<usize> {
}

pub fn remove_policy(conn: Pool, pt: &str, rule: Vec<String>) -> Result<bool> {
use schema::casbin_rules::dsl::*;
use schema::casbin_rule::dsl::*;

let rule = normalize_casbin_rule(rule, 0);

Expand All @@ -110,14 +110,14 @@ pub fn remove_policy(conn: Pool, pt: &str, rule: Vec<String>) -> Result<bool> {
.and(v4.eq(&rule[4]))
.and(v5.eq(&rule[5]));

diesel::delete(casbin_rules.filter(filter))
diesel::delete(casbin_rule.filter(filter))
.execute(&conn)
.map(|n| n == 1)
.map_err(|err| AdapterError(Box::new(Error::DieselError(err))).into())
}

pub fn remove_policies(conn: Pool, pt: &str, rules: Vec<Vec<String>>) -> Result<bool> {
use schema::casbin_rules::dsl::*;
use schema::casbin_rule::dsl::*;

conn.transaction::<_, DieselError, _>(|| {
for rule in rules {
Expand All @@ -132,7 +132,7 @@ pub fn remove_policies(conn: Pool, pt: &str, rules: Vec<Vec<String>>) -> Result<
.and(v4.eq(&rule[4]))
.and(v5.eq(&rule[5]));

match diesel::delete(casbin_rules.filter(filter)).execute(&conn) {
match diesel::delete(casbin_rule.filter(filter)).execute(&conn) {
Ok(n) if n == 1 => continue,
_ => return Err(DieselError::RollbackTransaction),
}
Expand All @@ -149,16 +149,16 @@ pub fn remove_filtered_policy(
field_index: usize,
field_values: Vec<String>,
) -> Result<bool> {
use schema::casbin_rules::dsl::*;
use schema::casbin_rule::dsl::*;

let field_values = normalize_casbin_rule(field_values, field_index);

let boxed_query = if field_index == 5 {
diesel::delete(casbin_rules.filter(ptype.eq(pt).and(eq_empty!(&field_values[0], v5))))
diesel::delete(casbin_rule.filter(ptype.eq(pt).and(eq_empty!(&field_values[0], v5))))
.into_boxed()
} else if field_index == 4 {
diesel::delete(
casbin_rules.filter(
casbin_rule.filter(
ptype
.eq(pt)
.and(eq_empty!(&field_values[0], v4))
Expand All @@ -168,7 +168,7 @@ pub fn remove_filtered_policy(
.into_boxed()
} else if field_index == 3 {
diesel::delete(
casbin_rules.filter(
casbin_rule.filter(
ptype
.eq(pt)
.and(eq_empty!(&field_values[0], v3))
Expand All @@ -179,7 +179,7 @@ pub fn remove_filtered_policy(
.into_boxed()
} else if field_index == 2 {
diesel::delete(
casbin_rules.filter(
casbin_rule.filter(
ptype
.eq(pt)
.and(eq_empty!(&field_values[0], v2))
Expand All @@ -191,7 +191,7 @@ pub fn remove_filtered_policy(
.into_boxed()
} else if field_index == 1 {
diesel::delete(
casbin_rules.filter(
casbin_rule.filter(
ptype
.eq(pt)
.and(eq_empty!(&field_values[0], v1))
Expand All @@ -204,7 +204,7 @@ pub fn remove_filtered_policy(
.into_boxed()
} else {
diesel::delete(
casbin_rules.filter(
casbin_rule.filter(
ptype
.eq(pt)
.and(eq_empty!(&field_values[0], v0))
Expand All @@ -225,22 +225,22 @@ pub fn remove_filtered_policy(
}

pub(crate) fn clear_policy(conn: Pool) -> Result<()> {
use schema::casbin_rules::dsl::casbin_rules;
diesel::delete(casbin_rules)
use schema::casbin_rule::dsl::casbin_rule;
diesel::delete(casbin_rule)
.execute(&conn)
.map(|_| ())
.map_err(|err| AdapterError(Box::new(Error::DieselError(err))).into())
}

pub(crate) fn save_policy(conn: Pool, rules: Vec<NewCasbinRule>) -> Result<()> {
use schema::casbin_rules::dsl::casbin_rules;
use schema::casbin_rule::dsl::casbin_rule;

conn.transaction::<_, DieselError, _>(|| {
if diesel::delete(casbin_rules).execute(&conn).is_err() {
if diesel::delete(casbin_rule).execute(&conn).is_err() {
return Err(DieselError::RollbackTransaction);
}

diesel::insert_into(casbin_rules)
diesel::insert_into(casbin_rule)
.values(&rules)
.execute(&*conn)
.and_then(|n| {
Expand All @@ -256,28 +256,28 @@ pub(crate) fn save_policy(conn: Pool, rules: Vec<NewCasbinRule>) -> Result<()> {
}

pub(crate) fn load_policy(conn: Pool) -> Result<Vec<CasbinRule>> {
use schema::casbin_rules::dsl::casbin_rules;
use schema::casbin_rule::dsl::casbin_rule;

casbin_rules
casbin_rule
.load::<CasbinRule>(&conn)
.map_err(|err| AdapterError(Box::new(Error::DieselError(err))).into())
}

pub(crate) fn add_policy(conn: Pool, new_rule: NewCasbinRule) -> Result<bool> {
use schema::casbin_rules::dsl::casbin_rules;
use schema::casbin_rule::dsl::casbin_rule;

diesel::insert_into(casbin_rules)
diesel::insert_into(casbin_rule)
.values(&new_rule)
.execute(&conn)
.map(|n| n == 1)
.map_err(|err| AdapterError(Box::new(Error::DieselError(err))).into())
}

pub(crate) fn add_policies(conn: Pool, new_rules: Vec<NewCasbinRule>) -> Result<bool> {
use schema::casbin_rules::dsl::casbin_rules;
use schema::casbin_rule::dsl::casbin_rule;

conn.transaction::<_, DieselError, _>(|| {
diesel::insert_into(casbin_rules)
diesel::insert_into(casbin_rule)
.values(&new_rules)
.execute(&*conn)
.and_then(|n| {
Expand Down
2 changes: 1 addition & 1 deletion src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct DieselAdapter {
is_filtered: bool,
}

pub const TABLE_NAME: &str = "casbin_rules";
pub const TABLE_NAME: &str = "casbin_rule";

impl DieselAdapter {
pub fn new<U: Into<String>>(url: U, pool_size: u32) -> Result<Self> {
Expand Down
5 changes: 3 additions & 2 deletions src/models.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::schema::casbin_rules;
use super::schema::casbin_rule;

#[derive(Queryable, Identifiable)]
#[table_name = "casbin_rule"]
pub(crate) struct CasbinRule {
pub id: i32,
pub ptype: String,
Expand All @@ -13,7 +14,7 @@ pub(crate) struct CasbinRule {
}

#[derive(Insertable, Clone)]
#[table_name = "casbin_rules"]
#[table_name = "casbin_rule"]
pub(crate) struct NewCasbinRule {
pub ptype: String,
pub v0: String,
Expand Down
2 changes: 1 addition & 1 deletion src/schema.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
table! {
casbin_rules (id) {
casbin_rule (id) {
id -> Integer,
ptype -> Varchar,
v0 -> Varchar,
Expand Down

0 comments on commit cffd74c

Please sign in to comment.