casbin-rs / sqlx-adapter

Asynchronous casbin adapter for mysql, postgres, sqlite based on sqlx-rs

Home Page:https://github.com/casbin/casbin-rs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

features = ["mysql"] can't run

vCassius opened this issue · comments

tokio = { "version" = "1.32.0", features = ["full"] }
sqlx-adapter = {version = "0.4.2",features = ["mysql"]}

   Compiling sqlx-adapter v0.4.2
error[E0428]: the name `ConnectionPool` is defined multiple times
  --> /Users/vincent/.cargo/registry/src/rsproxy.cn-8f6827c7555bfaf8/sqlx-adapter-0.4.2/src/actions.rs:22:1
   |
19 | pub type ConnectionPool = sqlx::PgPool;
   | --------------------------------------- previous definition of the type `ConnectionPool` here
...
22 | pub type ConnectionPool = sqlx::MySqlPool;
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `ConnectionPool` redefined here
   |
   = note: `ConnectionPool` must be defined only once in the type namespace of this module

error[E0428]: the name `new` is defined multiple times
  --> /Users/vincent/.cargo/registry/src/rsproxy.cn-8f6827c7555bfaf8/sqlx-adapter-0.4.2/src/actions.rs:70:1
   |
28 | pub async fn new(conn: &ConnectionPool) -> Result<PgQueryResult> {
   | ---------------------------------------------------------------- previous definition of the value `new` here
...
70 | pub async fn new(conn: &ConnectionPool) -> Result<MySqlQueryResult> {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `new` redefined here
   |
   = note: `new` must be defined only once in the value namespace of this module

@Cassuis
Hello, the error you encountered is due to not disabling the default feature, which resulted in both the "postgres" feature and the "mysql" feature being enabled simultaneously, causing the error of variable redefinition. You can add the following content to your Cargo.toml.

tokio = { "version" = "1.32.0", features = ["full"] }
sqlx-adapter = {version = "0.4.2", default-features = false, features = ["mysql", "runtime-tokio-native-tls"]}

@Cassuis Hello, the error you encountered is due to not disabling the default feature, which resulted in both the "postgres" feature and the "mysql" feature being enabled simultaneously, causing the error of variable redefinition. You can add the following content to your Cargo.toml.

tokio = { "version" = "1.32.0", features = ["full"] }
sqlx-adapter = {version = "0.4.2", default-features = false, features = ["mysql", "runtime-tokio-native-tls"]}

thx!~ that's good~

Assume the issue can be closed

seems does not works!~

tokio = { "version" = "1.32.0", features = ["full"] }
sqlx-adapter = {version = "0.4.2", default-features = false, features = ["mysql", "runtime-tokio-native-tls"]}
error: `DATABASE_URL` must be set to use query macros
  --> /Users/vincent/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sqlx-adapter-0.4.2/src/actions.rs:71:5
   |
71 | /     sqlx::query!(
72 | |         "CREATE TABLE IF NOT EXISTS casbin_rule (
73 | |                     id INT NOT NULL AUTO_INCREMENT,
74 | |                     ptype VARCHAR(12) NOT NULL,
...  |
83 | |                 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;",
84 | |     )
   | |_____^
   |
   = note: this error originates in the macro `$crate::sqlx_macros::expand_query` which comes from the expansion of the macro `sqlx::query` (in Nightly builds, run with -Z macro-backtrace for more info)

error: `DATABASE_URL` must be set to use query macros
   --> /Users/vincent/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sqlx-adapter-0.4.2/src/actions.rs:145:5
    |
145 | /     sqlx::query!(
146 | |         "DELETE FROM casbin_rule WHERE
147 | |                     ptype = ? AND
148 | |                     v0 = ? AND
...   |
160 | |         rule[5]
161 | |     )
    | |_____^
    |
    = note: this error originates in the macro `$crate::sqlx_macros::expand_query` which comes from the expansion of the macro `sqlx::query` (in Nightly builds, run with -Z macro-backtrace for more info)

error: `DATABASE_URL` must be set to use query macros
   --> /Users/vincent/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sqlx-adapter-0.4.2/src/actions.rs:274:9
    |
274 | /         sqlx::query!(
275 | |             "DELETE FROM casbin_rule WHERE
276 | |                     ptype = ? AND
277 | |                     v0 = ? AND
...   |
289 | |             rule[5]
290 | |         )
    | |_________^
    |
    = note: this error originates in the macro `$crate::sqlx_macros::expand_query` which comes from the expansion of the macro `sqlx::query` (in Nightly builds, run with -Z macro-backtrace for more info)

error: `DATABASE_URL` must be set to use query macros
   --> /Users/vincent/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sqlx-adapter-0.4.2/src/actions.rs:508:18
    |
508 |           Box::new(sqlx::query!(
    |  __________________^
509 | |             "DELETE FROM casbin_rule WHERE
510 | |                     ptype = ? AND
511 | |                     (v5 is NULL OR v5 = ?)",
512 | |             pt,
513 | |             field_values[5]
514 | |         ))
    | |_________^
    |
    = note: this error originates in the macro `$crate::sqlx_macros::expand_query` which comes from the expansion of the macro `sqlx::query` (in Nightly builds, run with -Z macro-backtrace for more info)

error: `DATABASE_URL` must be set to use query macros
   --> /Users/vincent/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sqlx-adapter-0.4.2/src/actions.rs:516:18
    |
516 |           Box::new(sqlx::query!(
    |  __________________^
517 | |             "DELETE FROM casbin_rule WHERE
518 | |                     ptype = ? AND
519 | |                     (v4 is NULL OR v4 = ?) AND
...   |
523 | |             field_values[5]
524 | |         ))
    | |_________^
    |
    = note: this error originates in the macro `$crate::sqlx_macros::expand_query` which comes from the expansion of the macro `sqlx::query` (in Nightly builds, run with -Z macro-backtrace for more info)

error: `DATABASE_URL` must be set to use query macros
   --> /Users/vincent/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sqlx-adapter-0.4.2/src/actions.rs:526:18
    |
526 |           Box::new(sqlx::query!(
    |  __________________^
527 | |             "DELETE FROM casbin_rule WHERE
528 | |                     ptype = ? AND
529 | |                     (v3 is NULL OR v3 = ?) AND
...   |
535 | |             field_values[5]
536 | |         ))
    | |_________^
    |
    = note: this error originates in the macro `$crate::sqlx_macros::expand_query` which comes from the expansion of the macro `sqlx::query` (in Nightly builds, run with -Z macro-backtrace for more info)

error: `DATABASE_URL` must be set to use query macros
   --> /Users/vincent/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sqlx-adapter-0.4.2/src/actions.rs:538:18
    |
538 |           Box::new(sqlx::query!(
    |  __________________^
539 | |             "DELETE FROM casbin_rule WHERE
540 | |                     ptype = ? AND
541 | |                     (v2 is NULL OR v2 = ?) AND
...   |
549 | |             field_values[5]
550 | |         ))
    | |_________^
    |
    = note: this error originates in the macro `$crate::sqlx_macros::expand_query` which comes from the expansion of the macro `sqlx::query` (in Nightly builds, run with -Z macro-backtrace for more info)

error: `DATABASE_URL` must be set to use query macros
   --> /Users/vincent/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sqlx-adapter-0.4.2/src/actions.rs:552:18
    |
552 |           Box::new(sqlx::query!(
    |  __________________^
553 | |             "DELETE FROM casbin_rule WHERE
554 | |                     ptype = ? AND
555 | |                     (v1 is NULL OR v1 = ?) AND
...   |
565 | |             field_values[5]
566 | |         ))
    | |_________^
    |
    = note: this error originates in the macro `$crate::sqlx_macros::expand_query` which comes from the expansion of the macro `sqlx::query` (in Nightly builds, run with -Z macro-backtrace for more info)

error: `DATABASE_URL` must be set to use query macros
   --> /Users/vincent/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sqlx-adapter-0.4.2/src/actions.rs:568:18
    |
568 |           Box::new(sqlx::query!(
    |  __________________^
569 | |             "DELETE FROM casbin_rule WHERE
570 | |                     ptype = ? AND
571 | |                     (v0 is NULL OR v0 = ?) AND
...   |
583 | |             field_values[5]
584 | |         ))
    | |_________^
    |
    = note: this error originates in the macro `$crate::sqlx_macros::expand_query` which comes from the expansion of the macro `sqlx::query` (in Nightly builds, run with -Z macro-backtrace for more info)

error: `DATABASE_URL` must be set to use query macros
   --> /Users/vincent/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sqlx-adapter-0.4.2/src/actions.rs:616:40
    |
616 |     let casbin_rule: Vec<CasbinRule> = sqlx::query_as!(CasbinRule, "SELECT * FROM casbin_rule")
    |                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: this error originates in the macro `$crate::sqlx_macros::expand_query` which comes from the expansion of the macro `sqlx::query_as` (in Nightly builds, run with -Z macro-backtrace for more info)

error: `DATABASE_URL` must be set to use query macros
   --> /Users/vincent/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sqlx-adapter-0.4.2/src/actions.rs:677:40
    |
677 |       let casbin_rule: Vec<CasbinRule> = sqlx::query_as!(
    |  ________________________________________^
678 | |         CasbinRule,
679 | |         "SELECT * from  casbin_rule WHERE (
680 | |             ptype LIKE 'g%' AND v0 LIKE ? AND v1 LIKE ? AND v2 LIKE ? AND v3 LIKE ? AND v4 LIKE ? AND v5 LIKE ? )
...   |
685 | |             p_filter[0], p_filter[1], p_filter[2], p_filter[3], p_filter[4], p_filter[5],
686 | |     )
    | |_____^
    |
    = note: this error originates in the macro `$crate::sqlx_macros::expand_query` which comes from the expansion of the macro `sqlx::query_as` (in Nightly builds, run with -Z macro-backtrace for more info)

error: `DATABASE_URL` must be set to use query macros
   --> /Users/vincent/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sqlx-adapter-0.4.2/src/actions.rs:805:5
    |
805 |     sqlx::query!("DELETE FROM casbin_rule")
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: this error originates in the macro `$crate::sqlx_macros::expand_query` which comes from the expansion of the macro `sqlx::query` (in Nightly builds, run with -Z macro-backtrace for more info)

error: `DATABASE_URL` must be set to use query macros
   --> /Users/vincent/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sqlx-adapter-0.4.2/src/actions.rs:810:9
    |
810 | /         sqlx::query!(
811 | |             "INSERT INTO casbin_rule ( ptype, v0, v1, v2, v3, v4, v5 )
812 | |                  VALUES ( ?, ?, ?, ?, ?, ?, ? )",
813 | |             rule.ptype,
...   |
819 | |             rule.v5
820 | |         )
    | |_________^
    |
    = note: this error originates in the macro `$crate::sqlx_macros::expand_query` which comes from the expansion of the macro `sqlx::query` (in Nightly builds, run with -Z macro-backtrace for more info)

error: `DATABASE_URL` must be set to use query macros
   --> /Users/vincent/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sqlx-adapter-0.4.2/src/actions.rs:883:5
    |
883 | /     sqlx::query!(
884 | |         "INSERT INTO casbin_rule ( ptype, v0, v1, v2, v3, v4, v5 )
885 | |                  VALUES ( ?, ?, ?, ?, ?, ?, ? )",
886 | |         rule.ptype,
...   |
892 | |         rule.v5
893 | |     )
    | |_____^
    |
    = note: this error originates in the macro `$crate::sqlx_macros::expand_query` which comes from the expansion of the macro `sqlx::query` (in Nightly builds, run with -Z macro-backtrace for more info)

error: `DATABASE_URL` must be set to use query macros
    --> /Users/vincent/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sqlx-adapter-0.4.2/src/actions.rs:990:9
     |
990  | /         sqlx::query!(
991  | |             "INSERT INTO casbin_rule ( ptype, v0, v1, v2, v3, v4, v5 )
992  | |                  VALUES ( ?, ?, ?, ?, ?, ?, ? )",
993  | |             rule.ptype,
...    |
999  | |             rule.v5
1000 | |         )
     | |_________^
     |
     = note: this error originates in the macro `$crate::sqlx_macros::expand_query` which comes from the expansion of the macro `sqlx::query` (in Nightly builds, run with -Z macro-backtrace for more info)

error: `DATABASE_URL` must be set to use query macros
    --> /Users/vincent/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sqlx-adapter-0.4.2/src/actions.rs:1059:5
     |
1059 |     sqlx::query!("DELETE FROM casbin_rule")
     |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     |
     = note: this error originates in the macro `$crate::sqlx_macros::expand_query` which comes from the expansion of the macro `sqlx::query` (in Nightly builds, run with -Z macro-backtrace for more info)

error: could not compile `sqlx-adapter` (lib) due to 16 previous errors
warning: build failed, waiting for other jobs to finish...

@Cassuis Do you have the correct database environment set up? If you want to use MySQL features, you need to do the following steps:

  1. Set up database environment
docker run -itd \
    --restart always \
    -e MYSQL_ALLOW_EMPTY_PASSWORD=yes \
    -e MYSQL_USER=casbin_rs \
    -e MYSQL_PASSWORD=casbin_rs \
    -e MYSQL_DATABASE=casbin \
    -p 3306:3306 \
    -v /srv/docker/mysql:/var/lib/mysql \
    mysql:8 \
    --default-authentication-plugin=mysql_native_password;
  1. Create table casbin_rule
# MySQL
mysql -h 127.0.0.1 -u casbin_rs -pcasbin_rs casbin 

CREATE TABLE IF NOT EXISTS casbin_rule (
    id INT NOT NULL AUTO_INCREMENT,
    ptype VARCHAR(12) NOT NULL,
    v0 VARCHAR(128) NOT NULL,
    v1 VARCHAR(128) NOT NULL,
    v2 VARCHAR(128) NOT NULL,
    v3 VARCHAR(128) NOT NULL,
    v4 VARCHAR(128) NOT NULL,
    v5 VARCHAR(128) NOT NULL,
    PRIMARY KEY(id),
    CONSTRAINT unique_key_sqlx_adapter UNIQUE(ptype, v0, v1, v2, v3, v4, v5)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  1. Configure env
export DATABASE_URL=mysql://casbin_rs:casbin_rs@localhost:3306/casbin
export POOL_SIZE=8

Then you can give it another try because offline mode is disabled due to the disabled default features. You must ensure that there is an online and accessible data service and properly configure the DATABASE_URL.

@Cassuis Do you have the correct database environment set up? If you want to use MySQL features, you need to do the following steps:

  1. Set up database environment
docker run -itd \
    --restart always \
    -e MYSQL_ALLOW_EMPTY_PASSWORD=yes \
    -e MYSQL_USER=casbin_rs \
    -e MYSQL_PASSWORD=casbin_rs \
    -e MYSQL_DATABASE=casbin \
    -p 3306:3306 \
    -v /srv/docker/mysql:/var/lib/mysql \
    mysql:8 \
    --default-authentication-plugin=mysql_native_password;
  1. Create table casbin_rule
# MySQL
mysql -h 127.0.0.1 -u casbin_rs -pcasbin_rs casbin 

CREATE TABLE IF NOT EXISTS casbin_rule (
    id INT NOT NULL AUTO_INCREMENT,
    ptype VARCHAR(12) NOT NULL,
    v0 VARCHAR(128) NOT NULL,
    v1 VARCHAR(128) NOT NULL,
    v2 VARCHAR(128) NOT NULL,
    v3 VARCHAR(128) NOT NULL,
    v4 VARCHAR(128) NOT NULL,
    v5 VARCHAR(128) NOT NULL,
    PRIMARY KEY(id),
    CONSTRAINT unique_key_sqlx_adapter UNIQUE(ptype, v0, v1, v2, v3, v4, v5)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  1. Configure env
export DATABASE_URL=mysql://casbin_rs:casbin_rs@localhost:3306/casbin
export POOL_SIZE=8

Then you can give it another try because offline mode is disabled due to the disabled default features. You must ensure that there is an online and accessible data service and properly configure the DATABASE_URL.

i'm sure not environment problem.cuz my casbin working good( golang version)