forked from oelmekki/pgrebase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sanity.go
63 lines (50 loc) · 1.39 KB
/
sanity.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"fmt"
)
type Sanity struct {
}
/*
* Check the fs is ready to be used
*/
func ( sanity *Sanity ) Check() ( err error ) {
if err = sanity.directoryExists() ; err != nil { return err }
if err = sanity.typedDirExists() ; err != nil { return err }
Cfg.ScanFiles()
if err = sanity.sqlFilesPresent() ; err != nil { return err }
return
}
/*
* Check the provided sql directory is indeed a directory
*/
func ( sanity *Sanity ) directoryExists() ( err error ) {
if ! IsDir( Cfg.SqlDirPath ) {
return fmt.Errorf( "%s is not a directory", Cfg.SqlDirPath )
}
return
}
/*
* At least one of functions/, views/, triggers/, types/ should exist
*/
func ( sanity *Sanity ) typedDirExists() ( err error ) {
directories := make( []string, 0 )
for _, typedDir := range []string{ "functions", "triggers", "types", "views" } {
path := Cfg.SqlDirPath + "/" + typedDir
if IsDir( path ) {
directories = append( directories, path )
}
}
if len( directories ) == 0 {
return fmt.Errorf( "No functions/, triggers/, types/ or views/ directory found in %s", Cfg.SqlDirPath )
}
return
}
/*
* No need to process any further if there are no sql files to load
*/
func ( sanity *Sanity ) sqlFilesPresent() ( err error ) {
if len( Cfg.FunctionFiles ) + len( Cfg.TriggerFiles ) + len( Cfg.ViewFiles ) == 0 {
return fmt.Errorf( "Didn't find any sql file in %s", Cfg.SqlDirPath )
}
return
}