Skip to content
This repository has been archived by the owner on Sep 4, 2021. It is now read-only.

controller: Move hstore to jsonb #1833

Merged
merged 1 commit into from
Sep 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions controller/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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") {
Expand Down Expand Up @@ -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
Copy link
Contributor

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 an else if err != nil

} 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This nukes the previous error, add a err != nil check above.

}
return app, err
}
Expand Down Expand Up @@ -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
Expand Down
17 changes: 0 additions & 17 deletions controller/common.go

This file was deleted.

21 changes: 9 additions & 12 deletions controller/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto on the err overwrite.

return d, err
}

Expand Down
28 changes: 9 additions & 19 deletions controller/formation.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package main

import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"sync"
"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/golang.org/x/net/context"
"github.com/flynn/flynn/controller/schema"
ct "github.com/flynn/flynn/controller/types"
Expand Down Expand Up @@ -45,14 +44,6 @@ func NewFormationRepo(db *postgres.DB, appRepo *AppRepo, releaseRepo *ReleaseRep
}
}

func procsHstore(m map[string]int) hstore.Hstore {
res := hstore.Hstore{Map: make(map[string]sql.NullString, len(m))}
for k, v := range m {
res.Map[k] = sql.NullString{String: strconv.Itoa(v), Valid: true}
}
return res
}

func (r *FormationRepo) validateFormProcs(f *ct.Formation) error {
release, err := r.releases.Get(f.ReleaseID)
if err != nil {
Expand All @@ -79,7 +70,10 @@ func (r *FormationRepo) Add(f *ct.Formation) error {
if err != nil {
return err
}
procs := procsHstore(f.Processes)
procs, err := json.Marshal(f.Processes)
if err != nil {
return err
}
err = tx.QueryRow("INSERT INTO formations (app_id, release_id, processes) VALUES ($1, $2, $3) RETURNING created_at, updated_at",
f.AppID, f.ReleaseID, procs).Scan(&f.CreatedAt, &f.UpdatedAt)
if postgres.IsUniquenessError(err, "") {
Expand Down Expand Up @@ -108,22 +102,18 @@ func (r *FormationRepo) Add(f *ct.Formation) error {

func scanFormation(s postgres.Scanner) (*ct.Formation, error) {
f := &ct.Formation{}
var procs hstore.Hstore
var procs []byte
err := s.Scan(&f.AppID, &f.ReleaseID, &procs, &f.CreatedAt, &f.UpdatedAt)
if err != nil {
if err == sql.ErrNoRows {
err = ErrNotFound
}
return nil, err
}
f.Processes = make(map[string]int, len(procs.Map))
for k, v := range procs.Map {
n, _ := strconv.Atoi(v.String)
if n > 0 {
f.Processes[k] = n
}
if len(procs) > 0 {
err = json.Unmarshal(procs, &f.Processes)
}
return f, nil
return f, err
}

func (r *FormationRepo) Get(appID, releaseID string) (*ct.Formation, error) {
Expand Down
17 changes: 7 additions & 10 deletions controller/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/flynn/flynn/Godeps/_workspace/src/github.com/flynn/go-sql"
"github.com/flynn/flynn/Godeps/_workspace/src/github.com/flynn/pq"
"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"
Expand Down Expand Up @@ -60,9 +59,12 @@ func (r *JobRepo) Get(id string) (*ct.Job, error) {
}

func (r *JobRepo) Add(job *ct.Job) error {
meta := metaToHstore(job.Meta)
meta, err := json.Marshal(job.Meta)
if err != nil {
return err
}
// TODO: actually validate
err := r.db.QueryRow("INSERT INTO job_cache (job_id, app_id, release_id, process_type, state, meta) VALUES ($1, $2, $3, $4, $5, $6) RETURNING created_at, updated_at",
err = r.db.QueryRow("INSERT INTO job_cache (job_id, app_id, release_id, process_type, state, meta) VALUES ($1, $2, $3, $4, $5, $6) RETURNING created_at, updated_at",
job.ID, job.AppID, job.ReleaseID, job.Type, job.State, meta).Scan(&job.CreatedAt, &job.UpdatedAt)
if postgres.IsUniquenessError(err, "") {
err = r.db.QueryRow("UPDATE job_cache SET state = $2, updated_at = now() WHERE job_id = $1 RETURNING created_at, updated_at",
Expand Down Expand Up @@ -90,20 +92,15 @@ func (r *JobRepo) Add(job *ct.Job) error {

func scanJob(s postgres.Scanner) (*ct.Job, error) {
job := &ct.Job{}
var meta hstore.Hstore
var meta []byte
err := s.Scan(&job.ID, &job.AppID, &job.ReleaseID, &job.Type, &job.State, &meta, &job.CreatedAt, &job.UpdatedAt)
if err != nil {
if err == sql.ErrNoRows {
err = ErrNotFound
}
return nil, err
}
if len(meta.Map) > 0 {
job.Meta = make(map[string]string, len(meta.Map))
for k, v := range meta.Map {
job.Meta[k] = v.String
}
}
err = json.Unmarshal(meta, &job.Meta)
return job, nil
}

Expand Down
2 changes: 1 addition & 1 deletion controller/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func scanRelease(s postgres.Scanner) (*ct.Release, error) {
if artifactID != nil {
release.ArtifactID = *artifactID
}
err = json.Unmarshal(data, release)
err = json.Unmarshal(data, &release)
return release, err
}

Expand Down
27 changes: 12 additions & 15 deletions controller/resource.go
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"
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another err bites the dust.

}
if appIDs != "" {
r.Apps = split(appIDs[1:len(appIDs)-1], ",")
Expand Down
11 changes: 5 additions & 6 deletions controller/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ func migrateDB(db *sql.DB) error {
m := postgres.NewMigrations()
m.Add(1,
`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`,
`CREATE EXTENSION IF NOT EXISTS "hstore"`,

`CREATE TABLE artifacts (
artifact_id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
Expand All @@ -34,7 +33,7 @@ func migrateDB(db *sql.DB) error {
app_id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
name text NOT NULL,
release_id uuid REFERENCES releases (release_id),
meta hstore,
meta jsonb,
strategy deployment_strategy NOT NULL DEFAULT 'all-at-once',
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
Expand Down Expand Up @@ -73,7 +72,7 @@ $$ LANGUAGE plpgsql`,
`CREATE TABLE formations (
app_id uuid NOT NULL REFERENCES apps (app_id),
release_id uuid NOT NULL REFERENCES releases (release_id),
processes hstore,
processes jsonb,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
deleted_at timestamptz,
Expand Down Expand Up @@ -104,7 +103,7 @@ $$ LANGUAGE plpgsql`,
resource_id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
provider_id uuid NOT NULL REFERENCES providers (provider_id),
external_id text NOT NULL,
env hstore,
env jsonb,
created_at timestamptz NOT NULL DEFAULT now(),
deleted_at timestamptz,
UNIQUE (provider_id, external_id)
Expand All @@ -126,7 +125,7 @@ $$ LANGUAGE plpgsql`,
release_id uuid NOT NULL REFERENCES releases (release_id),
process_type text,
state job_state NOT NULL,
meta hstore,
meta jsonb,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
)`,
Expand All @@ -150,7 +149,7 @@ $$ LANGUAGE plpgsql`,
old_release_id uuid REFERENCES releases (release_id),
new_release_id uuid NOT NULL REFERENCES releases (release_id),
strategy deployment_strategy NOT NULL,
processes hstore,
processes jsonb,
created_at timestamptz NOT NULL DEFAULT now(),
finished_at timestamptz)`,

Expand Down