From 3dde538e6f6548a33abfc8646953f34f8811dae1 Mon Sep 17 00:00:00 2001 From: "Giau. Tran Minh" Date: Sun, 25 Jun 2023 14:25:01 +0700 Subject: [PATCH] sql/mssql: initial diff and migrate (#1783) --- sql/mssql/convert.go | 2 +- sql/mssql/diff.go | 67 ++++++++++++++++++++++++++++++++++++++++++++ sql/mssql/driver.go | 6 ++-- sql/mssql/migrate.go | 34 ++++++++++++++++++++++ 4 files changed, 106 insertions(+), 3 deletions(-) create mode 100644 sql/mssql/diff.go create mode 100644 sql/mssql/migrate.go diff --git a/sql/mssql/convert.go b/sql/mssql/convert.go index ed7b104630f..b4a3e013ca8 100644 --- a/sql/mssql/convert.go +++ b/sql/mssql/convert.go @@ -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) diff --git a/sql/mssql/diff.go b/sql/mssql/diff.go new file mode 100644 index 00000000000..a835d1824a1 --- /dev/null +++ b/sql/mssql/diff.go @@ -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 +} diff --git a/sql/mssql/driver.go b/sql/mssql/driver.go index 062f3c5819e..f6a5475f59c 100644 --- a/sql/mssql/driver.go +++ b/sql/mssql/driver.go @@ -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 } diff --git a/sql/mssql/migrate.go b/sql/mssql/migrate.go new file mode 100644 index 00000000000..77c2928671e --- /dev/null +++ b/sql/mssql/migrate.go @@ -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 +}