-
Notifications
You must be signed in to change notification settings - Fork 235
/
postgrescheck.go
49 lines (44 loc) · 1.22 KB
/
postgrescheck.go
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
// 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 postgrescheck
import (
"fmt"
"ariga.io/atlas/schemahcl"
"ariga.io/atlas/sql/postgres"
"ariga.io/atlas/sql/sqlcheck"
"ariga.io/atlas/sql/sqlcheck/condrop"
"ariga.io/atlas/sql/sqlcheck/datadepend"
"ariga.io/atlas/sql/sqlcheck/destructive"
)
func addNotNull(p *datadepend.ColumnPass) (diags []sqlcheck.Diagnostic, err error) {
tt, err := postgres.FormatType(p.Column.Type.Type)
if err != nil {
return nil, err
}
return []sqlcheck.Diagnostic{
{
Pos: p.Change.Stmt.Pos,
Text: fmt.Sprintf(
"Adding a non-nullable %q column %q will fail in case table %q is not empty",
tt, p.Column.Name, p.Table.Name,
),
},
}, nil
}
func init() {
sqlcheck.Register(postgres.DriverName, func(r *schemahcl.Resource) ([]sqlcheck.Analyzer, error) {
ds, err := destructive.New(r)
if err != nil {
return nil, err
}
cd, err := condrop.New(r)
if err != nil {
return nil, err
}
dd, err := datadepend.New(r, datadepend.Handler{
AddNotNull: addNotNull,
})
return []sqlcheck.Analyzer{ds, dd, cd}, nil
})
}