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 14bce88
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 39 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
8 changes: 8 additions & 0 deletions registry/src/routes/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,17 @@ pub async fn publish(
.fetch_optional(&mut tx)
.await?;

// TODO(vini): change here
let system_deps = serde_json::to_value(&new_extension.system_dependencies)?;
let libraries = serde_json::to_value(&new_extension.libraries)?;

// Save configurations and libraries to the v1 tables
{
if let Some(configs) = &new_extension.configurations {}

if let Some(libraries) = &new_extension.libraries {}
}

match exists {
// TODO(ianstanton) Refactor into separate functions
Some(exists) => {
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 14bce88

Please sign in to comment.