Add HasDefault column metadata
This commit is contained in:
parent
f55edafc0b
commit
b7904cde4e
8 changed files with 120 additions and 11 deletions
|
|
@ -10,6 +10,7 @@ type Column struct {
|
||||||
IsPrimaryKey bool
|
IsPrimaryKey bool
|
||||||
IsNullable bool
|
IsNullable bool
|
||||||
IsGenerated bool
|
IsGenerated bool
|
||||||
|
HasDefault bool
|
||||||
DataType DataType
|
DataType DataType
|
||||||
Comment string
|
Comment string
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ func (m mySqlQuerySet) GetTablesMetaData(db *sql.DB, schemaName string, tableTyp
|
||||||
SELECT
|
SELECT
|
||||||
t.table_name as "table.name",
|
t.table_name as "table.name",
|
||||||
col.COLUMN_NAME AS "column.Name",
|
col.COLUMN_NAME AS "column.Name",
|
||||||
|
col.COLUMN_DEFAULT IS NOT NULL as "column.HasDefault",
|
||||||
col.IS_NULLABLE = "YES" AS "column.IsNullable",
|
col.IS_NULLABLE = "YES" AS "column.IsNullable",
|
||||||
col.COLUMN_COMMENT AS "column.Comment",
|
col.COLUMN_COMMENT AS "column.Comment",
|
||||||
COALESCE(pk.IsPrimaryKey, 0) AS "column.IsPrimaryKey",
|
COALESCE(pk.IsPrimaryKey, 0) AS "column.IsPrimaryKey",
|
||||||
|
|
|
||||||
|
|
@ -64,6 +64,7 @@ select
|
||||||
) as "column.IsPrimaryKey",
|
) as "column.IsPrimaryKey",
|
||||||
not attr.attnotnull as "column.isNullable",
|
not attr.attnotnull as "column.isNullable",
|
||||||
attr.attgenerated = 's' as "column.isGenerated",
|
attr.attgenerated = 's' as "column.isGenerated",
|
||||||
|
attr.atthasdef as "column.hasDefault",
|
||||||
(case tp.typtype
|
(case tp.typtype
|
||||||
when 'b' then 'base'
|
when 'b' then 'base'
|
||||||
when 'd' then 'base'
|
when 'd' then 'base'
|
||||||
|
|
|
||||||
|
|
@ -4,10 +4,11 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/go-jet/jet/v2/generator/metadata"
|
"github.com/go-jet/jet/v2/generator/metadata"
|
||||||
"github.com/go-jet/jet/v2/internal/utils/semantic"
|
"github.com/go-jet/jet/v2/internal/utils/semantic"
|
||||||
"github.com/go-jet/jet/v2/qrm"
|
"github.com/go-jet/jet/v2/qrm"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// sqliteQuerySet is dialect query set for SQLite
|
// sqliteQuerySet is dialect query set for SQLite
|
||||||
|
|
@ -74,11 +75,12 @@ func (p sqliteQuerySet) GetTableColumnsMetaData(db *sql.DB, schemaName string, t
|
||||||
}
|
}
|
||||||
|
|
||||||
var columnInfos []struct {
|
var columnInfos []struct {
|
||||||
Name string
|
Name string
|
||||||
Type string
|
Type string
|
||||||
NotNull int32
|
NotNull int32
|
||||||
Pk int32
|
DfltValue string
|
||||||
Hidden int32
|
Pk int32
|
||||||
|
Hidden int32
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = qrm.Query(context.Background(), db, tableInfoQuery, []interface{}{tableName}, &columnInfos)
|
_, err = qrm.Query(context.Background(), db, tableInfoQuery, []interface{}{tableName}, &columnInfos)
|
||||||
|
|
@ -91,12 +93,14 @@ func (p sqliteQuerySet) GetTableColumnsMetaData(db *sql.DB, schemaName string, t
|
||||||
for _, columnInfo := range columnInfos {
|
for _, columnInfo := range columnInfos {
|
||||||
columnType := strings.TrimSuffix(getColumnType(columnInfo.Type), " GENERATED ALWAYS")
|
columnType := strings.TrimSuffix(getColumnType(columnInfo.Type), " GENERATED ALWAYS")
|
||||||
isGenerated := columnInfo.Hidden == 2 || columnInfo.Hidden == 3 // stored or virtual column
|
isGenerated := columnInfo.Hidden == 2 || columnInfo.Hidden == 3 // stored or virtual column
|
||||||
|
hasDefault := columnInfo.DfltValue != ""
|
||||||
|
|
||||||
columns = append(columns, metadata.Column{
|
columns = append(columns, metadata.Column{
|
||||||
Name: columnInfo.Name,
|
Name: columnInfo.Name,
|
||||||
IsPrimaryKey: columnInfo.Pk != 0,
|
IsPrimaryKey: columnInfo.Pk != 0,
|
||||||
IsNullable: columnInfo.NotNull != 1,
|
IsNullable: columnInfo.NotNull != 1,
|
||||||
IsGenerated: isGenerated,
|
IsGenerated: isGenerated,
|
||||||
|
HasDefault: hasDefault,
|
||||||
DataType: metadata.DataType{
|
DataType: metadata.DataType{
|
||||||
Name: columnType,
|
Name: columnType,
|
||||||
Kind: metadata.BaseType,
|
Kind: metadata.BaseType,
|
||||||
|
|
|
||||||
|
|
@ -8,8 +8,11 @@ import (
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
"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/mysql"
|
||||||
|
"github.com/go-jet/jet/v2/generator/template"
|
||||||
"github.com/go-jet/jet/v2/internal/testutils"
|
"github.com/go-jet/jet/v2/internal/testutils"
|
||||||
|
mysql2 "github.com/go-jet/jet/v2/mysql"
|
||||||
"github.com/go-jet/jet/v2/tests/dbconfig"
|
"github.com/go-jet/jet/v2/tests/dbconfig"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -39,6 +42,39 @@ func TestGenerator(t *testing.T) {
|
||||||
require.NoError(t, err)
|
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) {
|
func TestCmdGenerator(t *testing.T) {
|
||||||
err := os.RemoveAll(genTestDir3)
|
err := os.RemoveAll(genTestDir3)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,9 @@ package postgres
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"path"
|
||||||
|
"testing"
|
||||||
|
|
||||||
"github.com/go-jet/jet/v2/generator/metadata"
|
"github.com/go-jet/jet/v2/generator/metadata"
|
||||||
"github.com/go-jet/jet/v2/generator/postgres"
|
"github.com/go-jet/jet/v2/generator/postgres"
|
||||||
"github.com/go-jet/jet/v2/generator/template"
|
"github.com/go-jet/jet/v2/generator/template"
|
||||||
|
|
@ -13,8 +16,6 @@ import (
|
||||||
"github.com/go-jet/jet/v2/tests/dbconfig"
|
"github.com/go-jet/jet/v2/tests/dbconfig"
|
||||||
file2 "github.com/go-jet/jet/v2/tests/internal/utils/file"
|
file2 "github.com/go-jet/jet/v2/tests/internal/utils/file"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"path"
|
|
||||||
"testing"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const tempTestDir = "./.tempTestDir"
|
const tempTestDir = "./.tempTestDir"
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@ package postgres
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/go-jet/jet/v2/tests/internal/utils/file"
|
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
@ -12,11 +11,14 @@ import (
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
"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/postgres"
|
||||||
|
"github.com/go-jet/jet/v2/generator/template"
|
||||||
"github.com/go-jet/jet/v2/internal/testutils"
|
"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/.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 {
|
func dsn(host string, port int, dbName, user, password string) string {
|
||||||
|
|
@ -208,6 +210,36 @@ func TestGenerator(t *testing.T) {
|
||||||
require.NoError(t, err)
|
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) {
|
func TestGeneratorSpecialCharacters(t *testing.T) {
|
||||||
t.SkipNow()
|
t.SkipNow()
|
||||||
err := postgres.Generate(genTestDir2, postgres.DBConnection{
|
err := postgres.Generate(genTestDir2, postgres.DBConnection{
|
||||||
|
|
|
||||||
|
|
@ -8,8 +8,11 @@ import (
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
"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/sqlite"
|
||||||
|
"github.com/go-jet/jet/v2/generator/template"
|
||||||
"github.com/go-jet/jet/v2/internal/testutils"
|
"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/.gentestdata/sqlite/sakila/model"
|
||||||
"github.com/go-jet/jet/v2/tests/internal/utils/repo"
|
"github.com/go-jet/jet/v2/tests/internal/utils/repo"
|
||||||
)
|
)
|
||||||
|
|
@ -58,6 +61,36 @@ func TestGenerator(t *testing.T) {
|
||||||
require.NoError(t, err)
|
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) {
|
func TestCmdGenerator(t *testing.T) {
|
||||||
cmd := exec.Command("jet", "-source=SQLite", "-dsn=file://"+testDatabaseFilePath, "-path="+genDestDir)
|
cmd := exec.Command("jet", "-source=SQLite", "-dsn=file://"+testDatabaseFilePath, "-path="+genDestDir)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue