Skip to content

Commit

Permalink
registry(v1): save Configuration and LoadableLibraries on publish
Browse files Browse the repository at this point in the history
  • Loading branch information
vrmiguel committed Dec 7, 2023
1 parent 9eaa819 commit b7d4211
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 43 deletions.
15 changes: 15 additions & 0 deletions registry/sqlx-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -1480,6 +1480,21 @@
},
"query": "\n UPDATE categories\n SET extension_count = extension_count + 1\n WHERE id = $1\n "
},
"f5c4444586051427aaf351b423d8dba82b213eef6e707a1cf5b54854769ce1b9": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Left": [
"Int4",
"Text",
"Bool",
"Int4"
]
}
},
"query": "INSERT INTO v1.extensions_loadable_libraries (extension_version_id, library_name, requires_restart, priority)\n VALUES ($1, $2, $3, $4)"
},
"f77d6c607d830e7ea1d564c34ec42b998e0c2657f4cdb8a89dd211a29b4a7d65": {
"describe": {
"columns": [],
Expand Down
9 changes: 4 additions & 5 deletions registry/src/openapi.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use utoipa::openapi::License;
use utoipa::OpenApi;

use crate::v1::repository::{
ExtensionConfigurationView, ExtensionPreloadLibrariesView, ExtensionView, TrunkProjectView,
};
use crate::v1::repository::{ExtensionView, TrunkProjectView};
use crate::v1::routes as v1;
use crate::views::extension_publish::{ExtensionConfiguration, LoadableLibrary};

// v1 API documentation
#[derive(OpenApi)]
Expand All @@ -17,8 +16,8 @@ use crate::v1::routes as v1;
),
components(schemas(
ExtensionView,
ExtensionPreloadLibrariesView,
ExtensionConfigurationView,
LoadableLibrary,
ExtensionConfiguration,
TrunkProjectView,
))
)]
Expand Down
7 changes: 5 additions & 2 deletions registry/src/routes/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,11 @@ async fn insert_into_v1(
registry: &Registry,
gzipped_archive: &[u8],
) -> anyhow::Result<()> {
let extension_views =
crate::v1::extractor::extract_extension_view(gzipped_archive, &new_extension.name)?;
let extension_views = crate::v1::extractor::extract_extension_view(
gzipped_archive,
&new_extension.name,
&new_extension,
)?;

let trunk_project = TrunkProjectView {
name: new_extension.name,
Expand Down
8 changes: 6 additions & 2 deletions registry/src/v1/extractor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use std::{
};
use tar::EntryType;

use crate::views::extension_publish::ExtensionUpload;

use super::repository::ExtensionView;

pub struct ControlFile {
Expand All @@ -17,6 +19,7 @@ pub struct ControlFile {
pub fn extract_extension_view(
tar_gz: &[u8],
trunk_project_name: &str,
new_extensions: &ExtensionUpload,
) -> anyhow::Result<Vec<ExtensionView>> {
let control_files = extract_control_files(tar_gz)?;

Expand All @@ -27,8 +30,9 @@ pub fn extract_extension_view(
version: control_file.default_version.unwrap_or_default(),
trunk_project_name: trunk_project_name.to_string(),
dependencies_extension_names: control_file.dependencies,
loadable_libraries: None,
configurations: None,
// TODO: should we clone this for every extension in a Trunk project?
loadable_libraries: new_extensions.libraries.clone(),
configurations: new_extensions.configurations.clone(),
})
.collect();

Expand Down
72 changes: 40 additions & 32 deletions registry/src/v1/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use utoipa::{ToResponse, ToSchema};

use crate::errors::Result;
use crate::repository::Registry;
use crate::views::extension_publish::{ExtensionConfiguration, LoadableLibrary};

#[derive(Debug, Clone, Serialize, Deserialize, ToSchema, ToResponse)]
pub struct TrunkProjectView {
Expand All @@ -14,28 +15,14 @@ pub struct TrunkProjectView {
pub extensions: Vec<ExtensionView>,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize, ToSchema)]
pub struct ExtensionConfigurationView {
pub name: String,
pub is_required: bool,
pub recommended_default: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct ExtensionPreloadLibrariesView {
library_name: String,
requires_restart: bool,
priority: i32,
}

#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct ExtensionView {
pub extension_name: String,
pub version: String,
pub trunk_project_name: String,
pub dependencies_extension_names: Option<Vec<String>>,
pub loadable_libraries: Option<Vec<ExtensionPreloadLibrariesView>>,
pub configurations: Option<Vec<ExtensionConfigurationView>>,
pub loadable_libraries: Option<Vec<LoadableLibrary>>,
pub configurations: Option<Vec<ExtensionConfiguration>>,
}

impl Registry {
Expand Down Expand Up @@ -360,29 +347,50 @@ impl Registry {
}

// 5. insert extension configurations
for config in configurations {
sqlx::query!(
self.insert_configurations(extension_version_id, configurations)
.await?;

// 6. insert shared preload libraries
self.insert_loadable_libraries(extension_version_id, loadable_libraries)
.await?;
}
Ok(())
}

async fn insert_configurations(
&self,
extension_version_id: i32,
configurations: impl Iterator<Item = &ExtensionConfiguration>,
) -> Result {
for config in configurations {
sqlx::query!(
"INSERT INTO v1.extension_configurations (extension_version_id, is_required, configuration_name, recommended_default_value)
VALUES ($1, $2, $3, $4)",
extension_version_id,
config.is_required,
config.name,
config.recommended_default,
config.configuration_name,
config.recommended_default_value,
).execute(&self.pool).await?;
}
}
Ok(())
}

// 6. insert shared preload libraries
for library in loadable_libraries {
sqlx::query!(
"INSERT INTO v1.extensions_loadable_libraries (extension_version_id, library_name, requires_restart, priority)
VALUES ($1, $2, $3, $4)",
extension_version_id,
library.library_name,
library.requires_restart,
library.priority,
).execute(&self.pool).await?;
}
async fn insert_loadable_libraries(
&self,
extension_version_id: i32,
loadable_libraries: impl Iterator<Item = &LoadableLibrary>,
) -> Result {
for library in loadable_libraries {
sqlx::query!(
"INSERT INTO v1.extensions_loadable_libraries (extension_version_id, library_name, requires_restart, priority)
VALUES ($1, $2, $3, $4)",
extension_version_id,
library.library_name,
library.requires_restart,
library.priority,
).execute(&self.pool).await?;
}

Ok(())
}
}
20 changes: 18 additions & 2 deletions registry/src/views/extension_publish.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! This module handles the expected information an extension should have
use std::collections::HashMap;

use serde::Deserialize;
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;

pub type SystemDependencies = HashMap<String, Vec<String>>;

Expand All @@ -17,5 +18,20 @@ pub struct ExtensionUpload {
pub repository: Option<String>,
pub categories: Option<Vec<String>>,
pub system_dependencies: Option<SystemDependencies>,
pub libraries: Option<Vec<String>>,
pub libraries: Option<Vec<LoadableLibrary>>,
pub configurations: Option<Vec<ExtensionConfiguration>>,
}

#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct LoadableLibrary {
pub library_name: String,
pub requires_restart: bool,
pub priority: i32,
}

#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct ExtensionConfiguration {
pub configuration_name: String,
pub is_required: bool,
pub recommended_default_value: Option<String>,
}

0 comments on commit b7d4211

Please sign in to comment.