From 58b734ef485e10096c81c79d4beb2b4fef0773fa Mon Sep 17 00:00:00 2001 From: m0t0k1ch1 Date: Sat, 28 Feb 2015 23:26:19 +0900 Subject: [PATCH 1/3] =?UTF-8?q?platform=5Ftype=20=E3=81=94=E3=81=A8?= =?UTF-8?q?=E3=81=AB=20revision=20=E3=82=92=E6=8C=AF=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/app.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app/models/app.go b/app/models/app.go index ae87601..1d9528d 100644 --- a/app/models/app.go +++ b/app/models/app.go @@ -77,10 +77,11 @@ func (app *App) Authorities(txn gorp.SqlExecutor) ([]*Authority, error) { return authorities, nil } -func (app *App) GetMaxRevisionByBundleVersion(txn gorp.SqlExecutor, bundleVersion string) (int, error) { +func (app *App) GetMaxRevisionByBundleVersion(txn gorp.SqlExecutor, platformType BundlePlatformType, bundleVersion string) (int, error) { revision, err := txn.SelectInt( - "SELECT IFNULL(MAX(revision), 0) FROM bundle WHERE app_id = ? AND bundle_version = ?", + "SELECT IFNULL(MAX(revision), 0) FROM bundle WHERE app_id = ? AND platform_type = ? AND bundle_version = ?", app.Id, + platformType, bundleVersion, ) return int(revision), err @@ -227,7 +228,7 @@ func (app *App) CreateBundle(dbm *gorp.DbMap, s *GoogleService, bundle *Bundle) // increment revision number & save application information err = Transact(dbm, func(txn gorp.SqlExecutor) error { - maxRevision, err := app.GetMaxRevisionByBundleVersion(txn, bundleInfo.Version) + maxRevision, err := app.GetMaxRevisionByBundleVersion(txn, bundle.PlatformType, bundleInfo.Version) if err != nil { return err } From 881fd5e172baf3b16cdbd28acf4c63f78437e066 Mon Sep 17 00:00:00 2001 From: Ichinose Shogo Date: Mon, 9 Mar 2015 01:09:05 +0900 Subject: [PATCH 2/3] add revision table --- app/controllers/gorp.go | 11 ++++++++- app/models/app.go | 55 +++++++++++++++++++++++++++++++++++------ 2 files changed, 57 insertions(+), 9 deletions(-) diff --git a/app/controllers/gorp.go b/app/controllers/gorp.go index 6744306..5678fe4 100644 --- a/app/controllers/gorp.go +++ b/app/controllers/gorp.go @@ -25,9 +25,15 @@ func InitDB() { appTableMap := Dbm.AddTableWithName(models.App{}, "app") appTableMap.SetKeys(true, "Id") appTableMap.ColMap("ApiToken").SetUnique(true) + appTableMap.ColMap("FileId").SetUnique(true) bundleTableMap := Dbm.AddTableWithName(models.Bundle{}, "bundle") bundleTableMap.SetKeys(true, "Id") + bundleTableMap.ColMap("FileId").SetUnique(true) + + revisionTableMap := Dbm.AddTableWithName(models.Revision{}, "revision") + revisionTableMap.SetKeys(true, "Id") + revisionTableMap.SetUniqueTogether("app_id", "platform_type", "bundle_version") authorityTableMap := Dbm.AddTableWithName(models.Authority{}, "authority") authorityTableMap.SetKeys(true, "Id") @@ -39,7 +45,10 @@ func InitDB() { auditTableMap.SetKeys(true, "Id") Dbm.TraceOn("[gorp]", revel.INFO) - Dbm.CreateTablesIfNotExists() + err := Dbm.CreateTablesIfNotExists() + if err != nil { + panic(err) + } } func getDbm() *gorp.DbMap { diff --git a/app/models/app.go b/app/models/app.go index 1d9528d..b890e30 100644 --- a/app/models/app.go +++ b/app/models/app.go @@ -3,6 +3,7 @@ package models import ( "crypto/hmac" "crypto/sha256" + "database/sql" "encoding/hex" "fmt" "strings" @@ -25,6 +26,14 @@ type App struct { UpdatedAt time.Time `db:"updated_at"` } +type Revision struct { + Id int `db:"id"` + AppId int `db:"app_id"` + PlatformType BundlePlatformType `db:"platform_type"` + BundleVersion string `db:"bundle_version"` + MaxRevision int `db:"max_revision"` +} + func (app *App) Bundles(txn gorp.SqlExecutor) ([]*Bundle, error) { var bundles []*Bundle _, err := txn.Select(&bundles, "SELECT * FROM bundle WHERE app_id = ? ORDER BY id DESC", app.Id) @@ -77,14 +86,44 @@ func (app *App) Authorities(txn gorp.SqlExecutor) ([]*Authority, error) { return authorities, nil } -func (app *App) GetMaxRevisionByBundleVersion(txn gorp.SqlExecutor, platformType BundlePlatformType, bundleVersion string) (int, error) { - revision, err := txn.SelectInt( - "SELECT IFNULL(MAX(revision), 0) FROM bundle WHERE app_id = ? AND platform_type = ? AND bundle_version = ?", +func (app *App) IncrementRevision(txn gorp.SqlExecutor, platformType BundlePlatformType, bundleVersion string) (int, error) { + var revision Revision + err := txn.SelectOne( + &revision, + "SELECT * FROM revision WHERE app_id = ? AND platform_type = ? AND bundle_version = ?", app.Id, platformType, bundleVersion, ) - return int(revision), err + + if err == sql.ErrNoRows { + var maxRevision int64 + maxRevision, err = txn.SelectInt( + "SELECT IFNULL(MAX(revision), 0) FROM bundle WHERE app_id = ? AND platform_type = ? AND bundle_version = ?", + app.Id, + platformType, + bundleVersion, + ) + if err != nil { + return 0, err + } + maxRevision++ + revision = Revision{ + AppId: app.Id, + PlatformType: platformType, + BundleVersion: bundleVersion, + MaxRevision: int(maxRevision), + } + err = txn.Insert(&revision) + } else if err == nil { + revision.MaxRevision++ + _, err = txn.Update(&revision) + } + + if err != nil { + return 0, err + } + return revision.MaxRevision, err } func NewToken() string { @@ -228,13 +267,13 @@ func (app *App) CreateBundle(dbm *gorp.DbMap, s *GoogleService, bundle *Bundle) // increment revision number & save application information err = Transact(dbm, func(txn gorp.SqlExecutor) error { - maxRevision, err := app.GetMaxRevisionByBundleVersion(txn, bundle.PlatformType, bundleInfo.Version) + nextRevision, err := app.IncrementRevision(txn, bundle.PlatformType, bundleInfo.Version) if err != nil { return err } - bundle.Revision = maxRevision + 1 + bundle.Revision = nextRevision bundle.FileName = bundle.BuildFileName() - return bundle.Save(txn) + return nil }) if err != nil { panic(err) @@ -250,7 +289,7 @@ func (app *App) CreateBundle(dbm *gorp.DbMap, s *GoogleService, bundle *Bundle) // update FileId bundle.FileId = driveFile.Id return Transact(dbm, func(txn gorp.SqlExecutor) error { - return bundle.Update(txn) + return bundle.Save(txn) }) } From da42f197bd9f26e596e327deffc4358a27777f0e Mon Sep 17 00:00:00 2001 From: Ichinose Shogo Date: Mon, 9 Mar 2015 11:34:09 +0900 Subject: [PATCH 3/3] add revision.go --- app/models/app.go | 20 +++++--------------- app/models/revision.go | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 15 deletions(-) create mode 100644 app/models/revision.go diff --git a/app/models/app.go b/app/models/app.go index b890e30..76f1fc6 100644 --- a/app/models/app.go +++ b/app/models/app.go @@ -26,14 +26,6 @@ type App struct { UpdatedAt time.Time `db:"updated_at"` } -type Revision struct { - Id int `db:"id"` - AppId int `db:"app_id"` - PlatformType BundlePlatformType `db:"platform_type"` - BundleVersion string `db:"bundle_version"` - MaxRevision int `db:"max_revision"` -} - func (app *App) Bundles(txn gorp.SqlExecutor) ([]*Bundle, error) { var bundles []*Bundle _, err := txn.Select(&bundles, "SELECT * FROM bundle WHERE app_id = ? ORDER BY id DESC", app.Id) @@ -87,10 +79,8 @@ func (app *App) Authorities(txn gorp.SqlExecutor) ([]*Authority, error) { } func (app *App) IncrementRevision(txn gorp.SqlExecutor, platformType BundlePlatformType, bundleVersion string) (int, error) { - var revision Revision - err := txn.SelectOne( - &revision, - "SELECT * FROM revision WHERE app_id = ? AND platform_type = ? AND bundle_version = ?", + revision, err := GetMaxRevision( + txn, app.Id, platformType, bundleVersion, @@ -108,16 +98,16 @@ func (app *App) IncrementRevision(txn gorp.SqlExecutor, platformType BundlePlatf return 0, err } maxRevision++ - revision = Revision{ + revision = &Revision{ AppId: app.Id, PlatformType: platformType, BundleVersion: bundleVersion, MaxRevision: int(maxRevision), } - err = txn.Insert(&revision) + err = revision.Save(txn) } else if err == nil { revision.MaxRevision++ - _, err = txn.Update(&revision) + err = revision.Update(txn) } if err != nil { diff --git a/app/models/revision.go b/app/models/revision.go new file mode 100644 index 0000000..3a82e39 --- /dev/null +++ b/app/models/revision.go @@ -0,0 +1,37 @@ +package models + +import ( + "github.com/coopernurse/gorp" +) + +type Revision struct { + Id int `db:"id"` + AppId int `db:"app_id"` + PlatformType BundlePlatformType `db:"platform_type"` + BundleVersion string `db:"bundle_version"` + MaxRevision int `db:"max_revision"` +} + +func GetMaxRevision(txn gorp.SqlExecutor, appId int, platformType BundlePlatformType, bundleVersion string) (*Revision, error) { + var revision Revision + err := txn.SelectOne( + &revision, + "SELECT * FROM revision WHERE app_id = ? AND platform_type = ? AND bundle_version = ?", + appId, + platformType, + bundleVersion, + ) + if err != nil { + return nil, err + } + return &revision, nil +} + +func (r *Revision) Save(txn gorp.SqlExecutor) error { + return txn.Insert(r) +} + +func (r *Revision) Update(txn gorp.SqlExecutor) error { + _, err := txn.Update(r) + return err +}