2021-03-02 00:44:54 +03:00
|
|
|
use super::connection::AnyConnection;
|
2021-02-14 00:03:37 +03:00
|
|
|
use crate::StdResult;
|
2021-02-08 23:42:13 +03:00
|
|
|
use std::fs;
|
2021-02-21 17:53:43 +03:00
|
|
|
use std::path::{Path, PathBuf};
|
2021-02-08 23:42:13 +03:00
|
|
|
|
2021-04-24 01:58:16 +03:00
|
|
|
#[derive(Debug, Clone)]
|
2021-02-08 23:42:13 +03:00
|
|
|
pub struct Migration {
|
2021-02-23 18:55:12 +03:00
|
|
|
upgrade_sql_file_path: PathBuf,
|
|
|
|
downgrade_sql_file_path: PathBuf,
|
2021-02-08 23:42:13 +03:00
|
|
|
name: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Migration {
|
2021-02-21 17:53:43 +03:00
|
|
|
pub(crate) fn new(directory: &Path) -> Option<Migration> {
|
2021-02-08 23:42:13 +03:00
|
|
|
if directory.is_dir() {
|
|
|
|
let name = directory
|
|
|
|
.file_name()
|
|
|
|
.and_then(|name| name.to_str())
|
|
|
|
.unwrap_or_default();
|
2021-02-23 18:55:12 +03:00
|
|
|
let upgrade_sql_file_path = directory.join("up.sql");
|
|
|
|
let downgrade_sql_file_path = directory.join("down.sql");
|
2021-02-08 23:42:13 +03:00
|
|
|
|
2021-02-23 18:55:12 +03:00
|
|
|
if upgrade_sql_file_path.exists() && downgrade_sql_file_path.exists() {
|
2021-02-08 23:42:13 +03:00
|
|
|
return Some(Migration {
|
2021-02-23 18:55:12 +03:00
|
|
|
upgrade_sql_file_path,
|
|
|
|
downgrade_sql_file_path,
|
2021-02-08 23:42:13 +03:00
|
|
|
name: String::from(name),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
2021-02-14 00:03:37 +03:00
|
|
|
}
|
2021-02-08 23:42:13 +03:00
|
|
|
|
2021-02-14 00:03:37 +03:00
|
|
|
impl Migration {
|
2021-02-08 23:42:13 +03:00
|
|
|
pub fn name(&self) -> &String {
|
|
|
|
&self.name
|
|
|
|
}
|
|
|
|
|
2021-02-15 13:06:09 +03:00
|
|
|
fn upgrade_sql_content(&self) -> StdResult<String> {
|
2021-02-23 18:55:12 +03:00
|
|
|
let content = fs::read_to_string(&self.upgrade_sql_file_path)?;
|
2021-02-15 13:06:09 +03:00
|
|
|
Ok(content)
|
|
|
|
}
|
2021-02-08 23:42:13 +03:00
|
|
|
|
2021-02-15 13:06:09 +03:00
|
|
|
fn downgrade_sql_content(&self) -> StdResult<String> {
|
2021-02-23 18:55:12 +03:00
|
|
|
let content = fs::read_to_string(&self.downgrade_sql_file_path)?;
|
2021-02-15 13:06:09 +03:00
|
|
|
Ok(content)
|
|
|
|
}
|
|
|
|
}
|
2021-02-08 23:42:13 +03:00
|
|
|
|
2021-03-02 00:44:54 +03:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct MigrationManager;
|
2021-02-15 13:06:09 +03:00
|
|
|
|
2021-02-18 12:29:09 +03:00
|
|
|
impl MigrationManager {
|
2021-03-02 00:44:54 +03:00
|
|
|
pub fn new() -> Self {
|
|
|
|
MigrationManager
|
2021-02-18 12:29:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-15 13:06:09 +03:00
|
|
|
pub fn is_migrations_table_not_found<D: std::fmt::Display>(error: D) -> bool {
|
2021-03-26 02:10:41 +03:00
|
|
|
let error_message = error.to_string();
|
|
|
|
|
2021-04-24 23:16:30 +03:00
|
|
|
fn is_postgres_error(error_message: &str) -> bool {
|
|
|
|
error_message.contains("relation") && error_message.ends_with("does not exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_mysql_error(error_message: &str) -> bool {
|
|
|
|
error_message.contains("ERROR 1146 (42S02)")
|
|
|
|
}
|
|
|
|
|
|
|
|
is_postgres_error(&error_message) || is_mysql_error(&error_message)
|
2021-02-15 13:06:09 +03:00
|
|
|
}
|
|
|
|
|
2021-03-02 00:44:54 +03:00
|
|
|
pub trait ManageMigration {
|
|
|
|
fn apply_sql(&self, conn: &mut AnyConnection, sql_content: &str) -> StdResult<()>;
|
2021-02-15 13:06:09 +03:00
|
|
|
|
2021-03-02 00:44:54 +03:00
|
|
|
fn create_migrations_table(&self, conn: &mut AnyConnection) -> StdResult<()>;
|
2021-02-08 23:42:13 +03:00
|
|
|
|
2021-03-02 00:44:54 +03:00
|
|
|
fn insert_migration_info(&self, conn: &mut AnyConnection, name: &str) -> StdResult<u64>;
|
2021-02-15 13:06:09 +03:00
|
|
|
|
2021-03-02 00:44:54 +03:00
|
|
|
fn delete_migration_info(&self, conn: &mut AnyConnection, name: &str) -> StdResult<u64>;
|
2021-02-15 13:06:09 +03:00
|
|
|
|
2021-03-02 00:44:54 +03:00
|
|
|
fn applied_migration_names(&self, conn: &mut AnyConnection) -> StdResult<Vec<String>>;
|
2021-02-18 12:29:09 +03:00
|
|
|
|
2021-03-02 00:44:54 +03:00
|
|
|
fn upgrade(&self, conn: &mut AnyConnection, migration: &Migration) -> StdResult<()> {
|
2021-02-15 13:06:09 +03:00
|
|
|
let content = migration.upgrade_sql_content()?;
|
|
|
|
|
2021-03-02 00:44:54 +03:00
|
|
|
self.create_migrations_table(conn)?;
|
|
|
|
self.apply_sql(conn, &content)?;
|
|
|
|
self.insert_migration_info(conn, migration.name())?;
|
2021-02-08 23:42:13 +03:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-02-15 13:06:09 +03:00
|
|
|
|
2021-03-02 00:44:54 +03:00
|
|
|
fn downgrade(&self, conn: &mut AnyConnection, migration: &Migration) -> StdResult<()> {
|
2021-02-15 13:06:09 +03:00
|
|
|
let content = migration.downgrade_sql_content()?;
|
|
|
|
|
2021-03-02 00:44:54 +03:00
|
|
|
self.apply_sql(conn, &content)?;
|
|
|
|
self.delete_migration_info(conn, migration.name())?;
|
2021-02-15 13:06:09 +03:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-02 00:44:54 +03:00
|
|
|
impl ManageMigration for MigrationManager {
|
|
|
|
fn apply_sql(&self, conn: &mut AnyConnection, sql_content: &str) -> StdResult<()> {
|
|
|
|
conn.batch_execute(sql_content)
|
2021-02-15 13:06:09 +03:00
|
|
|
}
|
|
|
|
|
2021-03-02 00:44:54 +03:00
|
|
|
fn create_migrations_table(&self, conn: &mut AnyConnection) -> StdResult<()> {
|
2021-04-08 00:49:38 +03:00
|
|
|
let stmt = conn.create_migration_table_stmt();
|
2021-03-26 02:10:41 +03:00
|
|
|
conn.batch_execute(&stmt)
|
2021-02-15 13:47:45 +03:00
|
|
|
}
|
|
|
|
|
2021-03-02 00:44:54 +03:00
|
|
|
fn insert_migration_info(&self, conn: &mut AnyConnection, name: &str) -> StdResult<u64> {
|
|
|
|
conn.execute("INSERT INTO migrations (name) VALUES ($1)", &[&name])
|
2021-02-15 13:47:45 +03:00
|
|
|
}
|
|
|
|
|
2021-03-02 00:44:54 +03:00
|
|
|
fn delete_migration_info(&self, conn: &mut AnyConnection, name: &str) -> StdResult<u64> {
|
|
|
|
conn.execute("DELETE FROM migrations WHERE name = $1", &[&name])
|
2021-02-15 13:47:45 +03:00
|
|
|
}
|
|
|
|
|
2021-03-02 00:44:54 +03:00
|
|
|
fn applied_migration_names(&self, conn: &mut AnyConnection) -> StdResult<Vec<String>> {
|
|
|
|
let res = conn
|
2021-02-18 12:29:09 +03:00
|
|
|
.query("SELECT name FROM migrations ORDER BY id DESC", &[])
|
|
|
|
.or_else(|e| {
|
|
|
|
if is_migrations_table_not_found(&e) {
|
|
|
|
Ok(Vec::new())
|
|
|
|
} else {
|
|
|
|
Err(e)
|
|
|
|
}
|
|
|
|
})?;
|
|
|
|
|
2021-02-19 22:37:27 +03:00
|
|
|
let applied_migration_names: Vec<String> = res
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|row| row.first().cloned())
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
Ok(applied_migration_names)
|
2021-02-18 12:29:09 +03:00
|
|
|
}
|
2021-02-15 13:47:45 +03:00
|
|
|
}
|
|
|
|
|
2021-02-15 13:06:09 +03:00
|
|
|
pub fn filter_pending_migrations(
|
|
|
|
migrations: Vec<Migration>,
|
|
|
|
applied_migration_names: &[String],
|
|
|
|
) -> Vec<Migration> {
|
|
|
|
migrations
|
|
|
|
.into_iter()
|
|
|
|
.filter(|m| !applied_migration_names.contains(m.name()))
|
|
|
|
.collect()
|
2021-02-08 23:42:13 +03:00
|
|
|
}
|