1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
// 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 sqlite
import (
"fmt"
"reflect"
"strconv"
"strings"
"ariga.io/atlas/sql/internal/sqlx"
"ariga.io/atlas/sql/schema"
)
// A Diff provides a SQLite implementation for sqlx.DiffDriver.
type Diff struct{}
// 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 SQLite.
return nil
}
// TableAttrDiff returns a changeset for migrating table attributes from one state to the other.
func (d *Diff) TableAttrDiff(from, to *schema.Table) ([]schema.Change, error) {
var changes []schema.Change
switch {
case sqlx.Has(from.Attrs, &WithoutRowID{}) && !sqlx.Has(to.Attrs, &WithoutRowID{}):
changes = append(changes, &schema.DropAttr{
A: &WithoutRowID{},
})
case !sqlx.Has(from.Attrs, &WithoutRowID{}) && sqlx.Has(to.Attrs, &WithoutRowID{}):
changes = append(changes, &schema.AddAttr{
A: &WithoutRowID{},
})
}
return append(changes, sqlx.CheckDiff(from, to)...), nil
}
// ColumnChange returns the schema changes (if any) for migrating one column to the other.
func (d *Diff) ColumnChange(_ *schema.Table, from, to *schema.Column) (schema.ChangeKind, error) {
change := sqlx.CommentChange(from.Attrs, to.Attrs)
if from.Type.Null != to.Type.Null {
change |= schema.ChangeNull
}
changed, err := d.typeChanged(from, to)
if err != nil {
return schema.NoChange, err
}
if changed {
change |= schema.ChangeType
}
if changed := d.defaultChanged(from, to); changed {
change |= schema.ChangeDefault
}
if d.generatedChanged(from, to) {
change |= schema.ChangeGenerated
}
return change, nil
}
// typeChanged reports if the column type was changed.
func (d *Diff) typeChanged(from, to *schema.Column) (bool, error) {
fromT, toT := from.Type.Type, to.Type.Type
if fromT == nil || toT == nil {
return false, fmt.Errorf("sqlite: missing type information for column %q", from.Name)
}
// Types are mismatched if they do not have the same "type affinity".
return reflect.TypeOf(fromT) != reflect.TypeOf(toT), nil
}
// defaultChanged reports if the default value of a column was changed.
func (d *Diff) defaultChanged(from, to *schema.Column) bool {
d1, ok1 := sqlx.DefaultValue(from)
d2, ok2 := sqlx.DefaultValue(to)
if ok1 != ok2 {
return true
}
if d1 == d2 {
return false
}
x1, err1 := sqlx.Unquote(d1)
x2, err2 := sqlx.Unquote(d2)
return err1 != nil || err2 != nil || x1 != x2
}
// generatedChanged reports if the generated expression of a column was changed.
func (*Diff) generatedChanged(from, to *schema.Column) bool {
var (
fromX, toX schema.GeneratedExpr
fromHas, toHas = sqlx.Has(from.Attrs, &fromX), sqlx.Has(to.Attrs, &toX)
)
return fromHas != toHas || fromHas && (sqlx.MayWrap(fromX.Expr) != sqlx.MayWrap(toX.Expr) || storedOrVirtual(fromX.Type) != storedOrVirtual(toX.Type))
}
// IsGeneratedIndexName reports if the index name was generated by the database.
// See: https://github.com/sqlite/sqlite/blob/e937df8/src/build.c#L3583.
func (d *Diff) IsGeneratedIndexName(t *schema.Table, idx *schema.Index) bool {
p := fmt.Sprintf("sqlite_autoindex_%s_", t.Name)
if !strings.HasPrefix(idx.Name, p) {
return false
}
i, err := strconv.ParseInt(strings.TrimPrefix(idx.Name, p), 10, 64)
return err == nil && i > 0
}
// IndexAttrChanged reports if the index attributes were changed.
func (*Diff) IndexAttrChanged(from, to []schema.Attr) bool {
var p1, p2 IndexPredicate
return sqlx.Has(from, &p1) != sqlx.Has(to, &p2) || (p1.P != p2.P && p1.P != sqlx.MayWrap(p2.P))
}
// IndexPartAttrChanged reports if the index-part attributes were changed.
func (*Diff) IndexPartAttrChanged(_, _ *schema.IndexPart) bool {
return false
}
// ReferenceChanged reports if the foreign key referential action was changed.
func (*Diff) ReferenceChanged(from, to schema.ReferenceOption) bool {
// According to SQLite, if an action is not explicitly
// specified, it defaults to "NO ACTION".
if from == "" {
from = schema.NoAction
}
if to == "" {
to = schema.NoAction
}
return from != to
}
// Normalize implements the sqlx.Normalizer interface.
func (d *Diff) Normalize(from, to *schema.Table) error {
used := make([]bool, len(to.ForeignKeys))
// In SQLite, there is no easy way to get the foreign-key constraint
// name, except for parsing the CREATE statement. Therefore, we check
// if there is a foreign-key with identical properties.
for _, fk1 := range from.ForeignKeys {
for i, fk2 := range to.ForeignKeys {
if used[i] {
continue
}
if fk2.Symbol == fk1.Symbol && !isNumber(fk1.Symbol) || sameFK(fk1, fk2) {
fk1.Symbol = fk2.Symbol
used[i] = true
}
}
}
return nil
}
func sameFK(fk1, fk2 *schema.ForeignKey) bool {
if fk1.Table.Name != fk2.Table.Name || fk1.RefTable.Name != fk2.RefTable.Name ||
len(fk1.Columns) != len(fk2.Columns) || len(fk1.RefColumns) != len(fk2.RefColumns) {
return false
}
for i, c1 := range fk1.Columns {
if c1.Name != fk2.Columns[i].Name {
return false
}
}
for i, c1 := range fk1.RefColumns {
if c1.Name != fk2.RefColumns[i].Name {
return false
}
}
return true
}
|