Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include postgres comments in output #392

Merged
merged 6 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions generator/metadata/enum_meta_data.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
package metadata

import "regexp"

// Enum metadata struct
type Enum struct {
Name string `sql:"primary_key"`
Values []string
Name string `sql:"primary_key"`
Comment string
Values []string
}

// GoLangComment returns enum comment without ascii control characters
func (e Enum) GoLangComment() string {
if e.Comment == "" {
return ""
}

// remove ascii control characters from string
return regexp.MustCompile(`[[:cntrl:]]+`).ReplaceAllString(e.Comment, "")
}
VolkerLieber marked this conversation as resolved.
Show resolved Hide resolved
13 changes: 13 additions & 0 deletions generator/metadata/table_meta_data.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package metadata

import "regexp"

// Table metadata struct
type Table struct {
Name string `sql:"primary_key"`
Comment string
Columns []Column
}

Expand All @@ -20,3 +23,13 @@ func (t Table) MutableColumns() []Column {

return ret
}

// GoLangComment returns table comment without ascii control characters
func (t Table) GoLangComment() string {
if t.Comment == "" {
return ""
}

// remove ascii control characters from string
return regexp.MustCompile(`[[:cntrl:]]+`).ReplaceAllString(t.Comment, "")
}
5 changes: 4 additions & 1 deletion generator/postgres/query_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type postgresQuerySet struct{}

