From 895ab9dd7f3f8e0d7495f5b24f0ced9db36529d2 Mon Sep 17 00:00:00 2001 From: Lauris BH Date: Tue, 1 Nov 2022 11:23:17 +0200 Subject: [PATCH 01/12] Add method to alter table column --- column.go | 11 +++++++++++ schema.go | 7 +++++++ schema_test.go | 18 +++++++++++++++--- table.go | 5 +++++ 4 files changed, 38 insertions(+), 3 deletions(-) diff --git a/column.go b/column.go index b21d0fec..fa76c821 100644 --- a/column.go +++ b/column.go @@ -75,6 +75,17 @@ func renameColumn(name string, newName string, options []ColumnOption) Column { return column } +func alterColumn(name string, typ ColumnType, options []ColumnOption) Column { + column := Column{ + Op: SchemaAlter, + Name: name, + Type: typ, + } + + applyColumnOptions(&column, options) + return column +} + func dropColumn(name string, options []ColumnOption) Column { column := Column{ Op: SchemaDrop, diff --git a/schema.go b/schema.go index b861d36a..9aeb1926 100644 --- a/schema.go +++ b/schema.go @@ -81,6 +81,13 @@ func (s *Schema) AddColumn(table string, name string, typ ColumnType, options .. s.add(at.Table) } +// AlterColumn with name and type. +func (s *Schema) AlterColumn(table string, name string, typ ColumnType, options ...ColumnOption) { + at := alterTable(table, nil) + at.AlterColumn(name, typ, options...) + s.add(at.Table) +} + // RenameColumn by name. func (s *Schema) RenameColumn(table string, name string, newName string, options ...ColumnOption) { at := alterTable(table, nil) diff --git a/schema_test.go b/schema_test.go index 9e0b7219..670a9abe 100644 --- a/schema_test.go +++ b/schema_test.go @@ -118,6 +118,20 @@ func TestSchema_AddColumn(t *testing.T) { }, schema.Migrations[0]) } +func TestSchema_AlterColumn(t *testing.T) { + var schema Schema + + schema.AlterColumn("products", "description", "", Default("test")) + + assert.Equal(t, Table{ + Op: SchemaAlter, + Name: "products", + Definitions: []TableDefinition{ + Column{Name: "description", Type: "", Op: SchemaAlter, Default: "test"}, + }, + }, schema.Migrations[0]) +} + func TestSchema_RenameColumn(t *testing.T) { var schema Schema @@ -218,9 +232,7 @@ func TestRaw_InternalTableDefinition(t *testing.T) { } func TestDo(t *testing.T) { - var ( - schema Schema - ) + var schema Schema schema.Do(func(ctx context.Context, repo Repository) error { return nil }) assert.NotNil(t, schema.Migrations[0]) diff --git a/table.go b/table.go index 1c0f23a2..8baba579 100644 --- a/table.go +++ b/table.go @@ -136,6 +136,11 @@ func (at *AlterTable) RenameColumn(name string, newName string, options ...Colum at.Definitions = append(at.Definitions, renameColumn(name, newName, options)) } +// AlterColumn with name and type. +func (at *AlterTable) AlterColumn(name string, typ ColumnType, options ...ColumnOption) { + at.Definitions = append(at.Definitions, alterColumn(name, typ, options)) +} + // DropColumn from this table. func (at *AlterTable) DropColumn(name string, options ...ColumnOption) { at.Definitions = append(at.Definitions, dropColumn(name, options)) From 7231b14b5fb2c8414336a0c80829c893886c542c Mon Sep 17 00:00:00 2001 From: Lauris BH Date: Tue, 8 Nov 2022 16:54:35 +0200 Subject: [PATCH 02/12] Refactor to use separate method for column type and constraint changes --- column.go | 56 ++++++++++++++++++++++++++++++++++------------- schema.go | 13 ++++++++--- schema_options.go | 3 +++ schema_test.go | 20 ++++++++++++++--- table.go | 11 +++++++--- 5 files changed, 79 insertions(+), 24 deletions(-) diff --git a/column.go b/column.go index fa76c821..64118ae6 100644 --- a/column.go +++ b/column.go @@ -34,21 +34,36 @@ const ( Time ColumnType = "TIME" ) +// ColumnConstraint bit flags. +type ColumnConstraint uint16 + +const ( + // ColumnConstraintType that column type has changed. + ColumnConstraintType ColumnConstraint = 1 << iota + // ColumnConstraintUnique that column uniqueness has changed. + ColumnConstraintUnique + // ColumnConstraintUnique that column required has changed. + ColumnConstraintRequired + // ColumnConstraintDefault that column default value has changed. + ColumnConstraintDefault +) + // Column definition. type Column struct { - Op SchemaOp - Name string - Type ColumnType - Rename string - Primary bool - Unique bool - Required bool - Unsigned bool - Limit int - Precision int - Scale int - Default any - Options string + Op SchemaOp + Name string + Type ColumnType + Rename string + AlterConstr ColumnConstraint + Primary bool + Unique bool + Required bool + Unsigned bool + Limit int + Precision int + Scale int + Default any + Options string } func (Column) internalTableDefinition() {} @@ -75,11 +90,22 @@ func renameColumn(name string, newName string, options []ColumnOption) Column { return column } -func alterColumn(name string, typ ColumnType, options []ColumnOption) Column { +func alterColumnType(name string, typ ColumnType, options []ColumnOption) Column { + column := Column{ + Op: SchemaAlter, + Name: name, + Type: typ, + AlterConstr: ColumnConstraintType, + } + + applyColumnOptions(&column, options) + return column +} + +func alterColumnConstraints(name string, options []ColumnOption) Column { column := Column{ Op: SchemaAlter, Name: name, - Type: typ, } applyColumnOptions(&column, options) diff --git a/schema.go b/schema.go index 9aeb1926..815a7bb7 100644 --- a/schema.go +++ b/schema.go @@ -81,10 +81,17 @@ func (s *Schema) AddColumn(table string, name string, typ ColumnType, options .. s.add(at.Table) } -// AlterColumn with name and type. -func (s *Schema) AlterColumn(table string, name string, typ ColumnType, options ...ColumnOption) { +// AlterColumnType with name. +func (s *Schema) AlterColumnType(table string, name string, typ ColumnType, options ...ColumnOption) { at := alterTable(table, nil) - at.AlterColumn(name, typ, options...) + at.AlterColumnType(name, typ, options...) + s.add(at.Table) +} + +// AlterColumnConstraints with name. +func (s *Schema) AlterColumnConstraints(table string, name string, options ...ColumnOption) { + at := alterTable(table, nil) + at.AlterColumnConstraints(name, options...) s.add(at.Table) } diff --git a/schema_options.go b/schema_options.go index 5ffe6e56..7c7276cf 100644 --- a/schema_options.go +++ b/schema_options.go @@ -47,6 +47,7 @@ func (r Primary) applyColumn(column *Column) { type Unique bool func (r Unique) applyColumn(column *Column) { + column.AlterConstr |= ColumnConstraintUnique column.Unique = bool(r) } @@ -58,6 +59,7 @@ func (r Unique) applyIndex(index *Index) { type Required bool func (r Required) applyColumn(column *Column) { + column.AlterConstr |= ColumnConstraintRequired column.Required = bool(r) } @@ -87,6 +89,7 @@ type defaultValue struct { } func (d defaultValue) applyColumn(column *Column) { + column.AlterConstr |= ColumnConstraintDefault column.Default = d.value } diff --git a/schema_test.go b/schema_test.go index 670a9abe..4d323291 100644 --- a/schema_test.go +++ b/schema_test.go @@ -118,16 +118,30 @@ func TestSchema_AddColumn(t *testing.T) { }, schema.Migrations[0]) } -func TestSchema_AlterColumn(t *testing.T) { +func TestSchema_AlterColumnType(t *testing.T) { var schema Schema - schema.AlterColumn("products", "description", "", Default("test")) + schema.AlterColumnType("products", "description", String, Limit(100)) assert.Equal(t, Table{ Op: SchemaAlter, Name: "products", Definitions: []TableDefinition{ - Column{Name: "description", Type: "", Op: SchemaAlter, Default: "test"}, + Column{Name: "description", Type: String, Op: SchemaAlter, Limit: 100, AlterConstr: ColumnConstraintType}, + }, + }, schema.Migrations[0]) +} + +func TestSchema_AlterColumnConstraints(t *testing.T) { + var schema Schema + + schema.AlterColumnConstraints("products", "description", Required(true), Default("")) + + assert.Equal(t, Table{ + Op: SchemaAlter, + Name: "products", + Definitions: []TableDefinition{ + Column{Name: "description", Op: SchemaAlter, Required: true, Default: "", AlterConstr: ColumnConstraintRequired | ColumnConstraintDefault}, }, }, schema.Migrations[0]) } diff --git a/table.go b/table.go index 8baba579..2569b422 100644 --- a/table.go +++ b/table.go @@ -136,9 +136,14 @@ func (at *AlterTable) RenameColumn(name string, newName string, options ...Colum at.Definitions = append(at.Definitions, renameColumn(name, newName, options)) } -// AlterColumn with name and type. -func (at *AlterTable) AlterColumn(name string, typ ColumnType, options ...ColumnOption) { - at.Definitions = append(at.Definitions, alterColumn(name, typ, options)) +// AlterColumnType with name. +func (at *AlterTable) AlterColumnType(name string, typ ColumnType, options ...ColumnOption) { + at.Definitions = append(at.Definitions, alterColumnType(name, typ, options)) +} + +// AlterColumnConstraints with name. +func (at *AlterTable) AlterColumnConstraints(name string, options ...ColumnOption) { + at.Definitions = append(at.Definitions, alterColumnConstraints(name, options)) } // DropColumn from this table. From bf877938a1d33554097e5c865c5246266443a495 Mon Sep 17 00:00:00 2001 From: Lauris BH Date: Tue, 8 Nov 2022 16:56:36 +0200 Subject: [PATCH 03/12] Rename for constraint field to be more generic --- column.go | 36 ++++++++++++++++++------------------ schema_options.go | 6 +++--- schema_test.go | 4 ++-- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/column.go b/column.go index 64118ae6..4278f208 100644 --- a/column.go +++ b/column.go @@ -50,20 +50,20 @@ const ( // Column definition. type Column struct { - Op SchemaOp - Name string - Type ColumnType - Rename string - AlterConstr ColumnConstraint - Primary bool - Unique bool - Required bool - Unsigned bool - Limit int - Precision int - Scale int - Default any - Options string + Op SchemaOp + Constr ColumnConstraint + Name string + Type ColumnType + Rename string + Primary bool + Unique bool + Required bool + Unsigned bool + Limit int + Precision int + Scale int + Default any + Options string } func (Column) internalTableDefinition() {} @@ -92,10 +92,10 @@ func renameColumn(name string, newName string, options []ColumnOption) Column { func alterColumnType(name string, typ ColumnType, options []ColumnOption) Column { column := Column{ - Op: SchemaAlter, - Name: name, - Type: typ, - AlterConstr: ColumnConstraintType, + Op: SchemaAlter, + Name: name, + Type: typ, + Constr: ColumnConstraintType, } applyColumnOptions(&column, options) diff --git a/schema_options.go b/schema_options.go index 7c7276cf..ee9e8b28 100644 --- a/schema_options.go +++ b/schema_options.go @@ -47,7 +47,7 @@ func (r Primary) applyColumn(column *Column) { type Unique bool func (r Unique) applyColumn(column *Column) { - column.AlterConstr |= ColumnConstraintUnique + column.Constr |= ColumnConstraintUnique column.Unique = bool(r) } @@ -59,7 +59,7 @@ func (r Unique) applyIndex(index *Index) { type Required bool func (r Required) applyColumn(column *Column) { - column.AlterConstr |= ColumnConstraintRequired + column.Constr |= ColumnConstraintRequired column.Required = bool(r) } @@ -89,7 +89,7 @@ type defaultValue struct { } func (d defaultValue) applyColumn(column *Column) { - column.AlterConstr |= ColumnConstraintDefault + column.Constr |= ColumnConstraintDefault column.Default = d.value } diff --git a/schema_test.go b/schema_test.go index 4d323291..ceec3e48 100644 --- a/schema_test.go +++ b/schema_test.go @@ -127,7 +127,7 @@ func TestSchema_AlterColumnType(t *testing.T) { Op: SchemaAlter, Name: "products", Definitions: []TableDefinition{ - Column{Name: "description", Type: String, Op: SchemaAlter, Limit: 100, AlterConstr: ColumnConstraintType}, + Column{Name: "description", Type: String, Op: SchemaAlter, Limit: 100, Constr: ColumnConstraintType}, }, }, schema.Migrations[0]) } @@ -141,7 +141,7 @@ func TestSchema_AlterColumnConstraints(t *testing.T) { Op: SchemaAlter, Name: "products", Definitions: []TableDefinition{ - Column{Name: "description", Op: SchemaAlter, Required: true, Default: "", AlterConstr: ColumnConstraintRequired | ColumnConstraintDefault}, + Column{Name: "description", Op: SchemaAlter, Required: true, Default: "", Constr: ColumnConstraintRequired | ColumnConstraintDefault}, }, }, schema.Migrations[0]) } From c0ffc2772e73d9817af42478a55489ba2fedbeac Mon Sep 17 00:00:00 2001 From: Lauris BH Date: Tue, 8 Nov 2022 17:08:07 +0200 Subject: [PATCH 04/12] Fix tests --- column.go | 7 ++--- column_test.go | 3 +++ schema_test.go | 12 ++++----- table_test.go | 72 ++++++++++++++++++++++++++++++-------------------- 4 files changed, 57 insertions(+), 37 deletions(-) diff --git a/column.go b/column.go index 4278f208..df6c94a0 100644 --- a/column.go +++ b/column.go @@ -70,9 +70,10 @@ func (Column) internalTableDefinition() {} func createColumn(name string, typ ColumnType, options []ColumnOption) Column { column := Column{ - Op: SchemaCreate, - Name: name, - Type: typ, + Op: SchemaCreate, + Name: name, + Type: typ, + Constr: ColumnConstraintType, } applyColumnOptions(&column, options) diff --git a/column_test.go b/column_test.go index 6c2a6d14..ea546e03 100644 --- a/column_test.go +++ b/column_test.go @@ -33,6 +33,7 @@ func TestCreateColumn(t *testing.T) { Precision: 5, Scale: 2, Default: 0, + Constr: ColumnConstraintType | ColumnConstraintUnique | ColumnConstraintRequired | ColumnConstraintDefault, Options: "options", }, column) } @@ -53,6 +54,7 @@ func TestRenameColumn(t *testing.T) { assert.Equal(t, Column{ Op: SchemaRename, + Constr: ColumnConstraintRequired | ColumnConstraintDefault, Name: "add", Rename: "rename", Required: true, @@ -81,6 +83,7 @@ func TestDropColumn(t *testing.T) { assert.Equal(t, Column{ Op: SchemaDrop, + Constr: ColumnConstraintRequired | ColumnConstraintDefault, Name: "drop", Required: true, Unsigned: true, diff --git a/schema_test.go b/schema_test.go index ceec3e48..9f02a28b 100644 --- a/schema_test.go +++ b/schema_test.go @@ -33,9 +33,9 @@ func TestSchema_CreateTable(t *testing.T) { Op: SchemaCreate, Name: "products", Definitions: []TableDefinition{ - Column{Name: "id", Type: ID, Primary: true}, - Column{Name: "name", Type: String}, - Column{Name: "description", Type: Text}, + Column{Name: "id", Type: ID, Primary: true, Constr: ColumnConstraintType}, + Column{Name: "name", Type: String, Constr: ColumnConstraintType}, + Column{Name: "description", Type: Text, Constr: ColumnConstraintType}, }, }, schema.Migrations[0]) @@ -48,7 +48,7 @@ func TestSchema_CreateTable(t *testing.T) { Name: "wishlists", Optional: true, Definitions: []TableDefinition{ - Column{Name: "id", Type: ID, Primary: true}, + Column{Name: "id", Type: ID, Primary: true, Constr: ColumnConstraintType}, }, }, schema.Migrations[1]) @@ -67,7 +67,7 @@ func TestSchema_AlterTable(t *testing.T) { Op: SchemaAlter, Name: "users", Definitions: []TableDefinition{ - Column{Name: "verified", Type: Bool, Op: SchemaCreate}, + Column{Name: "verified", Type: Bool, Op: SchemaCreate, Constr: ColumnConstraintType}, Column{Name: "name", Rename: "fullname", Op: SchemaRename}, }, }, schema.Migrations[0]) @@ -113,7 +113,7 @@ func TestSchema_AddColumn(t *testing.T) { Op: SchemaAlter, Name: "products", Definitions: []TableDefinition{ - Column{Name: "description", Type: String, Op: SchemaCreate}, + Column{Name: "description", Type: String, Op: SchemaCreate, Constr: ColumnConstraintType}, }, }, schema.Migrations[0]) } diff --git a/table_test.go b/table_test.go index 33d6b8e6..2be648c0 100644 --- a/table_test.go +++ b/table_test.go @@ -12,8 +12,9 @@ func TestTable(t *testing.T) { t.Run("Column", func(t *testing.T) { table.Column("column", String) assert.Equal(t, Column{ - Name: "column", - Type: String, + Name: "column", + Type: String, + Constr: ColumnConstraintType, }, table.Definitions[len(table.Definitions)-1]) }) @@ -23,6 +24,7 @@ func TestTable(t *testing.T) { Name: "id", Type: ID, Primary: true, + Constr: ColumnConstraintType, }, table.Definitions[len(table.Definitions)-1]) }) @@ -32,110 +34,124 @@ func TestTable(t *testing.T) { Name: "big_id", Type: BigID, Primary: true, + Constr: ColumnConstraintType, }, table.Definitions[len(table.Definitions)-1]) }) t.Run("IDNotPrimaryKey", func(t *testing.T) { table.ID("id", Primary(false)) assert.Equal(t, Column{ - Name: "id", - Type: ID, + Name: "id", + Type: ID, + Constr: ColumnConstraintType, }, table.Definitions[len(table.Definitions)-1]) }) t.Run("Bool", func(t *testing.T) { table.Bool("boolean") assert.Equal(t, Column{ - Name: "boolean", - Type: Bool, + Name: "boolean", + Type: Bool, + Constr: ColumnConstraintType, }, table.Definitions[len(table.Definitions)-1]) }) t.Run("SmallInt", func(t *testing.T) { table.SmallInt("smallint") assert.Equal(t, Column{ - Name: "smallint", - Type: SmallInt, + Name: "smallint", + Type: SmallInt, + Constr: ColumnConstraintType, }, table.Definitions[len(table.Definitions)-1]) }) t.Run("Int", func(t *testing.T) { table.Int("integer") assert.Equal(t, Column{ - Name: "integer", - Type: Int, + Name: "integer", + Type: Int, + Constr: ColumnConstraintType, }, table.Definitions[len(table.Definitions)-1]) }) t.Run("BigInt", func(t *testing.T) { table.BigInt("bigint") assert.Equal(t, Column{ - Name: "bigint", - Type: BigInt, + Name: "bigint", + Type: BigInt, + Constr: ColumnConstraintType, }, table.Definitions[len(table.Definitions)-1]) }) t.Run("Float", func(t *testing.T) { table.Float("float") assert.Equal(t, Column{ - Name: "float", - Type: Float, + Name: "float", + Type: Float, + Constr: ColumnConstraintType, }, table.Definitions[len(table.Definitions)-1]) }) t.Run("Decimal", func(t *testing.T) { table.Decimal("decimal") assert.Equal(t, Column{ - Name: "decimal", - Type: Decimal, + Name: "decimal", + Type: Decimal, + Constr: ColumnConstraintType, }, table.Definitions[len(table.Definitions)-1]) }) t.Run("String", func(t *testing.T) { table.String("string") assert.Equal(t, Column{ - Name: "string", - Type: String, + Name: "string", + Type: String, + Constr: ColumnConstraintType, }, table.Definitions[len(table.Definitions)-1]) }) t.Run("Text", func(t *testing.T) { table.Text("text") assert.Equal(t, Column{ - Name: "text", - Type: Text, + Name: "text", + Type: Text, + Constr: ColumnConstraintType, }, table.Definitions[len(table.Definitions)-1]) }) t.Run("JSON", func(t *testing.T) { table.JSON("json") assert.Equal(t, Column{ - Name: "json", - Type: JSON, + Name: "json", + Type: JSON, + Constr: ColumnConstraintType, }, table.Definitions[len(table.Definitions)-1]) }) t.Run("Date", func(t *testing.T) { table.Date("date") assert.Equal(t, Column{ - Name: "date", - Type: Date, + Name: "date", + Type: Date, + Constr: ColumnConstraintType, }, table.Definitions[len(table.Definitions)-1]) }) t.Run("DateTime", func(t *testing.T) { table.DateTime("datetime") assert.Equal(t, Column{ - Name: "datetime", - Type: DateTime, + Name: "datetime", + Type: DateTime, + Constr: ColumnConstraintType, }, table.Definitions[len(table.Definitions)-1]) }) t.Run("Time", func(t *testing.T) { table.Time("time") assert.Equal(t, Column{ - Name: "time", - Type: Time, + Name: "time", + Type: Time, + Constr: ColumnConstraintType, }, table.Definitions[len(table.Definitions)-1]) }) From bded116319614035bae571f9f2f5189953023e1a Mon Sep 17 00:00:00 2001 From: Lauris BH Date: Tue, 8 Nov 2022 22:35:34 +0200 Subject: [PATCH 05/12] Unique can not be dropped by column, remove currently --- column.go | 2 -- column_test.go | 2 +- schema_options.go | 1 - 3 files changed, 1 insertion(+), 4 deletions(-) diff --git a/column.go b/column.go index df6c94a0..ba2166fa 100644 --- a/column.go +++ b/column.go @@ -40,8 +40,6 @@ type ColumnConstraint uint16 const ( // ColumnConstraintType that column type has changed. ColumnConstraintType ColumnConstraint = 1 << iota - // ColumnConstraintUnique that column uniqueness has changed. - ColumnConstraintUnique // ColumnConstraintUnique that column required has changed. ColumnConstraintRequired // ColumnConstraintDefault that column default value has changed. diff --git a/column_test.go b/column_test.go index ea546e03..3b43ea4f 100644 --- a/column_test.go +++ b/column_test.go @@ -33,7 +33,7 @@ func TestCreateColumn(t *testing.T) { Precision: 5, Scale: 2, Default: 0, - Constr: ColumnConstraintType | ColumnConstraintUnique | ColumnConstraintRequired | ColumnConstraintDefault, + Constr: ColumnConstraintType | ColumnConstraintRequired | ColumnConstraintDefault, Options: "options", }, column) } diff --git a/schema_options.go b/schema_options.go index ee9e8b28..937d9fc9 100644 --- a/schema_options.go +++ b/schema_options.go @@ -47,7 +47,6 @@ func (r Primary) applyColumn(column *Column) { type Unique bool func (r Unique) applyColumn(column *Column) { - column.Constr |= ColumnConstraintUnique column.Unique = bool(r) } From 1b07f1a5dddc788f6c41a0020f2e26c3c65cde8e Mon Sep 17 00:00:00 2001 From: Lauris BH Date: Tue, 8 Nov 2022 23:46:43 +0200 Subject: [PATCH 06/12] Refactor to constraints be only for alter column --- column.go | 57 +++++++++++++++++++++---------------- column_test.go | 3 -- query.go | 16 +++++------ schema_options.go | 41 +++++++++++++++++++++++++-- schema_test.go | 17 +++++------ table.go | 10 +++++-- table_test.go | 72 ++++++++++++++++++----------------------------- 7 files changed, 124 insertions(+), 92 deletions(-) diff --git a/column.go b/column.go index ba2166fa..4cc44047 100644 --- a/column.go +++ b/column.go @@ -34,22 +34,22 @@ const ( Time ColumnType = "TIME" ) -// ColumnConstraint bit flags. -type ColumnConstraint uint16 +// AlterColumnConstraint enum. +type AlterColumnConstraint uint16 const ( - // ColumnConstraintType that column type has changed. - ColumnConstraintType ColumnConstraint = 1 << iota - // ColumnConstraintUnique that column required has changed. - ColumnConstraintRequired - // ColumnConstraintDefault that column default value has changed. - ColumnConstraintDefault + // AlterColumnType operation. + AlterColumnType AlterColumnConstraint = iota + 1 + // AlterColumnRequired operation. + AlterColumnRequired + // AlterColumnDefault operation. + AlterColumnDefault ) // Column definition. type Column struct { Op SchemaOp - Constr ColumnConstraint + Constr AlterColumnConstraint Name string Type ColumnType Rename string @@ -68,10 +68,9 @@ func (Column) internalTableDefinition() {} func createColumn(name string, typ ColumnType, options []ColumnOption) Column { column := Column{ - Op: SchemaCreate, - Name: name, - Type: typ, - Constr: ColumnConstraintType, + Op: SchemaCreate, + Name: name, + Type: typ, } applyColumnOptions(&column, options) @@ -89,26 +88,36 @@ func renameColumn(name string, newName string, options []ColumnOption) Column { return column } -func alterColumnType(name string, typ ColumnType, options []ColumnOption) Column { +func alterColumnType(name string, typ ColumnType, options []ColumnOption) []Column { column := Column{ Op: SchemaAlter, Name: name, Type: typ, - Constr: ColumnConstraintType, + Constr: AlterColumnType, + } + for _, option := range options { + if option.isConstraint() { + continue + } + option.applyColumn(&column) } - applyColumnOptions(&column, options) - return column + return append([]Column{column}, alterColumnConstraints(name, options)...) } -func alterColumnConstraints(name string, options []ColumnOption) Column { - column := Column{ - Op: SchemaAlter, - Name: name, +func alterColumnConstraints(name string, options []ColumnOption) []Column { + constrs := make([]Column, 0, len(options)) + for _, option := range options { + if option.isConstraint() { + column := Column{ + Op: SchemaAlter, + Name: name, + } + option.applyColumn(&column) + constrs = append(constrs, column) + } } - - applyColumnOptions(&column, options) - return column + return constrs } func dropColumn(name string, options []ColumnOption) Column { diff --git a/column_test.go b/column_test.go index 3b43ea4f..6c2a6d14 100644 --- a/column_test.go +++ b/column_test.go @@ -33,7 +33,6 @@ func TestCreateColumn(t *testing.T) { Precision: 5, Scale: 2, Default: 0, - Constr: ColumnConstraintType | ColumnConstraintRequired | ColumnConstraintDefault, Options: "options", }, column) } @@ -54,7 +53,6 @@ func TestRenameColumn(t *testing.T) { assert.Equal(t, Column{ Op: SchemaRename, - Constr: ColumnConstraintRequired | ColumnConstraintDefault, Name: "add", Rename: "rename", Required: true, @@ -83,7 +81,6 @@ func TestDropColumn(t *testing.T) { assert.Equal(t, Column{ Op: SchemaDrop, - Constr: ColumnConstraintRequired | ColumnConstraintDefault, Name: "drop", Required: true, Unsigned: true, diff --git a/query.go b/query.go index 58445b62..9be2a3b1 100644 --- a/query.go +++ b/query.go @@ -16,9 +16,7 @@ type QueryPopulator interface { // Build for given table using given queriers. func Build(table string, queriers ...Querier) Query { - var ( - query = newQuery() - ) + query := newQuery() if len(queriers) > 0 { _, query.empty = queriers[0].(Query) @@ -255,9 +253,7 @@ func (q Query) Sort(fields ...string) Query { // SortAsc query. func (q Query) SortAsc(fields ...string) Query { - var ( - offset = len(q.SortQuery) - ) + offset := len(q.SortQuery) q.SortQuery = append(q.SortQuery, make([]SortQuery, len(fields))...) for i := range fields { @@ -269,9 +265,7 @@ func (q Query) SortAsc(fields ...string) Query { // SortDesc query. func (q Query) SortDesc(fields ...string) Query { - var ( - offset = len(q.SortQuery) - ) + offset := len(q.SortQuery) q.SortQuery = append(q.SortQuery, make([]SortQuery, len(fields))...) for i := range fields { @@ -538,6 +532,10 @@ func (l Limit) applyColumn(column *Column) { column.Limit = int(l) } +func (l Limit) isConstraint() bool { + return false +} + // Lock query. // This query will be ignored if used outside of transaction. type Lock string diff --git a/schema_options.go b/schema_options.go index 937d9fc9..e0077738 100644 --- a/schema_options.go +++ b/schema_options.go @@ -16,6 +16,7 @@ func applyTableOptions(table *Table, options []TableOption) { // Available options are: Nil, Unsigned, Limit, Precision, Scale, Default, Comment, Options. type ColumnOption interface { applyColumn(column *Column) + isConstraint() bool } func applyColumnOptions(column *Column, options []ColumnOption) { @@ -43,6 +44,10 @@ func (r Primary) applyColumn(column *Column) { column.Primary = bool(r) } +func (r Primary) isConstraint() bool { + return false +} + // Unique set column as unique. type Unique bool @@ -50,6 +55,10 @@ func (r Unique) applyColumn(column *Column) { column.Unique = bool(r) } +func (r Unique) isConstraint() bool { + return false +} + func (r Unique) applyIndex(index *Index) { index.Unique = bool(r) } @@ -58,8 +67,14 @@ func (r Unique) applyIndex(index *Index) { type Required bool func (r Required) applyColumn(column *Column) { - column.Constr |= ColumnConstraintRequired column.Required = bool(r) + if column.Op == SchemaAlter { + column.Constr = AlterColumnRequired + } +} + +func (r Required) isConstraint() bool { + return true } // Unsigned sets integer column to be unsigned. @@ -69,6 +84,10 @@ func (u Unsigned) applyColumn(column *Column) { column.Unsigned = bool(u) } +func (r Unsigned) isConstraint() bool { + return false +} + // Precision defines the precision for the decimal fields, representing the total number of digits in the number. type Precision int @@ -76,6 +95,10 @@ func (p Precision) applyColumn(column *Column) { column.Precision = int(p) } +func (p Precision) isConstraint() bool { + return false +} + // Scale Defines the scale for the decimal fields, representing the number of digits after the decimal point. type Scale int @@ -83,13 +106,23 @@ func (s Scale) applyColumn(column *Column) { column.Scale = int(s) } +func (s Scale) isConstraint() bool { + return false +} + type defaultValue struct { value any } func (d defaultValue) applyColumn(column *Column) { - column.Constr |= ColumnConstraintDefault column.Default = d.value + if column.Op == SchemaAlter { + column.Constr = AlterColumnDefault + } +} + +func (d defaultValue) isConstraint() bool { + return true } // Default allows to set a default value on the column.). @@ -122,6 +155,10 @@ func (o Options) applyColumn(column *Column) { column.Options = string(o) } +func (o Options) isConstraint() bool { + return false +} + func (o Options) applyIndex(index *Index) { index.Options = string(o) } diff --git a/schema_test.go b/schema_test.go index 9f02a28b..6936ca5f 100644 --- a/schema_test.go +++ b/schema_test.go @@ -33,9 +33,9 @@ func TestSchema_CreateTable(t *testing.T) { Op: SchemaCreate, Name: "products", Definitions: []TableDefinition{ - Column{Name: "id", Type: ID, Primary: true, Constr: ColumnConstraintType}, - Column{Name: "name", Type: String, Constr: ColumnConstraintType}, - Column{Name: "description", Type: Text, Constr: ColumnConstraintType}, + Column{Name: "id", Type: ID, Primary: true}, + Column{Name: "name", Type: String}, + Column{Name: "description", Type: Text}, }, }, schema.Migrations[0]) @@ -48,7 +48,7 @@ func TestSchema_CreateTable(t *testing.T) { Name: "wishlists", Optional: true, Definitions: []TableDefinition{ - Column{Name: "id", Type: ID, Primary: true, Constr: ColumnConstraintType}, + Column{Name: "id", Type: ID, Primary: true}, }, }, schema.Migrations[1]) @@ -67,7 +67,7 @@ func TestSchema_AlterTable(t *testing.T) { Op: SchemaAlter, Name: "users", Definitions: []TableDefinition{ - Column{Name: "verified", Type: Bool, Op: SchemaCreate, Constr: ColumnConstraintType}, + Column{Name: "verified", Type: Bool, Op: SchemaCreate}, Column{Name: "name", Rename: "fullname", Op: SchemaRename}, }, }, schema.Migrations[0]) @@ -113,7 +113,7 @@ func TestSchema_AddColumn(t *testing.T) { Op: SchemaAlter, Name: "products", Definitions: []TableDefinition{ - Column{Name: "description", Type: String, Op: SchemaCreate, Constr: ColumnConstraintType}, + Column{Name: "description", Type: String, Op: SchemaCreate}, }, }, schema.Migrations[0]) } @@ -127,7 +127,7 @@ func TestSchema_AlterColumnType(t *testing.T) { Op: SchemaAlter, Name: "products", Definitions: []TableDefinition{ - Column{Name: "description", Type: String, Op: SchemaAlter, Limit: 100, Constr: ColumnConstraintType}, + Column{Name: "description", Type: String, Op: SchemaAlter, Limit: 100, Constr: AlterColumnType}, }, }, schema.Migrations[0]) } @@ -141,7 +141,8 @@ func TestSchema_AlterColumnConstraints(t *testing.T) { Op: SchemaAlter, Name: "products", Definitions: []TableDefinition{ - Column{Name: "description", Op: SchemaAlter, Required: true, Default: "", Constr: ColumnConstraintRequired | ColumnConstraintDefault}, + Column{Name: "description", Op: SchemaAlter, Required: true, Constr: AlterColumnRequired}, + Column{Name: "description", Op: SchemaAlter, Default: "", Constr: AlterColumnDefault}, }, }, schema.Migrations[0]) } diff --git a/table.go b/table.go index 2569b422..e036aa3d 100644 --- a/table.go +++ b/table.go @@ -138,12 +138,18 @@ func (at *AlterTable) RenameColumn(name string, newName string, options ...Colum // AlterColumnType with name. func (at *AlterTable) AlterColumnType(name string, typ ColumnType, options ...ColumnOption) { - at.Definitions = append(at.Definitions, alterColumnType(name, typ, options)) + defs := alterColumnType(name, typ, options) + for _, def := range defs { + at.Definitions = append(at.Definitions, def) + } } // AlterColumnConstraints with name. func (at *AlterTable) AlterColumnConstraints(name string, options ...ColumnOption) { - at.Definitions = append(at.Definitions, alterColumnConstraints(name, options)) + defs := alterColumnConstraints(name, options) + for _, def := range defs { + at.Definitions = append(at.Definitions, def) + } } // DropColumn from this table. diff --git a/table_test.go b/table_test.go index 2be648c0..33d6b8e6 100644 --- a/table_test.go +++ b/table_test.go @@ -12,9 +12,8 @@ func TestTable(t *testing.T) { t.Run("Column", func(t *testing.T) { table.Column("column", String) assert.Equal(t, Column{ - Name: "column", - Type: String, - Constr: ColumnConstraintType, + Name: "column", + Type: String, }, table.Definitions[len(table.Definitions)-1]) }) @@ -24,7 +23,6 @@ func TestTable(t *testing.T) { Name: "id", Type: ID, Primary: true, - Constr: ColumnConstraintType, }, table.Definitions[len(table.Definitions)-1]) }) @@ -34,124 +32,110 @@ func TestTable(t *testing.T) { Name: "big_id", Type: BigID, Primary: true, - Constr: ColumnConstraintType, }, table.Definitions[len(table.Definitions)-1]) }) t.Run("IDNotPrimaryKey", func(t *testing.T) { table.ID("id", Primary(false)) assert.Equal(t, Column{ - Name: "id", - Type: ID, - Constr: ColumnConstraintType, + Name: "id", + Type: ID, }, table.Definitions[len(table.Definitions)-1]) }) t.Run("Bool", func(t *testing.T) { table.Bool("boolean") assert.Equal(t, Column{ - Name: "boolean", - Type: Bool, - Constr: ColumnConstraintType, + Name: "boolean", + Type: Bool, }, table.Definitions[len(table.Definitions)-1]) }) t.Run("SmallInt", func(t *testing.T) { table.SmallInt("smallint") assert.Equal(t, Column{ - Name: "smallint", - Type: SmallInt, - Constr: ColumnConstraintType, + Name: "smallint", + Type: SmallInt, }, table.Definitions[len(table.Definitions)-1]) }) t.Run("Int", func(t *testing.T) { table.Int("integer") assert.Equal(t, Column{ - Name: "integer", - Type: Int, - Constr: ColumnConstraintType, + Name: "integer", + Type: Int, }, table.Definitions[len(table.Definitions)-1]) }) t.Run("BigInt", func(t *testing.T) { table.BigInt("bigint") assert.Equal(t, Column{ - Name: "bigint", - Type: BigInt, - Constr: ColumnConstraintType, + Name: "bigint", + Type: BigInt, }, table.Definitions[len(table.Definitions)-1]) }) t.Run("Float", func(t *testing.T) { table.Float("float") assert.Equal(t, Column{ - Name: "float", - Type: Float, - Constr: ColumnConstraintType, + Name: "float", + Type: Float, }, table.Definitions[len(table.Definitions)-1]) }) t.Run("Decimal", func(t *testing.T) { table.Decimal("decimal") assert.Equal(t, Column{ - Name: "decimal", - Type: Decimal, - Constr: ColumnConstraintType, + Name: "decimal", + Type: Decimal, }, table.Definitions[len(table.Definitions)-1]) }) t.Run("String", func(t *testing.T) { table.String("string") assert.Equal(t, Column{ - Name: "string", - Type: String, - Constr: ColumnConstraintType, + Name: "string", + Type: String, }, table.Definitions[len(table.Definitions)-1]) }) t.Run("Text", func(t *testing.T) { table.Text("text") assert.Equal(t, Column{ - Name: "text", - Type: Text, - Constr: ColumnConstraintType, + Name: "text", + Type: Text, }, table.Definitions[len(table.Definitions)-1]) }) t.Run("JSON", func(t *testing.T) { table.JSON("json") assert.Equal(t, Column{ - Name: "json", - Type: JSON, - Constr: ColumnConstraintType, + Name: "json", + Type: JSON, }, table.Definitions[len(table.Definitions)-1]) }) t.Run("Date", func(t *testing.T) { table.Date("date") assert.Equal(t, Column{ - Name: "date", - Type: Date, - Constr: ColumnConstraintType, + Name: "date", + Type: Date, }, table.Definitions[len(table.Definitions)-1]) }) t.Run("DateTime", func(t *testing.T) { table.DateTime("datetime") assert.Equal(t, Column{ - Name: "datetime", - Type: DateTime, - Constr: ColumnConstraintType, + Name: "datetime", + Type: DateTime, }, table.Definitions[len(table.Definitions)-1]) }) t.Run("Time", func(t *testing.T) { table.Time("time") assert.Equal(t, Column{ - Name: "time", - Type: Time, - Constr: ColumnConstraintType, + Name: "time", + Type: Time, }, table.Definitions[len(table.Definitions)-1]) }) From 2fc0dda46df26c0750b93654a3f4b92f52f17362 Mon Sep 17 00:00:00 2001 From: Lauris BH Date: Wed, 9 Nov 2022 00:02:29 +0200 Subject: [PATCH 07/12] Update test coverage --- schema_test.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/schema_test.go b/schema_test.go index 6936ca5f..3c36309d 100644 --- a/schema_test.go +++ b/schema_test.go @@ -118,10 +118,10 @@ func TestSchema_AddColumn(t *testing.T) { }, schema.Migrations[0]) } -func TestSchema_AlterColumnType(t *testing.T) { +func TestSchema_AlterColumnTypeString(t *testing.T) { var schema Schema - schema.AlterColumnType("products", "description", String, Limit(100)) + schema.AlterColumnType("products", "description", String, Limit(100), Unique(false), Primary(false)) assert.Equal(t, Table{ Op: SchemaAlter, @@ -132,6 +132,20 @@ func TestSchema_AlterColumnType(t *testing.T) { }, schema.Migrations[0]) } +func TestSchema_AlterColumnTypeNumber(t *testing.T) { + var schema Schema + + schema.AlterColumnType("products", "description", Decimal, Scale(10), Precision(2), Unsigned(true), Options("")) + + assert.Equal(t, Table{ + Op: SchemaAlter, + Name: "products", + Definitions: []TableDefinition{ + Column{Name: "description", Type: Decimal, Op: SchemaAlter, Scale: 10, Precision: 2, Unsigned: true, Constr: AlterColumnType}, + }, + }, schema.Migrations[0]) +} + func TestSchema_AlterColumnConstraints(t *testing.T) { var schema Schema From 01ff1107ab2b798993c5bbad0b114d4e65437c49 Mon Sep 17 00:00:00 2001 From: Lauris BH Date: Wed, 9 Nov 2022 01:12:18 +0200 Subject: [PATCH 08/12] Refactor for better readability --- column.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/column.go b/column.go index 4cc44047..e49f83d4 100644 --- a/column.go +++ b/column.go @@ -108,14 +108,15 @@ func alterColumnType(name string, typ ColumnType, options []ColumnOption) []Colu func alterColumnConstraints(name string, options []ColumnOption) []Column { constrs := make([]Column, 0, len(options)) for _, option := range options { - if option.isConstraint() { - column := Column{ - Op: SchemaAlter, - Name: name, - } - option.applyColumn(&column) - constrs = append(constrs, column) + if !option.isConstraint() { + continue } + column := Column{ + Op: SchemaAlter, + Name: name, + } + option.applyColumn(&column) + constrs = append(constrs, column) } return constrs } From 85179902ccebd0552109d04dd26496e04a145026 Mon Sep 17 00:00:00 2001 From: Lauris BH Date: Wed, 9 Nov 2022 01:20:09 +0200 Subject: [PATCH 09/12] Add alter column test case --- column_test.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/column_test.go b/column_test.go index 6c2a6d14..0ab46b64 100644 --- a/column_test.go +++ b/column_test.go @@ -65,6 +65,32 @@ func TestRenameColumn(t *testing.T) { }, column) } +func TestAlterColumn(t *testing.T) { + var ( + options = []ColumnOption{ + Required(true), + Limit(1000), + } + columns = alterColumnType("alter", String, options) + ) + + assert.Equal(t, []Column{ + { + Op: SchemaAlter, + Constr: AlterColumnType, + Type: String, + Name: "alter", + Limit: 1000, + }, + { + Op: SchemaAlter, + Constr: AlterColumnRequired, + Name: "alter", + Required: true, + }, + }, columns) +} + func TestDropColumn(t *testing.T) { var ( options = []ColumnOption{ From 7ba3e45b767e4799bcc15dc6883099c5b7c4ca96 Mon Sep 17 00:00:00 2001 From: Lauris BH Date: Wed, 9 Nov 2022 11:35:03 +0200 Subject: [PATCH 10/12] Review suggestions --- column.go | 20 ++++++++++---------- column_test.go | 18 +++++++++--------- schema.go | 6 +++--- schema_options.go | 4 ++-- schema_test.go | 12 ++++++------ table.go | 6 +++--- 6 files changed, 33 insertions(+), 33 deletions(-) diff --git a/column.go b/column.go index e49f83d4..bb73eb0b 100644 --- a/column.go +++ b/column.go @@ -34,12 +34,12 @@ const ( Time ColumnType = "TIME" ) -// AlterColumnConstraint enum. -type AlterColumnConstraint uint16 +// AlterColumnMode enum. +type AlterColumnMode uint16 const ( // AlterColumnType operation. - AlterColumnType AlterColumnConstraint = iota + 1 + AlterColumnType AlterColumnMode = iota + 1 // AlterColumnRequired operation. AlterColumnRequired // AlterColumnDefault operation. @@ -49,7 +49,7 @@ const ( // Column definition. type Column struct { Op SchemaOp - Constr AlterColumnConstraint + AlterMode AlterColumnMode Name string Type ColumnType Rename string @@ -90,10 +90,10 @@ func renameColumn(name string, newName string, options []ColumnOption) Column { func alterColumnType(name string, typ ColumnType, options []ColumnOption) []Column { column := Column{ - Op: SchemaAlter, - Name: name, - Type: typ, - Constr: AlterColumnType, + Op: SchemaAlter, + Name: name, + Type: typ, + AlterMode: AlterColumnType, } for _, option := range options { if option.isConstraint() { @@ -102,10 +102,10 @@ func alterColumnType(name string, typ ColumnType, options []ColumnOption) []Colu option.applyColumn(&column) } - return append([]Column{column}, alterColumnConstraints(name, options)...) + return append([]Column{column}, alterColumn(name, options)...) } -func alterColumnConstraints(name string, options []ColumnOption) []Column { +func alterColumn(name string, options []ColumnOption) []Column { constrs := make([]Column, 0, len(options)) for _, option := range options { if !option.isConstraint() { diff --git a/column_test.go b/column_test.go index 0ab46b64..40a146c7 100644 --- a/column_test.go +++ b/column_test.go @@ -76,17 +76,17 @@ func TestAlterColumn(t *testing.T) { assert.Equal(t, []Column{ { - Op: SchemaAlter, - Constr: AlterColumnType, - Type: String, - Name: "alter", - Limit: 1000, + Op: SchemaAlter, + AlterMode: AlterColumnType, + Type: String, + Name: "alter", + Limit: 1000, }, { - Op: SchemaAlter, - Constr: AlterColumnRequired, - Name: "alter", - Required: true, + Op: SchemaAlter, + AlterMode: AlterColumnRequired, + Name: "alter", + Required: true, }, }, columns) } diff --git a/schema.go b/schema.go index 815a7bb7..27109e28 100644 --- a/schema.go +++ b/schema.go @@ -88,10 +88,10 @@ func (s *Schema) AlterColumnType(table string, name string, typ ColumnType, opti s.add(at.Table) } -// AlterColumnConstraints with name. -func (s *Schema) AlterColumnConstraints(table string, name string, options ...ColumnOption) { +// AlterColumn with name. +func (s *Schema) AlterColumn(table string, name string, options ...ColumnOption) { at := alterTable(table, nil) - at.AlterColumnConstraints(name, options...) + at.AlterColumn(name, options...) s.add(at.Table) } diff --git a/schema_options.go b/schema_options.go index e0077738..9ad48f35 100644 --- a/schema_options.go +++ b/schema_options.go @@ -69,7 +69,7 @@ type Required bool func (r Required) applyColumn(column *Column) { column.Required = bool(r) if column.Op == SchemaAlter { - column.Constr = AlterColumnRequired + column.AlterMode = AlterColumnRequired } } @@ -117,7 +117,7 @@ type defaultValue struct { func (d defaultValue) applyColumn(column *Column) { column.Default = d.value if column.Op == SchemaAlter { - column.Constr = AlterColumnDefault + column.AlterMode = AlterColumnDefault } } diff --git a/schema_test.go b/schema_test.go index 3c36309d..320aeef9 100644 --- a/schema_test.go +++ b/schema_test.go @@ -127,7 +127,7 @@ func TestSchema_AlterColumnTypeString(t *testing.T) { Op: SchemaAlter, Name: "products", Definitions: []TableDefinition{ - Column{Name: "description", Type: String, Op: SchemaAlter, Limit: 100, Constr: AlterColumnType}, + Column{Name: "description", Type: String, Op: SchemaAlter, Limit: 100, AlterMode: AlterColumnType}, }, }, schema.Migrations[0]) } @@ -141,22 +141,22 @@ func TestSchema_AlterColumnTypeNumber(t *testing.T) { Op: SchemaAlter, Name: "products", Definitions: []TableDefinition{ - Column{Name: "description", Type: Decimal, Op: SchemaAlter, Scale: 10, Precision: 2, Unsigned: true, Constr: AlterColumnType}, + Column{Name: "description", Type: Decimal, Op: SchemaAlter, Scale: 10, Precision: 2, Unsigned: true, AlterMode: AlterColumnType}, }, }, schema.Migrations[0]) } -func TestSchema_AlterColumnConstraints(t *testing.T) { +func TestSchema_AlterColumn(t *testing.T) { var schema Schema - schema.AlterColumnConstraints("products", "description", Required(true), Default("")) + schema.AlterColumn("products", "description", Required(true), Default("")) assert.Equal(t, Table{ Op: SchemaAlter, Name: "products", Definitions: []TableDefinition{ - Column{Name: "description", Op: SchemaAlter, Required: true, Constr: AlterColumnRequired}, - Column{Name: "description", Op: SchemaAlter, Default: "", Constr: AlterColumnDefault}, + Column{Name: "description", Op: SchemaAlter, Required: true, AlterMode: AlterColumnRequired}, + Column{Name: "description", Op: SchemaAlter, Default: "", AlterMode: AlterColumnDefault}, }, }, schema.Migrations[0]) } diff --git a/table.go b/table.go index e036aa3d..a15120be 100644 --- a/table.go +++ b/table.go @@ -144,9 +144,9 @@ func (at *AlterTable) AlterColumnType(name string, typ ColumnType, options ...Co } } -// AlterColumnConstraints with name. -func (at *AlterTable) AlterColumnConstraints(name string, options ...ColumnOption) { - defs := alterColumnConstraints(name, options) +// AlterColumn with name. +func (at *AlterTable) AlterColumn(name string, options ...ColumnOption) { + defs := alterColumn(name, options) for _, def := range defs { at.Definitions = append(at.Definitions, def) } From 244aeed53a126071769f53e826ff7de36026231c Mon Sep 17 00:00:00 2001 From: Lauris BH Date: Thu, 10 Nov 2022 02:37:16 +0200 Subject: [PATCH 11/12] Rename variable --- column.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/column.go b/column.go index bb73eb0b..31f0d1c6 100644 --- a/column.go +++ b/column.go @@ -106,7 +106,7 @@ func alterColumnType(name string, typ ColumnType, options []ColumnOption) []Colu } func alterColumn(name string, options []ColumnOption) []Column { - constrs := make([]Column, 0, len(options)) + columns := make([]Column, 0, len(options)) for _, option := range options { if !option.isConstraint() { continue @@ -116,9 +116,9 @@ func alterColumn(name string, options []ColumnOption) []Column { Name: name, } option.applyColumn(&column) - constrs = append(constrs, column) + columns = append(columns, column) } - return constrs + return columns } func dropColumn(name string, options []ColumnOption) Column { From a04418b7c786b520d59ef40308ce11abecff2c32 Mon Sep 17 00:00:00 2001 From: Lauris BH Date: Thu, 10 Nov 2022 14:26:04 +0200 Subject: [PATCH 12/12] Improve documentation --- schema.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/schema.go b/schema.go index 27109e28..6f2cc0f7 100644 --- a/schema.go +++ b/schema.go @@ -82,6 +82,10 @@ func (s *Schema) AddColumn(table string, name string, typ ColumnType, options .. } // AlterColumnType with name. +// +// Allows also changing other constraints like [rel.Default] and [rel.Required]. +// +// WARNING: Not supported by SQLite driver. func (s *Schema) AlterColumnType(table string, name string, typ ColumnType, options ...ColumnOption) { at := alterTable(table, nil) at.AlterColumnType(name, typ, options...) @@ -89,6 +93,13 @@ func (s *Schema) AlterColumnType(table string, name string, typ ColumnType, opti } // AlterColumn with name. +// +// Only [rel.Default] and [rel.Required] are supported. +// Support for underlying drivers might wary. For example PostgreSQL supports both, +// while Microsoft SQL Server and MySQL/MariaDB only supports [rel.Default]. +// See [Schema.AlterColumnType] if other constraints need to be changed also. +// +// WARNING: Not supported by SQLite driver. func (s *Schema) AlterColumn(table string, name string, options ...ColumnOption) { at := alterTable(table, nil) at.AlterColumn(name, options...)