· 8 years ago · Dec 01, 2017, 11:14 PM
1package main
2
3import (
4 "bytes"
5 "go/format"
6 "io/ioutil"
7 "strings"
8 "text/template"
9)
10
11// TmplData ..
12type tmplData struct {
13 Pkgname string
14 Schemas []schemaConfig
15}
16
17type schemaConfig struct {
18 Name string
19 Pkey string
20}
21
22var schema = `
23{{define "schema"}}
24package {{ .Pkgname }}
25
26import "database/sql"
27
28{{- range .Schemas }}
29// Schema{{ title .Name }} defines the schema for a {{ .Name }} database
30const Schema{{ title .Name }} = ` + "`" + `
31CREATE TABLE IF NOT EXISTS files (
32 id {{ .Pkey }},
33 url TEXT NOT NULL,
34 sourceurl TEXT NOT NULL,
35 mediatype TEXT NOT NULL,
36 title TEXT NOT NULL,
37 description TEXT NOT NULL,
38 thumbnailurl TEXT NOT NULL,
39 odescription TEXT NOT NULL,
40 ouploaded TEXT NOT NULL,
41 oauthor TEXT NOT NULL,
42 uploaded INTEGER NOT NULL,
43 authorid INTEGER NOT NULL,
44 deleted INTEGER NOT NULL,
45 hash TEXT NOT NULL
46);
47
48CREATE TABLE IF NOT EXISTS users (
49 id {{ .Pkey }},
50 username TEXT NOT NULL,
51 avatarurl TEXT NOT NULL,
52 joined TEXT NOT NULL,
53 salt TEXT NOT NULL,
54 passwordhash TEXT NOT NULL,
55 role INTEGER NOT NULL,
56 settings TEXT NOT NULL,
57 banned INTEGER NOT NULL
58);
59
60
61CREATE TABLE IF NOT EXISTS user_sessions (
62 id {{ .Pkey }},
63 key TEXT NOT NULL,
64 expires INTEGER NOT NULL,
65 user_id INTEGER NOT NULL,
66 UNIQUE(key),
67 FOREIGN KEY(user_id) REFERENCES users(id)
68);
69
70CREATE TABLE IF NOT EXISTS collections (
71 id {{ .Pkey }},
72 name TEXT NOT NULL,
73 description TEXT NOT NULL,
74 thumbnailurl TEXT NOT NULL,
75 author_id INT NOT NULL,
76 uploaded INTEGER NOT NULL
77);
78
79CREATE TABLE IF NOT EXISTS collection_files (
80 id {{ .Pkey }},
81 file_id INTEGER NOT NULL,
82 collection_id INTEGER NOT NULL,
83 UNIQUE(file_id, collection_id),
84 FOREIGN KEY(file_id) REFERENCES files(id),
85 FOREIGN KEY(collection_id) REFERENCES collections(id)
86);
87
88CREATE TABLE IF NOT EXISTS tags (
89 id {{ .Pkey }},
90 name TEXT NOT NULL,
91 UNIQUE(name)
92);
93
94CREATE TABLE IF NOT EXISTS file_tags (
95 id {{ .Pkey }},
96 file_id INTEGER NOT NULL,
97 tag_id INTEGER NOT NULL,
98 UNIQUE(file_id, tag_id),
99 FOREIGN KEY(file_id) REFERENCES files(id),
100 FOREIGN KEY(tag_id) REFERENCES tags(id)
101);` + "`" + `
102
103{{- end}}
104
105// InitDB Initializes a database with the proper schema
106func InitDB(db *sql.DB, driver string) error {
107 // Initialize the database
108
109 var schema string
110
111 switch driver {
112 {{- range .Schemas }}
113 case "{{.Name}}":
114 schema = Schema{{title .Name}}
115 {{- end}}
116 default:
117 schema = Schema{{title ((index .Schemas 0).Name)}}
118 }
119
120 return nil
121}
122
123{{ end }}
124`
125
126func main() {
127 var dest bytes.Buffer
128
129 tmpl := template.New("")
130 tmpl.Funcs(template.FuncMap{
131 "title": strings.Title,
132 })
133 template.Must(tmpl.Parse(schema))
134 tmpl.ExecuteTemplate(&dest, "schema", tmplData{
135 Pkgname: "west",
136 Schemas: []schemaConfig{
137 schemaConfig{
138 Name: "postgres",
139 Pkey: "SERIAL PRIMARY KEY",
140 },
141 schemaConfig{
142 Name: "sqlite3",
143 Pkey: "INTEGER PRIMARY KEY AUTOINCREMENT",
144 },
145 schemaConfig{
146 Name: "mysql",
147 Pkey: "INTEGER PRIMARY KEY AUTO_INCREMENT",
148 },
149 },
150 })
151
152 formatted, err := format.Source(dest.Bytes())
153 if err != nil {
154 ioutil.WriteFile("schema_error.txt", dest.Bytes(), 0600)
155 panic(err)
156 }
157
158 ioutil.WriteFile("schemas.gen.go", formatted, 0600)
159}