Skip to content

Commit b929c19

Browse files
committed
fix: correct vector example error
1 parent 4efd2db commit b929c19

File tree

5 files changed

+100
-12
lines changed

5 files changed

+100
-12
lines changed

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ hex = "0.4.3"
3333
home = "0.5.9"
3434
http = "1.1.0"
3535
human_bytes = { version = "0.4.3", default-features = false }
36+
indoc = "2.0.5"
3637
md-5 = "0.10.6"
3738
num-format = "0.4.4"
3839
quick-xml = "0.36.1"
@@ -56,6 +57,7 @@ tempfile = "3.10.1"
5657
thiserror = "1.0.63"
5758
tokio = "1.39.2"
5859
tracing = "0.1.40"
60+
tracing-subscriber = "0.3.18"
5961
url = "2.5.2"
6062
xz2 = "0.1.7"
6163
zip = "2.1.6"

examples/vector_extension/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ version.workspace = true
77

88
[dependencies]
99
anyhow = { workspace = true }
10+
indoc = { workspace = true }
1011
postgresql_extensions = { path = "../../postgresql_extensions" }
1112
postgresql_embedded = { path = "../../postgresql_embedded" }
1213
sqlx = { workspace = true, features = ["runtime-tokio"] }
14+
tracing = { workspace = true }
15+
tracing-subscriber = { workspace = true }
1316
tokio = { workspace = true, features = ["full"] }

examples/vector_extension/src/main.rs

Lines changed: 83 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,118 @@
22
#![deny(clippy::pedantic)]
33

44
use anyhow::Result;
5+
use indoc::indoc;
56
use postgresql_embedded::{PostgreSQL, Settings, VersionReq};
67
use sqlx::PgPool;
8+
use tracing::info;
79

10+
/// Example of how to install and configure the vector extension.
11+
///
12+
/// See: <https://github.com/tensorchord/pgvecto.rs/?tab=readme-ov-file#quick-start>
813
#[tokio::main]
914
async fn main() -> Result<()> {
15+
tracing_subscriber::fmt().compact().init();
16+
17+
info!("Installing PostgreSQL");
1018
let settings = Settings {
1119
version: VersionReq::parse("=16.3.0")?,
1220
..Default::default()
1321
};
22+
let mut postgresql = PostgreSQL::new(settings);
23+
postgresql.setup().await?;
1424

15-
println!("Installing the vector extension from TensorChord");
25+
info!("Installing the vector extension from TensorChord");
1626
postgresql_extensions::install(
17-
&settings,
27+
postgresql.settings(),
1828
"tensor-chord",
1929
"pgvecto.rs",
2030
&VersionReq::parse("=0.3.0")?,
2131
)
2232
.await?;
2333

24-
println!("Installing PostgreSQL");
25-
let mut postgresql = PostgreSQL::new(settings);
26-
postgresql.setup().await?;
34+
info!("Starting PostgreSQL");
2735
postgresql.start().await?;
2836

2937
let database_name = "vector-example";
30-
println!("Creating database {database_name}");
38+
info!("Creating database {database_name}");
3139
postgresql.create_database(database_name).await?;
3240

33-
println!("Connecting to database {database_name}");
41+
info!("Configuring extension");
3442
let settings = postgresql.settings();
3543
let database_url = settings.url(database_name);
3644
let pool = PgPool::connect(database_url.as_str()).await?;
45+
configure_extension(&pool).await?;
46+
pool.close().await;
47+
48+
info!("Restarting database");
49+
postgresql.stop().await?;
50+
postgresql.start().await?;
51+
52+
info!("Enabling extension");
53+
let pool = PgPool::connect(database_url.as_str()).await?;
54+
enable_extension(&pool).await?;
55+
56+
info!("Creating table");
57+
create_table(&pool).await?;
58+
59+
info!("Creating data");
60+
create_data(&pool).await?;
61+
62+
info!("Stopping database");
63+
postgresql.stop().await?;
64+
Ok(())
65+
}
3766

38-
println!("Configuring the vector extension");
67+
async fn configure_extension(pool: &PgPool) -> Result<()> {
68+
sqlx::query("ALTER SYSTEM SET shared_preload_libraries = \"vectors.so\"")
69+
.execute(pool)
70+
.await?;
71+
sqlx::query("ALTER SYSTEM SET search_path = \"$user\", public, vectors")
72+
.execute(pool)
73+
.await?;
74+
Ok(())
75+
}
76+
77+
async fn enable_extension(pool: &PgPool) -> Result<()> {
78+
sqlx::query("DROP EXTENSION IF EXISTS vectors")
79+
.execute(pool)
80+
.await?;
3981
sqlx::query("CREATE EXTENSION vectors")
40-
.execute(&pool)
82+
.execute(pool)
4183
.await?;
84+
Ok(())
85+
}
4286

43-
println!("Stopping database");
44-
postgresql.stop().await?;
87+
async fn create_table(pool: &PgPool) -> Result<()> {
88+
sqlx::query(indoc! {"
89+
CREATE TABLE IF NOT EXISTS items (
90+
id bigserial PRIMARY KEY,
91+
embedding vector(3) NOT NULL
92+
)
93+
"})
94+
.execute(pool)
95+
.await?;
96+
Ok(())
97+
}
98+
99+
async fn create_data(pool: &PgPool) -> Result<()> {
100+
sqlx::query(indoc! {"
101+
INSERT INTO items (embedding)
102+
VALUES
103+
('[1,2,3]'),
104+
('[4,5,6]')
105+
"})
106+
.execute(pool)
107+
.await?;
108+
sqlx::query(indoc! {"
109+
INSERT INTO items (embedding)
110+
VALUES
111+
(ARRAY[1, 2, 3]::real[]),
112+
(ARRAY[4, 5, 6]::real[]
113+
)
114+
"})
115+
.execute(pool)
116+
.await?;
45117
Ok(())
46118
}
47119

postgresql_extensions/tests/steampipe.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#[cfg(all(target_os = "linux", feature = "steampipe"))]
1+
#[cfg(any(target_os = "linux", target_os = "macos"))]
2+
#[cfg(feature = "steampipe")]
23
#[tokio::test]
34
async fn test_lifecycle() -> anyhow::Result<()> {
45
let installation_dir = tempfile::tempdir()?.path().to_path_buf();

0 commit comments

Comments
 (0)