2 migrations::{Migration, MigrationContext},
6use anyhow::{anyhow, Context};
8use db::{Conn, DbConn, DbLocker};
18pub use crate::state::State;
25 pub fn new(connection_string: &str) -> anyhow::Result<Reshape> {
26 let config: Config = connection_string.parse()?;
27 Self::new_with_config(&config)
30 pub fn new_with_options(
36 ) -> anyhow::Result<Reshape> {
37 let mut config = Config::new();
45 Self::new_with_config(&config)
48 fn new_with_config(config: &Config) -> anyhow::Result<Reshape> {
49 let db = DbLocker::connect(config)?;
55 migrations: impl IntoIterator<Item = Migration>,
56 ) -> anyhow::Result<()> {
58 let mut state = State::load(db)?;
59 migrate(db, &mut state, migrations)
63 pub fn complete(&mut self) -> anyhow::Result<()> {
65 let mut state = State::load(db)?;
66 complete(db, &mut state)
70 pub fn abort(&mut self) -> anyhow::Result<()> {
72 let mut state = State::load(db)?;
77 pub fn remove(&mut self) -> anyhow::Result<()> {
79 let mut state = State::load(db)?;
81 // Remove migration schemas and views
82 if let Some(current_migration) = &state::current_migration(db)? {
84 "DROP SCHEMA IF EXISTS {} CASCADE",
85 schema_name_for_migration(current_migration)
89 if let State::InProgress { migrations } = &state {
90 let target_migration = migrations.last().unwrap().name.to_string();
92 "DROP SCHEMA IF EXISTS {} CASCADE",
93 schema_name_for_migration(&target_migration)
98 let schema = Schema::new();
99 for table in schema.get_tables(db)? {
102 DROP TABLE IF EXISTS "{}" CASCADE
109 let enums: Vec<String> = db
110 .query("SELECT typname FROM pg_type WHERE typcategory = 'E'")?
112 .map(|row| row.get("typname"))
114 for enum_type in enums {
115 db.run(&format!("DROP TYPE {}", enum_type))?;
121 println!("Reshape and all data has been removed");
128pub fn latest_schema_from_migrations(migrations: &[Migration]) -> Option<String> {
131 .map(|migration| schema_name_for_migration(&migration.name))
134pub fn schema_query_for_migration(migration_name: &str) -> String {
135 let schema_name = schema_name_for_migration(migration_name);
136 format!("SET search_path TO {}", schema_name)
139fn schema_name_for_migration(migration_name: &str) -> String {
140 format!("migration_{}", migration_name)
146 migrations: impl IntoIterator<Item = Migration>,
147) -> anyhow::Result<()> {
148 // Make sure no migration is in progress
149 if let State::InProgress { .. } = &state {
151 "Migration already in progress, please complete using 'reshape migration complete'"
156 if let State::Completing { .. } = &state {
158 "Migration already in progress and has started completion, please finish using 'reshape migration complete'"
163 // Determine which migrations need to be applied by comparing the provided migrations
164 // with the already applied ones stored in the state. This will throw an error if the
165 // two sets of migrations don't agree, for example if a new migration has been added
166 // in between two existing ones.
167 let remaining_migrations = state::remaining_migrations(db, migrations)?;
168 if remaining_migrations.is_empty() {
169 println!("No migrations left to apply");
173 // If we have already started applying some migrations we need to ensure that
174 // they are the same ones we want to apply now
175 if let State::Applying {
176 migrations: existing_migrations,
179 if existing_migrations != &remaining_migrations {
181 "a previous migration seems to have failed without cleaning up. Please run `reshape migration abort` and then run migrate again."
186 // Move to the "Applying" state which is necessary as we can't run the migrations
187 // and state update as a single transaction. If a migration unexpectedly fails without
188 // automatically aborting, this state saves us from dangling migrations. It forces the user
189 // to either run migrate again (which works as all migrations are idempotent) or abort.
190 state.applying(remaining_migrations.clone());
193 println!("Applying {} migrations\n", remaining_migrations.len());
195 let target_migration = remaining_migrations.last().unwrap().name.to_string();
196 helpers::set_up_helpers(db, &target_migration).context("failed to set up helpers")?;
198 let mut new_schema = Schema::new();
199 let mut last_migration_index = usize::MAX;
200 let mut last_action_index = usize::MAX;
201 let mut result: anyhow::Result<()> = Ok(());
203 'outer: for (migration_index, migration) in remaining_migrations.iter().enumerate() {
204 println!("Migrating '{}':", migration.name);
205 last_migration_index = migration_index;
207 for (action_index, action) in migration.actions.iter().enumerate() {
208 last_action_index = action_index;
210 let description = action.describe();
211 print!(" + {} ", description);
214 MigrationContext::new(migration_index, action_index, state::current_migration(db)?);
216 .run(&ctx, db, &new_schema)
217 .with_context(|| format!("failed to {}", description));
220 action.update_schema(&ctx, &mut new_schema);
221 println!("{}", "done".green());
223 println!("{}", "failed".red());
231 // If a migration failed, we abort all the migrations that were applied
232 if let Err(err) = result {
233 println!("A migration failed, aborting migrations that have already been applied");
235 // Set to the Aborting state. This is to ensure that the failed
236 // migration is fully aborted and nothing is left dangling.
237 // If the abort is interrupted for any reason, the user can try again
238 // by running `reshape migration abort`.
240 remaining_migrations.clone(),
241 last_migration_index + 1,
242 last_action_index + 1,
251 // Create schema and views for migration
252 create_schema_for_migration(db, &target_migration, &new_schema)
253 .with_context(|| format!("failed to create schema for migration {}", target_migration))?;
255 // Update state once migrations have been performed
256 state.in_progress(remaining_migrations);
257 state.save(db).context("failed to save in-progress state")?;
259 println!("Migrations have been applied and the new schema is ready for use:");
261 " - Run '{}' from your application to use the latest schema",
262 schema_query_for_migration(&target_migration)
265 " - Run 'reshape migration complete' once your application has been updated and the previous schema is no longer in use"
270fn complete(db: &mut DbConn, state: &mut State) -> anyhow::Result<()> {
271 // Make sure a migration is in progress
272 let (remaining_migrations, starting_migration_index, starting_action_index) = match state.clone() {
273 State::InProgress { migrations } => {
274 // Move into the Completing state. Once in this state,
275 // the migration can't be aborted and must be completed.
276 state.completing(migrations.clone(), 0, 0);
277 state.save(db).context("failed to save state")?;
283 current_migration_index,
285 } => (migrations, current_migration_index, current_action_index),
286 State::Aborting { .. } => {
287 return Err(anyhow!("migration been aborted and can't be completed. Please finish using `reshape migration abort`."))
289 State::Applying { .. } => {
290 return Err(anyhow!("a previous migration unexpectedly failed. Please run `reshape migrate` to try applying the migration again."))
293 println!("No migration in progress");
298 // Remove previous migration's schema
299 if let Some(current_migration) = &state::current_migration(db)? {
301 "DROP SCHEMA IF EXISTS {} CASCADE",
302 schema_name_for_migration(current_migration)
304 .context("failed to remove previous migration's schema")?;
307 for (migration_index, migration) in remaining_migrations.iter().enumerate() {
308 // Skip all the migrations which have already been completed
309 if migration_index < starting_migration_index {
313 println!("Completing '{}':", migration.name);
315 for (action_index, action) in migration.actions.iter().enumerate() {
316 // Skip all actions which have already been completed
317 if migration_index == starting_migration_index && action_index < starting_action_index {
321 let description = action.describe();
322 print!(" + {} ", description);
325 MigrationContext::new(migration_index, action_index, state::current_migration(db)?);
327 // Update state to indicate that this action has been completed.
328 // We won't save this new state until after the action has completed.
330 remaining_migrations.clone(),
335 // This did_save check is necessary because of the borrow checker.
336 // The Transaction which might be returned from action.complete
337 // contains a mutable reference to self.db. We need the Transaction
338 // to be dropped before we can save the state using self.db instead,
339 // which we achieve here by limiting the lifetime of the Transaction
344 .with_context(|| format!("failed to complete migration {}", migration.name))
345 .with_context(|| format!("failed to complete action: {}", description));
347 let maybe_transaction = match result {
348 Ok(maybe_transaction) => {
349 println!("{}", "done".green());
353 println!("{}", "failed".red());
358 // Update state with which migrations and actions have been completed.
359 // Each action can create and return a transaction if they need atomicity.
360 // We use this transaction to update the state to ensure the action only completes.
362 // We want to use a single transaction for each action to keep the length of
363 // the transaction as short as possible. Wherever possible, we don't want to
364 // use a transaction at all.
365 if let Some(mut transaction) = maybe_transaction {
367 .save(&mut transaction)
368 .context("failed to save state after completing action")?;
371 .context("failed to commit transaction")?;
379 // If the action didn't return a transaction we save the state normally instead
383 .context("failed to save state after completing action")?;
390 // Remove helpers which are no longer in use
391 helpers::tear_down_helpers(db).context("failed to tear down helpers")?;
395 .context("failed to update state as completed")?;
400fn abort(db: &mut DbConn, state: &mut State) -> anyhow::Result<()> {
401 let (remaining_migrations, last_migration_index, last_action_index) = match state.clone() {
402 State::InProgress { migrations } | State::Applying { migrations } => {
403 // Set to the Aborting state. Once this is done, the migration has to
404 // be fully aborted and can't be completed.
405 state.aborting(migrations.clone(), 0, 0);
408 (migrations, usize::MAX, usize::MAX)
412 last_migration_index,
414 } => (migrations, last_migration_index, last_action_index),
415 State::Completing { .. } => {
416 return Err(anyhow!("migration completion has already been started. Please run `reshape migration complete` again to finish it."));
419 println!("No migration is in progress");
424 // Remove new migration's schema
425 let target_migration = remaining_migrations.last().unwrap().name.to_string();
426 let schema_name = schema_name_for_migration(&target_migration);
427 db.run(&format!("DROP SCHEMA IF EXISTS {} CASCADE", schema_name,))
428 .with_context(|| format!("failed to drop schema {}", schema_name))?;
430 // Abort all pending migrations
431 // Abort all migrations in reverse order
432 for (migration_index, migration) in remaining_migrations.iter().enumerate().rev() {
433 // Skip migrations which shouldn't be aborted
434 // The reason can be that they have already been aborted or that
435 // the migration was never applied in the first place.
436 if migration_index >= last_migration_index {
440 print!("Aborting '{}' ", migration.name);
442 for (action_index, action) in migration.actions.iter().enumerate().rev() {
443 // Skip actions which shouldn't be aborted
444 // The reason can be that they have already been aborted or that
445 // the action was never applied in the first place.
446 if migration_index == last_migration_index - 1 && action_index >= last_action_index {
451 MigrationContext::new(migration_index, action_index, state::current_migration(db)?);
454 .with_context(|| format!("failed to abort migration {}", migration.name))
455 .with_context(|| format!("failed to abort action: {}", action.describe()))?;
457 // Update state with which migrations and actions have been aborted.
458 // We don't need to run this in a transaction as aborts are idempotent.
459 state.aborting(remaining_migrations.to_vec(), migration_index, action_index);
460 state.save(db).context("failed to save state")?;
463 println!("{}", "done".green());
466 helpers::tear_down_helpers(db).context("failed to tear down helpers")?;
468 *state = State::Idle;
469 state.save(db).context("failed to save state")?;
474fn create_schema_for_migration(
476 migration_name: &str,
478) -> anyhow::Result<()> {
479 // Create schema for migration
480 let schema_name = schema_name_for_migration(migration_name);
481 db.run(&format!("CREATE SCHEMA IF NOT EXISTS {}", schema_name))
484 "failed to create schema {} for migration {}",
485 schema_name, migration_name
489 // Create views inside schema
490 for table in schema.get_tables(db)? {
491 create_view_for_table(db, &table, &schema_name)?;
497fn create_view_for_table(db: &mut impl Conn, table: &Table, schema: &str) -> anyhow::Result<()> {
498 let select_columns: Vec<String> = table
504 "{real_name}" AS "{alias}"
506 real_name = column.real_name,
514 CREATE OR REPLACE VIEW {schema}."{view_name}" AS
519 table_name = table.real_name,
520 view_name = table.name,
521 columns = select_columns.join(","),
523 .with_context(|| format!("failed to create view for table {}", table.name))?;
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list