diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 10763f36..6bc81323 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -28,7 +28,7 @@ jobs: - 5432:5432 env: - DB_CONNECTION_STRING: postgresql://postgres:postgres@localhost:5432/postgres + DATABASE_URL: postgresql://postgres:postgres@localhost:5432/postgres steps: - name: 🏗 Setup repository diff --git a/Cargo.lock b/Cargo.lock index c1b9bd66..6519bb12 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -24,7 +24,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011" dependencies = [ "cfg-if", - "getrandom", "once_cell", "version_check", "zerocopy", @@ -118,7 +117,7 @@ dependencies = [ "polling 2.8.0", "rustix 0.37.27", "slab", - "socket2", + "socket2 0.4.10", "waker-fn", ] @@ -253,9 +252,9 @@ dependencies = [ [[package]] name = "base64" -version = "0.21.7" +version = "0.22.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" [[package]] name = "base64ct" @@ -378,9 +377,9 @@ dependencies = [ [[package]] name = "cmake" -version = "0.1.51" +version = "0.1.52" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fb1e43aa7fd152b1f968787f7dbcdeb306d1867ff373c69955211876c053f91a" +checksum = "c682c223677e0e5b6b7f63a64b9351844c3f1b1678a68b7ee617e30fb082620e" dependencies = [ "cc", ] @@ -591,6 +590,12 @@ dependencies = [ "syn 2.0.89", ] +[[package]] +name = "dotenv" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f" + [[package]] name = "dotenvy" version = "0.15.7" @@ -916,15 +921,15 @@ dependencies = [ [[package]] name = "hashbrown" -version = "0.15.1" +version = "0.15.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3a9bfc1af68b1726ea47d3d5109de126281def866b33970e10fbab11b5dafab3" +checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289" [[package]] name = "hashlink" -version = "0.8.4" +version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e8094feaf31ff591f651a2664fb9cfd92bba7a60ce3197265e9482ebe753c8f7" +checksum = "6ba4ff7128dee98c7dc9794b6a411377e1404dba1c97deb8d1a55297bd25d8af" dependencies = [ "hashbrown 0.14.5", ] @@ -934,9 +939,12 @@ name = "heck" version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" -dependencies = [ - "unicode-segmentation", -] + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" [[package]] name = "hermit-abi" @@ -1135,7 +1143,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "707907fe3c25f5424cce2cb7e1cbcafee6bdbe735ca90ef77c29e84591e5b9da" dependencies = [ "equivalent", - "hashbrown 0.15.1", + "hashbrown 0.15.2", ] [[package]] @@ -1181,9 +1189,9 @@ dependencies = [ [[package]] name = "itoa" -version = "1.0.13" +version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "540654e97a3f4470a492cd30ff187bc95d89557a903a2bbf112e2fae98104ef2" +checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674" [[package]] name = "js-sys" @@ -1220,9 +1228,9 @@ checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" [[package]] name = "libc" -version = "0.2.164" +version = "0.2.166" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "433bfe06b8c75da9b2e3fbea6e5329ff87748f0b144ef75306e674c3f6f7c13f" +checksum = "c2ccc108bbc0b1331bd061864e7cd823c0cab660bbe6970e66e2c0614decde36" [[package]] name = "libloading" @@ -1252,9 +1260,9 @@ dependencies = [ [[package]] name = "libsqlite3-sys" -version = "0.27.0" +version = "0.30.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf4e226dcd58b4be396f7bd3c20da8fdee2911400705297ba7d2d7cc2c30f716" +checksum = "2e99fb7a497b1e3339bc746195567ed8d3e24945ecd636e3619d20b9de9e9149" dependencies = [ "cc", "pkg-config", @@ -1380,6 +1388,18 @@ dependencies = [ "adler2", ] +[[package]] +name = "mio" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80e04d1dcff3aae0704555fe5fee3bcfaf3d1fdf8a7e521d5b9d2b42acb52cec" +dependencies = [ + "hermit-abi 0.3.9", + "libc", + "wasi", + "windows-sys 0.52.0", +] + [[package]] name = "multimap" version = "0.8.3" @@ -1621,8 +1641,10 @@ version = "0.0.0" dependencies = [ "async-std", "pg_schema_cache", + "pg_test_utils", "sqlx", "text-size", + "tokio", "tree-sitter", "tree_sitter_sql", ] @@ -1814,6 +1836,16 @@ dependencies = [ "text-size", ] +[[package]] +name = "pg_test_utils" +version = "0.0.0" +dependencies = [ + "anyhow", + "dotenv", + "sqlx", + "uuid", +] + [[package]] name = "pg_type_resolver" version = "0.0.0" @@ -2019,7 +2051,7 @@ dependencies = [ "bytes", "cfg-if", "cmake", - "heck", + "heck 0.4.1", "itertools", "lazy_static", "log", @@ -2196,9 +2228,9 @@ dependencies = [ [[package]] name = "rsa" -version = "0.9.6" +version = "0.9.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5d0e5124fcb30e76a7e79bfee683a2746db83784b86289f6251b54b7950a0dfc" +checksum = "47c75d7c5c6b673e58bf54d8544a9f432e3a925b0e80f7cd3602ab5c50c55519" dependencies = [ "const-oid", "digest", @@ -2255,31 +2287,41 @@ dependencies = [ [[package]] name = "rustls" -version = "0.21.12" +version = "0.23.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f56a14d1f48b391359b22f731fd4bd7e43c97f3c50eee276f3aa09c94784d3e" +checksum = "9c9cc1d47e243d655ace55ed38201c19ae02c148ae56412ab8750e8f0166ab7f" dependencies = [ + "once_cell", "ring", + "rustls-pki-types", "rustls-webpki", - "sct", + "subtle", + "zeroize", ] [[package]] name = "rustls-pemfile" -version = "1.0.4" +version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c74cae0a4cf6ccbbf5f359f08efdf8ee7e1dc532573bf0db71968cb56b1448c" +checksum = "dce314e5fee3f39953d46bb63bb8a46d40c2f8fb7cc5a3b6cab2bde9721d6e50" dependencies = [ - "base64", + "rustls-pki-types", ] +[[package]] +name = "rustls-pki-types" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16f1201b3c9a7ee8039bcadc17b7e605e2945b27eee7631788c1bd2b0643674b" + [[package]] name = "rustls-webpki" -version = "0.101.7" +version = "0.102.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b6275d1ee7a1cd780b64aca7726599a1dbc893b1e64144529e55c3c2f745765" +checksum = "64ca1bc8749bd4cf37b5ce386cc146580777b4e8572c7b97baf22c83f444bee9" dependencies = [ "ring", + "rustls-pki-types", "untrusted", ] @@ -2295,16 +2337,6 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" -[[package]] -name = "sct" -version = "0.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da046153aa2352493d6cb7da4b6e5c0c057d8a1d0a9aa8560baffdd945acd414" -dependencies = [ - "ring", - "untrusted", -] - [[package]] name = "serde" version = "1.0.215" @@ -2357,6 +2389,18 @@ dependencies = [ "syn 2.0.89", ] +[[package]] +name = "serde_urlencoded" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd" +dependencies = [ + "form_urlencoded", + "itoa", + "ryu", + "serde", +] + [[package]] name = "sha1" version = "0.10.6" @@ -2394,6 +2438,15 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" +[[package]] +name = "signal-hook-registry" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9e9e0b4211b72e7b8b6e85c807d36c212bdb33ea8587f7569562a84df5465b1" +dependencies = [ + "libc", +] + [[package]] name = "signature" version = "2.2.0" @@ -2424,6 +2477,9 @@ name = "smallvec" version = "1.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" +dependencies = [ + "serde", +] [[package]] name = "socket2" @@ -2435,6 +2491,16 @@ dependencies = [ "winapi", ] +[[package]] +name = "socket2" +version = "0.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce305eb0b4296696835b71df73eb912e0f1ffd2556a501fcede6e0c50349191c" +dependencies = [ + "libc", + "windows-sys 0.52.0", +] + [[package]] name = "spin" version = "0.9.8" @@ -2472,9 +2538,9 @@ dependencies = [ [[package]] name = "sqlx" -version = "0.7.4" +version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c9a2ccff1a000a5a59cd33da541d9f2fdcd9e6e8229cc200565942bff36d0aaa" +checksum = "93334716a037193fac19df402f8571269c84a00852f6a7066b5d2616dcd64d3e" dependencies = [ "sqlx-core", "sqlx-macros", @@ -2485,11 +2551,10 @@ dependencies = [ [[package]] name = "sqlx-core" -version = "0.7.4" +version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "24ba59a9342a3d9bab6c56c118be528b27c9b60e490080e9711a04dccac83ef6" +checksum = "d4d8060b456358185f7d50c55d9b5066ad956956fddec42ee2e8567134a8936e" dependencies = [ - "ahash", "async-io 1.13.0", "async-std", "atoi", @@ -2498,12 +2563,13 @@ dependencies = [ "crc", "crossbeam-queue", "either", - "event-listener 2.5.3", + "event-listener 5.3.1", "futures-channel", "futures-core", "futures-intrusive", "futures-io", "futures-util", + "hashbrown 0.14.5", "hashlink", "hex", "indexmap", @@ -2527,27 +2593,27 @@ dependencies = [ [[package]] name = "sqlx-macros" -version = "0.7.4" +version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ea40e2345eb2faa9e1e5e326db8c34711317d2b5e08d0d5741619048a803127" +checksum = "cac0692bcc9de3b073e8d747391827297e075c7710ff6276d9f7a1f3d58c6657" dependencies = [ "proc-macro2", "quote", "sqlx-core", "sqlx-macros-core", - "syn 1.0.109", + "syn 2.0.89", ] [[package]] name = "sqlx-macros-core" -version = "0.7.4" +version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5833ef53aaa16d860e92123292f1f6a3d53c34ba8b1969f152ef1a7bb803f3c8" +checksum = "1804e8a7c7865599c9c79be146dc8a9fd8cc86935fa641d3ea58e5f0688abaa5" dependencies = [ "async-std", "dotenvy", "either", - "heck", + "heck 0.5.0", "hex", "once_cell", "proc-macro2", @@ -2559,16 +2625,16 @@ dependencies = [ "sqlx-mysql", "sqlx-postgres", "sqlx-sqlite", - "syn 1.0.109", + "syn 2.0.89", "tempfile", "url", ] [[package]] name = "sqlx-mysql" -version = "0.7.4" +version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1ed31390216d20e538e447a7a9b959e06ed9fc51c37b514b46eb758016ecd418" +checksum = "64bb4714269afa44aef2755150a0fc19d756fb580a67db8885608cf02f47d06a" dependencies = [ "atoi", "base64", @@ -2608,9 +2674,9 @@ dependencies = [ [[package]] name = "sqlx-postgres" -version = "0.7.4" +version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7c824eb80b894f926f89a0b9da0c7f435d27cdd35b8c655b114e58223918577e" +checksum = "6fa91a732d854c5d7726349bb4bb879bb9478993ceb764247660aee25f67c2f8" dependencies = [ "atoi", "base64", @@ -2646,9 +2712,9 @@ dependencies = [ [[package]] name = "sqlx-sqlite" -version = "0.7.4" +version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b244ef0a8414da0bed4bb1910426e890b19e5e9bccc27ada6b797d05c55ae0aa" +checksum = "d5b2cf34a45953bfd3daaf3db0f7a7878ab9b7a6b91b422d24a7a9e4c857b680" dependencies = [ "atoi", "flume", @@ -2661,10 +2727,10 @@ dependencies = [ "log", "percent-encoding", "serde", + "serde_urlencoded", "sqlx-core", "tracing", "url", - "urlencoding", ] [[package]] @@ -2832,8 +2898,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "22cfb5bee7a6a52939ca9224d6ac897bb669134078daa8735560897f69de4d33" dependencies = [ "backtrace", + "bytes", + "libc", + "mio", + "parking_lot", "pin-project-lite", + "signal-hook-registry", + "socket2 0.5.7", "tokio-macros", + "windows-sys 0.52.0", ] [[package]] @@ -2951,9 +3024,9 @@ dependencies = [ [[package]] name = "tracing-attributes" -version = "0.1.27" +version = "0.1.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" +checksum = "395ae124c09f9e6918a2310af6038fba074bcf474ac352496d5910dd59a2226d" dependencies = [ "proc-macro2", "quote", @@ -2962,9 +3035,9 @@ dependencies = [ [[package]] name = "tracing-core" -version = "0.1.32" +version = "0.1.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" +checksum = "e672c95779cf947c5311f83787af4fa8fffd12fb27e4993211a84bdfd9610f9c" dependencies = [ "once_cell", "valuable", @@ -3085,12 +3158,6 @@ dependencies = [ "serde", ] -[[package]] -name = "urlencoding" -version = "2.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "daf8dba3b7eb870caf1ddeed7bc9d2a049f3cfdfae7cb521b087cc33ae4c49da" - [[package]] name = "utf16_iter" version = "1.0.5" @@ -3103,6 +3170,15 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" +[[package]] +name = "uuid" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8c5f0a0af699448548ad1a2fbf920fb4bee257eae39953ba95cb84891a0446a" +dependencies = [ + "getrandom", +] + [[package]] name = "valuable" version = "0.1.0" @@ -3224,9 +3300,12 @@ dependencies = [ [[package]] name = "webpki-roots" -version = "0.25.4" +version = "0.26.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f20c57d8d7db6d3b86154206ae5d8fba62dd39573114de97c2cb0578251f8e1" +checksum = "5d642ff16b7e79272ae451b7322067cdc17cadf68c23264be9d94a32319efe7e" +dependencies = [ + "rustls-pki-types", +] [[package]] name = "which" diff --git a/Cargo.toml b/Cargo.toml index a3219565..f4a063ea 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,12 +9,15 @@ resolver = "2" [workspace.package] rust-version = "1.71" + + [workspace.dependencies] # supporting crates unrelated to postgres line_index = { path = "./lib/line_index", version = "0.0.0" } tree_sitter_sql = { path = "./lib/tree_sitter_sql", version = "0.0.0" } tree-sitter = "0.20.10" tracing = "0.1.40" +sqlx = { version = "0.8.2", features = [ "runtime-async-std", "tls-rustls", "postgres", "json" ] } # postgres specific crates pg_lexer = { path = "./crates/pg_lexer", version = "0.0.0" } @@ -37,6 +40,7 @@ pg_inlay_hints = { path = "./crates/pg_inlay_hints", version = "0.0.0" } pg_lint = { path = "./crates/pg_lint", version = "0.0.0" } pg_workspace = { path = "./crates/pg_workspace", version = "0.0.0" } +pg_test_utils = { path = "./crates/pg_test_utils" } # parser = { path = "./crates/parser", version = "0.0.0" } # sql_parser = { path = "./crates/sql_parser", version = "0.0.0" } # sql_parser_codegen = { path = "./crates/sql_parser_codegen", version = "0.0.0" } diff --git a/crates/pg_commands/Cargo.toml b/crates/pg_commands/Cargo.toml index 1ab7bbf8..ad01ce92 100644 --- a/crates/pg_commands/Cargo.toml +++ b/crates/pg_commands/Cargo.toml @@ -5,11 +5,9 @@ edition = "2021" [dependencies] text-size = "1.1.1" -sqlx = { version = "0.7.3", features = [ "runtime-async-std", "tls-rustls", "postgres", "json" ] } async-std = "1.12.0" anyhow = "1.0.62" - -[dev-dependencies] +sqlx.workspace = true [lib] doctest = false diff --git a/crates/pg_completions/Cargo.toml b/crates/pg_completions/Cargo.toml index 5e414a23..7ff13e50 100644 --- a/crates/pg_completions/Cargo.toml +++ b/crates/pg_completions/Cargo.toml @@ -5,15 +5,17 @@ edition = "2021" [dependencies] async-std = "1.12.0" -sqlx = { version = "0.7.3", features = [ "runtime-async-std", "tls-rustls", "postgres", "json" ] } text-size = "1.1.1" tree-sitter.workspace = true tree_sitter_sql.workspace = true pg_schema_cache.workspace = true +pg_test_utils.workspace = true -[dev-dependencies] +sqlx.workspace = true + +tokio = { version = "1.41.1", features = ["full"] } [lib] doctest = false diff --git a/crates/pg_completions/src/lib.rs b/crates/pg_completions/src/lib.rs index 6b0b19b4..382e1fc4 100644 --- a/crates/pg_completions/src/lib.rs +++ b/crates/pg_completions/src/lib.rs @@ -63,19 +63,18 @@ pub fn complete<'a>(params: &'a CompletionParams<'a>) -> CompletionResult<'a> { #[cfg(test)] mod tests { - use async_std::task::block_on; use pg_schema_cache::SchemaCache; - use sqlx::PgPool; + use pg_test_utils::test_database::*; - use crate::{complete, CompletionParams}; + use sqlx::Executor; - #[test] - fn test_complete() { - let input = "select id from c;"; + use crate::{complete, CompletionParams}; - let conn_string = std::env::var("DB_CONNECTION_STRING").unwrap(); + #[tokio::test] + async fn test_complete() { + let pool = get_new_test_db().await; - let pool = block_on(PgPool::connect(conn_string.as_str())).unwrap(); + let input = "select id from c;"; let mut parser = tree_sitter::Parser::new(); parser @@ -84,7 +83,7 @@ mod tests { let tree = parser.parse(input, None).unwrap(); - let schema_cache = block_on(SchemaCache::load(&pool)); + let schema_cache = SchemaCache::load(&pool).await; let p = CompletionParams { position: 15.into(), @@ -98,13 +97,50 @@ mod tests { assert!(result.items.len() > 0); } - #[test] - fn test_complete_two() { + #[tokio::test] + async fn test_complete_two() { + let pool = get_new_test_db().await; + let input = "select id, name, test1231234123, unknown from co;"; - let conn_string = std::env::var("DB_CONNECTION_STRING").unwrap(); + let mut parser = tree_sitter::Parser::new(); + parser + .set_language(tree_sitter_sql::language()) + .expect("Error loading sql language"); - let pool = block_on(PgPool::connect(conn_string.as_str())).unwrap(); + let tree = parser.parse(input, None).unwrap(); + let schema_cache = SchemaCache::load(&pool).await; + + let p = CompletionParams { + position: 47.into(), + schema: &schema_cache, + text: input, + tree: Some(&tree), + }; + + let result = complete(&p); + + assert!(result.items.len() > 0); + } + + #[tokio::test] + async fn test_complete_three() { + let test_db = get_new_test_db().await; + + let setup = r#" + create table users ( + id serial primary key, + name text, + password text + ); + "#; + + test_db + .execute(setup) + .await + .expect("Failed to execute setup query"); + + let input = "select * from u"; let mut parser = tree_sitter::Parser::new(); parser @@ -112,10 +148,10 @@ mod tests { .expect("Error loading sql language"); let tree = parser.parse(input, None).unwrap(); - let schema_cache = block_on(SchemaCache::load(&pool)); + let schema_cache = SchemaCache::load(&test_db).await; let p = CompletionParams { - position: 47.into(), + position: ((input.len() - 1) as u32).into(), schema: &schema_cache, text: input, tree: Some(&tree), @@ -123,6 +159,7 @@ mod tests { let result = complete(&p); + // TODO: actually assert that we get good autocompletion suggestions assert!(result.items.len() > 0); } } diff --git a/crates/pg_inlay_hints/Cargo.toml b/crates/pg_inlay_hints/Cargo.toml index dd3d4358..4f3773c6 100644 --- a/crates/pg_inlay_hints/Cargo.toml +++ b/crates/pg_inlay_hints/Cargo.toml @@ -11,7 +11,7 @@ pg_type_resolver.workspace = true pg_syntax.workspace = true tree-sitter.workspace = true tree_sitter_sql.workspace = true -sqlx = { version = "0.7.3", features = [ "runtime-async-std", "tls-rustls", "postgres", "json" ] } +sqlx.workspace = true [dev-dependencies] async-std = "1.12.0" diff --git a/crates/pg_inlay_hints/src/functions_args.rs b/crates/pg_inlay_hints/src/functions_args.rs index 84623a17..e1a45b3a 100644 --- a/crates/pg_inlay_hints/src/functions_args.rs +++ b/crates/pg_inlay_hints/src/functions_args.rs @@ -95,7 +95,7 @@ mod tests { fn test_function_args() { let input = "select lower('TEST')"; - let conn_string = std::env::var("DB_CONNECTION_STRING").unwrap(); + let conn_string = std::env::var("DATABASE_URL").unwrap(); let pool = block_on(PgPool::connect(conn_string.as_str())).unwrap(); diff --git a/crates/pg_lsp/Cargo.toml b/crates/pg_lsp/Cargo.toml index 76dde337..69560b55 100644 --- a/crates/pg_lsp/Cargo.toml +++ b/crates/pg_lsp/Cargo.toml @@ -16,13 +16,13 @@ lsp-types = "0.95.0" serde = "1.0.195" serde_json = "1.0.114" anyhow = "1.0.81" -sqlx = { version = "0.7.3", features = [ "runtime-async-std", "tls-rustls", "postgres", "json" ] } async-std = "1.12.0" threadpool = "1.8.1" dashmap = "5.5.3" text-size = "1.1.1" line_index.workspace = true +sqlx.workspace = true pg_hover.workspace = true pg_fs.workspace = true diff --git a/crates/pg_schema_cache/Cargo.toml b/crates/pg_schema_cache/Cargo.toml index 9086bb98..2cfb97e4 100644 --- a/crates/pg_schema_cache/Cargo.toml +++ b/crates/pg_schema_cache/Cargo.toml @@ -4,10 +4,11 @@ version = "0.0.0" edition = "2021" [dependencies] -sqlx = { version = "0.7.3", features = [ "runtime-async-std", "tls-rustls", "postgres", "json" ] } async-std = { version = "1.12.0" } serde = "1.0.195" serde_json = "1.0.114" +sqlx.workspace = true + [lib] doctest = false diff --git a/crates/pg_schema_cache/src/schema_cache.rs b/crates/pg_schema_cache/src/schema_cache.rs index cf17697c..f0e019cd 100644 --- a/crates/pg_schema_cache/src/schema_cache.rs +++ b/crates/pg_schema_cache/src/schema_cache.rs @@ -83,7 +83,7 @@ mod tests { #[test] fn test_schema_cache() { - let conn_string = std::env::var("DB_CONNECTION_STRING").unwrap(); + let conn_string = std::env::var("DATABASE_URL").unwrap(); let pool = async_std::task::block_on(PgPool::connect(conn_string.as_str())).unwrap(); diff --git a/crates/pg_test_utils/Cargo.toml b/crates/pg_test_utils/Cargo.toml new file mode 100644 index 00000000..a61688ee --- /dev/null +++ b/crates/pg_test_utils/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "pg_test_utils" +version = "0.0.0" +edition = "2021" + +[dependencies] +anyhow = "1.0.81" +uuid = { version = "1.11.0", features = ["v4"] } + +sqlx.workspace = true +dotenv = "0.15.0" diff --git a/crates/pg_test_utils/src/lib.rs b/crates/pg_test_utils/src/lib.rs new file mode 100644 index 00000000..4d6d3070 --- /dev/null +++ b/crates/pg_test_utils/src/lib.rs @@ -0,0 +1 @@ +pub mod test_database; diff --git a/crates/pg_test_utils/src/test_database.rs b/crates/pg_test_utils/src/test_database.rs new file mode 100644 index 00000000..a828f3fd --- /dev/null +++ b/crates/pg_test_utils/src/test_database.rs @@ -0,0 +1,44 @@ +use sqlx::{postgres::PgConnectOptions, Executor, PgPool}; +use uuid::Uuid; + +// TODO: Work with proper config objects instead of a connection_string. +// With the current implementation, we can't parse the password from the connection string. +pub async fn get_new_test_db() -> PgPool { + dotenv::dotenv() + .ok() + .expect("Unable to load .env file for tests"); + + let connection_string = std::env::var("DATABASE_URL").expect("DATABASE_URL not set"); + let password = std::env::var("DB_PASSWORD").unwrap_or("postgres".into()); + + let options_from_conn_str: PgConnectOptions = connection_string + .parse() + .expect("Invalid Connection String"); + + let host = options_from_conn_str.get_host(); + assert!( + host == "localhost" || host == "127.0.0.1", + "Running tests against non-local database!" + ); + + let options_without_db_name = PgConnectOptions::new() + .host(host) + .port(options_from_conn_str.get_port()) + .username(options_from_conn_str.get_username()) + .password(&password); + + let postgres = sqlx::PgPool::connect_with(options_without_db_name.clone()) + .await + .expect("Unable to connect to test postgres instance"); + + let database_name = Uuid::new_v4().to_string(); + + postgres + .execute(format!(r#"create database "{}";"#, database_name).as_str()) + .await + .expect("Failed to create test database."); + + sqlx::PgPool::connect_with(options_without_db_name.database(&database_name)) + .await + .expect("Could not connect to test database") +} diff --git a/crates/pg_typecheck/Cargo.toml b/crates/pg_typecheck/Cargo.toml index 89f5262e..091c1c53 100644 --- a/crates/pg_typecheck/Cargo.toml +++ b/crates/pg_typecheck/Cargo.toml @@ -8,9 +8,12 @@ pg_base_db.workspace = true pg_schema_cache.workspace = true pg_syntax.workspace = true pg_query_ext.workspace = true + +sqlx.workspace = true + text-size = "1.1.1" async-std = "1.12.0" -sqlx = { version = "0.7.3", features = [ "runtime-async-std", "tls-rustls", "postgres", "json" ] } + [dev-dependencies] diff --git a/crates/pg_typecheck/src/lib.rs b/crates/pg_typecheck/src/lib.rs index 6a53e766..07d0716c 100644 --- a/crates/pg_typecheck/src/lib.rs +++ b/crates/pg_typecheck/src/lib.rs @@ -93,7 +93,7 @@ mod tests { fn test_check_sql() { let input = "select id, unknown from contact;"; - let conn_string = std::env::var("DB_CONNECTION_STRING").unwrap(); + let conn_string = std::env::var("DATABASE_URL").unwrap(); let pool = block_on(PgPool::connect(conn_string.as_str())).unwrap(); diff --git a/crates/pg_workspace/Cargo.toml b/crates/pg_workspace/Cargo.toml index 4297ff3f..6fa96d80 100644 --- a/crates/pg_workspace/Cargo.toml +++ b/crates/pg_workspace/Cargo.toml @@ -7,7 +7,6 @@ edition = "2021" text-size = "1.1.1" dashmap = "5.5.3" async-std = "1.12.0" -sqlx = { version = "0.7.3", features = [ "runtime-async-std", "tls-rustls", "postgres", "json" ] } pg_base_db.workspace = true pg_fs.workspace = true @@ -21,6 +20,7 @@ pg_schema_cache.workspace = true tree-sitter.workspace = true tree_sitter_sql.workspace = true +sqlx.workspace = true [dev-dependencies]