-
-
Notifications
You must be signed in to change notification settings - Fork 94
Add SyncConnectionWrapper type #146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fec4622
Add missing dummy instrumentation for AsyncWrapper
momobel e77ef84
Depend on tokio/net with async-connection-wrapper
momobel c721091
Introduce SyncConnectionWrapper with async API around diesel::Connection
momobel 58bcc91
Improve SyncConnectionWrapper documentation and tests
momobel 1039eab
Replace sql_function with define_sql_function
momobel bd40d8d
Update the CI
weiznich 1b9a4fd
Make the `SyncConnectionWrapper` poolable
weiznich 2eb75b2
Another round of CI config fixes
weiznich d5a1d4f
Use diesel master branch as dependency
weiznich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,19 +21,16 @@ jobs: | |
fail-fast: false | ||
matrix: | ||
rust: ["stable", "beta", "nightly"] | ||
backend: ["postgres", "mysql"] | ||
os: [ubuntu-latest, macos-latest, windows-latest] | ||
backend: ["postgres", "mysql", "sqlite"] | ||
os: [ubuntu-latest, macos-latest, macos-14, windows-2019] | ||
runs-on: ${{ matrix.os }} | ||
steps: | ||
- name: Checkout sources | ||
uses: actions/checkout@v2 | ||
uses: actions/checkout@v4 | ||
|
||
- name: Cache cargo registry | ||
uses: actions/cache@v2 | ||
uses: Swatinem/rust-cache@v2 | ||
with: | ||
path: | | ||
~/.cargo/registry | ||
~/.cargo/git | ||
key: ${{ runner.os }}-${{ matrix.backend }}-cargo-${{ hashFiles('**/Cargo.toml') }} | ||
|
||
- name: Set environment variables | ||
|
@@ -66,24 +63,86 @@ jobs: | |
mysql -e "create database diesel_test; create database diesel_unit_test; grant all on \`diesel_%\`.* to 'root'@'localhost';" -uroot -proot | ||
echo "DATABASE_URL=mysql://root:root@localhost/diesel_test" >> $GITHUB_ENV | ||
|
||
- name: Install sqlite (Linux) | ||
if: runner.os == 'Linux' && matrix.backend == 'sqlite' | ||
run: | | ||
curl -fsS --retry 3 -o sqlite-autoconf-3400100.tar.gz https://www.sqlite.org/2022/sqlite-autoconf-3400100.tar.gz | ||
tar zxf sqlite-autoconf-3400100.tar.gz | ||
cd sqlite-autoconf-3400100 | ||
CFLAGS="$CFLAGS -O2 -fno-strict-aliasing \ | ||
-DSQLITE_DEFAULT_FOREIGN_KEYS=1 \ | ||
-DSQLITE_SECURE_DELETE \ | ||
-DSQLITE_ENABLE_COLUMN_METADATA \ | ||
-DSQLITE_ENABLE_FTS3_PARENTHESIS \ | ||
-DSQLITE_ENABLE_RTREE=1 \ | ||
-DSQLITE_SOUNDEX=1 \ | ||
-DSQLITE_ENABLE_UNLOCK_NOTIFY \ | ||
-DSQLITE_OMIT_LOOKASIDE=1 \ | ||
-DSQLITE_ENABLE_DBSTAT_VTAB \ | ||
-DSQLITE_ENABLE_UPDATE_DELETE_LIMIT=1 \ | ||
-DSQLITE_ENABLE_LOAD_EXTENSION \ | ||
-DSQLITE_ENABLE_JSON1 \ | ||
-DSQLITE_LIKE_DOESNT_MATCH_BLOBS \ | ||
-DSQLITE_THREADSAFE=1 \ | ||
-DSQLITE_ENABLE_FTS3_TOKENIZER=1 \ | ||
-DSQLITE_MAX_SCHEMA_RETRY=25 \ | ||
-DSQLITE_ENABLE_PREUPDATE_HOOK \ | ||
-DSQLITE_ENABLE_SESSION \ | ||
-DSQLITE_ENABLE_STMTVTAB \ | ||
-DSQLITE_MAX_VARIABLE_NUMBER=250000" \ | ||
./configure --prefix=/usr \ | ||
--enable-threadsafe \ | ||
--enable-dynamic-extensions \ | ||
--libdir=/usr/lib/x86_64-linux-gnu \ | ||
--libexecdir=/usr/lib/x86_64-linux-gnu/sqlite3 | ||
sudo make | ||
sudo make install | ||
echo "DATABASE_URL=/tmp/test.db" >> $GITHUB_ENV | ||
|
||
- name: Install postgres (MacOS) | ||
if: runner.os == 'macOS' && matrix.backend == 'postgres' | ||
if: matrix.os == 'macos-latest' && matrix.backend == 'postgres' | ||
run: | | ||
initdb -D /usr/local/var/postgres | ||
pg_ctl -D /usr/local/var/postgres start | ||
sleep 3 | ||
createuser -s postgres | ||
echo "DATABASE_URL=postgres://postgres@localhost/" >> $GITHUB_ENV | ||
|
||
- name: Install postgres (MacOS M1) | ||
if: matrix.os == 'macos-14' && matrix.backend == 'postgres' | ||
run: | | ||
brew install postgresql | ||
brew services start postgresql@14 | ||
sleep 3 | ||
createuser -s postgres | ||
echo "DATABASE_URL=postgres://postgres@localhost/" >> $GITHUB_ENV | ||
- name: Install sqlite (MacOS) | ||
if: runner.os == 'macOS' && matrix.backend == 'sqlite' | ||
run: | | ||
brew install sqlite | ||
echo "DATABASE_URL=/tmp/test.db" >> $GITHUB_ENV | ||
|
||
- name: Install mysql (MacOS) | ||
if: runner.os == 'macOS' && matrix.backend == 'mysql' | ||
if: matrix.os == 'macos-latest' && matrix.backend == 'mysql' | ||
run: | | ||
brew install [email protected] | ||
/usr/local/opt/[email protected]/bin/mysql_install_db | ||
/usr/local/opt/[email protected]/bin/mysql.server start | ||
sleep 3 | ||
/usr/local/opt/[email protected]/bin/mysqladmin -u runner password diesel | ||
/usr/local/opt/[email protected]/bin/mysql -e "create database diesel_test; create database diesel_unit_test; grant all on \`diesel_%\`.* to 'runner'@'localhost';" -urunner | ||
echo "DATABASE_URL=mysql://runner:diesel@localhost/diesel_test" >> $GITHUB_ENV | ||
|
||
- name: Install mysql (MacOS M1) | ||
if: matrix.os == 'macos-14' && matrix.backend == 'mysql' | ||
run: | | ||
brew install --overwrite [email protected] | ||
/usr/local/opt/[email protected]/bin/mysql_install_db | ||
/usr/local/opt/[email protected]/bin/mysql.server start | ||
brew install [email protected] | ||
ls /opt/homebrew/opt/[email protected] | ||
/opt/homebrew/opt/[email protected]/bin/mysql_install_db | ||
/opt/homebrew/opt/[email protected]/bin/mysql.server start | ||
sleep 3 | ||
/usr/local/opt/mariadb@10.8/bin/mysql -e "ALTER USER 'runner'@'localhost' IDENTIFIED BY 'diesel';" -urunner | ||
/usr/local/opt/mariadb@10.8/bin/mysql -e "create database diesel_test; create database diesel_unit_test; grant all on \`diesel_%\`.* to 'runner'@'localhost';" -urunner -pdiesel | ||
/opt/homebrew/opt/mariadb@11.3/bin/mysqladmin -u runner password diesel | ||
/opt/homebrew/opt/mariadb@11.3/bin/mysql -e "create database diesel_test; create database diesel_unit_test; grant all on \`diesel_%\`.* to 'runner'@'localhost';" -urunner | ||
echo "DATABASE_URL=mysql://runner:diesel@localhost/diesel_test" >> $GITHUB_ENV | ||
|
||
- name: Install postgres (Windows) | ||
|
@@ -106,6 +165,22 @@ jobs: | |
run: | | ||
echo "DATABASE_URL=mysql://root@localhost/diesel_test" >> $GITHUB_ENV | ||
|
||
- name: Install sqlite (Windows) | ||
if: runner.os == 'Windows' && matrix.backend == 'sqlite' | ||
shell: cmd | ||
run: | | ||
choco install sqlite | ||
cd /D C:\ProgramData\chocolatey\lib\SQLite\tools | ||
call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvars64.bat" | ||
lib /machine:x64 /def:sqlite3.def /out:sqlite3.lib | ||
- name: Set variables for sqlite (Windows) | ||
if: runner.os == 'Windows' && matrix.backend == 'sqlite' | ||
shell: bash | ||
run: | | ||
echo "C:\ProgramData\chocolatey\lib\SQLite\tools" >> $GITHUB_PATH | ||
echo "SQLITE3_LIB_DIR=C:\ProgramData\chocolatey\lib\SQLite\tools" >> $GITHUB_ENV | ||
echo "DATABASE_URL=C:\test.db" >> $GITHUB_ENV | ||
|
||
- name: Install rust toolchain | ||
uses: dtolnay/rust-toolchain@master | ||
with: | ||
|
@@ -115,26 +190,29 @@ jobs: | |
|
||
- name: Test diesel_async | ||
run: cargo +${{ matrix.rust }} test --manifest-path Cargo.toml --no-default-features --features "${{ matrix.backend }} deadpool bb8 mobc" | ||
- name: Run examples | ||
|
||
- name: Run examples (Postgres) | ||
if: matrix.backend == 'postgres' | ||
run: | | ||
cargo +${{ matrix.rust }} check --manifest-path examples/postgres/pooled-with-rustls/Cargo.toml | ||
cargo +${{ matrix.rust }} check --manifest-path examples/postgres/run-pending-migrations-with-rustls/Cargo.toml | ||
|
||
- name: Run examples (Sqlite) | ||
if: matrix.backend == 'sqlite' | ||
run: | | ||
cargo +${{ matrix.rust }} check --manifest-path examples/sync-wrapper/Cargo.toml | ||
|
||
rustfmt_and_clippy: | ||
name: Check rustfmt style && run clippy | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: actions/checkout@v4 | ||
- uses: dtolnay/rust-toolchain@stable | ||
with: | ||
components: clippy, rustfmt | ||
- name: Cache cargo registry | ||
uses: actions/cache@v2 | ||
uses: Swatinem/rust-cache@v2 | ||
with: | ||
path: | | ||
~/.cargo/registry | ||
~/.cargo/git | ||
key: clippy-cargo-${{ hashFiles('**/Cargo.toml') }} | ||
|
||
- name: Remove potential newer clippy.toml from dependencies | ||
|
@@ -152,12 +230,14 @@ jobs: | |
name: Check Minimal supported rust version (1.65.0) | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/checkout@v4 | ||
- uses: dtolnay/[email protected] | ||
- uses: dtolnay/rust-toolchain@nightly | ||
- uses: taiki-e/install-action@cargo-hack | ||
- uses: taiki-e/install-action@cargo-minimal-versions | ||
- name: Check diesel-async | ||
# cannot test mysql yet as that crate | ||
# has broken min-version dependencies | ||
# cannot test sqlite yet as that crate | ||
# as broken min-version dependencies as well | ||
run: cargo +stable minimal-versions check -p diesel-async --features "postgres bb8 deadpool mobc" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[package] | ||
name = "sync-wrapper" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
diesel = { version = "2.1.0", default-features = false } | ||
diesel-async = { version = "0.4.0", path = "../../", features = ["sync-connection-wrapper", "async-connection-wrapper"] } | ||
diesel_migrations = "2.1.0" | ||
futures-util = "0.3.21" | ||
tokio = { version = "1.2.0", default-features = false, features = ["macros", "rt-multi-thread"] } | ||
|
||
[features] | ||
default = ["sqlite"] | ||
sqlite = ["diesel-async/sqlite"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# For documentation on how to configure this file, | ||
# see https://diesel.rs/guides/configuring-diesel-cli | ||
|
||
[print_schema] | ||
file = "src/schema.rs" | ||
custom_type_derives = ["diesel::query_builder::QueryId"] | ||
|
||
[migrations_directory] | ||
dir = "migrations" |
Empty file.
1 change: 1 addition & 0 deletions
1
examples/sync-wrapper/migrations/00000000000000_diesel_initial_setup/down.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TABLE IF EXISTS users; |
3 changes: 3 additions & 0 deletions
3
examples/sync-wrapper/migrations/00000000000000_diesel_initial_setup/up.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT); | ||
|
||
INSERT INTO users(id, name) VALUES(123, 'hello world'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
use diesel::prelude::*; | ||
use diesel::sqlite::{Sqlite, SqliteConnection}; | ||
use diesel_async::async_connection_wrapper::AsyncConnectionWrapper; | ||
use diesel_async::sync_connection_wrapper::SyncConnectionWrapper; | ||
use diesel_async::{AsyncConnection, RunQueryDsl, SimpleAsyncConnection}; | ||
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness}; | ||
|
||
// ordinary diesel model setup | ||
|
||
table! { | ||
users { | ||
id -> Integer, | ||
name -> Text, | ||
} | ||
} | ||
|
||
#[derive(Debug, Queryable, Selectable)] | ||
#[diesel(table_name = users)] | ||
struct User { | ||
id: i32, | ||
name: String, | ||
} | ||
|
||
const MIGRATIONS: EmbeddedMigrations = embed_migrations!(); | ||
|
||
type InnerConnection = SqliteConnection; | ||
|
||
type InnerDB = Sqlite; | ||
|
||
async fn establish(db_url: &str) -> ConnectionResult<SyncConnectionWrapper<InnerConnection>> { | ||
// It is necessary to specify the specific inner connection type because of inference issues | ||
SyncConnectionWrapper::<SqliteConnection>::establish(db_url).await | ||
weiznich marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
async fn run_migrations<A>(async_connection: A) -> Result<(), Box<dyn std::error::Error>> | ||
where | ||
A: AsyncConnection<Backend = InnerDB> + 'static, | ||
{ | ||
let mut async_wrapper: AsyncConnectionWrapper<A> = | ||
AsyncConnectionWrapper::from(async_connection); | ||
|
||
tokio::task::spawn_blocking(move || { | ||
async_wrapper.run_pending_migrations(MIGRATIONS).unwrap(); | ||
}) | ||
.await | ||
.map_err(|e| Box::new(e) as Box<dyn std::error::Error>) | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
let db_url = std::env::var("DATABASE_URL").expect("Env var `DATABASE_URL` not set"); | ||
|
||
// create an async connection for the migrations | ||
let sync_wrapper: SyncConnectionWrapper<InnerConnection> = establish(&db_url).await?; | ||
run_migrations(sync_wrapper).await?; | ||
|
||
let mut sync_wrapper: SyncConnectionWrapper<InnerConnection> = establish(&db_url).await?; | ||
|
||
sync_wrapper.batch_execute("DELETE FROM users").await?; | ||
|
||
sync_wrapper | ||
.batch_execute("INSERT INTO users(id, name) VALUES (3, 'toto')") | ||
.await?; | ||
|
||
let data: Vec<User> = users::table | ||
.select(User::as_select()) | ||
.load(&mut sync_wrapper) | ||
.await?; | ||
println!("{data:?}"); | ||
|
||
diesel::delete(users::table) | ||
.execute(&mut sync_wrapper) | ||
.await?; | ||
|
||
diesel::insert_into(users::table) | ||
.values((users::id.eq(1), users::name.eq("iLuke"))) | ||
.execute(&mut sync_wrapper) | ||
.await?; | ||
|
||
let data: Vec<User> = users::table | ||
.filter(users::id.gt(0)) | ||
.or_filter(users::name.like("%Luke")) | ||
.select(User::as_select()) | ||
.load(&mut sync_wrapper) | ||
.await?; | ||
println!("{data:?}"); | ||
|
||
Ok(()) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.