Giter VIP home page Giter VIP logo

activerecord-clean-db-structure's Introduction

activerecord-clean-db-structure

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.

Installation

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.

Caveats

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.

You can disable this part of the cleaning process in your config/environments/<environment>.rb (or config/application.rb):

Rails.application.configure do
  config.activerecord_clean_db_structure.ignore_ids = true
end

Other options

You can optionally have indexes following the respective tables setting indexes_after_tables:

Rails.application.configure do
  config.activerecord_clean_db_structure.indexes_after_tables = true
end

When it is enabled the structure looks like this:

CREATE TABLE public.users (
    id SERIAL PRIMARY KEY,
    tenant_id integer,
    email text NOT NULL
);

CREATE INDEX index_users_on_tentant_id ON public.users USING btree (tenant_id);
CREATE UNIQUE INDEX index_users_on_email ON public.users USING btree (email);

To enable sorting the table column definitions alphabetically, discarding the actual order provided by pg_dump, set order_column_definitions:

Rails.application.configure do
  config.activerecord_clean_db_structure.order_column_definitions = true
end

You can have the schema_migrations values reorganized to prevent merge conflicts by setting order_schema_migrations_values:

Rails.application.configure do
  config.activerecord_clean_db_structure.order_schema_migrations_values = true
end

When it is enabled the values are ordered chronological and the semicolon is placed on a separate line:

INSERT INTO "schema_migrations" (version) VALUES
 ('20190503120501')
,('20190508123941')
,('20190508132644')
;

Authors

License

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

activerecord-clean-db-structure's People

Contributors

clemens avatar ermolaev avatar giovannibonetti avatar lfittl avatar nvanoorschot avatar palkan avatar ream88 avatar woodhull avatar xronos-i-am avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.