k1LoW / tbls

tbls is a CI-Friendly tool for document a database, written in Go.
MIT License
3.32k stars 164 forks source link

Add "Arguments" to the field to sort in function category. #525

Closed corydoraspanda closed 8 months ago

corydoraspanda commented 8 months ago

What happened

When creating functions with the same name but different numbers of arguments, sorting might not work as expected.

For example, in postgres, if you repeatedly create and delete the following function, the difference will appear with tbls diff.

CREATE OR REPLACE FUNCTION add(integer, integer) RETURNS integer
    AS 'select $1 + $2;'
    LANGUAGE SQL
    IMMUTABLE
    RETURNS NULL ON NULL INPUT;

CREATE OR REPLACE FUNCTION add(integer, integer, integer) RETURNS integer
    AS 'select $1 + $2 + $3;'
    LANGUAGE SQL
    IMMUTABLE
    RETURNS NULL ON NULL INPUT;

diff

root@aa87a023bf4b:/workspace/tbls# ./tbls diff pg://postgres:pgpass@postgres95/testdb?sslmode=disable  --sort -c testdata/test_tbls_postgres.yml sample/postgres95a
diff 'sample/postgres95a/README.md' 'tbls doc pg://postgres:*****@postgres95/testdb?sslmode=disable'
--- sample/postgres95a/README.md
+++ tbls doc pg://postgres:*****@postgres95/testdb?sslmode=disable
@@ -29,8 +29,8 @@

 | Name | ReturnType | Arguments | Type |
 | ---- | ------- | ------- | ---- |
+| public.add | int4 | integer, integer | FUNCTION |
 | public.add | int4 | integer, integer, integer | FUNCTION |
-| public.add | int4 | integer, integer | FUNCTION |
 | public.update_updated | trigger |  | FUNCTION |
 | public.uuid_generate_v1 | uuid |  | FUNCTION |
 | public.uuid_generate_v1mc | uuid |  | FUNCTION |

What you expected to happened

Function arguments are also included in the sorting target.

What stack trace or error message from tbls did you see?

Nothing

Anything else we need to know?

Proposal for how to fix it

diff --git a/schema/schema.go b/schema/schema.go
index 2b9f6f0..6097e76 100644
--- a/schema/schema.go
+++ b/schema/schema.go
@@ -290,7 +290,10 @@ func (s *Schema) Sort() error {
        return s.Relations[i].Table.Name < s.Relations[j].Table.Name
    })
    sort.SliceStable(s.Functions, func(i, j int) bool {
-       return s.Functions[i].Name < s.Functions[j].Name
+       if s.Functions[i].Name != s.Functions[j].Name {
+           return s.Functions[i].Name < s.Functions[j].Name
+       }
+       return s.Functions[i].Arguments < s.Functions[j].Arguments
    })
    sort.SliceStable(s.Viewpoints, func(i, j int) bool {
        return s.Viewpoints[i].Name < s.Viewpoints[j].Name
diff --git a/schema/schema_test.go b/schema/schema_test.go
index ab4c0fd..6678e6d 100644
--- a/schema/schema_test.go
+++ b/schema/schema_test.go
@@ -210,6 +210,16 @@ func TestSchema_Sort(t *testing.T) {
                },
            },
        },
+       Functions: []*Function{
+           &Function{
+               Name:   "b",
+               Arguments: "arg b",
+           },
+           &Function{
+               Name:   "b",
+               Arguments: "arg a",
+           },
+       },
    }
    if err := schema.Sort(); err != nil {
        t.Error(err)
@@ -224,6 +234,11 @@ func TestSchema_Sort(t *testing.T) {
    if got2 != want2 {
        t.Errorf("got %v\nwant %v", got2, want2)
    }
+   want3 := "arg a"
+   got3 := schema.Functions[0].Arguments
+   if got3 != want3 {
+       t.Errorf("got %v\nwant %v", got3, want3)
+   }
 }

 func TestRepair(t *testing.T) {

Environment

k1LoW commented 8 months ago

@corydoraspanda Thank you! LGTM!

corydoraspanda commented 8 months ago

@k1LoW Thank you. It works.