Skip to content

Commit 2d7d56f

Browse files
committed
Merge remote-tracking branch 'origin/main' into fix/postgres-left-join-rewrite-nullability
2 parents 4add1ed + 3da582a commit 2d7d56f

9 files changed

Lines changed: 85 additions & 7 deletions

File tree

.github/workflows/sqlx.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,22 @@ jobs:
301301
SQLX_OFFLINE_DIR: .sqlx
302302
RUSTFLAGS: -D warnings --cfg postgres="${{ matrix.postgres }}"
303303
304+
# Run tests again with implied linux user
305+
- run: |
306+
SKIP_ARGS=()
307+
for test in $PG_ISOLATED_TESTS; do
308+
SKIP_ARGS+=(--skip "$test")
309+
done
310+
cargo test \
311+
--no-default-features \
312+
--features any,postgres,macros,migrate,_unstable-all-types,runtime-${{ matrix.runtime }},tls-${{ matrix.tls }} \
313+
-- \
314+
"${SKIP_ARGS[@]}"
315+
env:
316+
DATABASE_URL: postgres:///sqlx?password=runner-password
317+
SQLX_OFFLINE_DIR: .sqlx
318+
RUSTFLAGS: -D warnings --cfg postgres="${{ matrix.postgres }}"
319+
304320
# Run the `test-attr` test again to cover cleanup.
305321
- run: >
306322
cargo test

sqlx-core/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ percent-encoding = "2.3.0"
9191
serde = { version = "1.0.219", features = ["derive", "rc"], optional = true }
9292
serde_json = { version = "1.0.142", features = ["raw_value"], optional = true }
9393
toml = { version = "0.8.16", optional = true }
94-
sha2 = { version = "0.10.0", default-features = false, optional = true }
94+
sha2 = { workspace = true, optional = true }
9595
#sqlformat = "0.2.0"
9696
tokio-stream = { version = "0.1.8", features = ["fs"], optional = true }
9797
tracing = { version = "0.1.37", features = ["log"] }

sqlx-macros-core/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ either = "1.6.1"
7575
proc-macro2 = { version = "1.0.83", default-features = false }
7676
serde = { version = "1.0.219", features = ["derive"] }
7777
serde_json = { version = "1.0.142" }
78-
sha2 = { version = "0.10.0" }
78+
sha2 = { workspace = true }
7979
syn = { version = "2.0.87", default-features = false, features = ["full", "derive", "parsing", "printing", "clone-impls"] }
8080
quote = { version = "1.0.35", default-features = false }
8181
url = { version = "2.2.2" }

sqlx-macros-core/src/query/metadata.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ pub struct MacrosEnv {
2222

2323
impl Metadata {
2424
pub fn env(&self) -> crate::Result<Arc<MacrosEnv>> {
25-
self.env
26-
.get_or_try_init(|builder| load_env(&self.manifest_dir, &self.config, builder))
25+
let workspace_root = self.workspace_root();
26+
27+
self.env.get_or_try_init(|builder| {
28+
load_env(&self.manifest_dir, &workspace_root, &self.config, builder)
29+
})
2730
}
2831

2932
pub fn workspace_root(&self) -> PathBuf {
@@ -91,6 +94,7 @@ pub fn try_for_crate() -> crate::Result<Arc<Metadata>> {
9194

9295
fn load_env(
9396
manifest_dir: &Path,
97+
workspace_root: &Path,
9498
config: &Config,
9599
builder: &mut MtimeCacheBuilder,
96100
) -> crate::Result<Arc<MacrosEnv>> {
@@ -108,7 +112,22 @@ fn load_env(
108112
offline: None,
109113
};
110114

111-
for dir in manifest_dir.ancestors() {
115+
// https://github.com/launchbadge/sqlx/issues/4276
116+
let dirs = if manifest_dir.starts_with(workspace_root) {
117+
// Often just `[manifest_dir, workspace_dir]` but project structures can absolutely
118+
// be more complicated
119+
manifest_dir
120+
.ancestors()
121+
.take_while(|dir| dir.starts_with(workspace_root))
122+
.collect::<Vec<_>>()
123+
} else {
124+
// Thinking of edge cases, there's the possibility that the package directory
125+
// isn't actually a child of the workspace directory. There isn't really any other sane
126+
// thing to do here; we shouldn't traverse into unrelated paths.
127+
[manifest_dir, workspace_root].to_vec()
128+
};
129+
130+
for dir in dirs {
112131
let path = dir.join(".env");
113132

114133
let dotenv = match dotenvy::from_path_iter(&path) {

sqlx-postgres/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ num-bigint = { version = "0.4.3", optional = true }
6464
smallvec = { version = "1.13.1" }
6565
stringprep = "0.1.2"
6666
tracing = { version = "0.1.37", features = ["log"] }
67-
whoami = { version = "2.0.2", default-features = false }
67+
whoami = { version = "2.0.2", features = ["std"], default-features = false }
6868

6969
dotenvy.workspace = true
7070
thiserror.workspace = true

sqlx-sqlite/src/connection/execute.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,15 @@ impl Iterator for ExecuteIter<'_> {
108108
Ok(false) => {
109109
let last_insert_rowid = self.handle.last_insert_rowid();
110110

111-
let changes = statement.handle.changes();
111+
// `sqlite3_changes()` returns the row count for the most recently completed
112+
// INSERT/UPDATE/DELETE on the connection, not necessarily this statement.
113+
// For read-only statements (SELECT, BEGIN, COMMIT, etc.) we must report 0.
114+
// See https://sqlite.org/c3ref/changes.html
115+
let changes = if statement.handle.read_only() {
116+
0
117+
} else {
118+
statement.handle.changes()
119+
};
112120
self.logger.increase_rows_affected(changes);
113121

114122
let done = SqliteQueryResult {

tests/postgres/setup.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
-- Create extra user to be used on runner
2+
CREATE USER runner WITH SUPERUSER PASSWORD 'runner-password';
3+
14
-- https://www.postgresql.org/docs/current/ltree.html
25
CREATE EXTENSION IF NOT EXISTS ltree;
36

tests/sqlite/migrations_issue_4300/000_init.sql

Whitespace-only changes.

tests/sqlite/sqlite.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,3 +1437,35 @@ async fn issue_3982() -> anyhow::Result<()> {
14371437

14381438
Ok(())
14391439
}
1440+
1441+
#[sqlx_macros::test]
1442+
async fn issue_4300() -> anyhow::Result<()> {
1443+
use sqlx::migrate::Migrator;
1444+
use sqlx::sqlite::SqlitePoolOptions;
1445+
use std::path::Path;
1446+
1447+
let pool = SqlitePoolOptions::new().connect("sqlite::memory:").await?;
1448+
1449+
let migrator = Migrator::new(Path::new("tests/sqlite/migrations_issue_4300")).await?;
1450+
migrator.run(&pool).await?;
1451+
1452+
sqlx::query("CREATE TABLE my_table ( qqq TEXT )")
1453+
.execute(&pool)
1454+
.await?;
1455+
1456+
sqlx::query("CREATE TABLE other_table ( www TEXT )")
1457+
.execute(&pool)
1458+
.await?;
1459+
1460+
sqlx::query("INSERT INTO my_table (qqq) VALUES ('temporary')")
1461+
.execute(&pool)
1462+
.await?;
1463+
1464+
let result = sqlx::query("BEGIN TRANSACTION; COMMIT;")
1465+
.execute(&pool)
1466+
.await?;
1467+
1468+
assert_eq!(result.rows_affected(), 0);
1469+
1470+
Ok(())
1471+
}

0 commit comments

Comments
 (0)