@@ -5,7 +5,7 @@ use crate::{
6use anyhow::{anyhow, Context};
6use anyhow::{anyhow, Context};
7use colored::*;
7use colored::*;
8−use db::{Conn, DbConn};
8+use db::{Conn, DbConn, DbLocker};
9use postgres::Config;
9use postgres::Config;
10use schema::Table;
10use schema::Table;
@@ -18,8 +18,7 @@ mod state;
18pub use crate::state::State;
18pub use crate::state::State;
20pub struct Reshape {
20pub struct Reshape {
21− pub state: State,
21+ db: DbLocker,
25impl Reshape {
24impl Reshape {
@@ -45,494 +44,480 @@ impl Reshape {
47 fn new_with_config(config: &Config) -> anyhow::Result<Reshape> {
46 fn new_with_config(config: &Config) -> anyhow::Result<Reshape> {
48− let mut db = DbConn::connect(config)?;
47+ let db = DbLocker::connect(config)?;
49− let state = State::load(&mut db)?;
48+ Ok(Reshape { db })
51− Ok(Reshape { db, state })
51+ pub fn migrate(
53+ migrations: impl IntoIterator<Item = Migration>,
54+ ) -> anyhow::Result<()> {
56+ let mut state = State::load(db)?;
57+ migrate(db, &mut state, migrations)
54− pub fn migrate<T>(&mut self, migrations: T) -> anyhow::Result<()>
61+ pub fn complete(&mut self) -> anyhow::Result<()> {
55− where
62+ self.db.lock(|db| {
56− T: IntoIterator<Item = Migration>,
58− self.state = State::load(&mut self.db)?;
63+ let mut state = State::load(db)?;
64+ complete(db, &mut state)
60− // Make sure no migration is in progress
68+ pub fn abort(&mut self) -> anyhow::Result<()> {
61− if let State::InProgress { .. } = &self.state {
69+ self.db.lock(|db| {
62− println!("Migration already in progress, please complete using 'reshape complete'");
70+ let mut state = State::load(db)?;
63− return Ok(());
71+ abort(db, &mut state)
66− if let State::Completing { .. } = &self.state {
75+ pub fn remove(&mut self) -> anyhow::Result<()> {
67− println!(
76+ self.db.lock(|db| {
68− "Migration already in progress and has started completion, please finish using 'reshape complete'"
77+ let mut state = State::load(db)?;
73− // Determine which migrations need to be applied by comparing the provided migrations
79+ // Remove migration schemas and views
74− // with the already applied ones stored in the state. This will throw an error if the
75− // two sets of migrations don't agree, for example if a new migration has been added
76− // in between two existing ones.
77− let current_migration = state::current_migration(&mut self.db)?;
80+ if let Some(current_migration) = &state::current_migration(db)? {
78− let remaining_migrations = state::remaining_migrations(&mut self.db, migrations)?;
81+ db.run(&format!(
79− if remaining_migrations.is_empty() {
82+ "DROP SCHEMA IF EXISTS {} CASCADE",
80− println!("No migrations left to apply");
83+ schema_name_for_migration(current_migration)
81− return Ok(());
84+ ))?;
84− // If we have already started applying some migrations we need to ensure that
87+ if let State::InProgress { migrations } = &state {
85− // they are the same ones we want to apply now
88+ let target_migration = migrations.last().unwrap().name.to_string();
86− if let State::Applying {
89+ db.run(&format!(
87− migrations: existing_migrations,
90+ "DROP SCHEMA IF EXISTS {} CASCADE",
88− } = &self.state
91+ schema_name_for_migration(&target_migration)
90− if existing_migrations != &remaining_migrations {
92− "a previous migration seems to have failed without cleaning up. Please run `reshape abort` and then run migrate again."
97− // Move to the "Applying" state which is necessary as we can't run the migrations
95+ // Remove all tables
98− // and state update as a single transaction. If a migration unexpectedly fails without
96+ let schema = Schema::new();
99− // automatically aborting, this state saves us from dangling migrations. It forces the user
97+ for table in schema.get_tables(db)? {
100− // to either run migrate again (which works as all migrations are idempotent) or abort.
98+ db.run(&format!(
101− self.state.applying(remaining_migrations.clone());
99+ r#"
102− self.state.save(&mut self.db)?;
100+ DROP TABLE IF EXISTS "{}" CASCADE
104− println!("Applying {} migrations\n", remaining_migrations.len());
106+ // Reset state
106− helpers::set_up_helpers(&mut self.db, ¤t_migration)
109+ println!("Reshape and all data has been removed");
107− .context("failed to set up helpers")?;
109− let mut new_schema = Schema::new();
111+ Ok(())
110− let mut last_migration_index = usize::MAX;
112+ })
111− let mut last_action_index = usize::MAX;
113+ }
112− let mut result: anyhow::Result<()> = Ok(());
114+}
116+pub fn latest_schema_from_migrations(migrations: &[Migration]) -> Option<String> {
119+ .map(|migration| schema_name_for_migration(&migration.name))
114− for (migration_index, migration) in remaining_migrations.iter().enumerate() {
122+pub fn schema_query_for_migration(migration_name: &str) -> String {
115− println!("Migrating '{}':", migration.name);
123+ let schema_name = schema_name_for_migration(migration_name);
116− last_migration_index = migration_index;
124+ format!("SET search_path TO {}", schema_name)
118− for (action_index, action) in migration.actions.iter().enumerate() {
127+fn schema_name_for_migration(migration_name: &str) -> String {
119− last_action_index = action_index;
128+ format!("migration_{}", migration_name)
121− let description = action.describe();
131+fn migrate(
122− print!(" + {} ", description);
132+ db: &mut DbConn,
134+ migrations: impl IntoIterator<Item = Migration>,
135+) -> anyhow::Result<()> {
136+ // Make sure no migration is in progress
137+ if let State::InProgress { .. } = &state {
138+ println!("Migration already in progress, please complete using 'reshape complete'");
124− let ctx = MigrationContext::new(migration_index, action_index);
142+ if let State::Completing { .. } = &state {
125− result = action
143+ println!(
126− .run(&ctx, &mut self.db, &new_schema)
144+ "Migration already in progress and has started completion, please finish using 'reshape complete'"
127− .with_context(|| format!("failed to {}", description));
145+ );
129− if result.is_ok() {
149+ // Determine which migrations need to be applied by comparing the provided migrations
130− action.update_schema(&ctx, &mut new_schema);
150+ // with the already applied ones stored in the state. This will throw an error if the
131− println!("{}", "done".green());
151+ // two sets of migrations don't agree, for example if a new migration has been added
132− } else {
152+ // in between two existing ones.
133− println!("{}", "failed".red());
153+ let current_migration = state::current_migration(db)?;
134− break;
154+ let remaining_migrations = state::remaining_migrations(db, migrations)?;
135− }
155+ if remaining_migrations.is_empty() {
156+ println!("No migrations left to apply");
138− println!();
160+ // If we have already started applying some migrations we need to ensure that
161+ // they are the same ones we want to apply now
162+ if let State::Applying {
163+ migrations: existing_migrations,
166+ if existing_migrations != &remaining_migrations {
168+ "a previous migration seems to have failed without cleaning up. Please run `reshape abort` and then run migrate again."
141− // If a migration failed, we abort all the migrations that were applied
173+ // Move to the "Applying" state which is necessary as we can't run the migrations
142− if let Err(err) = result {
174+ // and state update as a single transaction. If a migration unexpectedly fails without
143− println!("A migration failed, aborting migrations that have already been applied");
175+ // automatically aborting, this state saves us from dangling migrations. It forces the user
176+ // to either run migrate again (which works as all migrations are idempotent) or abort.
177+ state.applying(remaining_migrations.clone());
145− // Set to the Aborting state. This is to ensure that the failed
146− // migration is fully aborted and nothing is left dangling.
147− // If the abort is interrupted for any reason, the user can try again
148− // by running `reshape abort`.
149− self.state.aborting(
150− remaining_migrations.clone(),
180+ println!("Applying {} migrations\n", remaining_migrations.len());
151− last_migration_index + 1,
152− last_action_index + 1,
155− // Abort will only
182+ helpers::set_up_helpers(db, ¤t_migration).context("failed to set up helpers")?;
158− return Err(err);
184+ let mut new_schema = Schema::new();
159− }
185+ let mut last_migration_index = usize::MAX;
186+ let mut last_action_index = usize::MAX;
187+ let mut result: anyhow::Result<()> = Ok(());
161− // Create schema and views for migration
189+ for (migration_index, migration) in remaining_migrations.iter().enumerate() {
162− let target_migration = remaining_migrations.last().unwrap().name.to_string();
190+ println!("Migrating '{}':", migration.name);
163− self.create_schema_for_migration(&target_migration, &new_schema)
191+ last_migration_index = migration_index;
165− format!("failed to create schema for migration {}", target_migration)
168− // Update state once migrations have been performed
169− self.state.in_progress(remaining_migrations);
172− .context("failed to save in-progress state")?;
174− // If we started from a blank slate, we can finish the migration immediately
175− if current_migration.is_none() {
176− println!("Automatically completing migrations\n");
177− self.complete_migration()
178− .context("failed to automatically complete migrations")?;
180− println!("Migrations complete:");
182− " - Run '{}' from your application to use the latest schema",
183− schema_query_for_migration(&target_migration)
186− println!("Migrations have been applied and the new schema is ready for use:");
188− " - Run '{}' from your application to use the latest schema",
189− schema_query_for_migration(&target_migration)
192− " - Run 'reshape complete' once your application has been updated and the previous schema is no longer in use"
196− Ok(())
193+ for (action_index, action) in migration.actions.iter().enumerate() {
197− }
194+ last_action_index = action_index;
199− pub fn complete_migration(&mut self) -> anyhow::Result<()> {
196+ let description = action.describe();
200− // Make sure a migration is in progress
197+ print!(" + {} ", description);
201− let (remaining_migrations, starting_migration_index, starting_action_index) = match self.state.clone() {
202− State::InProgress { migrations } => {
203− // Move into the Completing state. Once in this state,
204− // the migration can't be aborted and must be completed.
205− self.state.completing(migrations.clone(), 0, 0);
206− self.state.save(&mut self.db).context("failed to save state")?;
212− current_migration_index,
213− current_action_index
214− } => (migrations, current_migration_index, current_action_index),
215− State::Aborting { .. } => {
216− return Err(anyhow!("migration been aborted and can't be completed. Please finish using `reshape abort`."))
218− State::Applying { .. } => {
219− return Err(anyhow!("a previous migration unexpectedly failed. Please run `reshape migrate` to try applying the migration again."))
222− println!("No migration in progress");
227− // Remove previous migration's schema
199+ let ctx = MigrationContext::new(migration_index, action_index);
228− if let Some(current_migration) = &state::current_migration(&mut self.db)? {
200+ result = action
229− self.db
201+ .run(&ctx, db, &new_schema)
230− .run(&format!(
202+ .with_context(|| format!("failed to {}", description));
231− "DROP SCHEMA IF EXISTS {} CASCADE",
232− schema_name_for_migration(current_migration)
234− .context("failed to remove previous migration's schema")?;
237− for (migration_index, migration) in remaining_migrations.iter().enumerate() {
204+ if result.is_ok() {
238− // Skip all the migrations which have already been completed
205+ action.update_schema(&ctx, &mut new_schema);
239− if migration_index < starting_migration_index {
206+ println!("{}", "done".green());
240− continue;
207+ } else {
208+ println!("{}", "failed".red());
243− println!("Completing '{}':", migration.name);
213+ println!();
245− for (action_index, action) in migration.actions.iter().enumerate() {
216+ // If a migration failed, we abort all the migrations that were applied
246− // Skip all actions which have already been completed
217+ if let Err(err) = result {
247− if migration_index == starting_migration_index
218+ println!("A migration failed, aborting migrations that have already been applied");
248− && action_index < starting_action_index
253− let description = action.describe();
220+ // Set to the Aborting state. This is to ensure that the failed
254− print!(" + {} ", description);
221+ // migration is fully aborted and nothing is left dangling.
222+ // If the abort is interrupted for any reason, the user can try again
223+ // by running `reshape abort`.
225+ remaining_migrations.clone(),
226+ last_migration_index + 1,
227+ last_action_index + 1,
256− let ctx = MigrationContext::new(migration_index, action_index);
230+ // Abort will only
258− // Update state to indicate that this action has been completed.
233+ return Err(err);
259− // We won't save this new state until after the action has completed.
234+ }
260− self.state.completing(
235+
261− remaining_migrations.clone(),
236+ // Create schema and views for migration
262− migration_index + 1,
237+ let target_migration = remaining_migrations.last().unwrap().name.to_string();
263− action_index + 1,
238+ create_schema_for_migration(db, &target_migration, &new_schema)
239+ .with_context(|| format!("failed to create schema for migration {}", target_migration))?;
241+ // Update state once migrations have been performed
242+ state.in_progress(remaining_migrations);
243+ state.save(db).context("failed to save in-progress state")?;
245+ // If we started from a blank slate, we can finish the migration immediately
246+ if current_migration.is_none() {
247+ println!("Automatically completing migrations\n");
248+ complete(db, state).context("failed to automatically complete migrations")?;
250+ println!("Migrations complete:");
252+ " - Run '{}' from your application to use the latest schema",
253+ schema_query_for_migration(&target_migration)
256+ println!("Migrations have been applied and the new schema is ready for use:");
258+ " - Run '{}' from your application to use the latest schema",
259+ schema_query_for_migration(&target_migration)
262+ " - Run 'reshape complete' once your application has been updated and the previous schema is no longer in use"
266− // This did_save check is necessary because of the borrow checker.
266+ Ok(())
267− // The Transaction which might be returned from action.complete
268− // contains a mutable reference to self.db. We need the Transaction
269− // to be dropped before we can save the state using self.db instead,
270− // which we achieve here by limiting the lifetime of the Transaction
271− // with a new block.
274− .complete(&ctx, &mut self.db)
275− .with_context(|| format!("failed to complete migration {}", migration.name))
276− .with_context(|| format!("failed to complete action: {}", description));
278− let maybe_transaction = match result {
279− Ok(maybe_transaction) => {
280− println!("{}", "done".green());
284− println!("{}", "failed".red());
289− // Update state with which migrations and actions have been completed.
290− // Each action can create and return a transaction if they need atomicity.
291− // We use this transaction to update the state to ensure the action only completes.
293− // We want to use a single transaction for each action to keep the length of
294− // the transaction as short as possible. Wherever possible, we don't want to
295− // use a transaction at all.
296− if let Some(mut transaction) = maybe_transaction {
298− .save(&mut transaction)
299− .context("failed to save state after completing action")?;
302− .context("failed to commit transaction")?;
310− // If the action didn't return a transaction we save the state normally instead
269+fn complete(db: &mut DbConn, state: &mut State) -> anyhow::Result<()> {
311− if !did_save {
270+ // Make sure a migration is in progress
312− self.state
271+ let (remaining_migrations, starting_migration_index, starting_action_index) = match state.clone() {
313− .save(&mut self.db)
272+ State::InProgress { migrations } => {
273+ // Move into the Completing state. Once in this state,
274+ // the migration can't be aborted and must be completed.
275+ state.completing(migrations.clone(), 0, 0);
314− .context("failed to save state after completing action")?;
276+ state.save(db).context("failed to save state")?;
282+ current_migration_index,
283+ current_action_index
284+ } => (migrations, current_migration_index, current_action_index),
285+ State::Aborting { .. } => {
286+ return Err(anyhow!("migration been aborted and can't be completed. Please finish using `reshape abort`."))
288+ State::Applying { .. } => {
289+ return Err(anyhow!("a previous migration unexpectedly failed. Please run `reshape migrate` to try applying the migration again."))
292+ println!("No migration in progress");
318− println!();
297+ // Remove previous migration's schema
298+ if let Some(current_migration) = &state::current_migration(db)? {
300+ "DROP SCHEMA IF EXISTS {} CASCADE",
301+ schema_name_for_migration(current_migration)
303+ .context("failed to remove previous migration's schema")?;
306+ for (migration_index, migration) in remaining_migrations.iter().enumerate() {
307+ // Skip all the migrations which have already been completed
308+ if migration_index < starting_migration_index {
321− // Remove helpers which are no longer in use
312+ println!("Completing '{}':", migration.name);
322− helpers::tear_down_helpers(&mut self.db).context("failed to tear down helpers")?;
324− self.state
314+ for (action_index, action) in migration.actions.iter().enumerate() {
325− .complete(&mut self.db)
315+ // Skip all actions which have already been completed
326− .context("failed to update state as completed")?;
316+ if migration_index == starting_migration_index && action_index < starting_action_index {
328− Ok(())
320+ let description = action.describe();
329− }
321+ print!(" + {} ", description);
331− fn create_schema_for_migration(
323+ let ctx = MigrationContext::new(migration_index, action_index);
333− migration_name: &str,
335− ) -> anyhow::Result<()> {
336− // Create schema for migration
337− let schema_name = schema_name_for_migration(migration_name);
339− .run(&format!("CREATE SCHEMA IF NOT EXISTS {}", schema_name))
342− "failed to create schema {} for migration {}",
343− schema_name, migration_name
347− // Create views inside schema
348− for table in schema.get_tables(&mut self.db)? {
349− Self::create_view_for_table(&mut self.db, &table, &schema_name)?;
352− Ok(())
325+ // Update state to indicate that this action has been completed.
353− }
326+ // We won't save this new state until after the action has completed.
328+ remaining_migrations.clone(),
329+ migration_index + 1,
355− fn create_view_for_table(
333+ // This did_save check is necessary because of the borrow checker.
356− db: &mut impl Conn,
334+ // The Transaction which might be returned from action.complete
357− table: &Table,
335+ // contains a mutable reference to self.db. We need the Transaction
358− schema: &str,
336+ // to be dropped before we can save the state using self.db instead,
359− ) -> anyhow::Result<()> {
337+ // which we achieve here by limiting the lifetime of the Transaction
360− let select_columns: Vec<String> = table
338+ // with a new block.
361− .columns
339+ let did_save = {
362− .iter()
340+ let result = action
363− .map(|column| {
341+ .complete(&ctx, db)
364− format!(
342+ .with_context(|| format!("failed to complete migration {}", migration.name))
365− r#"
343+ .with_context(|| format!("failed to complete action: {}", description));
366− "{real_name}" AS "{alias}"
344+
367− "#,
345+ let maybe_transaction = match result {
368− real_name = column.real_name,
346+ Ok(maybe_transaction) => {
369− alias = column.name,
347+ println!("{}", "done".green());
370− )
348+ maybe_transaction
372− .collect();
350+ Err(e) => {
351+ println!("{}", "failed".red());
374− db.run(&format!(
356+ // Update state with which migrations and actions have been completed.
375− r#"
357+ // Each action can create and return a transaction if they need atomicity.
376− CREATE OR REPLACE VIEW {schema}."{view_name}" AS
358+ // We use this transaction to update the state to ensure the action only completes.
377− SELECT {columns}
359+ // once.
378− FROM "{table_name}"
360+ // We want to use a single transaction for each action to keep the length of
379− "#,
361+ // the transaction as short as possible. Wherever possible, we don't want to
380− schema = schema,
362+ // use a transaction at all.
381− table_name = table.real_name,
363+ if let Some(mut transaction) = maybe_transaction {
382− view_name = table.name,
364+ state
383− columns = select_columns.join(","),
365+ .save(&mut transaction)
384− ))
366+ .context("failed to save state after completing action")?;
385− .with_context(|| format!("failed to create view for table {}", table.name))?;
367+ transaction
369+ .context("failed to commit transaction")?;
390− pub fn remove(&mut self) -> anyhow::Result<()> {
377+ // If the action didn't return a transaction we save the state normally instead
391− // Remove migration schemas and views
378+ if !did_save {
392− if let Some(current_migration) = &state::current_migration(&mut self.db)? {
379+ state
393− self.db.run(&format!(
380+ .save(db)
394− "DROP SCHEMA IF EXISTS {} CASCADE",
381+ .context("failed to save state after completing action")?;
395− schema_name_for_migration(current_migration)
382+ }
399− if let State::InProgress { migrations } = &self.state {
385+ println!();
400− let target_migration = migrations.last().unwrap().name.to_string();
401− self.db.run(&format!(
402− "DROP SCHEMA IF EXISTS {} CASCADE",
403− schema_name_for_migration(&target_migration)
407− // Remove all tables
388+ // Remove helpers which are no longer in use
408− let schema = Schema::new();
389+ helpers::tear_down_helpers(db).context("failed to tear down helpers")?;
409− for table in schema.get_tables(&mut self.db)? {
410− self.db.run(&format!(
412− DROP TABLE IF EXISTS "{}" CASCADE
418− // Reset state
391+ state
419− self.state.clear(&mut self.db)?;
392+ .complete(db)
393+ .context("failed to update state as completed")?;
421− println!("Reshape and all data has been removed");
395+ Ok(())
423− Ok(())
398+fn abort(db: &mut DbConn, state: &mut State) -> anyhow::Result<()> {
424− }
399+ let (remaining_migrations, last_migration_index, last_action_index) = match state.clone() {
400+ State::InProgress { migrations } | State::Applying { migrations } => {
401+ // Set to the Aborting state. Once this is done, the migration has to
402+ // be fully aborted and can't be completed.
403+ state.aborting(migrations.clone(), 0, 0);
426− pub fn abort(&mut self) -> anyhow::Result<()> {
427− let (remaining_migrations, last_migration_index, last_action_index) = match self
431− State::InProgress { migrations } | State::Applying { migrations } => {
432− // Set to the Aborting state. Once this is done, the migration has to
433− // be fully aborted and can't be completed.
434− self.state.aborting(migrations.clone(), 0, 0);
435− self.state.save(&mut self.db)?;
437− (migrations, usize::MAX, usize::MAX)
406+ (migrations, usize::MAX, usize::MAX)
439− State::Aborting {
408+ State::Aborting {
441− last_migration_index,
443− } => (migrations, last_migration_index, last_action_index),
444− State::Completing { .. } => {
445− return Err(anyhow!("Migration completion has already been started. Please run `reshape complete` again to finish it."));
448− println!("No migration is in progress");
453− // Remove new migration's schema
454− let target_migration = remaining_migrations.last().unwrap().name.to_string();
455− let schema_name = schema_name_for_migration(&target_migration);
457− .run(&format!("DROP SCHEMA IF EXISTS {} CASCADE", schema_name,))
458− .with_context(|| format!("failed to drop schema {}", schema_name))?;
460− // Abort all pending migrations
409+ migrations,
461− self.abort_migrations(
462− &remaining_migrations,
463 last_migration_index,
410 last_migration_index,
464 last_action_index,
411 last_action_index,
465− )?;
412+ } => (migrations, last_migration_index, last_action_index),
466−
413+ State::Completing { .. } => {
467− helpers::tear_down_helpers(&mut self.db).context("failed to tear down helpers")?;
414+ return Err(anyhow!("Migration completion has already been started. Please run `reshape complete` again to finish it."));
469− self.state = State::Idle;
416+ State::Idle => {
470− self.state
417+ println!("No migration is in progress");
471− .save(&mut self.db)
418+ return Ok(());
472− .context("failed to save state")?;
419+ }
422+ // Remove new migration's schema
423+ let target_migration = remaining_migrations.last().unwrap().name.to_string();
424+ let schema_name = schema_name_for_migration(&target_migration);
425+ db.run(&format!("DROP SCHEMA IF EXISTS {} CASCADE", schema_name,))
426+ .with_context(|| format!("failed to drop schema {}", schema_name))?;
428+ // Abort all pending migrations
429+ // Abort all migrations in reverse order
430+ for (migration_index, migration) in remaining_migrations.iter().enumerate().rev() {
431+ // Skip migrations which shouldn't be aborted
432+ // The reason can be that they have already been aborted or that
433+ // the migration was never applied in the first place.
434+ if migration_index >= last_migration_index {
474− Ok(())
438+ print!("Aborting '{}' ", migration.name);
477− fn abort_migrations(
479− migrations: &[Migration],
480− upper_migration_index: usize,
481− upper_action_index: usize,
482− ) -> anyhow::Result<()> {
483− // Abort all migrations in reverse order
484− for (migration_index, migration) in migrations.iter().enumerate().rev() {
440+ for (action_index, action) in migration.actions.iter().enumerate().rev() {
485− // Skip migrations which shouldn't be aborted
441+ // Skip actions which shouldn't be aborted
486 // The reason can be that they have already been aborted or that
442 // The reason can be that they have already been aborted or that
487− // the migration was never applied in the first place.
443+ // the action was never applied in the first place.
488− if migration_index >= upper_migration_index {
444+ if migration_index == last_migration_index - 1 && action_index >= last_action_index {
489 continue;
445 continue;
492− print!("Aborting '{}' ", migration.name);
494− for (action_index, action) in migration.actions.iter().enumerate().rev() {
495− // Skip actions which shouldn't be aborted
496− // The reason can be that they have already been aborted or that
497− // the action was never applied in the first place.
498− if migration_index == upper_migration_index - 1
499− && action_index >= upper_action_index
504− let ctx = MigrationContext::new(migration_index, action_index);
448+ let ctx = MigrationContext::new(migration_index, action_index);
506− .abort(&ctx, &mut self.db)
450+ .abort(&ctx, db)
507− .with_context(|| format!("failed to abort migration {}", migration.name))
451+ .with_context(|| format!("failed to abort migration {}", migration.name))
508− .with_context(|| format!("failed to abort action: {}", action.describe()))?;
452+ .with_context(|| format!("failed to abort action: {}", action.describe()))?;
510− // Update state with which migrations and actions have been aborted.
511− // We don't need to run this in a transaction as aborts are idempotent.
513− .aborting(migrations.to_vec(), migration_index, action_index);
516− .context("failed to save state")?;
519− println!("{}", "done".green());
454+ // Update state with which migrations and actions have been aborted.
455+ // We don't need to run this in a transaction as aborts are idempotent.
456+ state.aborting(remaining_migrations.to_vec(), migration_index, action_index);
457+ state.save(db).context("failed to save state")?;
460+ println!("{}", "done".green());
525−pub fn latest_schema_from_migrations(migrations: &[Migration]) -> Option<String> {
463+ helpers::tear_down_helpers(db).context("failed to tear down helpers")?;
527− .last()
465+ *state = State::Idle;
528− .map(|migration| schema_name_for_migration(&migration.name))
466+ state.save(db).context("failed to save state")?;
471+fn create_schema_for_migration(
531−pub fn schema_query_for_migration(migration_name: &str) -> String {
473+ migration_name: &str,
475+) -> anyhow::Result<()> {
476+ // Create schema for migration
532 let schema_name = schema_name_for_migration(migration_name);
477 let schema_name = schema_name_for_migration(migration_name);
533− format!("SET search_path TO {}", schema_name)
478+ db.run(&format!("CREATE SCHEMA IF NOT EXISTS {}", schema_name))
481+ "failed to create schema {} for migration {}",
482+ schema_name, migration_name
486+ // Create views inside schema
487+ for table in schema.get_tables(db)? {
488+ create_view_for_table(db, &table, &schema_name)?;
536−fn schema_name_for_migration(migration_name: &str) -> String {
494+fn create_view_for_table(db: &mut impl Conn, table: &Table, schema: &str) -> anyhow::Result<()> {
537− format!("migration_{}", migration_name)
495+ let select_columns: Vec<String> = table
501+ "{real_name}" AS "{alias}"
503+ real_name = column.real_name,
504+ alias = column.name,
511+ CREATE OR REPLACE VIEW {schema}."{view_name}" AS
516+ table_name = table.real_name,
517+ view_name = table.name,
518+ columns = select_columns.join(","),
520+ .with_context(|| format!("failed to create view for table {}", table.name))?;