You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

728 lines
20 KiB

9 years ago
9 years ago
9 years ago
9 years ago
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package migrations
  5. import (
  6. "bytes"
  7. "encoding/json"
  8. "fmt"
  9. "io/ioutil"
  10. "os"
  11. "path"
  12. "path/filepath"
  13. "strings"
  14. "time"
  15. "github.com/Unknwon/com"
  16. "github.com/go-xorm/xorm"
  17. gouuid "github.com/satori/go.uuid"
  18. "gopkg.in/ini.v1"
  19. "code.gitea.io/gitea/modules/base"
  20. "code.gitea.io/gitea/modules/log"
  21. "code.gitea.io/gitea/modules/setting"
  22. )
  23. const minDBVersion = 4
  24. // Migration describes on migration from lower version to high version
  25. type Migration interface {
  26. Description() string
  27. Migrate(*xorm.Engine) error
  28. }
  29. type migration struct {
  30. description string
  31. migrate func(*xorm.Engine) error
  32. }
  33. // NewMigration creates a new migration
  34. func NewMigration(desc string, fn func(*xorm.Engine) error) Migration {
  35. return &migration{desc, fn}
  36. }
  37. // Description returns the migration's description
  38. func (m *migration) Description() string {
  39. return m.description
  40. }
  41. // Migrate executes the migration
  42. func (m *migration) Migrate(x *xorm.Engine) error {
  43. return m.migrate(x)
  44. }
  45. // Version describes the version table. Should have only one row with id==1
  46. type Version struct {
  47. ID int64 `xorm:"pk autoincr"`
  48. Version int64
  49. }
  50. // This is a sequence of migrations. Add new migrations to the bottom of the list.
  51. // If you want to "retire" a migration, remove it from the top of the list and
  52. // update minDBVersion accordingly
  53. var migrations = []Migration{
  54. // v0 -> v4: before 0.6.0 -> 0.7.33
  55. NewMigration("fix locale file load panic", fixLocaleFileLoadPanic), // V4 -> V5:v0.6.0
  56. NewMigration("trim action compare URL prefix", trimCommitActionAppURLPrefix), // V5 -> V6:v0.6.3
  57. NewMigration("generate issue-label from issue", issueToIssueLabel), // V6 -> V7:v0.6.4
  58. NewMigration("refactor attachment table", attachmentRefactor), // V7 -> V8:v0.6.4
  59. NewMigration("rename pull request fields", renamePullRequestFields), // V8 -> V9:v0.6.16
  60. NewMigration("clean up migrate repo info", cleanUpMigrateRepoInfo), // V9 -> V10:v0.6.20
  61. NewMigration("generate rands and salt for organizations", generateOrgRandsAndSalt), // V10 -> V11:v0.8.5
  62. NewMigration("convert date to unix timestamp", convertDateToUnix), // V11 -> V12:v0.9.2
  63. NewMigration("convert LDAP UseSSL option to SecurityProtocol", ldapUseSSLToSecurityProtocol), // V12 -> V13:v0.9.37
  64. // v13 -> v14:v0.9.87
  65. NewMigration("set comment updated with created", setCommentUpdatedWithCreated),
  66. // v14 -> v15
  67. NewMigration("create user column diff view style", createUserColumnDiffViewStyle),
  68. // v15 -> v16
  69. NewMigration("create user column allow create organization", createAllowCreateOrganizationColumn),
  70. // V16 -> v17
  71. NewMigration("create repo unit table and add units for all repos", addUnitsToTables),
  72. }
  73. // Migrate database to current version
  74. func Migrate(x *xorm.Engine) error {
  75. if err := x.Sync(new(Version)); err != nil {
  76. return fmt.Errorf("sync: %v", err)
  77. }
  78. currentVersion := &Version{ID: 1}
  79. has, err := x.Get(currentVersion)
  80. if err != nil {
  81. return fmt.Errorf("get: %v", err)
  82. } else if !has {
  83. // If the version record does not exist we think
  84. // it is a fresh installation and we can skip all migrations.
  85. currentVersion.ID = 0
  86. currentVersion.Version = int64(minDBVersion + len(migrations))
  87. if _, err = x.InsertOne(currentVersion); err != nil {
  88. return fmt.Errorf("insert: %v", err)
  89. }
  90. }
  91. v := currentVersion.Version
  92. if minDBVersion > v {
  93. log.Fatal(4, `Gitea no longer supports auto-migration from your previously installed version.
  94. Please try to upgrade to a lower version (>= v0.6.0) first, then upgrade to current version.`)
  95. return nil
  96. }
  97. if int(v-minDBVersion) > len(migrations) {
  98. // User downgraded Gitea.
  99. currentVersion.Version = int64(len(migrations) + minDBVersion)
  100. _, err = x.Id(1).Update(currentVersion)
  101. return err
  102. }
  103. for i, m := range migrations[v-minDBVersion:] {
  104. log.Info("Migration: %s", m.Description())
  105. if err = m.Migrate(x); err != nil {
  106. return fmt.Errorf("do migrate: %v", err)
  107. }
  108. currentVersion.Version = v + int64(i) + 1
  109. if _, err = x.Id(1).Update(currentVersion); err != nil {
  110. return err
  111. }
  112. }
  113. return nil
  114. }
  115. func sessionRelease(sess *xorm.Session) {
  116. if !sess.IsCommitedOrRollbacked {
  117. sess.Rollback()
  118. }
  119. sess.Close()
  120. }
  121. func fixLocaleFileLoadPanic(_ *xorm.Engine) error {
  122. cfg, err := ini.Load(setting.CustomConf)
  123. if err != nil {
  124. return fmt.Errorf("load custom config: %v", err)
  125. }
  126. cfg.DeleteSection("i18n")
  127. if err = cfg.SaveTo(setting.CustomConf); err != nil {
  128. return fmt.Errorf("save custom config: %v", err)
  129. }
  130. setting.Langs = strings.Split(strings.Replace(strings.Join(setting.Langs, ","), "fr-CA", "fr-FR", 1), ",")
  131. return nil
  132. }
  133. func trimCommitActionAppURLPrefix(x *xorm.Engine) error {
  134. type PushCommit struct {
  135. Sha1 string
  136. Message string
  137. AuthorEmail string
  138. AuthorName string
  139. }
  140. type PushCommits struct {
  141. Len int
  142. Commits []*PushCommit
  143. CompareURL string `json:"CompareUrl"`
  144. }
  145. type Action struct {
  146. ID int64 `xorm:"pk autoincr"`
  147. Content string `xorm:"TEXT"`
  148. }
  149. results, err := x.Query("SELECT `id`,`content` FROM `action` WHERE `op_type`=?", 5)
  150. if err != nil {
  151. return fmt.Errorf("select commit actions: %v", err)
  152. }
  153. sess := x.NewSession()
  154. defer sessionRelease(sess)
  155. if err = sess.Begin(); err != nil {
  156. return err
  157. }
  158. var pushCommits *PushCommits
  159. for _, action := range results {
  160. actID := com.StrTo(string(action["id"])).MustInt64()
  161. if actID == 0 {
  162. continue
  163. }
  164. pushCommits = new(PushCommits)
  165. if err = json.Unmarshal(action["content"], pushCommits); err != nil {
  166. return fmt.Errorf("unmarshal action content[%d]: %v", actID, err)
  167. }
  168. infos := strings.Split(pushCommits.CompareURL, "/")
  169. if len(infos) <= 4 {
  170. continue
  171. }
  172. pushCommits.CompareURL = strings.Join(infos[len(infos)-4:], "/")
  173. p, err := json.Marshal(pushCommits)
  174. if err != nil {
  175. return fmt.Errorf("marshal action content[%d]: %v", actID, err)
  176. }
  177. if _, err = sess.Id(actID).Update(&Action{
  178. Content: string(p),
  179. }); err != nil {
  180. return fmt.Errorf("update action[%d]: %v", actID, err)
  181. }
  182. }
  183. return sess.Commit()
  184. }
  185. func issueToIssueLabel(x *xorm.Engine) error {
  186. type IssueLabel struct {
  187. ID int64 `xorm:"pk autoincr"`
  188. IssueID int64 `xorm:"UNIQUE(s)"`
  189. LabelID int64 `xorm:"UNIQUE(s)"`
  190. }
  191. issueLabels := make([]*IssueLabel, 0, 50)
  192. results, err := x.Query("SELECT `id`,`label_ids` FROM `issue`")
  193. if err != nil {
  194. if strings.Contains(err.Error(), "no such column") ||
  195. strings.Contains(err.Error(), "Unknown column") {
  196. return nil
  197. }
  198. return fmt.Errorf("select issues: %v", err)
  199. }
  200. for _, issue := range results {
  201. issueID := com.StrTo(issue["id"]).MustInt64()
  202. // Just in case legacy code can have duplicated IDs for same label.
  203. mark := make(map[int64]bool)
  204. for _, idStr := range strings.Split(string(issue["label_ids"]), "|") {
  205. labelID := com.StrTo(strings.TrimPrefix(idStr, "$")).MustInt64()
  206. if labelID == 0 || mark[labelID] {
  207. continue
  208. }
  209. mark[labelID] = true
  210. issueLabels = append(issueLabels, &IssueLabel{
  211. IssueID: issueID,
  212. LabelID: labelID,
  213. })
  214. }
  215. }
  216. sess := x.NewSession()
  217. defer sessionRelease(sess)
  218. if err = sess.Begin(); err != nil {
  219. return err
  220. }
  221. if err = sess.Sync2(new(IssueLabel)); err != nil {
  222. return fmt.Errorf("Sync2: %v", err)
  223. } else if _, err = sess.Insert(issueLabels); err != nil {
  224. return fmt.Errorf("insert issue-labels: %v", err)
  225. }
  226. return sess.Commit()
  227. }
  228. func attachmentRefactor(x *xorm.Engine) error {
  229. type Attachment struct {
  230. ID int64 `xorm:"pk autoincr"`
  231. UUID string `xorm:"uuid INDEX"`
  232. // For rename purpose.
  233. Path string `xorm:"-"`
  234. NewPath string `xorm:"-"`
  235. }
  236. results, err := x.Query("SELECT * FROM `attachment`")
  237. if err != nil {
  238. return fmt.Errorf("select attachments: %v", err)
  239. }
  240. attachments := make([]*Attachment, 0, len(results))
  241. for _, attach := range results {
  242. if !com.IsExist(string(attach["path"])) {
  243. // If the attachment is already missing, there is no point to update it.
  244. continue
  245. }
  246. attachments = append(attachments, &Attachment{
  247. ID: com.StrTo(attach["id"]).MustInt64(),
  248. UUID: gouuid.NewV4().String(),
  249. Path: string(attach["path"]),
  250. })
  251. }
  252. sess := x.NewSession()
  253. defer sessionRelease(sess)
  254. if err = sess.Begin(); err != nil {
  255. return err
  256. }
  257. if err = sess.Sync2(new(Attachment)); err != nil {
  258. return fmt.Errorf("Sync2: %v", err)
  259. }
  260. // Note: Roll back for rename can be a dead loop,
  261. // so produces a backup file.
  262. var buf bytes.Buffer
  263. buf.WriteString("# old path -> new path\n")
  264. // Update database first because this is where error happens the most often.
  265. for _, attach := range attachments {
  266. if _, err = sess.Id(attach.ID).Update(attach); err != nil {
  267. return err
  268. }
  269. attach.NewPath = path.Join(setting.AttachmentPath, attach.UUID[0:1], attach.UUID[1:2], attach.UUID)
  270. buf.WriteString(attach.Path)
  271. buf.WriteString("\t")
  272. buf.WriteString(attach.NewPath)
  273. buf.WriteString("\n")
  274. }
  275. // Then rename attachments.
  276. isSucceed := true
  277. defer func() {
  278. if isSucceed {
  279. return
  280. }
  281. dumpPath := path.Join(setting.LogRootPath, "attachment_path.dump")
  282. ioutil.WriteFile(dumpPath, buf.Bytes(), 0666)
  283. log.Info("Failed to rename some attachments, old and new paths are saved into: %s", dumpPath)
  284. }()
  285. for _, attach := range attachments {
  286. if err = os.MkdirAll(path.Dir(attach.NewPath), os.ModePerm); err != nil {
  287. isSucceed = false
  288. return err
  289. }
  290. if err = os.Rename(attach.Path, attach.NewPath); err != nil {
  291. isSucceed = false
  292. return err
  293. }
  294. }
  295. return sess.Commit()
  296. }
  297. func renamePullRequestFields(x *xorm.Engine) (err error) {
  298. type PullRequest struct {
  299. ID int64 `xorm:"pk autoincr"`
  300. PullID int64 `xorm:"INDEX"`
  301. PullIndex int64
  302. HeadBarcnh string
  303. IssueID int64 `xorm:"INDEX"`
  304. Index int64
  305. HeadBranch string
  306. }
  307. if err = x.Sync(new(PullRequest)); err != nil {
  308. return fmt.Errorf("sync: %v", err)
  309. }
  310. results, err := x.Query("SELECT `id`,`pull_id`,`pull_index`,`head_barcnh` FROM `pull_request`")
  311. if err != nil {
  312. if strings.Contains(err.Error(), "no such column") {
  313. return nil
  314. }
  315. return fmt.Errorf("select pull requests: %v", err)
  316. }
  317. sess := x.NewSession()
  318. defer sessionRelease(sess)
  319. if err = sess.Begin(); err != nil {
  320. return err
  321. }
  322. var pull *PullRequest
  323. for _, pr := range results {
  324. pull = &PullRequest{
  325. ID: com.StrTo(pr["id"]).MustInt64(),
  326. IssueID: com.StrTo(pr["pull_id"]).MustInt64(),
  327. Index: com.StrTo(pr["pull_index"]).MustInt64(),
  328. HeadBranch: string(pr["head_barcnh"]),
  329. }
  330. if pull.Index == 0 {
  331. continue
  332. }
  333. if _, err = sess.Id(pull.ID).Update(pull); err != nil {
  334. return err
  335. }
  336. }
  337. return sess.Commit()
  338. }
  339. func cleanUpMigrateRepoInfo(x *xorm.Engine) (err error) {
  340. type (
  341. User struct {
  342. ID int64 `xorm:"pk autoincr"`
  343. LowerName string
  344. }
  345. Repository struct {
  346. ID int64 `xorm:"pk autoincr"`
  347. OwnerID int64
  348. LowerName string
  349. }
  350. )
  351. repos := make([]*Repository, 0, 25)
  352. if err = x.Where("is_mirror=?", false).Find(&repos); err != nil {
  353. return fmt.Errorf("select all non-mirror repositories: %v", err)
  354. }
  355. var user *User
  356. for _, repo := range repos {
  357. user = &User{ID: repo.OwnerID}
  358. has, err := x.Get(user)
  359. if err != nil {
  360. return fmt.Errorf("get owner of repository[%d - %d]: %v", repo.ID, repo.OwnerID, err)
  361. } else if !has {
  362. continue
  363. }
  364. configPath := filepath.Join(setting.RepoRootPath, user.LowerName, repo.LowerName+".git/config")
  365. // In case repository file is somehow missing.
  366. if !com.IsFile(configPath) {
  367. continue
  368. }
  369. cfg, err := ini.Load(configPath)
  370. if err != nil {
  371. return fmt.Errorf("open config file: %v", err)
  372. }
  373. cfg.DeleteSection("remote \"origin\"")
  374. if err = cfg.SaveToIndent(configPath, "\t"); err != nil {
  375. return fmt.Errorf("save config file: %v", err)
  376. }
  377. }
  378. return nil
  379. }
  380. func generateOrgRandsAndSalt(x *xorm.Engine) (err error) {
  381. type User struct {
  382. ID int64 `xorm:"pk autoincr"`
  383. Rands string `xorm:"VARCHAR(10)"`
  384. Salt string `xorm:"VARCHAR(10)"`
  385. }
  386. orgs := make([]*User, 0, 10)
  387. if err = x.Where("type=1").And("rands=''").Find(&orgs); err != nil {
  388. return fmt.Errorf("select all organizations: %v", err)
  389. }
  390. sess := x.NewSession()
  391. defer sessionRelease(sess)
  392. if err = sess.Begin(); err != nil {
  393. return err
  394. }
  395. for _, org := range orgs {
  396. if org.Rands, err = base.GetRandomString(10); err != nil {
  397. return err
  398. }
  399. if org.Salt, err = base.GetRandomString(10); err != nil {
  400. return err
  401. }
  402. if _, err = sess.Id(org.ID).Update(org); err != nil {
  403. return err
  404. }
  405. }
  406. return sess.Commit()
  407. }
  408. // TAction defines the struct for migrating table action
  409. type TAction struct {
  410. ID int64 `xorm:"pk autoincr"`
  411. CreatedUnix int64
  412. }
  413. // TableName will be invoked by XORM to customrize the table name
  414. func (t *TAction) TableName() string { return "action" }
  415. // TNotice defines the struct for migrating table notice
  416. type TNotice struct {
  417. ID int64 `xorm:"pk autoincr"`
  418. CreatedUnix int64
  419. }
  420. // TableName will be invoked by XORM to customrize the table name
  421. func (t *TNotice) TableName() string { return "notice" }
  422. // TComment defines the struct for migrating table comment
  423. type TComment struct {
  424. ID int64 `xorm:"pk autoincr"`
  425. CreatedUnix int64
  426. }
  427. // TableName will be invoked by XORM to customrize the table name
  428. func (t *TComment) TableName() string { return "comment" }
  429. // TIssue defines the struct for migrating table issue
  430. type TIssue struct {
  431. ID int64 `xorm:"pk autoincr"`
  432. DeadlineUnix int64
  433. CreatedUnix int64
  434. UpdatedUnix int64
  435. }
  436. // TableName will be invoked by XORM to customrize the table name
  437. func (t *TIssue) TableName() string { return "issue" }
  438. // TMilestone defines the struct for migrating table milestone
  439. type TMilestone struct {
  440. ID int64 `xorm:"pk autoincr"`
  441. DeadlineUnix int64
  442. ClosedDateUnix int64
  443. }
  444. // TableName will be invoked by XORM to customrize the table name
  445. func (t *TMilestone) TableName() string { return "milestone" }
  446. // TAttachment defines the struct for migrating table attachment
  447. type TAttachment struct {
  448. ID int64 `xorm:"pk autoincr"`
  449. CreatedUnix int64
  450. }
  451. // TableName will be invoked by XORM to customrize the table name
  452. func (t *TAttachment) TableName() string { return "attachment" }
  453. // TLoginSource defines the struct for migrating table login_source
  454. type TLoginSource struct {
  455. ID int64 `xorm:"pk autoincr"`
  456. CreatedUnix int64
  457. UpdatedUnix int64
  458. }
  459. // TableName will be invoked by XORM to customrize the table name
  460. func (t *TLoginSource) TableName() string { return "login_source" }
  461. // TPull defines the struct for migrating table pull_request
  462. type TPull struct {
  463. ID int64 `xorm:"pk autoincr"`
  464. MergedUnix int64
  465. }
  466. // TableName will be invoked by XORM to customrize the table name
  467. func (t *TPull) TableName() string { return "pull_request" }
  468. // TRelease defines the struct for migrating table release
  469. type TRelease struct {
  470. ID int64 `xorm:"pk autoincr"`
  471. CreatedUnix int64
  472. }
  473. // TableName will be invoked by XORM to customrize the table name
  474. func (t *TRelease) TableName() string { return "release" }
  475. // TRepo defines the struct for migrating table repository
  476. type TRepo struct {
  477. ID int64 `xorm:"pk autoincr"`
  478. CreatedUnix int64
  479. UpdatedUnix int64
  480. }
  481. // TableName will be invoked by XORM to customrize the table name
  482. func (t *TRepo) TableName() string { return "repository" }
  483. // TMirror defines the struct for migrating table mirror
  484. type TMirror struct {
  485. ID int64 `xorm:"pk autoincr"`
  486. UpdatedUnix int64
  487. NextUpdateUnix int64
  488. }
  489. // TableName will be invoked by XORM to customrize the table name
  490. func (t *TMirror) TableName() string { return "mirror" }
  491. // TPublicKey defines the struct for migrating table public_key
  492. type TPublicKey struct {
  493. ID int64 `xorm:"pk autoincr"`
  494. CreatedUnix int64
  495. UpdatedUnix int64
  496. }
  497. // TableName will be invoked by XORM to customrize the table name
  498. func (t *TPublicKey) TableName() string { return "public_key" }
  499. // TDeployKey defines the struct for migrating table deploy_key
  500. type TDeployKey struct {
  501. ID int64 `xorm:"pk autoincr"`
  502. CreatedUnix int64
  503. UpdatedUnix int64
  504. }
  505. // TableName will be invoked by XORM to customrize the table name
  506. func (t *TDeployKey) TableName() string { return "deploy_key" }
  507. // TAccessToken defines the struct for migrating table access_token
  508. type TAccessToken struct {
  509. ID int64 `xorm:"pk autoincr"`
  510. CreatedUnix int64
  511. UpdatedUnix int64
  512. }
  513. // TableName will be invoked by XORM to customrize the table name
  514. func (t *TAccessToken) TableName() string { return "access_token" }
  515. // TUser defines the struct for migrating table user
  516. type TUser struct {
  517. ID int64 `xorm:"pk autoincr"`
  518. CreatedUnix int64
  519. UpdatedUnix int64
  520. }
  521. // TableName will be invoked by XORM to customrize the table name
  522. func (t *TUser) TableName() string { return "user" }
  523. // TWebhook defines the struct for migrating table webhook
  524. type TWebhook struct {
  525. ID int64 `xorm:"pk autoincr"`
  526. CreatedUnix int64
  527. UpdatedUnix int64
  528. }
  529. // TableName will be invoked by XORM to customrize the table name
  530. func (t *TWebhook) TableName() string { return "webhook" }
  531. func convertDateToUnix(x *xorm.Engine) (err error) {
  532. log.Info("This migration could take up to minutes, please be patient.")
  533. type Bean struct {
  534. ID int64 `xorm:"pk autoincr"`
  535. Created time.Time
  536. Updated time.Time
  537. Merged time.Time
  538. Deadline time.Time
  539. ClosedDate time.Time
  540. NextUpdate time.Time
  541. }
  542. var tables = []struct {
  543. name string
  544. cols []string
  545. bean interface{}
  546. }{
  547. {"action", []string{"created"}, new(TAction)},
  548. {"notice", []string{"created"}, new(TNotice)},
  549. {"comment", []string{"created"}, new(TComment)},
  550. {"issue", []string{"deadline", "created", "updated"}, new(TIssue)},
  551. {"milestone", []string{"deadline", "closed_date"}, new(TMilestone)},
  552. {"attachment", []string{"created"}, new(TAttachment)},
  553. {"login_source", []string{"created", "updated"}, new(TLoginSource)},
  554. {"pull_request", []string{"merged"}, new(TPull)},
  555. {"release", []string{"created"}, new(TRelease)},
  556. {"repository", []string{"created", "updated"}, new(TRepo)},
  557. {"mirror", []string{"updated", "next_update"}, new(TMirror)},
  558. {"public_key", []string{"created", "updated"}, new(TPublicKey)},
  559. {"deploy_key", []string{"created", "updated"}, new(TDeployKey)},
  560. {"access_token", []string{"created", "updated"}, new(TAccessToken)},
  561. {"user", []string{"created", "updated"}, new(TUser)},
  562. {"webhook", []string{"created", "updated"}, new(TWebhook)},
  563. }
  564. for _, table := range tables {
  565. log.Info("Converting table: %s", table.name)
  566. if err = x.Sync2(table.bean); err != nil {
  567. return fmt.Errorf("Sync [table: %s]: %v", table.name, err)
  568. }
  569. offset := 0
  570. for {
  571. beans := make([]*Bean, 0, 100)
  572. if err = x.SQL(fmt.Sprintf("SELECT * FROM `%s` ORDER BY id ASC LIMIT 100 OFFSET %d",
  573. table.name, offset)).Find(&beans); err != nil {
  574. return fmt.Errorf("select beans [table: %s, offset: %d]: %v", table.name, offset, err)
  575. }
  576. log.Trace("Table [%s]: offset: %d, beans: %d", table.name, offset, len(beans))
  577. if len(beans) == 0 {
  578. break
  579. }
  580. offset += 100
  581. baseSQL := "UPDATE `" + table.name + "` SET "
  582. for _, bean := range beans {
  583. valSQLs := make([]string, 0, len(table.cols))
  584. for _, col := range table.cols {
  585. fieldSQL := ""
  586. fieldSQL += col + "_unix = "
  587. switch col {
  588. case "deadline":
  589. if bean.Deadline.IsZero() {
  590. continue
  591. }
  592. fieldSQL += com.ToStr(bean.Deadline.Unix())
  593. case "created":
  594. fieldSQL += com.ToStr(bean.Created.Unix())
  595. case "updated":
  596. fieldSQL += com.ToStr(bean.Updated.Unix())
  597. case "closed_date":
  598. fieldSQL += com.ToStr(bean.ClosedDate.Unix())
  599. case "merged":
  600. fieldSQL += com.ToStr(bean.Merged.Unix())
  601. case "next_update":
  602. fieldSQL += com.ToStr(bean.NextUpdate.Unix())
  603. }
  604. valSQLs = append(valSQLs, fieldSQL)
  605. }
  606. if len(valSQLs) == 0 {
  607. continue
  608. }
  609. if _, err = x.Exec(baseSQL + strings.Join(valSQLs, ",") + " WHERE id = " + com.ToStr(bean.ID)); err != nil {
  610. return fmt.Errorf("update bean [table: %s, id: %d]: %v", table.name, bean.ID, err)
  611. }
  612. }
  613. }
  614. }
  615. return nil
  616. }