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

migrations.go 20 kB

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