All Projects → lfittl → Activerecord Clean Db Structure

lfittl / Activerecord Clean Db Structure

Licence: bsd-3-clause
Automatic cleanup for the Rails db/structure.sql file (ActiveRecord/PostgreSQL)

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Activerecord Clean Db Structure

Pg party
ActiveRecord PostgreSQL Partitioning
Stars: ✭ 294 (+191.09%)
Mutual labels:  activerecord, postgresql, postgres, rails
Activerecord Postgres enum
Integrate PostgreSQL's enum data type into ActiveRecord's schema and migrations.
Stars: ✭ 227 (+124.75%)
Mutual labels:  activerecord, postgresql, postgres, rails
Rein
Database constraints made easy for ActiveRecord.
Stars: ✭ 657 (+550.5%)
Mutual labels:  activerecord, database, postgres, rails
Scenic
Scenic is maintained by Derek Prior, Caleb Hearth, and you, our contributors.
Stars: ✭ 2,856 (+2727.72%)
Mutual labels:  activerecord, database, postgres, rails
Ar Uuid
Override migration methods to support UUID columns without having to be explicit about it.
Stars: ✭ 41 (-59.41%)
Mutual labels:  activerecord, postgresql, postgres, rails
Zero downtime migrations
Zero downtime migrations with ActiveRecord 3+ and PostgreSQL
Stars: ✭ 513 (+407.92%)
Mutual labels:  activerecord, database, postgres, rails
Niklick
Rails Versioned API solution template for hipsters! (Ruby, Ruby on Rails, REST API, GraphQL, Docker, RSpec, Devise, Postgress DB)
Stars: ✭ 39 (-61.39%)
Mutual labels:  database, postgresql, postgres, rails
Prisma
Next-generation ORM for Node.js & TypeScript | PostgreSQL, MySQL, MariaDB, SQL Server, SQLite & MongoDB (Preview)
Stars: ✭ 18,168 (+17888.12%)
Mutual labels:  database, postgresql, postgres
Go Kallax
Kallax is a PostgreSQL typesafe ORM for the Go language.
Stars: ✭ 853 (+744.55%)
Mutual labels:  database, postgresql, postgres
Graphjin
GraphJin - Build APIs in 5 minutes with GraphQL. An instant GraphQL to SQL compiler.
Stars: ✭ 1,264 (+1151.49%)
Mutual labels:  database, postgresql, rails
Activerecord Sqlserver Adapter
SQL Server Adapter For Rails
Stars: ✭ 910 (+800.99%)
Mutual labels:  activerecord, database, rails
Electrocrud
Database CRUD Application Built on Electron | MySQL, Postgres, SQLite
Stars: ✭ 1,267 (+1154.46%)
Mutual labels:  database, postgresql, postgres
Node Pg Migrate
Node.js database migration management for Postgresql
Stars: ✭ 838 (+729.7%)
Mutual labels:  database, postgresql, postgres
Active record doctor
Identify database issues before they hit production.
Stars: ✭ 865 (+756.44%)
Mutual labels:  activerecord, database, rails
Docker Postgres
A docker container running PostgreSQL
Stars: ✭ 22 (-78.22%)
Mutual labels:  database, postgresql, postgres
Awesome Postgres
A curated list of awesome PostgreSQL software, libraries, tools and resources, inspired by awesome-mysql
Stars: ✭ 7,468 (+7294.06%)
Mutual labels:  database, postgresql, postgres
Efcore.pg
Entity Framework Core provider for PostgreSQL
Stars: ✭ 838 (+729.7%)
Mutual labels:  database, postgresql, postgres
Monogamy
Add table-level database locking to ActiveRecord
Stars: ✭ 12 (-88.12%)
Mutual labels:  activerecord, postgresql, postgres
Goqu
SQL builder and query library for golang
Stars: ✭ 984 (+874.26%)
Mutual labels:  database, postgresql, postgres
Pgcli
Postgres CLI with autocompletion and syntax highlighting
Stars: ✭ 9,985 (+9786.14%)
Mutual labels:  database, postgresql, postgres

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].