Archived
1
0
Fork 0

fix: applied migrations

This commit is contained in:
Dmitriy Pleshevskiy 2021-06-06 14:52:06 +03:00
parent 9d86069e8f
commit 14cee3eaea
10 changed files with 71 additions and 33 deletions

View file

@ -70,8 +70,11 @@ impl ManageMigrations for Client {
.map_err(|_| Error::FailedDeleteMigration)
}
fn applied_migrations(&mut self) -> MigraResult<migration::List> {
let stmt = format!("SELECT name FROM {}", &self.migrations_table_name);
fn get_applied_migrations(&mut self) -> MigraResult<migration::List> {
let stmt = format!(
"SELECT name FROM {} ORDER BY id DESC",
&self.migrations_table_name
);
self.conn
.query::<String, _>(stmt)

View file

@ -73,8 +73,11 @@ impl ManageMigrations for Client {
.map_err(|_| Error::FailedDeleteMigration)
}
fn applied_migrations(&mut self) -> MigraResult<migration::List> {
let stmt = format!("SELECT name FROM {}", &self.migrations_table_name);
fn get_applied_migrations(&mut self) -> MigraResult<migration::List> {
let stmt = format!(
"SELECT name FROM {} ORDER BY id DESC",
&self.migrations_table_name
);
self.client
.query(stmt.as_str(), &[])

View file

@ -67,8 +67,11 @@ impl ManageMigrations for Client {
.map_err(|_| Error::FailedDeleteMigration)
}
fn applied_migrations(&mut self) -> MigraResult<migration::List> {
let stmt = format!("SELECT name FROM {}", &self.migrations_table_name);
fn get_applied_migrations(&mut self) -> MigraResult<migration::List> {
let stmt = format!(
"SELECT name FROM {} ORDER BY id DESC",
&self.migrations_table_name
);
self.conn
.prepare(&stmt)

View file

@ -24,14 +24,7 @@ pub fn get_all_migrations(dir_path: &Path) -> MigraResult<migration::List> {
}
entries.sort();
let file_names = entries
.iter()
.filter_map(|path| path.file_name())
.filter_map(std::ffi::OsStr::to_str)
.collect::<Vec<_>>();
Ok(migration::List::from(file_names))
Ok(migration::List::from(entries))
}
#[must_use]
@ -39,9 +32,5 @@ pub fn filter_pending_migrations(
all_migrations: &migration::List,
applied_migrations: &migration::List,
) -> migration::List {
all_migrations
.clone()
.iter()
.filter(|m| !applied_migrations.contains(m))
.collect()
all_migrations.exclude(applied_migrations)
}

View file

@ -1,5 +1,6 @@
use crate::error::{Error, MigraResult, StdResult};
use crate::migration::{self, Migration};
use std::path::Path;
pub trait BatchExecute {
fn batch_execute(&mut self, sql: &str) -> StdResult<()>;
@ -33,12 +34,16 @@ pub trait ManageMigrations: BatchExecute {
fn delete_migration(&mut self, name: &str) -> MigraResult<u64>;
fn applied_migrations(&mut self) -> MigraResult<migration::List>;
fn get_applied_migrations(&mut self) -> MigraResult<migration::List>;
fn get_extended_applied_migrations(&mut self, prefix: &Path) -> MigraResult<migration::List> {
self.get_applied_migrations()
.map(|migrations| migrations.extend_with_path_prefix(prefix))
}
fn apply_upgrade_migration(&mut self, migration: &Migration) -> MigraResult<()> {
let content = migration.read_upgrade_migration_sql()?;
self.create_migrations_table()?;
self.apply_sql(&content)?;
self.insert_migration(migration.name())?;

View file

@ -15,21 +15,37 @@ pub struct Migration {
impl Migration {
#[must_use]
pub fn new(path: &Path) -> Self {
Migration::with_name(
path,
path.file_name()
.and_then(std::ffi::OsStr::to_str)
.expect("Cannot read migration name"),
)
}
#[must_use]
pub fn with_name(path: &Path, name: &str) -> Self {
Migration {
path: PathBuf::from(path),
name: path
.file_name()
.and_then(std::ffi::OsStr::to_str)
.expect("Cannot read migration name")
.to_string(),
name: name.to_owned(),
}
}
#[must_use]
pub fn path(&self) -> &Path {
&self.path
}
#[must_use]
pub fn name(&self) -> &String {
&self.name
}
#[must_use]
pub fn extend_with_path_prefix(&self, prefix: &Path) -> Self {
Migration::with_name(&prefix.join(self.path()), self.name())
}
pub fn read_upgrade_migration_sql(&self) -> io::Result<String> {
fs::read_to_string(self.path.join(UPGRADE_MIGRATION_FILE_NAME))
}
@ -136,8 +152,17 @@ impl List {
#[must_use]
pub fn exclude(&self, list: &List) -> List {
self.iter()
.filter(|migration| !list.contains(migration))
self.inner
.iter()
.filter(|migration| !list.contains_name(migration.name()))
.collect()
}
#[must_use]
pub fn extend_with_path_prefix(&self, prefix: &Path) -> Self {
self.inner
.iter()
.map(|m| m.extend_with_path_prefix(prefix))
.collect()
}
}

View file

@ -25,7 +25,7 @@ impl App {
}
pub fn run_command(&self) -> StdResult<()> {
match dbg!(self.app_opt.command.clone()) {
match self.app_opt.command.clone() {
Command::Init => {
commands::initialize_migra_manifest(self)?;
}

View file

@ -12,7 +12,10 @@ pub(crate) fn rollback_applied_migrations(app: &App, opts: &DowngradeCommandOpt)
&config.database.connection_string()?,
)?;
let applied_migrations = client.applied_migrations()?;
client.create_migrations_table()?;
let applied_migrations =
client.get_extended_applied_migrations(&config.migration_dir_path())?;
let all_migrations = migra::fs::get_all_migrations(&config.migration_dir_path())?;
let rollback_migrations_number = if opts.all_migrations {
@ -21,6 +24,8 @@ pub(crate) fn rollback_applied_migrations(app: &App, opts: &DowngradeCommandOpt)
cmp::min(opts.migrations_number, applied_migrations.len())
};
dbg!(&rollback_migrations_number);
maybe_with_transaction(
opts.transaction_opts.single_transaction,
&mut client,
@ -28,7 +33,7 @@ pub(crate) fn rollback_applied_migrations(app: &App, opts: &DowngradeCommandOpt)
applied_migrations[..rollback_migrations_number]
.iter()
.try_for_each(|applied_migration| {
if all_migrations.contains(applied_migration) {
if all_migrations.contains_name(applied_migration.name()) {
println!("downgrade {}...", applied_migration.name());
maybe_with_transaction(
!opts.transaction_opts.single_transaction,

View file

@ -10,7 +10,9 @@ pub(crate) fn print_migration_lists(app: &App) -> StdResult<()> {
let applied_migrations = match config.database.connection_string() {
Ok(ref database_connection_string) => {
let mut client = client::create(&config.database.client(), database_connection_string)?;
let applied_migrations = client.applied_migrations()?;
let applied_migrations = client
.get_applied_migrations()
.unwrap_or_else(|_| migration::List::new());
show_applied_migrations(&applied_migrations);

View file

@ -12,7 +12,10 @@ pub(crate) fn upgrade_pending_migrations(app: &App, opts: &UpgradeCommandOpt) ->
&config.database.connection_string()?,
)?;
let applied_migration_names = client.applied_migrations()?;
client.create_migrations_table()?;
let applied_migration_names =
client.get_extended_applied_migrations(&config.migration_dir_path())?;
let all_migrations = migra::fs::get_all_migrations(&config.migration_dir_path())?;
let pending_migrations =