---
title: "Create idempotent migrations"
description: "When you want to write destructive migrations please use if\\exists: true when you’re trying to remove a table and check for a table existence when you want to add or remove a column…"
canonical: https://oozou.com/til/create-idempotent-migrations-28
locale: en
date: 2020-10-13
topic: ruby-on-rails
---

# Create idempotent migrations

> When you want to write destructive migrations please use if\exists: true when you’re trying to remove a table and check for a table existence when you want to add or remove a column…

Published: 2020-10-13 · Topic: [ruby on rails](https://oozou.com/til-topics/ruby-on-rails) ([markdown](https://oozou.com/til-topics/ruby-on-rails.md))

When you want to write destructive migrations please use *if\_exists: true* when you’re trying to remove a table and check for a table existence when you want to add or remove a column *ActiveRecord::Base.connection.table\_exists?*

Case 1:

```ruby
def up
  drop_table :kittens, if_exists: true
end
```

Case 2:

```ruby
def up
  return unless ActiveRecord::Base.connection.table_exists?(:kittens)
  add_column :kittens, :kind, :integer
end
```

---

Contact OOZOU: hello@oozou.com · +66 2544 0000 · https://oozou.com/contact

More for AI agents: https://oozou.com/llms.txt · every page has a markdown twin at its URL plus ".md"
