Automatic cleanup for the Rails db/structure.sql file (ActiveRecord/PostgreSQL)
Clone or download
Latest commit 022237e Mar 12, 2018


Ever been annoyed at a constantly changing db/structure.sql file when using ActiveRecord and Postgres?

Spent hours trying to decipher why that one team member keeps changing the file?

This library is here to help!

It cleans away all the unnecessary output in the file every time its updated automatically. This helps avoid merge conflicts, as well as increase readability.


Add the following to your Gemfile:

gem 'activerecord-clean-db-structure'

This will automatically hook the library into your rake db:migrate task.

Supported Rails versions

Whilst there is no reason this shouldn't work on earlier versions, this has only been tested on Rails 4.2 and newer.

It also assumes you use ActiveRecord with PostgreSQL - other ORMs or databases are not supported.


Currently the library assumes all your id columns are either SERIAL, BIGSERIAL or uuid. It also assumes the id is the primary key.

Multi-column primary keys, as well as tables that don't have id as the primary key are not supported right now, and might lead to wrong output.

Patches and test cases welcome!



Copyright (c) 2017, Lukas Fittl
activerecord-clean-db-structure is licensed under the 3-clause BSD license, see LICENSE file for details.