stripe / pg-schema-diff

Go library for diffing Postgres schemas and generating SQL migrations
MIT License
278 stars 20 forks source link

Add DOT encoding support for internal/graph #70

Closed kevinmingtarja closed 10 months ago

kevinmingtarja commented 10 months ago

Description

This PR adds support for encoding the dependency graph to DOT format, which is used by Graphviz, a popular graph visualization software. This will allow us to visualize our dependency graphs for debugging purposes.

Motivation

Closes #38

Testing

Example output:

I have a fresh new postgres instance and my target schema is this:

CREATE TABLE foobar(
    id INT,
    foo VARCHAR(255),
    bar TEXT,
    fizz INT,
    PRIMARY KEY (foo, id)
) PARTITION BY LIST (foo);
CREATE TABLE foobar_1 PARTITION OF foobar FOR VALUES IN ('foo_1');
CREATE TABLE foobar_2 PARTITION OF foobar FOR VALUES IN ('foo_2');
CREATE TABLE foobar_3 PARTITION OF foobar FOR VALUES IN ('foo_3');

Based on the diffs and dependency graph, The DOT encoding will output:

digraph G {
fontname="Helvetica,Arial,sans-serif"
node [fontname="Helvetica,Arial,sans-serif"]
edge [fontname="Helvetica,Arial,sans-serif"]
n0 [label="ADDALTER_table_foobar"]
n1 [label="ADDALTER_table_foobar_2"]
n2 [label="DELETE_table_foobar_2"]
n3 [label="ADDALTER_table_foobar_3"]
n4 [label="DELETE_table_foobar_3"]
n5 [label="DELETE_table_foobar"]
n6 [label="ADDALTER_table_foobar_1"]
n7 [label="DELETE_table_foobar_1"]
n0 -> n1
n0 -> n3
n0 -> n6
n2 -> n1
n4 -> n3
n5 -> n0
n7 -> n6
}
Screen Shot 2023-09-06 at 17 54 51
CLAassistant commented 10 months ago

CLA assistant check
All committers have signed the CLA.

kevinmingtarja commented 10 months ago

Thanks for the feedback @bplunkett-stripe ! I just pushed my changes based on the review.

kevinmingtarja commented 10 months ago

I just did make lint_fix. It should be good to go now!

bplunkett-stripe commented 10 months ago

Nice! Let's hop back onto the ticket to figure out the structure of the CLI for this