-
Notifications
You must be signed in to change notification settings - Fork 3
/
template.go
65 lines (57 loc) · 1.42 KB
/
template.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
package pgdump
import (
"database/sql"
"os"
"text/template"
)
type DumpInfo struct {
DumpVersion string
ServerVersion string
CompleteTime string
ThreadsNumber int
}
func getServerVersion(db *sql.DB) string {
var version string
query := "SELECT version();"
row := db.QueryRow(query)
if err := row.Scan(&version); err != nil {
return "Unknown"
}
return version
}
func writeHeader(file *os.File, info DumpInfo) error {
const headerTemplate = `-- Go PostgreSQL Dump {{ .DumpVersion }}
--
-- Server version:
-- {{ .ServerVersion }}
-- Threads Used:
-- {{ .ThreadsNumber }}
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;
SET default_tablespace = '';
SET default_table_access_method = heap;
`
tmpl, err := template.New("header").Parse(headerTemplate)
if err != nil {
return err
}
return tmpl.Execute(file, info)
}
func writeFooter(file *os.File, info DumpInfo) error {
const footerTemplate = `--
-- Dump completed on {{ .CompleteTime }}
--`
tmpl, err := template.New("footer").Parse(footerTemplate)
if err != nil {
return err
}
return tmpl.Execute(file, info)
}