@@ -16,15 +16,17 @@ import (
1616
1717// Files tuple.
1818type Files struct {
19- schemas []string
20- tables []string
19+ function []string
20+ schemas []string
21+ tables []string
2122}
2223
2324var (
24- viewSuffix = "-schema-view.sql"
25- schemaSuffix = "-schema.sql"
26- tableSuffix = ".sql"
27- dbName = ""
25+ functionSuffix = "-schema-function.sql"
26+ viewSuffix = "-schema-view.sql"
27+ schemaSuffix = "-schema.sql"
28+ tableSuffix = ".sql"
29+ dbName = ""
2830)
2931
3032func loadFiles (log * xlog.Log , dir string ) * Files {
@@ -39,6 +41,8 @@ func loadFiles(log *xlog.Log, dir string) *Files {
3941 switch {
4042 case strings .HasSuffix (path , schemaSuffix ):
4143 files .schemas = append (files .schemas , path )
44+ case strings .HasSuffix (path , functionSuffix ):
45+ files .function = append (files .function , path )
4246 case strings .HasSuffix (path , viewSuffix ):
4347 views = append (views , path )
4448 default :
@@ -73,6 +77,24 @@ func restoreDatabaseSchema(log *xlog.Log, db string, conn *common.Connection) {
7377 log .Info ("restoring.database[%s]" , dbName )
7478}
7579
80+ func restoreFunctionSchema (log * xlog.Log , functions []string , conn * common.Connection ) {
81+ for _ , function := range functions {
82+ name := strings .TrimSuffix (filepath .Base (function ), functionSuffix )
83+
84+ dropQuery := fmt .Sprintf ("DROP FUNCTION IF EXISTS %s" , name )
85+ err := conn .Execute (dropQuery )
86+ common .AssertNil (err )
87+
88+ data , err := common .ReadFile (function )
89+ common .AssertNil (err )
90+ query := common .BytesToString (data )
91+
92+ err = conn .Execute (query )
93+ common .AssertNil (err )
94+ log .Info ("restoring.function[%s]" , name )
95+ }
96+ }
97+
7698func restoreTableSchema (log * xlog.Log , overwrite bool , tables []string , conn * common.Connection ) {
7799 for _ , table := range tables {
78100 // use
@@ -81,10 +103,7 @@ func restoreTableSchema(log *xlog.Log, overwrite bool, tables []string, conn *co
81103
82104 log .Info ("working.table[%s]" , name )
83105
84- err := conn .Execute (fmt .Sprintf ("USE `%s`" , dbName ))
85- common .AssertNil (err )
86-
87- err = conn .Execute ("SET FOREIGN_KEY_CHECKS=0" )
106+ err := conn .Execute ("SET FOREIGN_KEY_CHECKS=0" )
88107 common .AssertNil (err )
89108
90109 data , err := common .ReadFile (table )
@@ -146,16 +165,22 @@ func Loader(log *xlog.Log, args *common.Args) {
146165 common .AssertNil (err )
147166 defer pool .Close ()
148167
168+ t := time .Now ()
149169 files := loadFiles (log , args .Outdir )
150170
151- // database.
152171 conn := pool .Get ()
172+
173+ // database.
153174 restoreDatabaseSchema (log , args .Database , conn )
154- pool .Put (conn )
155175
176+ err = conn .Execute (fmt .Sprintf ("USE `%s`" , dbName ))
177+ common .AssertNil (err )
178+
179+ // function.
180+ restoreFunctionSchema (log , files .function , conn )
156181 // tables.
157- conn = pool .Get ()
158182 restoreTableSchema (log , args .OverwriteTables , files .schemas , conn )
183+
159184 pool .Put (conn )
160185
161186 // Shuffle the tables
@@ -166,7 +191,6 @@ func Loader(log *xlog.Log, args *common.Args) {
166191
167192 var wg sync.WaitGroup
168193 var bytes uint64
169- t := time .Now ()
170194 for _ , table := range files .tables {
171195 conn := pool .Get ()
172196 wg .Add (1 )
0 commit comments