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

Commit

Permalink
wip(backend): init database automatically with a schema.sql
Browse files Browse the repository at this point in the history
  • Loading branch information
evoxmusic committed Jan 3, 2024
1 parent ea01923 commit cf3cd37
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 4 deletions.
10 changes: 10 additions & 0 deletions backend/db/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- create a new flat table to store catalog execution results

CREATE TABLE IF NOT EXISTS catalog_execution_statuses
(
id UUID PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
status VARCHAR(255) NOT NULL,
input_payload JSONB NOT NULL
);
14 changes: 14 additions & 0 deletions backend/src/database.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use tokio_postgres::Client;

use crate::errors::QError;

/// Initialize the database by creating the tables
pub async fn init_database(pg_client: &Client) -> Result<(), QError> {
// read SQL schema from file
let sql_schema = include_str!("../db/schema.sql");

// execute SQL schema
let _ = pg_client.batch_execute(sql_schema).await?;

Ok(())
}
13 changes: 12 additions & 1 deletion backend/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
pub(crate) enum QError {}
use crate::errors::QError::PostgresError;

#[derive(Debug)]
pub enum QError {
PostgresError(tokio_postgres::Error),
}

impl From<tokio_postgres::Error> for QError {
fn from(pg_error: tokio_postgres::Error) -> Self {
PostgresError(pg_error)
}
}
22 changes: 20 additions & 2 deletions backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use tracing_subscriber::util::SubscriberInitExt;
use crate::catalog::controllers::{exec_catalog_service_post_validate_scripts, exec_catalog_service_validate_scripts, list_catalog_services, list_catalogs};
use crate::catalog::services::BackgroundWorkerTask;
use crate::cli::CLI;
use crate::database::init_database;
use crate::yaml_config::YamlConfig;

mod yaml_config;
Expand All @@ -23,6 +24,7 @@ mod errors;
mod cli;
mod constants;
mod catalog;
mod database;

pub async fn unknown_route(uri: Uri) -> (StatusCode, String) {
let message = format!("unknown route for {uri}");
Expand Down Expand Up @@ -66,21 +68,37 @@ async fn main() {
}
};

let (client, connect) = tokio_postgres::connect(
let (client, connection) = tokio_postgres::connect(
format!(
"host={} port={} user={} password={} dbname={}",
env::var("DB_HOST").unwrap_or_else(|_| "localhost".to_string()),
env::var("DB_PORT").unwrap_or_else(|_| "5432".to_string()),
env::var("DB_USER").unwrap_or_else(|_| "postgres".to_string()),
env::var("DB_PASSWORD").unwrap_or_else(|_| "postgres".to_string()),
env::var("DB_NAME").unwrap_or_else(|_| "qovery_portal".to_string()),
env::var("DB_NAME").unwrap_or_else(|_| "torii".to_string()),
).as_str(),
tokio_postgres::NoTls,
).await.unwrap_or_else(|err| {
error!("failed to connect to database: {}", err);
std::process::exit(1);
});

// The connection object performs the actual communication with the database,
// so spawn it off to run on its own.
tokio::spawn(async move {
if let Err(e) = connection.await {
// TODO handle connection error
error!("connection error: {}", e);
}
});

init_database(&client).await.unwrap_or_else(|err| {
error!("failed to initialize database: {:?}", err);
std::process::exit(1);
});

info!("database initialized and up to date");

let client = Arc::new(client);
let bgw_client = client.clone();

Expand Down
2 changes: 1 addition & 1 deletion docker-compose-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ services:
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: qovery_portal
POSTGRES_DB: torii
ports:
- 5432:5432

0 comments on commit cf3cd37

Please sign in to comment.