# Are migrations executed within a transaction?

**URL:** <https://www.ruby-forum.com/t/are-migrations-executed-within-a-transaction/59890>\
**Category:** Rails\
**Created:** [January 6, 2006, 10:49pm UTC](https://www.ruby-forum.com/t/are-migrations-executed-within-a-transaction/59890 "2006-01-06T22:49:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 6, 2006, 10:49pm UTC](https://www.ruby-forum.com/t/are-migrations-executed-within-a-transaction/59890/1 "2006-01-06T22:49:12Z")

</div>

Hi all,

Can I safely assume that each migration is excuted within a  
transaction, so that’s it’s safe to have migrations executing failing  
statements?

Thanks,

- Rowan

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 7, 2006, 12:43am UTC](https://www.ruby-forum.com/t/are-migrations-executed-within-a-transaction/59890/2 "2006-01-07T00:43:33Z")

</div>

Hi,

On 1/6/06, BigSmoke [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Can I safely assume that each migration is excuted within a  
> transaction, so that’s it’s safe to have migrations executing failing  
> statements?

Migrations aren’t run inside a transaction because they can’t be.  
None of the databases supported by Rails are able to perform DDL  
operations (CREATE TABLE, ALTER TABLE, etc.) inside a transaction  
(some raise errors, the others silently fail).

Always run your migrations on test data first!

–  
sam

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 7, 2006, 2:25am UTC](https://www.ruby-forum.com/t/are-migrations-executed-within-a-transaction/59890/3 "2006-01-07T02:25:49Z")

</div>

On 1/7/06, Sam S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> (some raise errors, the others silently fail).  
> I always had the impression that this did work in my Postgres  
> installation. Perhaps this could be because I created the schema’s in  
> the same transaction as in which I created the tables, views and  
> functions?

> Always run your migrations on test data first!

No worries! I was going to do that anyway 🙂 It can’t be pointed out  
often enough, though. I just thought it would be an easy way to undo  
incomplete migrations in case of errors. Luckily, now that I’ll use  
migrations to load the initial (schema) data, reloading all my schemas  
if something goes wrong will be less of a pain. AR makes life a whole  
lot easier again 🙂

- Rowan

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 7, 2006, 11:14am UTC](https://www.ruby-forum.com/t/are-migrations-executed-within-a-transaction/59890/4 "2006-01-07T11:14:27Z")

</div>

Sam S. wrote:  
\> Migrations aren’t run inside a transaction because they can’t be.  
\> None of the databases supported by Rails are able to perform DDL  
\> operations (CREATE TABLE, ALTER TABLE, etc.) inside a transaction

Additionally, migration can do more than DDL, even non-DB related  
operations (like generating a scaffold, executing plain ruby code,…).  
So, the DB part of the migration can work fine, while the rest failed.  
rolling back in that case can be tricky. My workaround is to write many  
smaller migrations, instead of a big one.

Alain

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 11, 2006, 5:41pm UTC](https://www.ruby-forum.com/t/are-migrations-executed-within-a-transaction/59890/5 "2006-01-11T17:41:09Z")

</div>

Sam S. wrote:

> Migrations aren’t run inside a transaction because they can’t be.  
> None of the databases supported by Rails are able to perform DDL  
> operations (CREATE TABLE, ALTER TABLE, etc.) inside a transaction  
> (some raise errors, the others silently fail).

I just ran a test on my postgres DB:

sicirec\_dev=# select \* from investment\_forms;  
id | name\_id  
----±--------  
1 | 34  
2 | 35  
(2 rows)

sicirec\_dev=# BEGIN;  
BEGIN  
sicirec\_dev=# alter table investment\_forms add column foobar INTEGER;  
ALTER TABLE  
sicirec\_dev=# select \* from investment\_forms;  
id | name\_id | foobar  
----±--------±-------  
1 | 34 |  
2 | 35 |  
(2 rows)

sicirec\_dev=# ROLLBACK;  
ROLLBACK  
sicirec\_dev=# select \* from investment\_forms;  
id | name\_id  
----±--------  
1 | 34  
2 | 35  
(2 rows)

So, it does seem to work. Am I being too simple with my alter table  
perhaps?
