Skip to content

Commit 6bd2396

Browse files
committed
Add support for DDL
1 parent c35def9 commit 6bd2396

File tree

4 files changed

+59
-7
lines changed

4 files changed

+59
-7
lines changed

parser.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,40 @@ func parse(s *postgres.Schema, tree pg.ParsetreeList) {
5151
continue
5252
}
5353
switch n := raw.Stmt.(type) {
54+
case nodes.AlterTableStmt:
55+
idx := -1
56+
for i, table := range s.Tables {
57+
if table.Name == *n.Relation.Relname {
58+
idx = i
59+
}
60+
}
61+
if idx < 0 {
62+
panic("could not find table " + *n.Relation.Relname)
63+
}
64+
65+
for _, cmd := range n.Cmds.Items {
66+
switch cmd := cmd.(type) {
67+
case nodes.AlterTableCmd:
68+
switch cmd.Subtype {
69+
case nodes.AT_AddColumn:
70+
switch n := cmd.Def.(type) {
71+
case nodes.ColumnDef:
72+
s.Tables[idx].Columns = append(s.Tables[idx].Columns, postgres.Column{
73+
Name: *n.Colname,
74+
Type: join(n.TypeName.Names, "."),
75+
GoName: structName(*n.Colname),
76+
NotNull: isNotNull(n),
77+
})
78+
}
79+
case nodes.AT_DropColumn:
80+
for i, c := range s.Tables[idx].Columns {
81+
if c.Name == *cmd.Name {
82+
s.Tables[idx].Columns = append(s.Tables[idx].Columns[:i], s.Tables[idx].Columns[i+1:]...)
83+
}
84+
}
85+
}
86+
}
87+
}
5488
case nodes.CreateStmt:
5589
table := postgres.Table{
5690
Name: *n.Relation.Relname,
@@ -69,6 +103,21 @@ func parse(s *postgres.Schema, tree pg.ParsetreeList) {
69103
}
70104
}
71105
s.Tables = append(s.Tables, table)
106+
case nodes.RenameStmt:
107+
switch n.RenameType {
108+
case nodes.OBJECT_TABLE:
109+
idx := -1
110+
for i, table := range s.Tables {
111+
if table.Name == *n.Relation.Relname {
112+
idx = i
113+
}
114+
}
115+
if idx < 0 {
116+
panic("could not find table " + *n.Relation.Relname)
117+
}
118+
s.Tables[idx].Name = *n.Newname
119+
s.Tables[idx].GoName = structName(*n.Newname)
120+
}
72121
default:
73122
// spew.Dump(n)
74123
}

testdata/ondeck/db.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ type City struct {
1313

1414
type Venue struct {
1515
ID int
16-
CreatedAt time.Time
1716
Slug string
1817
Name string
1918
City sql.NullString
2019
SpotifyPlaylist string
2120
SongkickID sql.NullString
21+
CreatedAt time.Time
2222
}
2323

2424
type dbtx interface {
@@ -192,7 +192,7 @@ func (q *Queries) GetCity(ctx context.Context, slug string) (City, error) {
192192
}
193193

194194
const getVenue = `-- name: GetVenue :one
195-
SELECT id, created_at, slug, name, city, spotify_playlist, songkick_id
195+
SELECT id, slug, name, city, spotify_playlist, songkick_id, created_at
196196
FROM venue
197197
WHERE slug = $1 AND city = $2
198198
`
@@ -208,7 +208,7 @@ func (q *Queries) GetVenue(ctx context.Context, slug string, city string) (Venue
208208
row = q.db.QueryRowContext(ctx, getVenue, slug, city)
209209
}
210210
var i Venue
211-
err := row.Scan(&i.ID, &i.CreatedAt, &i.Slug, &i.Name, &i.City, &i.SpotifyPlaylist, &i.SongkickID)
211+
err := row.Scan(&i.ID, &i.Slug, &i.Name, &i.City, &i.SpotifyPlaylist, &i.SongkickID, &i.CreatedAt)
212212
return i, err
213213
}
214214

@@ -251,7 +251,7 @@ func (q *Queries) ListCities(ctx context.Context) ([]City, error) {
251251
}
252252

253253
const listVenues = `-- name: ListVenues :many
254-
SELECT id, created_at, slug, name, city, spotify_playlist, songkick_id
254+
SELECT id, slug, name, city, spotify_playlist, songkick_id, created_at
255255
FROM venue
256256
WHERE city = $1
257257
ORDER BY name
@@ -275,7 +275,7 @@ func (q *Queries) ListVenues(ctx context.Context, city string) ([]Venue, error)
275275
items := []Venue{}
276276
for rows.Next() {
277277
var i Venue
278-
if err := rows.Scan(&i.ID, &i.CreatedAt, &i.Slug, &i.Name, &i.City, &i.SpotifyPlaylist, &i.SongkickID); err != nil {
278+
if err := rows.Scan(&i.ID, &i.Slug, &i.Name, &i.City, &i.SpotifyPlaylist, &i.SongkickID, &i.CreatedAt); err != nil {
279279
return nil, err
280280
}
281281
items = append(items, i)

testdata/ondeck/schema/0002_venue.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
CREATE TABLE venue (
1+
CREATE TABLE venues (
22
id SERIAL primary key,
3-
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
3+
dropped text,
44
slug text not null,
55
name text not null,
66
city text references city(slug),
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ALTER TABLE venues RENAME TO venue;
2+
ALTER TABLE venue ADD COLUMN created_at TIMESTAMP NOT NULL DEFAULT NOW();
3+
ALTER TABLE venue DROP COLUMN dropped;

0 commit comments

Comments
 (0)