# ActiveRecord migrations in a single transaction?

**URL:** <https://discuss.rubyonrails.org/t/activerecord-migrations-in-a-single-transaction/3482>\
**Category:** rubyonrails-talk\
**Created:** [October 23, 2006, 10:32pm UTC](https://discuss.rubyonrails.org/t/activerecord-migrations-in-a-single-transaction/3482 "2006-10-23T22:32:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stuart\_Grimshaw](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@Stuart\_Grimshaw](https://discuss.rubyonrails.org/u/Stuart_Grimshaw)\
**Post date:** [October 23, 2006, 10:32pm UTC](https://discuss.rubyonrails.org/t/activerecord-migrations-in-a-single-transaction/3482/1 "2006-10-23T22:32:27Z")

</div>

Is it possible to make Migrations occur in a single transaction, so that if the rake fails, it doesn't leave the database in a broken state, with half the changes made, and the other half incomplete?

Obviously, this depends on the database itself supporting transactions, but I'm using Postgres.

---

<div class="post-metadata">

**Author:** ![Jim\_Morris1](https://avatars.discourse-cdn.com/v4/letter/j/f4b2a3/32.png) [@Jim\_Morris1](https://discuss.rubyonrails.org/u/Jim_Morris1)\
**Post date:** [October 24, 2006, 12:13am UTC](https://discuss.rubyonrails.org/t/activerecord-migrations-in-a-single-transaction/3482/2 "2006-10-24T00:13:50Z")

</div>

Stuart Grimshaw wrote:

> Is it possible to make Migrations occur in a single transaction, so that if the rake fails, it doesn't leave the database in a broken state, with half the changes made, and the other half incomplete?
> 
> Obviously, this depends on the database itself supporting transactions, but I'm using Postgres.
> 
> \>

I wrap my migrations in a transaction using postgresql, and it seems to work fine.

---

<div class="post-metadata">

**Author:** ![Just\_Someone](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@Just\_Someone](https://discuss.rubyonrails.org/u/Just_Someone)\
**Post date:** [October 24, 2006, 12:18am UTC](https://discuss.rubyonrails.org/t/activerecord-migrations-in-a-single-transaction/3482/3 "2006-10-24T00:18:18Z")

</div>

Me too.

But you need a database backend that will transact DDL changes. Not all do. Postgres for sure does, and now I don't have any half-made transactions anymore.

I can give you more details if needed.

Bye,

Guy.

---

<div class="post-metadata">

**Author:** ![Sava\_Chankov](https://avatars.discourse-cdn.com/v4/letter/s/ed8c4c/32.png) [@Sava\_Chankov](https://discuss.rubyonrails.org/u/Sava_Chankov)\
**Post date:** [October 25, 2006, 12:05pm UTC](https://discuss.rubyonrails.org/t/activerecord-migrations-in-a-single-transaction/3482/4 "2006-10-25T12:05:35Z")

</div>

Stuart Grimshaw wrote:

> Is it possible to make Migrations occur in a single transaction, so that if the rake fails, it doesn't leave the database in a broken state, with half the changes made, and the other half incomplete?
> 
> Obviously, this depends on the database itself supporting transactions, but I'm using Postgres.

The easiest way for you is to use transactional\_migrations plugin. Thus you won't need to put explicit BEGIN and COMMIT DDL statements in migrations.

[http://www.redhillconsulting.com.au/rails\_plugins.html#transactional\_migrations](http://www.redhillconsulting.com.au/rails_plugins.html#transactional_migrations)

---

<div class="post-metadata">

**Author:** ![jmurillo](https://avatars.discourse-cdn.com/v4/letter/j/a5b964/32.png) [@jmurillo](https://discuss.rubyonrails.org/u/jmurillo)\
**Post date:** [November 1, 2006, 11:06pm UTC](https://discuss.rubyonrails.org/t/activerecord-migrations-in-a-single-transaction/3482/5 "2006-11-01T23:06:42Z")

</div>

Hello: You wrote in another thread, now outdated, about a way for getting decimal support for ActiveRecord, in version 1.1.6. Is there a way of having that code? Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Stuart\_Grimshaw](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@Stuart\_Grimshaw](https://discuss.rubyonrails.org/u/Stuart_Grimshaw)\
**Post date:** [November 7, 2006, 11:46pm UTC](https://discuss.rubyonrails.org/t/activerecord-migrations-in-a-single-transaction/3482/6 "2006-11-07T23:46:30Z")

</div>

Thanks Sava, this is what I went for in the end ...
