Skip to content

Commit

Permalink
fix generated columns in mutable columns for PostgreSQL
Browse files Browse the repository at this point in the history
Fix #209.
  • Loading branch information
rangzen committed Jan 30, 2023
1 parent ce3c622 commit a4191b4
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions generator/metadata/column_meta_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type Column struct {
Name string
IsPrimaryKey bool
IsNullable bool
IsGenerated bool
DataType DataType
}

Expand Down
2 changes: 1 addition & 1 deletion generator/metadata/table_meta_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func (t Table) MutableColumns() []Column {
var ret []Column

for _, column := range t.Columns {
if column.IsPrimaryKey {
if column.IsPrimaryKey || column.IsGenerated {
continue
}

Expand Down
1 change: 1 addition & 0 deletions generator/postgres/query_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ WITH primaryKeys AS (
)
SELECT column_name as "column.Name",
is_nullable = 'YES' as "column.isNullable",
is_generated = 'ALWAYS' as "column.isGenerated",
(EXISTS(SELECT 1 from primaryKeys as pk where pk.column_name = columns.column_name)) as "column.IsPrimaryKey",
dataType.kind as "dataType.Kind",
(case dataType.Kind when 'base' then data_type else LTRIM(udt_name, '_') end) as "dataType.Name",
Expand Down
1 change: 1 addition & 0 deletions tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ jet-gen-postgres:
jet -dsn=postgres://jet:jet@localhost:50901/jetdb?sslmode=disable -schema=chinook2 -path=./.gentestdata/
jet -dsn=postgres://jet:jet@localhost:50901/jetdb?sslmode=disable -schema=northwind -path=./.gentestdata/
jet -dsn=postgres://jet:jet@localhost:50901/jetdb?sslmode=disable -schema=test_sample -path=./.gentestdata/
jet -dsn=postgres://jet:jet@localhost:50901/jetdb?sslmode=disable -schema=test_generated -path=./.gentestdata/

jet-gen-mysql:
jet -source=mysql -dsn="jet:jet@tcp(localhost:50902)/dvds" -path=./.gentestdata/mysql
Expand Down
1 change: 1 addition & 0 deletions tests/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func initPostgresDB(dbType string, connectionString string) {
"test_sample",
"chinook",
"chinook2",
"test_generated",
}

for _, schemaName := range schemaNames {
Expand Down
15 changes: 15 additions & 0 deletions tests/postgres/generated_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package postgres

import (
. "github.com/go-jet/jet/v2/tests/.gentestdata/jetdb/test_generated/table"
"github.com/stretchr/testify/require"
"testing"
)

func TestMutableColumnsExcludeGeneratedColumn(t *testing.T) {
t.Run("should not have the generated column in mutableColumns", func(t *testing.T) {
require.Equal(t, 2, len(People.MutableColumns))
require.Equal(t, People.PeopleName, People.MutableColumns[0])
require.Equal(t, People.PeopleHeightCm, People.MutableColumns[1])
})
}

0 comments on commit a4191b4

Please sign in to comment.