This repository has been archived by the owner on Sep 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 594
controller: Move hstore to jsonb #1833
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,6 @@ import ( | |
"strings" | ||
|
||
"github.com/flynn/flynn/Godeps/_workspace/src/github.com/flynn/go-sql" | ||
"github.com/flynn/flynn/Godeps/_workspace/src/github.com/flynn/pq/hstore" | ||
"github.com/flynn/flynn/Godeps/_workspace/src/github.com/flynn/que-go" | ||
"github.com/flynn/flynn/Godeps/_workspace/src/golang.org/x/net/context" | ||
"github.com/flynn/flynn/controller/name" | ||
|
@@ -64,7 +63,10 @@ func (r *AppRepo) Add(data interface{}) error { | |
if app.Strategy == "" { | ||
app.Strategy = "all-at-once" | ||
} | ||
meta := metaToHstore(app.Meta) | ||
meta, err := json.Marshal(app.Meta) | ||
if err != nil { | ||
return err | ||
} | ||
if err := tx.QueryRow("INSERT INTO apps (app_id, name, meta, strategy) VALUES ($1, $2, $3, $4) RETURNING created_at, updated_at", app.ID, app.Name, meta, app.Strategy).Scan(&app.CreatedAt, &app.UpdatedAt); err != nil { | ||
tx.Rollback() | ||
if postgres.IsUniquenessError(err, "apps_name_idx") { | ||
|
@@ -99,20 +101,19 @@ func (r *AppRepo) Add(data interface{}) error { | |
|
||
func scanApp(s postgres.Scanner) (*ct.App, error) { | ||
app := &ct.App{} | ||
var meta hstore.Hstore | ||
var meta []byte | ||
var releaseID *string | ||
err := s.Scan(&app.ID, &app.Name, &meta, &app.Strategy, &releaseID, &app.CreatedAt, &app.UpdatedAt) | ||
if err == sql.ErrNoRows { | ||
err = ErrNotFound | ||
return nil, ErrNotFound | ||
} else if err != nil { | ||
return nil, err | ||
} | ||
if releaseID != nil { | ||
app.ReleaseID = *releaseID | ||
} | ||
if len(meta.Map) > 0 { | ||
app.Meta = make(map[string]string, len(meta.Map)) | ||
for k, v := range meta.Map { | ||
app.Meta[k] = v.String | ||
} | ||
if len(meta) > 0 { | ||
err = json.Unmarshal(meta, &app.Meta) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This nukes the previous error, add a |
||
} | ||
return app, err | ||
} | ||
|
@@ -172,18 +173,20 @@ func (r *AppRepo) Update(id string, data map[string]interface{}) (interface{}, e | |
tx.Rollback() | ||
return nil, fmt.Errorf("controller: expected map[string]interface{}, got %T", v) | ||
} | ||
var meta hstore.Hstore | ||
meta.Map = make(map[string]sql.NullString, len(data)) | ||
app.Meta = make(map[string]string, len(data)) | ||
for k, v := range data { | ||
s, ok := v.(string) | ||
if !ok { | ||
tx.Rollback() | ||
return nil, fmt.Errorf("controller: expected string, got %T", v) | ||
} | ||
meta.Map[k] = sql.NullString{String: s, Valid: true} | ||
app.Meta[k] = s | ||
} | ||
meta, err := json.Marshal(app.Meta) | ||
if err != nil { | ||
tx.Rollback() | ||
return nil, err | ||
} | ||
if _, err := tx.Exec("UPDATE apps SET meta = $2, updated_at = now() WHERE app_id = $1", app.ID, meta); err != nil { | ||
tx.Rollback() | ||
return nil, err | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,11 +4,9 @@ import ( | |
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/flynn/flynn/Godeps/_workspace/src/github.com/flynn/go-sql" | ||
"github.com/flynn/flynn/Godeps/_workspace/src/github.com/flynn/pq/hstore" | ||
"github.com/flynn/flynn/Godeps/_workspace/src/github.com/flynn/que-go" | ||
"github.com/flynn/flynn/Godeps/_workspace/src/github.com/jackc/pgx" | ||
"github.com/flynn/flynn/Godeps/_workspace/src/golang.org/x/net/context" | ||
|
@@ -39,7 +37,10 @@ func (r *DeploymentRepo) Add(data interface{}) (*ct.Deployment, error) { | |
if d.OldReleaseID != "" { | ||
oldReleaseID = &d.OldReleaseID | ||
} | ||
procs := procsHstore(d.Processes) | ||
procs, err := json.Marshal(d.Processes) | ||
if err != nil { | ||
return nil, err | ||
} | ||
tx, err := r.db.Begin() | ||
if err != nil { | ||
return nil, err | ||
|
@@ -138,26 +139,22 @@ func (r *DeploymentRepo) List(appID string) ([]*ct.Deployment, error) { | |
|
||
func scanDeployment(s postgres.Scanner) (*ct.Deployment, error) { | ||
d := &ct.Deployment{} | ||
var procs hstore.Hstore | ||
var procs []byte | ||
var oldReleaseID *string | ||
var status *string | ||
err := s.Scan(&d.ID, &d.AppID, &oldReleaseID, &d.NewReleaseID, &d.Strategy, &status, &procs, &d.CreatedAt, &d.FinishedAt) | ||
if err == sql.ErrNoRows { | ||
err = ErrNotFound | ||
return nil, ErrNotFound | ||
} else if err != nil { | ||
return nil, err | ||
} | ||
if oldReleaseID != nil { | ||
d.OldReleaseID = *oldReleaseID | ||
} | ||
if status != nil { | ||
d.Status = *status | ||
} | ||
d.Processes = make(map[string]int, len(procs.Map)) | ||
for k, v := range procs.Map { | ||
n, _ := strconv.Atoi(v.String) | ||
if n > 0 { | ||
d.Processes[k] = n | ||
} | ||
} | ||
err = json.Unmarshal(procs, &d.Processes) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto on the |
||
return d, err | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/flynn/flynn/Godeps/_workspace/src/github.com/flynn/go-sql" | ||
"github.com/flynn/flynn/Godeps/_workspace/src/github.com/flynn/pq/hstore" | ||
"github.com/flynn/flynn/Godeps/_workspace/src/golang.org/x/net/context" | ||
"github.com/flynn/flynn/controller/schema" | ||
ct "github.com/flynn/flynn/controller/types" | ||
|
@@ -32,10 +32,14 @@ func (rr *ResourceRepo) Add(r *ct.Resource) error { | |
if err != nil { | ||
return err | ||
} | ||
env, err := json.Marshal(r.Env) | ||
if err != nil { | ||
return err | ||
} | ||
err = tx.QueryRow(`INSERT INTO resources (resource_id, provider_id, external_id, env) | ||
VALUES ($1, $2, $3, $4) | ||
RETURNING created_at`, | ||
r.ID, r.ProviderID, r.ExternalID, envHstore(r.Env)).Scan(&r.CreatedAt) | ||
r.ID, r.ProviderID, r.ExternalID, env).Scan(&r.CreatedAt) | ||
if err != nil { | ||
tx.Rollback() | ||
return err | ||
|
@@ -70,14 +74,6 @@ func (rr *ResourceRepo) Add(r *ct.Resource) error { | |
return tx.Commit() | ||
} | ||
|
||
func envHstore(m map[string]string) hstore.Hstore { | ||
res := hstore.Hstore{Map: make(map[string]sql.NullString, len(m))} | ||
for k, v := range m { | ||
res.Map[k] = sql.NullString{String: v, Valid: true} | ||
} | ||
return res | ||
} | ||
|
||
func split(s string, sep string) []string { | ||
if s == "" { | ||
return nil | ||
|
@@ -87,15 +83,16 @@ func split(s string, sep string) []string { | |
|
||
func scanResource(s postgres.Scanner) (*ct.Resource, error) { | ||
r := &ct.Resource{} | ||
var env hstore.Hstore | ||
var env []byte | ||
var appIDs string | ||
err := s.Scan(&r.ID, &r.ProviderID, &r.ExternalID, &env, &appIDs, &r.CreatedAt) | ||
if err == sql.ErrNoRows { | ||
err = ErrNotFound | ||
return nil, ErrNotFound | ||
} else if err != nil { | ||
return nil, err | ||
} | ||
r.Env = make(map[string]string, len(env.Map)) | ||
for k, v := range env.Map { | ||
r.Env[k] = v.String | ||
if len(env) > 0 { | ||
err = json.Unmarshal(env, &r.Env) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another |
||
} | ||
if appIDs != "" { | ||
r.Apps = split(appIDs[1:len(appIDs)-1], ",") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't the only error that can be returned from
Scan
, probably should add anelse if err != nil