Add HasDefault column metadata

This commit is contained in:
Andrew Harding 2024-08-13 14:52:54 -06:00
parent f55edafc0b
commit b7904cde4e
8 changed files with 120 additions and 11 deletions

View file

@ -10,6 +10,7 @@ type Column struct {
IsPrimaryKey bool
IsNullable bool
IsGenerated bool
HasDefault bool
DataType DataType
Comment string
}

View file

@ -18,6 +18,7 @@ func (m mySqlQuerySet) GetTablesMetaData(db *sql.DB, schemaName string, tableTyp
SELECT
t.table_name as "table.name",
col.COLUMN_NAME AS "column.Name",
col.COLUMN_DEFAULT IS NOT NULL as "column.HasDefault",
col.IS_NULLABLE = "YES" AS "column.IsNullable",
col.COLUMN_COMMENT AS "column.Comment",
COALESCE(pk.IsPrimaryKey, 0) AS "column.IsPrimaryKey",

View file

@ -64,6 +64,7 @@ select
) as "column.IsPrimaryKey",
not attr.attnotnull as "column.isNullable",
attr.attgenerated = 's' as "column.isGenerated",
attr.atthasdef as "column.hasDefault",
(case tp.typtype
when 'b' then 'base'
when 'd' then 'base'

View file

@ -4,10 +4,11 @@ import (
"context"
"database/sql"
"fmt"
"strings"
"github.com/go-jet/jet/v2/generator/metadata"
"github.com/go-jet/jet/v2/internal/utils/semantic"
"github.com/go-jet/jet/v2/qrm"
"strings"
)
// sqliteQuerySet is dialect query set for SQLite
@ -77,6 +78,7 @@ func (p sqliteQuerySet) GetTableColumnsMetaData(db *sql.DB, schemaName string, t
Name string
Type string
NotNull int32
DfltValue string
Pk int32
Hidden int32
}
@ -91,12 +93,14 @@ func (p sqliteQuerySet) GetTableColumnsMetaData(db *sql.DB, schemaName string, t
for _, columnInfo := range columnInfos {
columnType := strings.TrimSuffix(getColumnType(columnInfo.Type), " GENERATED ALWAYS")
isGenerated := columnInfo.Hidden == 2 || columnInfo.Hidden == 3 // stored or virtual column
hasDefault := columnInfo.DfltValue != ""
columns = append(columns, metadata.Column{
Name: columnInfo.Name,
IsPrimaryKey: columnInfo.Pk != 0,
IsNullable: columnInfo.NotNull != 1,
IsGenerated: isGenerated,
HasDefault: hasDefault,
DataType: metadata.DataType{
Name: columnType,
Kind: metadata.BaseType,

View file

@ -8,8 +8,11 @@ import (
"github.com/stretchr/testify/require"
"github.com/go-jet/jet/v2/generator/metadata"
"github.com/go-jet/jet/v2/generator/mysql"
"github.com/go-jet/jet/v2/generator/template"
"github.com/go-jet/jet/v2/internal/testutils"
mysql2 "github.com/go-jet/jet/v2/mysql"
"github.com/go-jet/jet/v2/tests/dbconfig"
)
@ -39,6 +42,39 @@ func TestGenerator(t *testing.T) {
require.NoError(t, err)
}
func TestGenerator_TableMetadata(t *testing.T) {
var schema metadata.Schema
err := mysql.Generate(genTestDir3, dbConnection("dvds"),
template.Default(mysql2.Dialect).UseSchema(func(m metadata.Schema) template.Schema {
schema = m
return template.DefaultSchema(m)
}))
require.NoError(t, err)
// Spot check the actor table and assert that the emitted
// properties are as expected.
var got metadata.Table
for _, table := range schema.TablesMetaData {
if table.Name == "actor" {
got = table
}
}
want := metadata.Table{
Name: "actor",
Columns: []metadata.Column{
{Name: "actor_id", IsPrimaryKey: true, IsNullable: false, IsGenerated: false, HasDefault: false, DataType: metadata.DataType{Name: "smallint", Kind: "base", IsUnsigned: true}, Comment: ""},
{Name: "first_name", IsPrimaryKey: false, IsNullable: false, IsGenerated: false, HasDefault: false, DataType: metadata.DataType{Name: "varchar", Kind: "base", IsUnsigned: false}, Comment: ""},
{Name: "last_name", IsPrimaryKey: false, IsNullable: false, IsGenerated: false, HasDefault: false, DataType: metadata.DataType{Name: "varchar", Kind: "base", IsUnsigned: false}, Comment: ""},
{Name: "last_update", IsPrimaryKey: false, IsNullable: false, IsGenerated: false, HasDefault: true, DataType: metadata.DataType{Name: "timestamp", Kind: "base", IsUnsigned: false}, Comment: ""},
},
}
require.Equal(t, want, got)
err = os.RemoveAll(genTestDirRoot)
require.NoError(t, err)
}
func TestCmdGenerator(t *testing.T) {
err := os.RemoveAll(genTestDir3)
require.NoError(t, err)

View file

@ -3,6 +3,9 @@ package postgres
import (
"database/sql"
"fmt"
"path"
"testing"
"github.com/go-jet/jet/v2/generator/metadata"
"github.com/go-jet/jet/v2/generator/postgres"
"github.com/go-jet/jet/v2/generator/template"
@ -13,8 +16,6 @@ import (
"github.com/go-jet/jet/v2/tests/dbconfig"
file2 "github.com/go-jet/jet/v2/tests/internal/utils/file"
"github.com/stretchr/testify/require"
"path"
"testing"
)
const tempTestDir = "./.tempTestDir"

View file

@ -2,7 +2,6 @@ package postgres
import (
"fmt"
"github.com/go-jet/jet/v2/tests/internal/utils/file"
"os"
"os/exec"
"path/filepath"
@ -12,11 +11,14 @@ import (
"github.com/stretchr/testify/require"
"github.com/go-jet/jet/v2/generator/metadata"
"github.com/go-jet/jet/v2/generator/postgres"
"github.com/go-jet/jet/v2/generator/template"
"github.com/go-jet/jet/v2/internal/testutils"
"github.com/go-jet/jet/v2/tests/dbconfig"
postgres2 "github.com/go-jet/jet/v2/postgres"
"github.com/go-jet/jet/v2/tests/.gentestdata/jetdb/dvds/model"
"github.com/go-jet/jet/v2/tests/dbconfig"
"github.com/go-jet/jet/v2/tests/internal/utils/file"
)
func dsn(host string, port int, dbName, user, password string) string {
@ -208,6 +210,36 @@ func TestGenerator(t *testing.T) {
require.NoError(t, err)
}
func TestGenerator_TableMetadata(t *testing.T) {
var schema metadata.Schema
err := postgres.GenerateDSN(defaultDSN(), "dvds", genTestDir2,
template.Default(postgres2.Dialect).UseSchema(func(m metadata.Schema) template.Schema {
schema = m
return template.DefaultSchema(m)
}))
require.NoError(t, err)
// Spot check the actor table and assert that the emitted
// properties are as expected.
var got metadata.Table
for _, table := range schema.TablesMetaData {
if table.Name == "actor" {
got = table
}
}
want := metadata.Table{
Name: "actor",
Columns: []metadata.Column{
{Name: "actor_id", IsPrimaryKey: true, IsNullable: false, IsGenerated: false, HasDefault: true, DataType: metadata.DataType{Name: "int4", Kind: "base", IsUnsigned: false}, Comment: ""},
{Name: "first_name", IsPrimaryKey: false, IsNullable: false, IsGenerated: false, HasDefault: false, DataType: metadata.DataType{Name: "varchar", Kind: "base", IsUnsigned: false}, Comment: ""},
{Name: "last_name", IsPrimaryKey: false, IsNullable: false, IsGenerated: false, HasDefault: false, DataType: metadata.DataType{Name: "varchar", Kind: "base", IsUnsigned: false}, Comment: ""},
{Name: "last_update", IsPrimaryKey: false, IsNullable: false, IsGenerated: false, HasDefault: false, DataType: metadata.DataType{Name: "timestamp", Kind: "base", IsUnsigned: false}, Comment: ""},
},
}
require.Equal(t, want, got)
}
func TestGeneratorSpecialCharacters(t *testing.T) {
t.SkipNow()
err := postgres.Generate(genTestDir2, postgres.DBConnection{

View file

@ -8,8 +8,11 @@ import (
"github.com/stretchr/testify/require"
"github.com/go-jet/jet/v2/generator/metadata"
"github.com/go-jet/jet/v2/generator/sqlite"
"github.com/go-jet/jet/v2/generator/template"
"github.com/go-jet/jet/v2/internal/testutils"
sqlite2 "github.com/go-jet/jet/v2/sqlite"
"github.com/go-jet/jet/v2/tests/.gentestdata/sqlite/sakila/model"
"github.com/go-jet/jet/v2/tests/internal/utils/repo"
)
@ -58,6 +61,36 @@ func TestGenerator(t *testing.T) {
require.NoError(t, err)
}
func TestGenerator_TableMetadata(t *testing.T) {
var schema metadata.Schema
err := sqlite.GenerateDSN(testDatabaseFilePath, genDestDir,
template.Default(sqlite2.Dialect).UseSchema(func(m metadata.Schema) template.Schema {
schema = m
return template.DefaultSchema(m)
}))
require.NoError(t, err)
// Spot check the actor table and assert that the emitted
// properties are as expected.
var got metadata.Table
for _, table := range schema.TablesMetaData {
if table.Name == "actor" {
got = table
}
}
want := metadata.Table{
Name: "actor",
Columns: []metadata.Column{
{Name: "actor_id", IsPrimaryKey: true, IsNullable: false, IsGenerated: false, HasDefault: false, DataType: metadata.DataType{Name: "INTEGER", Kind: "base", IsUnsigned: false}, Comment: ""},
{Name: "first_name", IsPrimaryKey: false, IsNullable: false, IsGenerated: false, HasDefault: false, DataType: metadata.DataType{Name: "VARCHAR", Kind: "base", IsUnsigned: false}, Comment: ""},
{Name: "last_name", IsPrimaryKey: false, IsNullable: false, IsGenerated: false, HasDefault: false, DataType: metadata.DataType{Name: "VARCHAR", Kind: "base", IsUnsigned: false}, Comment: ""},
{Name: "last_update", IsPrimaryKey: false, IsNullable: false, IsGenerated: false, HasDefault: true, DataType: metadata.DataType{Name: "TIMESTAMP", Kind: "base", IsUnsigned: false}, Comment: ""},
},
}
require.Equal(t, want, got)
}
func TestCmdGenerator(t *testing.T) {
cmd := exec.Command("jet", "-source=SQLite", "-dsn=file://"+testDatabaseFilePath, "-path="+genDestDir)