StructureSqlFormatter - clean away all the unnecessary output in structure.sql every time you run mix format

Hi everyone,

I wrote a small formatter to make diffs of structure.sql for postgres more readable.

StructureSqlFormatter
GitHub

Context: I’m a fan of committing structure.sql together with the pull request. If working with a team, there are sometimes merge conflicts, and in general it can be hard to read the changes for reviewers. The formatter aims to fix it and produce a more clean file.

It removes all the unneeded things, for example the lines with specific postgres version (because developers often have different versions)

Example:

--- Dumped from database version 14.5
--- Dumped by pg_dump version 16.1

Or removes unneeded comments:
Before:

--
-- Name: public; Type: SCHEMA; Schema: -; Owner: -
--

After

-- Name: public; Type: SCHEMA

Also it changes the lines with the list of applied migrations:

-
-INSERT INTO public."schema_migrations" (version) VALUES (20231213193252);
-INSERT INTO public."schema_migrations" (version) VALUES (20231213194556);
-INSERT INTO public."schema_migrations" (version) VALUES (20231213205552);
-INSERT INTO public."schema_migrations" (version) VALUES (20231214132216);
-INSERT INTO public."schema_migrations" (version) VALUES (20231214162040);
-INSERT INTO public."schema_migrations" (version) VALUES (20231214165232);
+
+INSERT INTO public."schema_migrations" (version) VALUES
+ (20231213193252)
+,(20231213194556)
+,(20231213205552)
+,(20231214132216)
+,(20231214162040)
+,(20231214165232)
+;

Then for each migration there will only be a diff of 1 added line.

All options are configurable too!

It is inspired by lfittl/activerecord-clean-db-structure, I mostly reimplemented the regexpes in Elixir and changed some small things here and there.

6 Likes

This is very useful, thanks for making it.

1 Like