Skip to content

Commit

Permalink
review code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
lubennikovaav committed Oct 7, 2024
1 parent 177a790 commit 7d79b2a
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 38 deletions.
1 change: 0 additions & 1 deletion compute_tools/src/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ use remote_storage::{DownloadError, RemotePath};

use crate::checker::create_availability_check_data;
use crate::local_proxy;
use crate::installed_extensions::log_installed_extensions;
use crate::logger::inlinify;
use crate::pg_helpers::*;
use crate::spec::*;
Expand Down
8 changes: 4 additions & 4 deletions compute_tools/src/http/openapi_spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -420,10 +420,10 @@ components:
properties:
name:
type: string
lowest_version:
type: string
highest_version:
type: string
versions:
type: array
items:
type: string
n_databases:
type: integer

Expand Down
28 changes: 4 additions & 24 deletions compute_tools/src/installed_extensions.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use compute_api::responses::{InstalledExtension, InstalledExtenstions};
use compute_api::responses::{InstalledExtension, InstalledExtensions};
use std::collections::HashMap;
use std::collections::HashSet;
use url::Url;

use anyhow::{anyhow, Result};
use anyhow::Result;
use postgres::{Client, NoTls};
use tokio::task;
use tracing::info;

/// We don't reuse get_existing_dbs() just for code clarity
/// and to make database listing query here more explicit.
Expand Down Expand Up @@ -36,7 +35,7 @@ fn list_dbs(client: &mut Client) -> Result<Vec<String>> {
/// Connect to every database (see list_dbs above) and get the list of installed extensions.
/// Same extension can be installed in multiple databases with different versions,
/// we only keep the highest and lowest version across all databases.
pub async fn get_installed_extensions(connstr: Url) -> Result<InstalledExtenstions> {
pub async fn get_installed_extensions(connstr: Url) -> Result<InstalledExtensions> {
let mut connstr = connstr.clone();

task::spawn_blocking(move || {
Expand Down Expand Up @@ -73,28 +72,9 @@ pub async fn get_installed_extensions(connstr: Url) -> Result<InstalledExtenstio
}
}

Ok(InstalledExtenstions {
Ok(InstalledExtensions {
extensions: extensions_map.values().cloned().collect(),
})
})
.await?
}

pub fn log_installed_extensions(connstr: Url) -> Result<()> {
let connstr = connstr.clone();

let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.expect("failed to create runtime");
let result = rt
.block_on(get_installed_extensions(connstr))
.map_err(|e| anyhow!("failed to get installed extensions: {:?}", e))?;

info!(
"[INSTALLED_EXTENSIONS]: {}",
serde_json::to_string(&result)
.map_err(|e| anyhow!("failed to deserialize installed extensions: {:?}", e))?
);
Ok(())
}
2 changes: 1 addition & 1 deletion libs/compute_api/src/responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,6 @@ pub struct InstalledExtension {
}

#[derive(Clone, Debug, Default, Serialize)]
pub struct InstalledExtenstions {
pub struct InstalledExtensions {
pub extensions: Vec<InstalledExtension>,
}
19 changes: 11 additions & 8 deletions test_runner/regress/test_installed_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def test_installed_extensions(neon_simple_env: NeonEnv):

env = neon_simple_env

env.neon_cli.create_branch("test_installed_extensions")
env.create_branch("test_installed_extensions")

endpoint = env.endpoints.create_start("test_installed_extensions")

Expand All @@ -33,6 +33,8 @@ def test_installed_extensions(neon_simple_env: NeonEnv):
pg_conn = endpoint.connect(dbname="test_installed_extensions")
with pg_conn.cursor() as cur:
cur.execute("CREATE EXTENSION neon_test_utils")
cur.execute("SELECT default_version FROM pg_available_extensions WHERE name = 'neon_test_utils'");
neon_test_utilsn_version = cur.fetchone()[0]

with pg_conn.cursor() as cur:
cur.execute("CREATE EXTENSION neon version '1.1'")
Expand All @@ -47,25 +49,26 @@ def test_installed_extensions(neon_simple_env: NeonEnv):
info("Extensions: %s", res["extensions"])

# check that the neon_test_utils extension is installed only in 1 database
# and has the expected version
assert any(
ext["extname"] == "neon_test_utils"
and ext["versions"] == ["1.3"]
and ext["versions"] == [neon_test_utilsn_version]
and ext["n_databases"] == 1
for ext in res["extensions"]
), "The 'neon_test_utils' extension is missing"
)

# check that the plpgsql extension is installed in all databases
# this is a default extension that is always installed
assert any(
ext["extname"] == "plpgsql" and ext["n_databases"] == 4 for ext in res["extensions"]
), "The 'plpgsql' extension is missing"
)

# check that the neon extension is installed and has expected versions
for ext in res["extensions"]:
if ext["extname"] == "neon":
assert ext["n_databases"] == 2, "The 'neon' extension is missing"
assert ext["n_databases"] == 2
ext["versions"].sort()
assert ext["versions"] == ["1.1", "1.2"], "The 'neon' extension is missing"
assert ext["versions"] == ["1.1", "1.2"]

with pg_conn.cursor() as cur:
cur.execute("ALTER EXTENSION neon UPDATE TO '1.3'")
Expand All @@ -78,6 +81,6 @@ def test_installed_extensions(neon_simple_env: NeonEnv):
# check that the neon_test_utils extension is updated
for ext in res["extensions"]:
if ext["extname"] == "neon":
assert ext["n_databases"] == 2, "The 'neon' extension is missing"
assert ext["n_databases"] == 2
ext["versions"].sort()
assert ext["versions"] == ["1.2", "1.3"], "The 'neon' extension is missing"
assert ext["versions"] == ["1.2", "1.3"]

0 comments on commit 7d79b2a

Please sign in to comment.