mirror of
https://github.com/ent/ent.git
synced 2026-05-22 09:31:45 +03:00
dialect/sql/schema: alter table for postgres
Summary: Pull Request resolved: https://github.com/facebookincubator/ent/pull/115 Reviewed By: alexsn Differential Revision: D18065988 fbshipit-source-id: a7d33bbebd63b01659bc5ba562ac85642dcd3d83
This commit is contained in:
committed by
Facebook Github Bot
parent
dff3067639
commit
2789257849
@@ -191,7 +191,7 @@ func (m *Migrate) apply(ctx context.Context, tx dialect.Tx, table string, change
|
||||
}
|
||||
}
|
||||
}
|
||||
b := sql.AlterTable(table)
|
||||
b := sql.Dialect(m.Dialect()).AlterTable(table)
|
||||
for _, c := range change.column.add {
|
||||
b.AddColumn(m.cBuilder(c))
|
||||
}
|
||||
@@ -200,7 +200,7 @@ func (m *Migrate) apply(ctx context.Context, tx dialect.Tx, table string, change
|
||||
}
|
||||
if m.dropColumn {
|
||||
for _, c := range change.column.drop {
|
||||
b.DropColumn(sql.Column(c.Name))
|
||||
b.DropColumn(sql.Dialect(m.Dialect()).Column(c.Name))
|
||||
}
|
||||
}
|
||||
// if there's actual action to execute on ALTER TABLE.
|
||||
|
||||
@@ -70,7 +70,7 @@ func (d *Postgres) setRange(ctx context.Context, tx dialect.Tx, name string, val
|
||||
func (d *Postgres) table(ctx context.Context, tx dialect.Tx, name string) (*Table, error) {
|
||||
rows := &sql.Rows{}
|
||||
query, args := sql.Dialect(dialect.Postgres).
|
||||
Select("column_name", "data_type", "character_maximum_length", "is_nullable", "column_default").
|
||||
Select("column_name", "data_type", "is_nullable", "column_default").
|
||||
From(sql.Table("INFORMATION_SCHEMA.COLUMNS").Unquote()).
|
||||
Where(sql.EQ("table_schema", sql.Raw("CURRENT_SCHEMA()")).And().EQ("table_name", name)).Query()
|
||||
if err := tx.Query(ctx, query, args, rows); err != nil {
|
||||
@@ -147,7 +147,7 @@ WHERE t.oid = idx.indrelid
|
||||
func (d *Postgres) indexes(ctx context.Context, tx dialect.Tx, table string) (Indexes, error) {
|
||||
rows := &sql.Rows{}
|
||||
if err := tx.Query(ctx, fmt.Sprintf(indexesQuery, table), []interface{}{}, rows); err != nil {
|
||||
return nil, fmt.Errorf("querying indexes for table %s", table)
|
||||
return nil, fmt.Errorf("querying indexes for table %s: %v", table, err)
|
||||
}
|
||||
defer rows.Close()
|
||||
var (
|
||||
@@ -179,11 +179,10 @@ const maxCharSize = 10 << 20
|
||||
// scanColumn scans the information a column from column description.
|
||||
func (d *Postgres) scanColumn(c *Column, rows *sql.Rows) error {
|
||||
var (
|
||||
maxlen sql.NullInt64
|
||||
nullable sql.NullString
|
||||
defaults sql.NullString
|
||||
)
|
||||
if err := rows.Scan(&c.Name, &c.typ, &maxlen, &nullable, &defaults); err != nil {
|
||||
if err := rows.Scan(&c.Name, &c.typ, &nullable, &defaults); err != nil {
|
||||
return fmt.Errorf("scanning column description: %v", err)
|
||||
}
|
||||
if nullable.Valid {
|
||||
@@ -205,9 +204,8 @@ func (d *Postgres) scanColumn(c *Column, rows *sql.Rows) error {
|
||||
case "text":
|
||||
c.Type = field.TypeString
|
||||
c.Size = maxCharSize + 1
|
||||
case "character":
|
||||
case "character", "character varying":
|
||||
c.Type = field.TypeString
|
||||
c.Size = maxlen.Int64
|
||||
case "timestamp with time zone":
|
||||
c.Type = field.TypeTime
|
||||
case "bytea":
|
||||
|
||||
@@ -6,6 +6,8 @@ package schema
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math"
|
||||
"testing"
|
||||
|
||||
"github.com/facebookincubator/ent/schema/field"
|
||||
@@ -126,6 +128,43 @@ func TestPostgres_Create(t *testing.T) {
|
||||
mock.ExpectCommit()
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "add column to table",
|
||||
tables: []*Table{
|
||||
{
|
||||
Name: "users",
|
||||
Columns: []*Column{
|
||||
{Name: "id", Type: field.TypeInt, Increment: true},
|
||||
{Name: "name", Type: field.TypeString, Nullable: true},
|
||||
{Name: "text", Type: field.TypeString, Nullable: true, Size: math.MaxInt32},
|
||||
{Name: "age", Type: field.TypeInt},
|
||||
},
|
||||
PrimaryKey: []*Column{
|
||||
{Name: "id", Type: field.TypeInt, Increment: true},
|
||||
},
|
||||
},
|
||||
},
|
||||
before: func(mock sqlmock.Sqlmock) {
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectQuery(escape("SHOW server_version_num")).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"server_version_num"}).AddRow("120000"))
|
||||
mock.ExpectQuery(escape(`SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE "table_schema" = CURRENT_SCHEMA() AND "table_name" = $1`)).
|
||||
WithArgs("users").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(1))
|
||||
mock.ExpectQuery(escape(`SELECT "column_name", "data_type", "is_nullable", "column_default" FROM INFORMATION_SCHEMA.COLUMNS WHERE "table_schema" = CURRENT_SCHEMA() AND "table_name" = $1`)).
|
||||
WithArgs("users").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"column_name", "data_type", "is_nullable", "column_default"}).
|
||||
AddRow("id", "bigint", "NO", "NULL").
|
||||
AddRow("name", "character varying", "YES", "NULL").
|
||||
AddRow("text", "text", "YES", "NULL"))
|
||||
mock.ExpectQuery(escape(fmt.Sprintf(indexesQuery, "users"))).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"index_name", "column_name", "primary", "unique"}).
|
||||
AddRow("users_pkey", "id", "t", "t"))
|
||||
mock.ExpectExec(escape(`ALTER TABLE "users" ADD COLUMN "age" bigint NOT NULL`)).
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
mock.ExpectCommit()
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
|
||||
@@ -397,7 +397,7 @@ func (c *Column) ScanMySQL(rows *sql.Rows) error {
|
||||
c.Enums[i] = strings.Trim(e, "'")
|
||||
}
|
||||
}
|
||||
if defaults.Valid && defaults.String != Null {
|
||||
if defaults.Valid {
|
||||
return c.ScanDefault(defaults.String)
|
||||
}
|
||||
return nil
|
||||
@@ -429,6 +429,7 @@ func (c Column) FloatType() bool { return c.Type == field.TypeFloat32 || c.Type
|
||||
// ScanDefault scans the default value string to its interface type.
|
||||
func (c *Column) ScanDefault(value string) (err error) {
|
||||
switch {
|
||||
case value == Null: // ignore.
|
||||
case c.IntType():
|
||||
v := &sql.NullInt64{}
|
||||
if err := v.Scan(value); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user