Skip to content

Commit

Permalink
feat(rust): migrate an existing sqlite database to postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
etorreborre committed Jan 22, 2025
1 parent 7560ee1 commit cbdcc24
Show file tree
Hide file tree
Showing 21 changed files with 1,196 additions and 166 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 70 additions & 0 deletions NOTICE.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@ mod tests {
/// - A port for the TCP listener (must not clash with another authority port)
/// - An identity that should be trusted as an enroller
fn create_configuration(
database_configuration: DatabaseConfiguration,
authority: &Identifier,
port: u16,
trusted: &[Identifier],
Expand All @@ -501,9 +502,10 @@ mod tests {
PreTrustedIdentity::new(attributes, TimestampInSeconds(0), None, authority.clone()),
);
}

Ok(Configuration {
identifier: authority.clone(),
database_configuration: DatabaseConfiguration::postgres()?.unwrap(),
database_configuration,
project_identifier: "123456".to_string(),
tcp_listener_address: InternetAddress::new(&format!("127.0.0.1:{}", port)).unwrap(),
secure_channel_listener_name: None,
Expand Down Expand Up @@ -554,13 +556,13 @@ mod tests {
let identities = identities::create(db.clone(), node_name);
let authority = identities.identities_creation().create_identity().await?;

let configuration = create_configuration(&authority, port, trusted)?;
let configuration =
create_configuration(db.configuration.clone(), &authority, port, trusted)?;
let authority = Authority::create(&configuration, Some(db.clone())).await?;
authority_node::start_node(ctx, &configuration, authority.clone()).await?;
Ok(authority)
}

/// Add a member
/// Add a member
async fn add_member(
ctx: &Context,
Expand Down
20 changes: 12 additions & 8 deletions implementations/rust/ockam/ockam_api/src/cli_state/cli_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,17 +273,21 @@ impl CliState {
}

/// If the postgres database is configured, return the postgres configuration
///
pub(super) fn make_database_configuration(
mode: &CliStateMode,
) -> Result<DatabaseConfiguration> {
match DatabaseConfiguration::postgres()? {
Some(configuration) => Ok(configuration),
None => match mode {
CliStateMode::Persistent(root_path) => Ok(DatabaseConfiguration::sqlite(
root_path.join("database.sqlite3"),
)),
CliStateMode::InMemory => Ok(DatabaseConfiguration::sqlite_in_memory()),
},
match mode {
CliStateMode::Persistent(root_path) => {
let sqlite_path = root_path.join("database.sqlite3");
match DatabaseConfiguration::postgres_with_legacy_sqlite_path(Some(
sqlite_path.clone(),
))? {
Some(configuration) => Ok(configuration),
None => Ok(DatabaseConfiguration::sqlite(sqlite_path)),
}
}
CliStateMode::InMemory => Ok(DatabaseConfiguration::sqlite_in_memory()),
}
}

Expand Down
2 changes: 1 addition & 1 deletion implementations/rust/ockam/ockam_node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ opentelemetry = { version = "0.26.0", features = ["logs", "metrics", "trace"], o
regex = { version = "1.10.6", default-features = false, optional = true }
serde = { version = "1.0", default-features = false, features = ["derive"] }
serde_json = { version = "1", optional = true }
sqlx = { version = "0.8.3", optional = true, default-features = false, features = ["postgres", "sqlite", "any", "migrate", "runtime-tokio", "derive"] }
sqlx = { version = "0.8.3", optional = true, default-features = false, features = ["postgres", "sqlite", "any", "migrate", "derive", "runtime-tokio", "runtime-tokio-rustls"] }
sqlx-core = { version = "0.8.3", optional = true, default-features = false }
sqlx-postgres = { version = "0.8.2", optional = true, default-features = false }
sqlx-sqlite = { version = "0.8.2", optional = true, default-features = false }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub enum DatabaseConfiguration {
Postgres {
/// Connection string of the form postgres://[{user}:{password}@]{host}:{port}/{database_name}
connection_string: String,
/// Path to a SQLite database that needs to be migrated to the Postgres database.
legacy_sqlite_path: Option<PathBuf>,
},
}

Expand Down Expand Up @@ -72,10 +74,19 @@ impl DatabaseUser {
impl DatabaseConfiguration {
/// Create a postgres database configuration from an environment variable.
pub fn postgres() -> Result<Option<DatabaseConfiguration>> {
Self::postgres_with_legacy_sqlite_path(None)
}

/// Create a postgres database configuration from an environment variable.
/// An optional legacy sqlite path can be provided to migrate the sqlite database to postgres.
pub fn postgres_with_legacy_sqlite_path(
sqlite_path: Option<PathBuf>,
) -> Result<Option<DatabaseConfiguration>> {
if let Some(connection_string) = get_env::<String>(OCKAM_DATABASE_CONNECTION_URL)? {
check_connection_string_format(&connection_string)?;
Ok(Some(DatabaseConfiguration::Postgres {
connection_string: connection_string.to_owned(),
legacy_sqlite_path: sqlite_path,
}))
} else {
Ok(None)
Expand Down Expand Up @@ -122,6 +133,17 @@ impl DatabaseConfiguration {
}
}

/// Return the legacy sqlite path if any
pub fn legacy_sqlite_path(&self) -> Option<PathBuf> {
match self {
DatabaseConfiguration::SqliteInMemory { .. } => None,
DatabaseConfiguration::SqlitePersistent { .. } => None,
DatabaseConfiguration::Postgres {
legacy_sqlite_path, ..
} => legacy_sqlite_path.clone(),
}
}

/// Return the type of database that has been configured
pub fn connection_string(&self) -> String {
match self {
Expand All @@ -131,7 +153,9 @@ impl DatabaseConfiguration {
DatabaseConfiguration::SqlitePersistent { path, .. } => {
Self::create_sqlite_on_disk_connection_string(path)
}
DatabaseConfiguration::Postgres { connection_string } => connection_string.clone(),
DatabaseConfiguration::Postgres {
connection_string, ..
} => connection_string.clone(),
}
}

Expand Down
Loading

0 comments on commit cbdcc24

Please sign in to comment.