func (p postgresQuerySet) GetTablesMetaData(db *sql.DB, schemaName string, tableType metadata.TableType) ([]metadata.Table, error) {
query := `
SELECT table_name as "table.name"
SELECT table_name as "table.name", obj_description((table_schema||'.'||quote_ident(table_name))::regclass) as "table.comment"
VolkerLieber marked this conversation as resolved.
Show resolved Hide resolved
FROM information_schema.tables
WHERE table_schema = $1 and table_type = $2
ORDER BY table_name;
Expand Down Expand Up @@ -57,6 +57,7 @@ func getColumnsMetaData(db *sql.DB, schemaName string, tableName string) ([]meta
query := `
select
attr.attname as "column.Name",
dsc.description as "column.Comment",
VolkerLieber marked this conversation as resolved.
Show resolved Hide resolved
exists(
select 1
from pg_catalog.pg_index indx
Expand All @@ -81,6 +82,7 @@ from pg_catalog.pg_attribute as attr
join pg_catalog.pg_class as cls on cls.oid = attr.attrelid
join pg_catalog.pg_namespace as ns on ns.oid = cls.relnamespace
join pg_catalog.pg_type as tp on tp.oid = attr.atttypid
left join pg_catalog.pg_description as dsc on dsc.objoid = attr.attrelid and dsc.objsubid = attr.attnum
where
ns.nspname = $1 and
cls.relname = $2 and
Expand All @@ -101,6 +103,7 @@ order by
func (p postgresQuerySet) GetEnumsMetaData(db *sql.DB, schemaName string) ([]metadata.Enum, error) {
query := `
SELECT t.typname as "enum.name",
obj_description(t.oid) as "enum.comment",
e.enumlabel as "values"
FROM pg_catalog.pg_type t
JOIN pg_catalog.pg_enum e on t.oid = e.enumtypid
Expand Down
4 changes: 4 additions & 0 deletions generator/template/file_templates.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

111 changes: 111 additions & 0 deletions tests/postgres/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,13 +604,16 @@ func TestGeneratedAllTypesSQLBuilderFiles(t *testing.T) {
"mood.go", "person.go", "person_phone.go", "weird_names_table.go", "level.go", "user.go", "floats.go", "people.go",
"components.go", "vulnerabilities.go", "all_types_materialized_view.go", "sample_ranges.go")
testutils.AssertFileContent(t, modelDir+"/all_types.go", allTypesModelContent)
testutils.AssertFileContent(t, modelDir+"/link.go", linkModelContent)

testutils.AssertFileNamesEqual(t, tableDir, "all_types.go", "employee.go", "link.go",
"person.go", "person_phone.go", "weird_names_table.go", "user.go", "floats.go", "people.go", "table_use_schema.go",
"components.go", "vulnerabilities.go", "sample_ranges.go")
testutils.AssertFileContent(t, tableDir+"/all_types.go", allTypesTableContent)
testutils.AssertFileContent(t, tableDir+"/sample_ranges.go", sampleRangeTableContent)

testutils.AssertFileContent(t, tableDir+"/link.go", linkTableContent)

testutils.AssertFileNamesEqual(t, viewDir, "all_types_materialized_view.go", "all_types_view.go",
"view_use_schema.go")
}
Expand Down Expand Up @@ -650,6 +653,7 @@ package enum

import "github.com/go-jet/jet/v2/postgres"

// Level enum
var Level = &struct {
Level1 postgres.StringExpression
Level2 postgres.StringExpression
Expand Down Expand Up @@ -747,6 +751,25 @@ type AllTypes struct {
}
`

var linkModelContent = `
//
// Code generated by go-jet DO NOT EDIT.
//
// WARNING: Changes to this file may cause incorrect behavior
// and will be lost if the code is regenerated
//

package model

// Link table
type Link struct {
ID int64 ` + "`sql:\"primary_key\"`" + ` // this is link id
URL string // link url
Name string // Unicode characters comment ₲鬼佬℧⇄↻
Description *string // '"Z\%_
}
`

var allTypesTableContent = `
//
// Code generated by go-jet DO NOT EDIT.
Expand Down Expand Up @@ -1103,3 +1126,91 @@ func newSampleRangesTableImpl(schemaName, tableName, alias string) sampleRangesT
}
}
`

var linkTableContent = `
//
// Code generated by go-jet DO NOT EDIT.
//
// WARNING: Changes to this file may cause incorrect behavior
// and will be lost if the code is regenerated
//

package table

import (
"github.com/go-jet/jet/v2/postgres"
)

var Link = newLinkTable("test_sample", "link", "")

// Link table
type linkTable struct {
postgres.Table

// Columns
ID postgres.ColumnInteger // this is link id
URL postgres.ColumnString // link url
Name postgres.ColumnString // Unicode characters comment ₲鬼佬℧⇄↻
Description postgres.ColumnString // '"Z\%_

AllColumns postgres.ColumnList
MutableColumns postgres.ColumnList
}

type LinkTable struct {
linkTable

EXCLUDED linkTable
}

// AS creates new LinkTable with assigned alias
func (a LinkTable) AS(alias string) *LinkTable {
return newLinkTable(a.SchemaName(), a.TableName(), alias)
}

// Schema creates new LinkTable with assigned schema name
func (a LinkTable) FromSchema(schemaName string) *LinkTable {
return newLinkTable(schemaName, a.TableName(), a.Alias())
}

// WithPrefix creates new LinkTable with assigned table prefix
func (a LinkTable) WithPrefix(prefix string) *LinkTable {
return newLinkTable(a.SchemaName(), prefix+a.TableName(), a.TableName())
}

// WithSuffix creates new LinkTable with assigned table suffix
func (a LinkTable) WithSuffix(suffix string) *LinkTable {
return newLinkTable(a.SchemaName(), a.TableName()+suffix, a.TableName())
}

func newLinkTable(schemaName, tableName, alias string) *LinkTable {
return &LinkTable{
linkTable: newLinkTableImpl(schemaName, tableName, alias),
EXCLUDED: newLinkTableImpl("", "excluded", ""),
}
}

func newLinkTableImpl(schemaName, tableName, alias string) linkTable {
var (
IDColumn = postgres.IntegerColumn("id")
URLColumn = postgres.StringColumn("url")
NameColumn = postgres.StringColumn("name")
DescriptionColumn = postgres.StringColumn("description")
allColumns = postgres.ColumnList{IDColumn, URLColumn, NameColumn, DescriptionColumn}
mutableColumns = postgres.ColumnList{URLColumn, NameColumn, DescriptionColumn}
)

return linkTable{
Table: postgres.NewTable(schemaName, tableName, alias, allColumns...),

//Columns
ID: IDColumn,
URL: URLColumn,
Name: NameColumn,
Description: DescriptionColumn,

AllColumns: allColumns,
MutableColumns: mutableColumns,
}
}
`