Skip to content

Commit

Permalink
feat: add support for postgres (sqlx and diesel)
Browse files Browse the repository at this point in the history
  • Loading branch information
nlopes committed Aug 29, 2023
1 parent 69cf8ff commit 81a56a1
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ runtime-async-std = ["sqlx?/runtime-async-std", "dep:async-std"]
runtime-tokio = ["sqlx?/runtime-tokio", "dep:tokio"]
sqlite = ["sqlx?/sqlite", "diesel?/sqlite", "diesel_migrations?/sqlite"]
mysql = ["sqlx?/mysql", "diesel?/mysql", "diesel_migrations?/mysql", "url", "percent-encoding"]
postgres = ["sqlx?/postgres", "url", "percent-encoding"]
postgres = ["sqlx?/postgres", "diesel?/postgres", "url", "percent-encoding"]

[dependencies]
async-std = { version = "1", optional = true } # this has to include default due to task::block_on usage
Expand Down
13 changes: 7 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ mod sqlite;
#[cfg_attr(docsrs, doc(cfg(feature = "mysql")))]
mod mysql;

//#[cfg(feature = "postgres")]
//mod postgres;
#[cfg(feature = "postgres")]
#[cfg_attr(docsrs, doc(cfg(feature = "mysql")))]
mod postgres;

#[cfg(all(
feature = "macros",
Expand Down Expand Up @@ -72,8 +73,8 @@ impl Default for ConnectionUrl {
let conn = ConnectionUrl(String::from(sqlite::DEFAULT_CONNECTION_URL));
#[cfg(feature = "mysql")]
let conn = ConnectionUrl(String::from(mysql::DEFAULT_CONNECTION_URL));
//#[cfg(feature = "postgres")]
//let conn = ConnectionUrl(String::from(postgres::DEFAULT_CONNECTION_URL));
#[cfg(feature = "postgres")]
let conn = ConnectionUrl(String::from(postgres::DEFAULT_CONNECTION_URL));

conn
}
Expand Down Expand Up @@ -158,10 +159,10 @@ impl DatabaseSchema {
pub async fn dump(&self) -> Result<(), Error> {
#[cfg(all(feature = "mysql", any(feature = "sqlx", feature = "diesel")))]
use crate::mysql::write_structure_sql;
#[cfg(all(feature = "postgres", any(feature = "sqlx", feature = "diesel")))]
use crate::postgres::write_structure_sql;
#[cfg(all(feature = "sqlite", any(feature = "sqlx", feature = "diesel")))]
use crate::sqlite::write_structure_sql;
//#[cfg(all(feature = "postgres", any(feature = "sqlx", feature = "diesel")))]
//use crate::postgres::write_structure_sql;

write_structure_sql(
&self.0.connection_url.0,
Expand Down
64 changes: 64 additions & 0 deletions src/postgres/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//! Implementation of the `postgres` feature

pub(crate) const DEFAULT_CONNECTION_URL: &str = "postgresql://root:@localhost:5432/postgres";

use crate::error::Error;

#[allow(unused_results)]
#[cfg(any(feature = "sqlx", feature = "diesel"))]
pub(crate) async fn write_structure_sql<P: AsRef<std::path::Path>, Q: AsRef<std::path::Path>>(
connection_url: &str,
migrations_path: P,
destination_path: Q,
) -> Result<(), Error> {
migrate(connection_url, migrations_path).await?;

let mut cmd = std::process::Command::new("pg_dump");
cmd.arg("--schema-only")
.arg("--no-owner")
.arg("--no-privileges")
.arg("--file")
.arg(destination_path.as_ref())
.arg(connection_url);
crate::process::run(&mut cmd).await?;
Ok(())
}

#[cfg(feature = "sqlx")]
async fn migrate<P: AsRef<std::path::Path>>(
connection_url: &str,
migrations_path: P,
) -> Result<(), sqlx::Error> {
use sqlx::{
migrate::{Migrate, Migrator},
postgres::PgConnectOptions,
ConnectOptions,
};
use std::str::FromStr;

let mut conn = PgConnectOptions::from_str(connection_url)?
.connect()
.await?;

// Ensure the migrations table exists before we run the migrations
conn.ensure_migrations_table().await?;

let migrator = Migrator::new(migrations_path.as_ref()).await?;
migrator.run_direct(&mut conn).await?;
Ok(())
}

#[cfg(feature = "diesel")]
async fn migrate<P: AsRef<std::path::Path>>(
connection_url: &str,
migrations_path: P,
) -> Result<(), Error> {
use diesel::Connection;
use diesel_migrations::{FileBasedMigrations, HarnessWithOutput, MigrationHarness};
let mut conn = diesel::PgConnection::establish(connection_url)?;
let migrations = FileBasedMigrations::from_path(migrations_path)?;
let _ = HarnessWithOutput::write_to_stdout(&mut conn)
.run_pending_migrations(migrations)
.map(|_| ());
Ok(())
}

0 comments on commit 81a56a1

Please sign in to comment.