Skip to content
This repository has been archived by the owner on Dec 21, 2024. It is now read-only.

Commit

Permalink
fix(opengb): one db per env
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterPtato committed Jun 19, 2024
1 parent 4951a00 commit fac57e2
Show file tree
Hide file tree
Showing 139 changed files with 2,161 additions and 1,984 deletions.
138 changes: 17 additions & 121 deletions cli/src/commands/backend/database.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{collections::HashMap, path::PathBuf};
use std::path::PathBuf;

use clap::Parser;
use cli_core::rivet_api::{apis, models};
use cli_core::rivet_api::apis;
use global_error::prelude::*;
use uuid::Uuid;

Expand All @@ -22,12 +22,6 @@ pub enum PassthroughSubCommand {
}

impl PassthroughSubCommand {
pub fn path(&self) -> Option<&PathBuf> {
match self {
PassthroughSubCommand::Database { path, .. } => path.as_ref(),
}
}

pub fn get_cmd(&self) -> &Opts {
match self {
PassthroughSubCommand::Database { command, .. } => command.get_cmd(),
Expand Down Expand Up @@ -69,40 +63,21 @@ pub struct Opts {
pub modules: Vec<String>,
}

pub async fn provision_databases(
pub async fn provision_database(
ctx: &cli_core::Ctx,
opengb_project_path: &PathBuf,
project_id: Uuid,
env_id: Uuid,
) -> GlobalResult<()> {
rivet_term::status::info("Provisioning databases", "");

// Structure DB names
let project_config = super::read_project_config(opengb_project_path).await?;
let modules = project_config
.modules
.keys()
.map(|module_name| {
(
module_name.clone(),
models::EeOpengbModule {
db: Some(Box::new(models::EeOpengbModuleDb {
name: format!("module_{module_name}"),
})),
},
)
})
.collect::<HashMap<_, _>>();

apis::ee_cloud_opengb_projects_envs_api::ee_cloud_opengb_projects_envs_provision_databases(
apis::ee_cloud_backend_projects_envs_api::ee_cloud_backend_projects_envs_provision_database(
&ctx.openapi_config_cloud,
&project_id.to_string(),
&env_id.to_string(),
models::EeCloudOpengbProjectsEnvsProvisionDatabasesRequest { modules },
)
.await?;

// Fetch remote DB URLs
// Fetch remote DB URL
let mut global_project_config = global_config::mutate_project(|config| {
config
.opengb
Expand All @@ -117,31 +92,19 @@ pub async fn provision_databases(
.entry(env_id)
.or_default();

let missing_dbs = project_config
.modules
.keys()
.map(|module_name| format!("module_{module_name}"))
.filter(|db_name| env_config.databases.get(db_name).is_none())
.collect::<Vec<_>>();

if !missing_dbs.is_empty() {
rivet_term::status::info("Fetching connections", "");

let db_urls_res = ee_cloud_opengb_projects_envs_get_db_urls(
&ctx.openapi_config_cloud,
&project_id.to_string(),
&env_id.to_string(),
missing_dbs,
)
.await?;
if env_config.url.is_none() {
rivet_term::status::info("Fetching connection", "");

let db_url_res =
apis::ee_cloud_backend_projects_envs_api::ee_cloud_backend_projects_envs_get_db_url(
&ctx.openapi_config_cloud,
&project_id.to_string(),
&env_id.to_string(),
)
.await?;

// Add missing db urls
env_config.databases.extend(
db_urls_res
.databases
.into_iter()
.map(|(db_name, db_url)| (db_name, global_config::OpenGbDatabase { url: db_url })),
);
// Add missing db url
env_config.url = db_url_res.url;

// Update cache
global_config::try_mutate_project(|config| {
Expand All @@ -157,70 +120,3 @@ pub async fn provision_databases(

Ok(())
}

// The OpenAPI generated client doesn't handle list query parameters correctly, have to patch it here
pub async fn ee_cloud_opengb_projects_envs_get_db_urls(
configuration: &apis::configuration::Configuration,
project_id: &str,
environment_id: &str,
dbs: Vec<String>,
) -> Result<
models::EeCloudOpengbProjectsEnvsGetDbUrlsResponse,
apis::Error<apis::ee_cloud_opengb_projects_envs_api::EeCloudOpengbProjectsEnvsGetDbUrlsError>,
> {
let local_var_configuration = configuration;

let local_var_client = &local_var_configuration.client;

let local_var_uri_str = format!(
"{}/cloud/opengb/projects/{project_id}/environments/{environment_id}/db",
local_var_configuration.base_path,
project_id = apis::urlencode(project_id),
environment_id = apis::urlencode(environment_id)
);
let mut local_var_req_builder =
local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());

local_var_req_builder = match "multi" {
"multi" => local_var_req_builder.query(
&dbs.into_iter()
.map(|p| ("dbs".to_owned(), p))
.collect::<Vec<_>>(),
),
_ => local_var_req_builder.query(&[(
"dbs",
&dbs.into_iter()
.map(|p| p.to_string())
.collect::<Vec<_>>()
.join(",")
.to_string(),
)]),
};
if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
local_var_req_builder =
local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
}
if let Some(ref local_var_token) = local_var_configuration.bearer_access_token {
local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
};

let local_var_req = local_var_req_builder.build()?;
let local_var_resp = local_var_client.execute(local_var_req).await?;

let local_var_status = local_var_resp.status();
let local_var_content = local_var_resp.text().await?;

if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
serde_json::from_str(&local_var_content).map_err(apis::Error::from)
} else {
let local_var_entity: Option<
apis::ee_cloud_opengb_projects_envs_api::EeCloudOpengbProjectsEnvsGetDbUrlsError,
> = serde_json::from_str(&local_var_content).ok();
let local_var_error = apis::ResponseContent {
status: local_var_status,
content: local_var_content,
entity: local_var_entity,
};
Err(apis::Error::ResponseError(local_var_error))
}
}
65 changes: 22 additions & 43 deletions cli/src/commands/backend/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ impl Opts {
let project = get_or_create_project(ctx).await?;
let project_id_str = project.project_id.to_string();

let envs_res = apis::ee_cloud_opengb_projects_envs_api::ee_cloud_opengb_projects_envs_list(
&ctx.openapi_config_cloud,
&project_id_str,
None,
)
.await?;
let envs_res =
apis::ee_cloud_backend_projects_envs_api::ee_cloud_backend_projects_envs_list(
&ctx.openapi_config_cloud,
&project_id_str,
None,
)
.await?;

let env = unwrap!(
envs_res
Expand Down Expand Up @@ -61,43 +62,39 @@ impl Opts {
"--runtime".into(),
"cloudflare-workers".into(),
],
env: Vec::new(),
env: HashMap::new(),
cwd: project_path.clone(),
})
.await?;
ensure!(cmd.success(), "Failed to build OpenGB project");

super::database::provision_databases(
ctx,
&project_path,
project.project_id,
env.environment_id,
)
.await?;
super::database::provision_database(ctx, project.project_id, env.environment_id).await?;

let databases = global_config::try_read_project(|config| {
let db_url = global_config::try_read_project(|config| {
let project = unwrap!(config.opengb.projects.get(&project.project_id));
let env = unwrap!(project.environments.get(&env.environment_id));

Ok(env.databases.clone())
Ok(env.url.clone())
})
.await?;

eprintln!();
rivet_term::status::info("Migrating databases", "");

// Migrate
let mut migrate_env = Vec::new();
for (db_name, db) in databases {
migrate_env.push((format!("DATABASE_URL_{}", db_name), db.url.clone()));
}
let mut migrate_env = HashMap::new();
migrate_env.insert(
"DATABASE_URL".to_string(),
unwrap!(db_url, "no db url for env"),
);

let migrate_cmd = run_opengb_command(OpenGbCommandOpts {
args: vec!["db".into(), "deploy".into()],
env: migrate_env,
cwd: project_path.clone(),
})
.await?;
ensure!(migrate_cmd.success(), "Failed to migrate OpenGB databases",);
ensure!(migrate_cmd.success(), "Failed to migrate OpenGB databases");

// Read files for upload
let gen_manifest = read_generated_manifest(&project_path).await?;
Expand Down Expand Up @@ -131,11 +128,11 @@ impl Opts {
);

let prepare_res = unwrap!(
apis::ee_cloud_opengb_projects_envs_api::ee_cloud_opengb_projects_envs_prepare_deploy(
apis::ee_cloud_backend_projects_envs_api::ee_cloud_backend_projects_envs_prepare_deploy(
&ctx.openapi_config_cloud,
&project_id_str,
&env_id,
models::EeCloudOpengbProjectsEnvsPrepareDeployRequest {
models::EeCloudBackendProjectsEnvsPrepareDeployRequest {
files: files.iter().map(|f| f.prepared.clone()).collect(),
},
)
Expand Down Expand Up @@ -177,30 +174,12 @@ impl Opts {
eprintln!();
rivet_term::status::info("Deploying environment", &env.display_name);

// Structure DB names
let project_config = super::read_project_config(&project_path).await?;
let modules = project_config
.modules
.keys()
.map(|module_name| {
(
module_name.clone(),
models::EeOpengbModule {
db: Some(Box::new(models::EeOpengbModuleDb {
name: format!("module_{module_name}"),
})),
},
)
})
.collect::<HashMap<_, _>>();

let deploy_res =
apis::ee_cloud_opengb_projects_envs_api::ee_cloud_opengb_projects_envs_deploy(
apis::ee_cloud_backend_projects_envs_api::ee_cloud_backend_projects_envs_deploy(
&ctx.openapi_config_cloud,
&project_id_str,
&env_id,
models::EeCloudOpengbProjectsEnvsDeployRequest {
modules,
models::EeCloudBackendProjectsEnvsDeployRequest {
upload_id: prepare_res.upload_id,
},
)
Expand Down
10 changes: 5 additions & 5 deletions cli/src/commands/backend/environment/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ impl Opts {

rivet_term::status::info("Creating environment", &self.display_name);

apis::ee_cloud_opengb_projects_envs_api::ee_cloud_opengb_projects_envs_create(
apis::ee_cloud_backend_projects_envs_api::ee_cloud_backend_projects_envs_create(
&ctx.openapi_config_cloud,
project.project_id.to_string().as_str(),
models::EeCloudOpengbProjectsEnvsCreateRequest {
models::EeCloudBackendProjectsEnvsCreateRequest {
display_name: self.display_name.clone(),
name_id: self.name_id.clone(),
tier: self.tier.into(),
Expand All @@ -56,11 +56,11 @@ enum Tier {
Dedicated,
}

impl From<Tier> for models::EeOpengbTier {
impl From<Tier> for models::EeBackendTier {
fn from(value: Tier) -> Self {
match value {
Tier::Shared => models::EeOpengbTier::Shared,
Tier::Dedicated => models::EeOpengbTier::Dedicated,
Tier::Shared => models::EeBackendTier::Shared,
Tier::Dedicated => models::EeBackendTier::Dedicated,
}
}
}
Loading

0 comments on commit fac57e2

Please sign in to comment.