-
Notifications
You must be signed in to change notification settings - Fork 321
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4977 from systeminit/nick/eng-2851
Add audit database migrations
- Loading branch information
Showing
18 changed files
with
314 additions
and
126 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
SELECT 1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
//! Contains functionality for setting up and communicating with the audit database. | ||
use serde::{Deserialize, Serialize}; | ||
use si_data_pg::{PgPool, PgPoolConfig, PgPoolError}; | ||
use telemetry::prelude::*; | ||
use thiserror::Error; | ||
|
||
mod migrate; | ||
|
||
pub use migrate::{migrate, AuditDatabaseMigrationError}; | ||
|
||
/// The name of the audit database. | ||
pub const DBNAME: &str = "si_audit"; | ||
const APPLICATION_NAME: &str = "si-audit"; | ||
|
||
#[allow(missing_docs)] | ||
#[derive(Error, Debug)] | ||
pub enum AuditDatabaseContextError { | ||
#[error("pg pool error: {0}")] | ||
PgPool(#[from] PgPoolError), | ||
} | ||
|
||
type Result<T> = std::result::Result<T, AuditDatabaseContextError>; | ||
|
||
/// The context used for communicating with and setting up the audit database. | ||
#[allow(missing_debug_implementations)] | ||
#[derive(Clone)] | ||
pub struct AuditDatabaseContext { | ||
pg_pool: PgPool, | ||
} | ||
|
||
impl AuditDatabaseContext { | ||
/// Creates an [`AuditDatabaseContext`] from an [`AuditDatabaseConfig`]. | ||
#[instrument(level = "info", name = "audit.context.from_config", skip_all)] | ||
pub async fn from_config(config: &AuditDatabaseConfig) -> Result<Self> { | ||
Ok(Self { | ||
pg_pool: PgPool::new(&config.pg).await?, | ||
}) | ||
} | ||
} | ||
|
||
/// The configuration used for communicating with and setting up the audit database. | ||
#[derive(Debug, Clone, Deserialize, Serialize)] | ||
pub struct AuditDatabaseConfig { | ||
/// The configuration for the PostgreSQL pool. | ||
/// | ||
/// _Note:_ this is called "pg" for ease of use with layered load configuration files. | ||
pub pg: PgPoolConfig, | ||
} | ||
|
||
impl Default for AuditDatabaseConfig { | ||
fn default() -> Self { | ||
Self { | ||
pg: PgPoolConfig { | ||
dbname: DBNAME.into(), | ||
application_name: APPLICATION_NAME.into(), | ||
..Default::default() | ||
}, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//! Contains functionality for migrating the audit database. | ||
use si_data_pg::{PgPool, PgPoolError}; | ||
use telemetry::prelude::*; | ||
use thiserror::Error; | ||
|
||
use super::AuditDatabaseContext; | ||
|
||
#[allow(missing_docs)] | ||
#[derive(Error, Debug)] | ||
pub enum AuditDatabaseMigrationError { | ||
#[error("pg pool error: {0}")] | ||
PgPool(#[from] PgPoolError), | ||
} | ||
|
||
type Result<T> = std::result::Result<T, AuditDatabaseMigrationError>; | ||
|
||
/// Performs migrations for the audit database. | ||
#[instrument(level = "info", name = "audit.init.migrate", skip_all)] | ||
pub async fn migrate(context: &AuditDatabaseContext) -> Result<()> { | ||
migrate_inner(&context.pg_pool).await | ||
} | ||
|
||
#[instrument(level = "info", name = "audit.init.migrate.inner", skip_all)] | ||
async fn migrate_inner(pg: &PgPool) -> Result<()> { | ||
pg.migrate(embedded::migrations::runner()).await?; | ||
Ok(()) | ||
} | ||
|
||
mod embedded { | ||
use refinery::embed_migrations; | ||
|
||
embed_migrations!("./src/migrations"); | ||
} |
Oops, something went wrong.