Skip to content

Commit

Permalink
#374 working with column comment for MySQL/MariaDB and Postgres datab…
Browse files Browse the repository at this point in the history
…ases

* Added extracting column comment value for MySQL/MariaDB databases
* add passing comment value in LoadColumns
* Fix the standard template
* Restore code cleared during a generation
* Generated new column.xo.go
* Added Comment field and SQL query to fill the new field
  • Loading branch information
evassilyev authored and kenshaw committed May 1, 2023
1 parent 1859960 commit a6ebaff
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 12 deletions.
1 change: 1 addition & 0 deletions cmd/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ func LoadColumns(ctx context.Context, args *Args, table *xo.Table) error {
Default: defaultValue,
IsPrimary: c.IsPrimaryKey,
IsSequence: sqMap[c.ColumnName],
Comment: c.Comment.String,
}
table.Columns = append(table.Columns, col)
if col.IsPrimary {
Expand Down
10 changes: 7 additions & 3 deletions gen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ WHERE n.nspname = %%schema string%%
ENDSQL

# postgres table column list query
FIELDS='FieldOrdinal int,ColumnName string,DataType string,NotNull bool,DefaultValue sql.NullString,IsPrimaryKey bool'
FIELDS='FieldOrdinal int,ColumnName string,DataType string,NotNull bool,DefaultValue sql.NullString,IsPrimaryKey bool,Comment sql.NullString'
COMMENT='{{ . }} is a column.'
$XOBIN query $PGDB -M -B -2 -T Column -F PostgresTableColumns -Z "$FIELDS" --type-comment "$COMMENT" -o $DEST $@ << ENDSQL
SELECT
Expand All @@ -181,7 +181,8 @@ SELECT
format_type(a.atttypid, a.atttypmod)::varchar AS data_type,
a.attnotnull::boolean AS not_null,
COALESCE(pg_get_expr(ad.adbin, ad.adrelid), '')::varchar AS default_value,
COALESCE(ct.contype = 'p', false)::boolean AS is_primary_key
COALESCE(ct.contype = 'p', false)::boolean AS is_primary_key,
d.description::varchar as comment
FROM pg_attribute a
JOIN ONLY pg_class c ON c.oid = a.attrelid
JOIN ONLY pg_namespace n ON n.oid = c.relnamespace
Expand All @@ -190,6 +191,8 @@ FROM pg_attribute a
AND ct.contype = 'p'
LEFT JOIN pg_attrdef ad ON ad.adrelid = c.oid
AND ad.adnum = a.attnum
LEFT JOIN pg_description d on d.objoid = c.oid
AND d.objsubid = a.attnum
WHERE a.attisdropped = false
AND n.nspname = %%schema string%%
AND c.relname = %%table string%%
Expand Down Expand Up @@ -403,7 +406,8 @@ SELECT
IF(data_type = 'enum', column_name, column_type) AS data_type,
IF(is_nullable = 'YES', false, true) AS not_null,
column_default AS default_value,
IF(column_key = 'PRI', true, false) AS is_primary_key
IF(column_key = 'PRI', true, false) AS is_primary_key,
column_comment AS comment
FROM information_schema.columns
WHERE table_schema = %%schema string%%
AND table_name = %%table string%%
Expand Down
13 changes: 9 additions & 4 deletions models/column.xo.go

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

1 change: 1 addition & 0 deletions templates/go/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,7 @@ func convertField(ctx context.Context, tf transformFunc, f xo.Field) (Field, err
Zero: zero,
IsPrimary: f.IsPrimary,
IsSequence: f.IsSequence,
Comment: f.Comment,
}, nil
}

Expand Down
11 changes: 6 additions & 5 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ type Field struct {
ConstValue *int `json:"const_value,omitempty"`
Interpolate bool `json:"interpolate,omitempty"`
Join bool `json:"join,omitempty"`
Comment string `json:"comment,omitempty"`
}

// Type holds information for a database type.
Expand All @@ -154,11 +155,11 @@ type Type struct {
//
// Expected formats:
//
// type
// type(precision)
// type(precision, scale)
// type(precision, scale) unsigned
// timestamp(n) with [local] time zone (oracle only)
// type
// type(precision)
// type(precision, scale)
// type(precision, scale) unsigned
// timestamp(n) with [local] time zone (oracle only)
//
// The returned type is stripped of precision and scale.
func ParseType(typ, driver string) (Type, error) {
Expand Down

0 comments on commit a6ebaff

Please sign in to comment.