Skip to content

Commit

Permalink
sql/mssql: initial diff and migrate (#1783)
Browse files Browse the repository at this point in the history
  • Loading branch information
giautm committed Jun 29, 2023
1 parent a95bdca commit 3dde538
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 3 deletions.
2 changes: 1 addition & 1 deletion sql/mssql/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ type columnDesc struct {
}

// ParseType returns the schema.Type value represented by the given raw type.
// The raw value is expected to follow the format in PostgreSQL information schema
// The raw value is expected to follow the format in MS-SQL information schema
// or as an input for the CREATE TABLE statement.
func ParseType(typ string) (schema.Type, error) {
d, err := parseColumn(typ)
Expand Down
67 changes: 67 additions & 0 deletions sql/mssql/diff.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2021-present The Atlas Authors. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.

package mssql

import (
"ariga.io/atlas/sql/internal/sqlx"
"ariga.io/atlas/sql/schema"
)

// DefaultDiff provides basic diffing capabilities for MS-SQL dialects.
// Note, it is recommended to call Open, create a new Driver and use its Differ
// when a database connection is available.
var DefaultDiff schema.Differ = &sqlx.Diff{DiffDriver: &diff{}}

// A diff provides a MS-SQL implementation for sqlx.DiffDriver.
type diff struct{ conn }

// SchemaAttrDiff returns a changeset for migrating schema attributes from one state to the other.
func (d *diff) SchemaAttrDiff(_, _ *schema.Schema) []schema.Change {
// No special schema attribute diffing for MS-SQL.
return nil
}

// TableAttrDiff returns a changeset for migrating table attributes from one state to the other.
func (d *diff) TableAttrDiff(_, _ *schema.Table) ([]schema.Change, error) {
// Not implemented yet.
return nil, nil
}

// ColumnChange returns the schema changes (if any) for migrating one column to the other.
func (d *diff) ColumnChange(_ *schema.Table, _, _ *schema.Column) (schema.ChangeKind, error) {
// Not implemented yet.
return schema.NoChange, nil
}

// IsGeneratedIndexName reports if the index name was generated by the database.
func (d *diff) IsGeneratedIndexName(_ *schema.Table, _ *schema.Index) bool {
// Not implemented yet.
return false
}

// IndexAttrChanged reports if the index attributes were changed.
// The default type is BTREE if no type was specified.
func (*diff) IndexAttrChanged(_, _ []schema.Attr) bool {
// Not implemented yet.
return false
}

// IndexPartAttrChanged reports if the index-part attributes were changed.
func (*diff) IndexPartAttrChanged(_, _ *schema.Index, _ int) bool {
// Not implemented yet.
return false
}

// ReferenceChanged reports if the foreign key referential action was changed.
func (*diff) ReferenceChanged(_, _ schema.ReferenceOption) bool {
// Not implemented yet.
return false
}

// AnnotateChanges implements the sqlx.ChangeAnnotator interface.
func (*diff) AnnotateChanges(_ []schema.Change, _ *schema.DiffOptions) error {
// Not implemented yet.
return nil
}
6 changes: 4 additions & 2 deletions sql/mssql/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ func Open(db schema.ExecQuerier) (migrate.Driver, error) {
return nil, fmt.Errorf("mssql: scan server property: %w", err)
}
return &Driver{
conn: c,
Inspector: &inspect{c},
conn: c,
Differ: &sqlx.Diff{DiffDriver: &diff{c}},
Inspector: &inspect{c},
PlanApplier: &planApply{c},
}, nil
}

Expand Down
34 changes: 34 additions & 0 deletions sql/mssql/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2021-present The Atlas Authors. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.

package mssql

import (
"context"

"ariga.io/atlas/sql/internal/sqlx"
"ariga.io/atlas/sql/migrate"
"ariga.io/atlas/sql/schema"
)

// DefaultPlan provides basic planning capabilities for MS-SQL dialects.
// Note, it is recommended to call Open, create a new Driver and use its
// migrate.PlanApplier when a database connection is available.
var DefaultPlan migrate.PlanApplier = &planApply{conn: conn{ExecQuerier: sqlx.NoRows}}

// A planApply provides migration capabilities for schema elements.
type planApply struct{ conn }

// ApplyChanges applies the changes on the database. An error is returned
// if the driver is unable to produce a plan to do so, or one of the statements
// is failed or unsupported.
func (p *planApply) ApplyChanges(ctx context.Context, changes []schema.Change, opts ...migrate.PlanOption) error {
return sqlx.ApplyChanges(ctx, changes, p, opts...)
}

// PlanChanges returns a migration plan for the given schema changes.
func (p *planApply) PlanChanges(_ context.Context, _ string, _ []schema.Change, _ ...migrate.PlanOption) (*migrate.Plan, error) {
// Not implemented yet.
return nil, nil
}

0 comments on commit 3dde538

Please sign in to comment.