# Migrations: Check Preconditions and Postconditions

**URL:** https://discourse.diesel.rs/t/migrations-check-preconditions-and-postconditions/155
**Category:** Uncategorized
**Created:** [June 4, 2020, 11:52am UTC](https://discourse.diesel.rs/t/migrations-check-preconditions-and-postconditions/155 "2020-06-04T11:52:49Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![dbrgn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.diesel.rs/dbrgn/32/83_2.png) [@dbrgn](https://discourse.diesel.rs/u/dbrgn)
#### Post date: [June 4, 2020, 11:52am UTC](https://discourse.diesel.rs/t/migrations-check-preconditions-and-postconditions/155/1 "2020-06-04T11:52:49Z")

</div>

I recently wanted to check a precondition from a Diesel migration (I wanted to ensure that a column I’m dropping is actually empty).

Based on [this article](https://medium.com/jw-player-engineering/recipe-verifying-postgresql-schema-migrations-with-plain-sql-6435e431cce1) I managed to come up with the following code:

```sql
-- Ensure that message field is empty
DO $$
BEGIN
    ASSERT (SELECT count(*) FROM broadcasts WHERE message is not null) = 0;
END $$;

-- Drop message column
ALTER TABLE broadcasts DROP COLUMN message;

```

If the precondition fails, the migration is aborted.

```auto
$ diesel migration run
Running migration 2020-06-04-113011_drop-message-field
Executing migration script migrations/2020-06-04-113011_drop-message-field/up.sql
Failed with: assertion failed

```

This is already quite nice, however it might be interesting to formalize this pattern. In [Squitch](https://sqitch.org/docs/manual/sqitchtutorial/) there seems to be a “verify”-step that ensures that a migration was successful.

What if Diesel would support optional `pre-check.sql` and `post-check.sql` scripts (next to `up.sql` and `down.sql`), to check preconditions and postconditions?

If a precondition is not met (i.e. raises an error), the migration is aborted. If a postcondition is not met, either the migration chain could be aborted with an error, or a rollback could be attempted.
