schemas_v1_db_queries_region - OmniCloudOrg/OmniOrchestrator GitHub Wiki
Path: src/schemas/v1/db/queries/region.rs
- async fn list_regions
- async fn list_provider_regions
- async fn get_region_by_id
- async fn create_region
- async fn update_region_status
- async fn delete_region
- async fn get_active_regions
pub async fn list_regions(
pool: &Pool<MySql>,
limit: Option<i64>,
offset: Option<i64>,
) -> anyhow::Result<Vec<Region>> {
// ... function body
}
Retrieves a paginated list of deployment regions. This function fetches regions from the database with optional pagination support. Results are ordered by creation time with the most recently created regions first.
-
pool
- Database connection pool for executing the query -
limit
- Optional maximum number of regions to return (defaults to 100 if not specified) -
offset
- Optional number of regions to skip (for pagination)
-
Ok(Vec<Region>)
- Successfully retrieved list of regions -
Err(anyhow::Error)
- Failed to fetch regions
This function uses SQLx's QueryBuilder to dynamically construct a SQL query based on whether pagination parameters are provided. This approach is more efficient than building strings manually and protects against SQL injection.
When both limit
and offset
are provided, standard SQL pagination is applied. If only limit
is provided, just the first N records are returned. If neither is provided, all regions are returned (with a safety limit of 100).
pub async fn list_provider_regions(
pool: &Pool<MySql>,
) -> anyhow::Result<Vec<ProviderRegion>> {
// ... function body
}
pub async fn get_region_by_id(pool: &Pool<MySql>, id: i64) -> anyhow::Result<Region> {
let region = sqlx::query_as::<_, Region>("SELECT * FROM regions WHERE id = ?")
.bind(id)
.fetch_one(pool)
.await
.context("Failed to fetch region")?;
Retrieves a specific region by its unique identifier. This function fetches detailed information about a single region record. It's typically used when specific region details are needed, such as for displaying region information or resource allocation.
-
pool
- Database connection pool for executing the query -
id
- Unique identifier of the region to retrieve
-
Ok(Region)
- Successfully retrieved region information -
Err(anyhow::Error)
- Failed to fetch region (including if not found)
Returns an error if no region with the given ID exists or if a database error occurs during the query execution.
pub async fn create_region(
pool: &Pool<MySql>,
name: &str,
provider: &str,
status: &str,
) -> anyhow::Result<Region> {
// ... function body
}
Creates a new deployment region in the system. This function registers a new region for application deployments. Regions typically represent geographical deployment locations or distinct cloud provider environments.
-
pool
- Database connection pool for executing the query -
name
- Name of the region (typically a geographical identifier like "us-east", "eu-west") -
provider
- Cloud or infrastructure provider (e.g., "aws", "gcp", "azure", "on-prem") -
status
- Initial status of the region (e.g., "provisioning", "active", "maintenance")
-
Ok(Region)
- Successfully created region record -
Err(anyhow::Error)
- Failed to create region record
This function uses a database transaction to ensure atomicity of the operation. If any part of the operation fails, the entire operation is rolled back.
Typical region status values include: - "provisioning" - Region is being set up and not yet ready for deployments - "active" - Region is fully operational and can accept deployments - "maintenance" - Region is temporarily unavailable for new deployments - "deprecated" - Region is being phased out, no new deployments accepted - "unavailable" - Region is not currently operational
pub async fn update_region_status(
pool: &Pool<MySql>,
id: i64,
status: &str,
) -> anyhow::Result<Region> {
// ... function body
}
Updates the status of an existing deployment region. This function changes the operational status of a region, which affects whether new deployments can be directed to it. Status changes are critical operations that can affect application availability and deployment strategies.
-
pool
- Database connection pool for executing the query -
id
- Unique identifier of the region to update -
status
- New status for the region (e.g., "active", "maintenance", "unavailable")
-
Ok(Region)
- Successfully updated region record -
Err(anyhow::Error)
- Failed to update region
This function uses a database transaction to ensure atomicity of the operation. If any part of the operation fails, the entire operation is rolled back.
Changing a region's status may have significant operational impacts: - Setting to "maintenance" or "unavailable" prevents new deployments - Status changes should be coordinated with deployment schedules - Monitoring systems may need to be updated based on region status - Load balancers may need reconfiguration after status changes
pub async fn delete_region(pool: &Pool<MySql>, id: i64) -> anyhow::Result<()> {
let mut tx = pool.begin().await?;
Deletes a deployment region from the system. This function permanently removes a region record from the database. It should be used with extreme caution, as it may affect deployed applications and infrastructure allocation.
-
pool
- Database connection pool for executing the query -
id
- Unique identifier of the region to delete
-
Ok(())
- Successfully deleted the region -
Err(anyhow::Error)
- Failed to delete the region
This operation is irreversible and potentially dangerous. Instead of deleting regions, consider changing their status to "deprecated" or "unavailable" first, and ensure no active deployments exist in the region before deletion.
Depending on the database schema and application logic: - Deployed applications in this region may lose their region reference - Foreign key constraints may prevent deletion if the region is in use - Monitoring, billing, and operational systems may be affected
This function uses a database transaction to ensure atomicity of the operation. If any part of the operation fails, the entire operation is rolled back.
pub async fn get_active_regions(pool: &Pool<MySql>) -> anyhow::Result<Vec<Region>> {
let regions = sqlx::query_as::<_, Region>(
"SELECT * FROM regions WHERE status = 'active' ORDER BY created_at DESC",
)
.fetch_all(pool)
.await
.context("Failed to fetch active regions")?;
Retrieves all active deployment regions. This function fetches all regions with a status of "active", indicating they are available for new deployments. It's typically used for deployment target selection and region availability displays.
-
pool
- Database connection pool for executing the query
-
Ok(Vec<Region>)
- Successfully retrieved list of active regions -
Err(anyhow::Error)
- Failed to fetch active regions
Common use cases include: - Populating region selection dropdowns in deployment interfaces - Determining valid deployment targets for automated processes - Calculating resource availability across active regions - Health status dashboards showing operational deployment locations
Results are filtered by status="active" and ordered by creation time, with the most recently created regions appearing first in the list.