refactor: rename support

This commit is contained in:
grandwizard28 2025-07-21 13:06:26 +05:30
parent 9a57c4e53f
commit e96eb187e4
No known key found for this signature in database
GPG Key ID: C7A26EDC5B7054B7
4 changed files with 61 additions and 40 deletions

View File

@ -38,7 +38,7 @@ func New(ctx context.Context, providerSettings factory.ProviderSettings, config
settings: settings, settings: settings,
operator: sqlschema.NewOperator(fmter, sqlschema.OperatorSupport{ operator: sqlschema.NewOperator(fmter, sqlschema.OperatorSupport{
SCreateAndDropConstraint: true, SCreateAndDropConstraint: true,
SAlterTableAddDropColumnIfNotExistsAndExists: true, SAlterTableAddAndDropColumnIfNotExistsAndExists: true,
SAlterTableAlterColumnSetAndDrop: true, SAlterTableAlterColumnSetAndDrop: true,
}), }),
}, nil }, nil

View File

@ -12,7 +12,7 @@ type OperatorSupport struct {
SCreateAndDropConstraint bool SCreateAndDropConstraint bool
// Support for `IF EXISTS` and `IF NOT EXISTS` in `ALTER TABLE ADD COLUMN` and `ALTER TABLE DROP COLUMN`. // Support for `IF EXISTS` and `IF NOT EXISTS` in `ALTER TABLE ADD COLUMN` and `ALTER TABLE DROP COLUMN`.
SAlterTableAddDropColumnIfNotExistsAndExists bool SAlterTableAddAndDropColumnIfNotExistsAndExists bool
// Support for altering columns such as `ALTER TABLE ALTER COLUMN SET NOT NULL`. // Support for altering columns such as `ALTER TABLE ALTER COLUMN SET NOT NULL`.
SAlterTableAlterColumnSetAndDrop bool SAlterTableAlterColumnSetAndDrop bool
@ -35,8 +35,10 @@ func (operator *Operator) CreateTable(table *Table) [][]byte {
} }
func (operator *Operator) RenameTable(table *Table, newName TableName) [][]byte { func (operator *Operator) RenameTable(table *Table, newName TableName) [][]byte {
sql := table.ToRenameSQL(operator.fmter, newName)
table.Name = newName table.Name = newName
return [][]byte{table.ToRenameSQL(operator.fmter, newName)}
return [][]byte{sql}
} }
func (operator *Operator) AlterTable(oldTable *Table, oldTableUniqueConstraints []*UniqueConstraint, newTable *Table) [][]byte { func (operator *Operator) AlterTable(oldTable *Table, oldTableUniqueConstraints []*UniqueConstraint, newTable *Table) [][]byte {
@ -142,7 +144,7 @@ func (operator *Operator) AddColumn(table *Table, uniqueConstraints []*UniqueCon
table.Columns = append(table.Columns, column) table.Columns = append(table.Columns, column)
sqls := [][]byte{ sqls := [][]byte{
column.ToAddSQL(operator.fmter, table.Name, operator.support.SAlterTableAddDropColumnIfNotExistsAndExists), column.ToAddSQL(operator.fmter, table.Name, operator.support.SAlterTableAddAndDropColumnIfNotExistsAndExists),
} }
if !column.Nullable { if !column.Nullable {
@ -226,7 +228,7 @@ func (operator *Operator) DropColumn(table *Table, column *Column) [][]byte {
table.Columns = append(table.Columns[:index], table.Columns[index+1:]...) table.Columns = append(table.Columns[:index], table.Columns[index+1:]...)
return [][]byte{column.ToDropSQL(operator.fmter, table.Name, operator.support.SAlterTableAddDropColumnIfNotExistsAndExists)} return [][]byte{column.ToDropSQL(operator.fmter, table.Name, operator.support.SAlterTableAddAndDropColumnIfNotExistsAndExists)}
} }
func (operator *Operator) CreateConstraint(table *Table, uniqueConstraints []*UniqueConstraint, constraint Constraint) [][]byte { func (operator *Operator) CreateConstraint(table *Table, uniqueConstraints []*UniqueConstraint, constraint Constraint) [][]byte {

View File

@ -59,7 +59,7 @@ func TestOperatorAddColumn(t *testing.T) {
expectedTable *Table expectedTable *Table
}{ }{
{ {
name: "NullableNoDefault_DoesNotExist_SAlterTableAddDropColumnIfNotExistsAndExistsTrue_SAlterTableAlterColumnSetAndDropTrue", name: "NullableNoDefault_DoesNotExist_SAlterTableAddAndDropColumnIfNotExistsAndExistsTrue_SAlterTableAlterColumnSetAndDropTrue",
table: &Table{ table: &Table{
Name: "users", Name: "users",
Columns: []*Column{ Columns: []*Column{
@ -69,7 +69,7 @@ func TestOperatorAddColumn(t *testing.T) {
column: &Column{Name: "name", DataType: DataTypeText, Nullable: true, Default: ""}, column: &Column{Name: "name", DataType: DataTypeText, Nullable: true, Default: ""},
val: nil, val: nil,
support: OperatorSupport{ support: OperatorSupport{
SAlterTableAddDropColumnIfNotExistsAndExists: true, SAlterTableAddAndDropColumnIfNotExistsAndExists: true,
SAlterTableAlterColumnSetAndDrop: true, SAlterTableAlterColumnSetAndDrop: true,
}, },
expectedSQLs: [][]byte{ expectedSQLs: [][]byte{
@ -84,7 +84,7 @@ func TestOperatorAddColumn(t *testing.T) {
}, },
}, },
{ {
name: "MismatchingDataType_DoesExist_SAlterTableAddDropColumnIfNotExistsAndExistsTrue_SAlterTableAlterColumnSetAndDropTrue", name: "MismatchingDataType_DoesExist_SAlterTableAddAndDropColumnIfNotExistsAndExistsTrue_SAlterTableAlterColumnSetAndDropTrue",
table: &Table{ table: &Table{
Name: "users", Name: "users",
Columns: []*Column{ Columns: []*Column{
@ -95,7 +95,7 @@ func TestOperatorAddColumn(t *testing.T) {
column: &Column{Name: "name", DataType: DataTypeBigInt, Nullable: true, Default: ""}, column: &Column{Name: "name", DataType: DataTypeBigInt, Nullable: true, Default: ""},
val: nil, val: nil,
support: OperatorSupport{ support: OperatorSupport{
SAlterTableAddDropColumnIfNotExistsAndExists: true, SAlterTableAddAndDropColumnIfNotExistsAndExists: true,
SAlterTableAlterColumnSetAndDrop: true, SAlterTableAlterColumnSetAndDrop: true,
}, },
expectedSQLs: [][]byte{}, expectedSQLs: [][]byte{},
@ -108,7 +108,7 @@ func TestOperatorAddColumn(t *testing.T) {
}, },
}, },
{ {
name: "NotNullableNoDefaultNoVal_DoesNotExist_SAlterTableAddDropColumnIfNotExistsAndExistsTrue_SAlterTableAlterColumnSetAndDropTrue", name: "NotNullableNoDefaultNoVal_DoesNotExist_SAlterTableAddAndDropColumnIfNotExistsAndExistsTrue_SAlterTableAlterColumnSetAndDropTrue",
table: &Table{ table: &Table{
Name: "users", Name: "users",
Columns: []*Column{ Columns: []*Column{
@ -118,7 +118,7 @@ func TestOperatorAddColumn(t *testing.T) {
column: &Column{Name: "name", DataType: DataTypeText, Nullable: false, Default: ""}, column: &Column{Name: "name", DataType: DataTypeText, Nullable: false, Default: ""},
val: nil, val: nil,
support: OperatorSupport{ support: OperatorSupport{
SAlterTableAddDropColumnIfNotExistsAndExists: true, SAlterTableAddAndDropColumnIfNotExistsAndExists: true,
SAlterTableAlterColumnSetAndDrop: true, SAlterTableAlterColumnSetAndDrop: true,
}, },
expectedSQLs: [][]byte{ expectedSQLs: [][]byte{
@ -135,7 +135,7 @@ func TestOperatorAddColumn(t *testing.T) {
}, },
}, },
{ {
name: "NotNullableNoDefault_DoesNotExist_SAlterTableAddDropColumnIfNotExistsAndExistsTrue_SAlterTableAlterColumnSetAndDropTrue", name: "NotNullableNoDefault_DoesNotExist_SAlterTableAddAndDropColumnIfNotExistsAndExistsTrue_SAlterTableAlterColumnSetAndDropTrue",
table: &Table{ table: &Table{
Name: "users", Name: "users",
Columns: []*Column{ Columns: []*Column{
@ -145,7 +145,7 @@ func TestOperatorAddColumn(t *testing.T) {
column: &Column{Name: "num", DataType: DataTypeInteger, Nullable: false, Default: ""}, column: &Column{Name: "num", DataType: DataTypeInteger, Nullable: false, Default: ""},
val: int64(100), val: int64(100),
support: OperatorSupport{ support: OperatorSupport{
SAlterTableAddDropColumnIfNotExistsAndExists: true, SAlterTableAddAndDropColumnIfNotExistsAndExists: true,
SAlterTableAlterColumnSetAndDrop: true, SAlterTableAlterColumnSetAndDrop: true,
}, },
expectedSQLs: [][]byte{ expectedSQLs: [][]byte{
@ -162,7 +162,7 @@ func TestOperatorAddColumn(t *testing.T) {
}, },
}, },
{ {
name: "NotNullableNoDefault_DoesNotExist_SAlterTableAddDropColumnIfNotExistsAndExistsTrue_SAlterTableAlterColumnSetAndDropFalse", name: "NotNullableNoDefault_DoesNotExist_SAlterTableAddAndDropColumnIfNotExistsAndExistsTrue_SAlterTableAlterColumnSetAndDropFalse",
table: &Table{ table: &Table{
Name: "users", Name: "users",
Columns: []*Column{ Columns: []*Column{
@ -176,7 +176,7 @@ func TestOperatorAddColumn(t *testing.T) {
{ColumnNames: []ColumnName{"name"}}, {ColumnNames: []ColumnName{"name"}},
}, },
support: OperatorSupport{ support: OperatorSupport{
SAlterTableAddDropColumnIfNotExistsAndExists: true, SAlterTableAddAndDropColumnIfNotExistsAndExists: true,
SAlterTableAlterColumnSetAndDrop: false, SAlterTableAlterColumnSetAndDrop: false,
}, },
expectedSQLs: [][]byte{ expectedSQLs: [][]byte{
@ -198,7 +198,7 @@ func TestOperatorAddColumn(t *testing.T) {
}, },
}, },
{ {
name: "MismatchingDataType_DoesExist_SAlterTableAddDropColumnIfNotExistsAndExistsTrue_SAlterTableAlterColumnSetAndDropFalse", name: "MismatchingDataType_DoesExist_SAlterTableAddAndDropColumnIfNotExistsAndExistsTrue_SAlterTableAlterColumnSetAndDropFalse",
table: &Table{ table: &Table{
Name: "users", Name: "users",
Columns: []*Column{ Columns: []*Column{
@ -209,7 +209,7 @@ func TestOperatorAddColumn(t *testing.T) {
column: &Column{Name: "name", DataType: DataTypeBigInt, Nullable: false, Default: ""}, column: &Column{Name: "name", DataType: DataTypeBigInt, Nullable: false, Default: ""},
val: nil, val: nil,
support: OperatorSupport{ support: OperatorSupport{
SAlterTableAddDropColumnIfNotExistsAndExists: true, SAlterTableAddAndDropColumnIfNotExistsAndExists: true,
SAlterTableAlterColumnSetAndDrop: false, SAlterTableAlterColumnSetAndDrop: false,
}, },
expectedSQLs: [][]byte{}, expectedSQLs: [][]byte{},
@ -730,7 +730,26 @@ func TestOperatorAlterTable(t *testing.T) {
expectedSQLs: [][]byte{}, expectedSQLs: [][]byte{},
}, },
{ {
name: "AddColumn_NullableNoDefault_SAlterTableAddDropColumnIfNotExistsAndExistsTrue", name: "RenameTable",
table: &Table{
Name: "users",
Columns: []*Column{
{Name: "id", DataType: DataTypeInteger, Nullable: false, Default: ""},
},
},
newTable: &Table{
Name: "users_new",
Columns: []*Column{
{Name: "id", DataType: DataTypeInteger, Nullable: false, Default: ""},
},
},
support: OperatorSupport{},
expectedSQLs: [][]byte{
[]byte(`ALTER TABLE "users" RENAME TO "users_new"`),
},
},
{
name: "AddColumn_NullableNoDefault_SAlterTableAddAndDropColumnIfNotExistsAndExistsTrue",
table: &Table{ table: &Table{
Name: "users", Name: "users",
Columns: []*Column{ Columns: []*Column{
@ -747,14 +766,14 @@ func TestOperatorAlterTable(t *testing.T) {
}, },
}, },
support: OperatorSupport{ support: OperatorSupport{
SAlterTableAddDropColumnIfNotExistsAndExists: true, SAlterTableAddAndDropColumnIfNotExistsAndExists: true,
}, },
expectedSQLs: [][]byte{ expectedSQLs: [][]byte{
[]byte(`ALTER TABLE "users" ADD COLUMN IF NOT EXISTS "age" INTEGER`), []byte(`ALTER TABLE "users" ADD COLUMN IF NOT EXISTS "age" INTEGER`),
}, },
}, },
{ {
name: "AddColumn_NullableNoDefault_SAlterTableAddDropColumnIfNotExistsAndExistsFalse", name: "AddColumn_NullableNoDefault_SAlterTableAddAndDropColumnIfNotExistsAndExistsFalse",
table: &Table{ table: &Table{
Name: "users", Name: "users",
Columns: []*Column{ Columns: []*Column{
@ -771,7 +790,7 @@ func TestOperatorAlterTable(t *testing.T) {
}, },
}, },
support: OperatorSupport{ support: OperatorSupport{
SAlterTableAddDropColumnIfNotExistsAndExists: false, SAlterTableAddAndDropColumnIfNotExistsAndExists: false,
}, },
expectedSQLs: [][]byte{ expectedSQLs: [][]byte{
[]byte(`ALTER TABLE "users" ADD COLUMN "age" INTEGER`), []byte(`ALTER TABLE "users" ADD COLUMN "age" INTEGER`),
@ -829,7 +848,7 @@ func TestOperatorAlterTable(t *testing.T) {
}, },
}, },
{ {
name: "DropPrimaryKeyConstraint_AlterColumn_SAlterTableAddDropColumnIfNotExistsAndExistsFalse_SAlterTableAlterColumnSetAndDropFalse_SCreateAndDropConstraintFalse", name: "DropPrimaryKeyConstraint_AlterColumnNullable_SAlterTableAddAndDropColumnIfNotExistsAndExistsFalse_SAlterTableAlterColumnSetAndDropFalse_SCreateAndDropConstraintFalse",
table: &Table{ table: &Table{
Name: "users", Name: "users",
Columns: []*Column{ Columns: []*Column{
@ -846,7 +865,7 @@ func TestOperatorAlterTable(t *testing.T) {
}, },
}, },
support: OperatorSupport{ support: OperatorSupport{
SAlterTableAddDropColumnIfNotExistsAndExists: false, SAlterTableAddAndDropColumnIfNotExistsAndExists: false,
SAlterTableAlterColumnSetAndDrop: false, SAlterTableAlterColumnSetAndDrop: false,
SCreateAndDropConstraint: false, SCreateAndDropConstraint: false,
}, },
@ -864,7 +883,7 @@ func TestOperatorAlterTable(t *testing.T) {
}, },
}, },
{ {
name: "DropPrimaryKeyConstraint_AlterColumn_SAlterTableAddDropColumnIfNotExistsAndExistsTrue_SAlterTableAlterColumnSetAndDropTrue_SCreateAndDropConstraintTrue", name: "DropPrimaryKeyConstraint_AlterColumnNullable_SAlterTableAddAndDropColumnIfNotExistsAndExistsTrue_SAlterTableAlterColumnSetAndDropTrue_SCreateAndDropConstraintTrue",
table: &Table{ table: &Table{
Name: "users", Name: "users",
Columns: []*Column{ Columns: []*Column{
@ -881,7 +900,7 @@ func TestOperatorAlterTable(t *testing.T) {
}, },
}, },
support: OperatorSupport{ support: OperatorSupport{
SAlterTableAddDropColumnIfNotExistsAndExists: true, SAlterTableAddAndDropColumnIfNotExistsAndExists: true,
SAlterTableAlterColumnSetAndDrop: true, SAlterTableAlterColumnSetAndDrop: true,
SCreateAndDropConstraint: true, SCreateAndDropConstraint: true,
}, },

View File

@ -34,7 +34,7 @@ func New(ctx context.Context, providerSettings factory.ProviderSettings, config
sqlstore: sqlstore, sqlstore: sqlstore,
operator: sqlschema.NewOperator(fmter, sqlschema.OperatorSupport{ operator: sqlschema.NewOperator(fmter, sqlschema.OperatorSupport{
SCreateAndDropConstraint: false, SCreateAndDropConstraint: false,
SAlterTableAddDropColumnIfNotExistsAndExists: false, SAlterTableAddAndDropColumnIfNotExistsAndExists: false,
SAlterTableAlterColumnSetAndDrop: false, SAlterTableAlterColumnSetAndDrop: false,
}), }),
}, nil }, nil