forked from oelmekki/pgrebase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
view.go
94 lines (77 loc) · 1.86 KB
/
view.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
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
package main
import (
"io/ioutil"
"fmt"
"regexp"
)
/*
* Load or reload all views found in FS.
*/
func LoadViews() ( err error ) {
successfulCount := len( Cfg.ViewFiles )
errors := make( []string, 0 )
bypass := make(map[string]bool)
files, err := ResolveDependencies( Cfg.ViewFiles, Cfg.SqlDirPath + "views" )
if err != nil { return err }
views := make( []*View, 0 )
for i := len( files ) - 1 ; i >= 0 ; i-- {
file := files[ i ]
view := View{}
view.Path = file
views = append( views, &view )
err = DownPass( &view, view.Path )
if err != nil {
successfulCount--
errors = append( errors, fmt.Sprintf( "%v\n", err ) )
bypass[ view.Path ] = true
}
}
for i := len( views ) - 1 ; i >= 0 ; i-- {
view := views[ i ]
if _, ignore := bypass[ view.Path ] ; ! ignore {
err = UpPass( view, view.Path )
if err != nil {
successfulCount--
errors = append( errors, fmt.Sprintf( "%v\n", err ) )
}
}
}
Report( "views", successfulCount, len( Cfg.ViewFiles ), errors )
return
}
type View struct {
CodeUnit
}
/*
* Load view definition from file
*/
func ( view *View ) Load() ( err error ) {
definition, err := ioutil.ReadFile( view.Path )
if err != nil { return err }
view.Definition = string( definition )
return
}
/*
* Parse view for name
*/
func ( view *View ) Parse() ( err error ) {
nameFinder := regexp.MustCompile( `(?is)CREATE(?:\s+OR\s+REPLACE)?\s+VIEW\s+(\S+)` )
subMatches := nameFinder.FindStringSubmatch( view.Definition )
if len( subMatches ) < 2 {
return fmt.Errorf( "Can't find a view in %s", view.Path )
}
view.Name = subMatches[1]
return
}
/*
* Drop existing view from pg
*/
func ( view *View ) Drop() ( err error ) {
return view.CodeUnit.Drop( `DROP VIEW IF EXISTS ` + view.Name )
}
/*
* Create the view in pg
*/
func ( view *View ) Create() ( err error ) {
return view.CodeUnit.Create( view.Definition )
}