From 212b7a9448fa3d7dbab02c040594b522358a4d58 Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Sat, 17 Dec 2022 17:46:52 -0800 Subject: [PATCH 1/9] add sql_table_overflow regression test --- e2etests/regression_test.go | 19 +++ .../sql_table_overflow/dagre/board.exp.json | 110 ++++++++++++++++++ .../sql_table_overflow/dagre/sketch.exp.svg | 39 +++++++ .../sql_table_overflow/elk/board.exp.json | 110 ++++++++++++++++++ .../sql_table_overflow/elk/sketch.exp.svg | 39 +++++++ 5 files changed, 317 insertions(+) create mode 100644 e2etests/testdata/regression/sql_table_overflow/dagre/board.exp.json create mode 100644 e2etests/testdata/regression/sql_table_overflow/dagre/sketch.exp.svg create mode 100644 e2etests/testdata/regression/sql_table_overflow/elk/board.exp.json create mode 100644 e2etests/testdata/regression/sql_table_overflow/elk/sketch.exp.svg diff --git a/e2etests/regression_test.go b/e2etests/regression_test.go index b9c2399f1..7df5efff3 100644 --- a/e2etests/regression_test.go +++ b/e2etests/regression_test.go @@ -52,6 +52,25 @@ foobar: { } foo -> foobar`, }, + { + name: "sql_table_overflow", + script: ` +table: sql_table_overflow { + shape: sql_table + short: loooooooooooooooooooong + loooooooooooooooooooong: short +} +table_constrained: sql_table_constrained_overflow { + shape: sql_table + short: loooooooooooooooooooong { + constraint: unique + } + loooooooooooooooooooong: short { + constraint: foreign_key + } +} +`, + }, } runa(t, tcs) diff --git a/e2etests/testdata/regression/sql_table_overflow/dagre/board.exp.json b/e2etests/testdata/regression/sql_table_overflow/dagre/board.exp.json new file mode 100644 index 000000000..c4c92edda --- /dev/null +++ b/e2etests/testdata/regression/sql_table_overflow/dagre/board.exp.json @@ -0,0 +1,110 @@ +{ + "name": "", + "shapes": [ + { + "id": "table", + "type": "sql_table", + "pos": { + "x": 0, + "y": 0 + }, + "width": 387, + "height": 108, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "#FFFFFF", + "stroke": "#0A0F25", + "shadow": false, + "3d": false, + "multiple": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": [ + { + "name": "short", + "type": "loooooooooooooooooooong", + "constraint": "", + "reference": "" + }, + { + "name": "loooooooooooooooooooong", + "type": "short", + "constraint": "", + "reference": "" + } + ], + "label": "sql_table_overflow", + "fontSize": 20, + "fontFamily": "DEFAULT", + "language": "", + "color": "#0A0F25", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 211, + "labelHeight": 36, + "zIndex": 0, + "level": 1 + }, + { + "id": "table_constrained", + "type": "sql_table", + "pos": { + "x": 447, + "y": 0 + }, + "width": 486, + "height": 108, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "#FFFFFF", + "stroke": "#0A0F25", + "shadow": false, + "3d": false, + "multiple": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": [ + { + "name": "short", + "type": "loooooooooooooooooooong", + "constraint": "unique", + "reference": "" + }, + { + "name": "loooooooooooooooooooong", + "type": "short", + "constraint": "foreign_key", + "reference": "" + } + ], + "label": "sql_table_constrained_overflow", + "fontSize": 20, + "fontFamily": "DEFAULT", + "language": "", + "color": "#0A0F25", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 350, + "labelHeight": 36, + "zIndex": 0, + "level": 1 + } + ], + "connections": [] +} diff --git a/e2etests/testdata/regression/sql_table_overflow/dagre/sketch.exp.svg b/e2etests/testdata/regression/sql_table_overflow/dagre/sketch.exp.svg new file mode 100644 index 000000000..5e016ed19 --- /dev/null +++ b/e2etests/testdata/regression/sql_table_overflow/dagre/sketch.exp.svg @@ -0,0 +1,39 @@ + +sql_table_overflowshort +loooooooooooooooooooong +loooooooooooooooooooong +short +sql_table_constrained_overflowshort +loooooooooooooooooooong +UNQloooooooooooooooooooong +short +FK + + + \ No newline at end of file diff --git a/e2etests/testdata/regression/sql_table_overflow/elk/board.exp.json b/e2etests/testdata/regression/sql_table_overflow/elk/board.exp.json new file mode 100644 index 000000000..d97bf998a --- /dev/null +++ b/e2etests/testdata/regression/sql_table_overflow/elk/board.exp.json @@ -0,0 +1,110 @@ +{ + "name": "", + "shapes": [ + { + "id": "table", + "type": "sql_table", + "pos": { + "x": 12, + "y": 12 + }, + "width": 387, + "height": 108, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "#FFFFFF", + "stroke": "#0A0F25", + "shadow": false, + "3d": false, + "multiple": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": [ + { + "name": "short", + "type": "loooooooooooooooooooong", + "constraint": "", + "reference": "" + }, + { + "name": "loooooooooooooooooooong", + "type": "short", + "constraint": "", + "reference": "" + } + ], + "label": "sql_table_overflow", + "fontSize": 20, + "fontFamily": "DEFAULT", + "language": "", + "color": "#0A0F25", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 211, + "labelHeight": 36, + "zIndex": 0, + "level": 1 + }, + { + "id": "table_constrained", + "type": "sql_table", + "pos": { + "x": 419, + "y": 12 + }, + "width": 486, + "height": 108, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "#FFFFFF", + "stroke": "#0A0F25", + "shadow": false, + "3d": false, + "multiple": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": [ + { + "name": "short", + "type": "loooooooooooooooooooong", + "constraint": "unique", + "reference": "" + }, + { + "name": "loooooooooooooooooooong", + "type": "short", + "constraint": "foreign_key", + "reference": "" + } + ], + "label": "sql_table_constrained_overflow", + "fontSize": 20, + "fontFamily": "DEFAULT", + "language": "", + "color": "#0A0F25", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 350, + "labelHeight": 36, + "zIndex": 0, + "level": 1 + } + ], + "connections": [] +} diff --git a/e2etests/testdata/regression/sql_table_overflow/elk/sketch.exp.svg b/e2etests/testdata/regression/sql_table_overflow/elk/sketch.exp.svg new file mode 100644 index 000000000..1bb059b9e --- /dev/null +++ b/e2etests/testdata/regression/sql_table_overflow/elk/sketch.exp.svg @@ -0,0 +1,39 @@ + +sql_table_overflowshort +loooooooooooooooooooong +loooooooooooooooooooong +short +sql_table_constrained_overflowshort +loooooooooooooooooooong +UNQloooooooooooooooooooong +short +FK + + + \ No newline at end of file From 75a21435caaea6d6ede091012d7a2a3754c36456 Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Sat, 17 Dec 2022 18:23:02 -0800 Subject: [PATCH 2/9] fix sql_table width measurement --- d2graph/d2graph.go | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/d2graph/d2graph.go b/d2graph/d2graph.go index 528df55c1..460e60ecd 100644 --- a/d2graph/d2graph.go +++ b/d2graph/d2graph.go @@ -995,23 +995,30 @@ func (g *Graph) SetDimensions(mtexts []*d2target.MText, ruler *textmeasure.Ruler obj.Width = float64(maxWidth + 100) case d2target.ShapeSQLTable: - maxWidth := dims.Width + maxNameWidth := 0. + maxTypeWidth := 0. + constraintWidth := 0. + font := d2fonts.SourceSansPro.Font(d2fonts.FONT_SIZE_L, d2fonts.FONT_STYLE_REGULAR) for _, c := range obj.SQLTable.Columns { - cdims := getTextDimensions(mtexts, ruler, c.Text()) - if cdims == nil { - return fmt.Errorf("dimensions for column %#v not found", c.Text()) + nameWidth, _ := ruler.MeasurePrecise(font, c.Name) + if maxNameWidth < nameWidth { + maxNameWidth = nameWidth } - lineWidth := cdims.Width - if maxWidth < lineWidth { - maxWidth = lineWidth + typeWidth, _ := ruler.MeasurePrecise(font, c.Type) + if maxTypeWidth < typeWidth { + maxTypeWidth = typeWidth + } + if c.Constraint != "" { + // covers UNQ constraint with padding + constraintWidth = 70. } } // The rows get padded a little due to header font being larger than row font obj.Height = float64(dims.Height * (len(obj.SQLTable.Columns) + 1)) - // Leave room for padding - obj.Width = float64(maxWidth + 100) + // Leave room for padding (20 on each side) + obj.Width = float64(maxNameWidth + maxTypeWidth + constraintWidth + 40) case d2target.ShapeText, d2target.ShapeCode: } From 6429beb3bb94346f6951be67d27200ceb42370f7 Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Sat, 17 Dec 2022 18:23:32 -0800 Subject: [PATCH 3/9] update tests --- .../sql_table_overflow/dagre/board.exp.json | 6 +++--- .../sql_table_overflow/dagre/sketch.exp.svg | 18 +++++++++--------- .../sql_table_overflow/elk/board.exp.json | 6 +++--- .../sql_table_overflow/elk/sketch.exp.svg | 18 +++++++++--------- 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/e2etests/testdata/regression/sql_table_overflow/dagre/board.exp.json b/e2etests/testdata/regression/sql_table_overflow/dagre/board.exp.json index c4c92edda..65fa36cf0 100644 --- a/e2etests/testdata/regression/sql_table_overflow/dagre/board.exp.json +++ b/e2etests/testdata/regression/sql_table_overflow/dagre/board.exp.json @@ -8,7 +8,7 @@ "x": 0, "y": 0 }, - "width": 387, + "width": 523, "height": 108, "opacity": 1, "strokeDash": 0, @@ -57,10 +57,10 @@ "id": "table_constrained", "type": "sql_table", "pos": { - "x": 447, + "x": 583, "y": 0 }, - "width": 486, + "width": 593, "height": 108, "opacity": 1, "strokeDash": 0, diff --git a/e2etests/testdata/regression/sql_table_overflow/dagre/sketch.exp.svg b/e2etests/testdata/regression/sql_table_overflow/dagre/sketch.exp.svg index 5e016ed19..d53b330bd 100644 --- a/e2etests/testdata/regression/sql_table_overflow/dagre/sketch.exp.svg +++ b/e2etests/testdata/regression/sql_table_overflow/dagre/sketch.exp.svg @@ -2,7 +2,7 @@ sql_table_overflowshort loooooooooooooooooooong -loooooooooooooooooooong +loooooooooooooooooooong short -sql_table_constrained_overflowshort -loooooooooooooooooooong -UNQloooooooooooooooooooong -short -FK - +sql_table_constrained_overflowshort +loooooooooooooooooooong +UNQloooooooooooooooooooong +short +FK + sql_table_overflowshort +sql_table_overflowshort loooooooooooooooooooong -loooooooooooooooooooong +loooooooooooooooooooong short -sql_table_constrained_overflowshort -loooooooooooooooooooong -UNQloooooooooooooooooooong -short -FK - +sql_table_constrained_overflowshort +loooooooooooooooooooong +UNQloooooooooooooooooooong +short +FK + sql_table_overflowshort -loooooooooooooooooooong -loooooooooooooooooooong -short -sql_table_constrained_overflowshort -loooooooooooooooooooong -UNQloooooooooooooooooooong -short -FK - +sql_table_overflowshort +loooooooooooooooooooong +loooooooooooooooooooong +short +sql_table_constrained_overflowshort +loooooooooooooooooooong +UNQloooooooooooooooooooong +short +FK + sql_table_overflowshort -loooooooooooooooooooong -loooooooooooooooooooong -short -sql_table_constrained_overflowshort -loooooooooooooooooooong -UNQloooooooooooooooooooong -short -FK - +sql_table_overflowshort +loooooooooooooooooooong +loooooooooooooooooooong +short +sql_table_constrained_overflowshort +loooooooooooooooooooong +UNQloooooooooooooooooooong +short +FK + usersid -int -name -string -email -string -password -string -last_login -datetime -productsid -int -price -decimal -sku -string -name -string -ordersid -int -user_id -int -product_id -int -shipmentsid -int -order_id -int -tracking_number -string -status -string - - +usersid +int +name +string +email +string +password +string +last_login +datetime +productsid +int +price +decimal +sku +string +name +string +ordersid +int +user_id +int +product_id +int +shipmentsid +int +order_id +int +tracking_number +string +status +string + + usersid -int -name -string -email -string -password -string -last_login -datetime -productsid -int -price -decimal -sku -string -name -string -ordersid -int -user_id -int -product_id -int -shipmentsid -int -order_id -int -tracking_number -string -status -string - - +usersid +int +name +string +email +string +password +string +last_login +datetime +productsid +int +price +decimal +sku +string +name +string +ordersid +int +user_id +int +product_id +int +shipmentsid +int +order_id +int +tracking_number +string +status +string + + sql_table_overflowshort -loooooooooooooooooooong -loooooooooooooooooooong -short -sql_table_constrained_overflowshort -loooooooooooooooooooong -UNQloooooooooooooooooooong -short -FK - +sql_table_overflowshort +loooooooooooooooooooong +loooooooooooooooooooong +short +sql_table_constrained_overflowshort +loooooooooooooooooooong +UNQloooooooooooooooooooong +short +FK + sql_table_overflowshort -loooooooooooooooooooong -loooooooooooooooooooong -short -sql_table_constrained_overflowshort -loooooooooooooooooooong -UNQloooooooooooooooooooong -short -FK - +sql_table_overflowshort +loooooooooooooooooooong +loooooooooooooooooooong +short +sql_table_constrained_overflowshort +loooooooooooooooooooong +UNQloooooooooooooooooooong +short +FK + usersid -int -name -string -email -string -password -string -last_login -datetime -productsid -int -price -decimal -sku -string -name -string -ordersid -int -user_id -int -product_id -int -shipmentsid -int -order_id -int -tracking_number -string -status -string - - +usersid +int +name +string +email +string +password +string +last_login +datetime +productsid +int +price +decimal +sku +string +name +string +ordersid +int +user_id +int +product_id +int +shipmentsid +int +order_id +int +tracking_number +string +status +string + + usersid -int -name -string -email -string -password -string -last_login -datetime -productsid -int -price -decimal -sku -string -name -string -ordersid -int -user_id -int -product_id -int -shipmentsid -int -order_id -int -tracking_number -string -status -string - - +usersid +int +name +string +email +string +password +string +last_login +datetime +productsid +int +price +decimal +sku +string +name +string +ordersid +int +user_id +int +product_id +int +shipmentsid +int +order_id +int +tracking_number +string +status +string + +