forked from xo/xo
-
Notifications
You must be signed in to change notification settings - Fork 3
/
djangomigration.xo.go
186 lines (149 loc) · 3.89 KB
/
djangomigration.xo.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Package postgres contains the types for schema 'public'.
package postgres
// GENERATED BY XO. DO NOT EDIT.
import (
"errors"
"time"
)
// DjangoMigration represents a row from 'public.django_migrations'.
type DjangoMigration struct {
ID int `json:"id"` // id
App string `json:"app"` // app
Name string `json:"name"` // name
Applied *time.Time `json:"applied"` // applied
// xo fields
_exists, _deleted bool
}
// Exists determines if the DjangoMigration exists in the database.
func (dm *DjangoMigration) Exists() bool {
return dm._exists
}
// Deleted provides information if the DjangoMigration has been deleted from the database.
func (dm *DjangoMigration) Deleted() bool {
return dm._deleted
}
// Insert inserts the DjangoMigration to the database.
func (dm *DjangoMigration) Insert(db XODB) error {
var err error
// if already exist, bail
if dm._exists {
return errors.New("insert failed: already exists")
}
// sql insert query, primary key provided by sequence
const sqlstr = `INSERT INTO public.django_migrations (` +
`app, name, applied` +
`) VALUES (` +
`$1, $2, $3` +
`) RETURNING id`
// run query
XOLog(sqlstr, dm.App, dm.Name, dm.Applied)
err = db.QueryRow(sqlstr, dm.App, dm.Name, dm.Applied).Scan(&dm.ID)
if err != nil {
return err
}
// set existence
dm._exists = true
return nil
}
// Update updates the DjangoMigration in the database.
func (dm *DjangoMigration) Update(db XODB) error {
var err error
// if doesn't exist, bail
if !dm._exists {
return errors.New("update failed: does not exist")
}
// if deleted, bail
if dm._deleted {
return errors.New("update failed: marked for deletion")
}
// sql query
const sqlstr = `UPDATE public.django_migrations SET (` +
`app, name, applied` +
`) = ( ` +
`$1, $2, $3` +
`) WHERE id = $4`
// run query
XOLog(sqlstr, dm.App, dm.Name, dm.Applied, dm.ID)
_, err = db.Exec(sqlstr, dm.App, dm.Name, dm.Applied, dm.ID)
return err
}
// Save saves the DjangoMigration to the database.
func (dm *DjangoMigration) Save(db XODB) error {
if dm.Exists() {
return dm.Update(db)
}
return dm.Insert(db)
}
// Upsert performs an upsert for DjangoMigration.
//
// NOTE: PostgreSQL 9.5+ only
func (dm *DjangoMigration) Upsert(db XODB) error {
var err error
// if already exist, bail
if dm._exists {
return errors.New("insert failed: already exists")
}
// sql query
const sqlstr = `INSERT INTO public.django_migrations (` +
`id, app, name, applied` +
`) VALUES (` +
`$1, $2, $3, $4` +
`) ON CONFLICT (id) DO UPDATE SET (` +
`id, app, name, applied` +
`) = (` +
`EXCLUDED.id, EXCLUDED.app, EXCLUDED.name, EXCLUDED.applied` +
`)`
// run query
XOLog(sqlstr, dm.ID, dm.App, dm.Name, dm.Applied)
_, err = db.Exec(sqlstr, dm.ID, dm.App, dm.Name, dm.Applied)
if err != nil {
return err
}
// set existence
dm._exists = true
return nil
}
// Delete deletes the DjangoMigration from the database.
func (dm *DjangoMigration) Delete(db XODB) error {
var err error
// if doesn't exist, bail
if !dm._exists {
return nil
}
// if deleted, bail
if dm._deleted {
return nil
}
// sql query
const sqlstr = `DELETE FROM public.django_migrations WHERE id = $1`
// run query
XOLog(sqlstr, dm.ID)
_, err = db.Exec(sqlstr, dm.ID)
if err != nil {
return err
}
// set deleted
dm._deleted = true
return nil
}
// DjangoMigrationByID retrieves a row from 'public.django_migrations' as a DjangoMigration.
//
// Generated from index 'django_migrations_pkey'.
func DjangoMigrationByID(db XODB, id int) (*DjangoMigration, error) {
var err error
// sql query
const sqlstr = `SELECT ` +
`id, app, name, applied ` +
`FROM public.django_migrations ` +
`WHERE id = $1`
// run query
XOLog(sqlstr, id)
dm := DjangoMigration{
_exists: true,
}
err = db.QueryRow(sqlstr, id).Scan(&dm.ID, &dm.App, &dm.Name, &dm.Applied)
if err != nil {
return nil, err
}
return &dm, nil
}