migrations.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. "fmt"
  7. "strings"
  8. "time"
  9. "github.com/Unknwon/com"
  10. "github.com/go-xorm/xorm"
  11. log "notabug.org/makenotabuggreatagain/clog"
  12. "notabug.org/hp/gogs/pkg/tool"
  13. )
  14. const _MIN_DB_VER = 10
  15. type Migration interface {
  16. Description() string
  17. Migrate(*xorm.Engine) error
  18. }
  19. type migration struct {
  20. description string
  21. migrate func(*xorm.Engine) error
  22. }
  23. func NewMigration(desc string, fn func(*xorm.Engine) error) Migration {
  24. return &migration{desc, fn}
  25. }
  26. func (m *migration) Description() string {
  27. return m.description
  28. }
  29. func (m *migration) Migrate(x *xorm.Engine) error {
  30. return m.migrate(x)
  31. }
  32. // The version table. Should have only one row with id==1
  33. type Version struct {
  34. ID int64
  35. Version int64
  36. }
  37. // This is a sequence of migrations. Add new migrations to the bottom of the list.
  38. // If you want to "retire" a migration, remove it from the top of the list and
  39. // update _MIN_VER_DB accordingly
  40. var migrations = []Migration{
  41. // v0 -> v4 : before 0.6.0 -> last support 0.7.33
  42. // v4 -> v10: before 0.7.0 -> last support 0.9.141
  43. NewMigration("generate rands and salt for organizations", generateOrgRandsAndSalt), // V10 -> V11:v0.8.5
  44. NewMigration("convert date to unix timestamp", convertDateToUnix), // V11 -> V12:v0.9.2
  45. NewMigration("convert LDAP UseSSL option to SecurityProtocol", ldapUseSSLToSecurityProtocol), // V12 -> V13:v0.9.37
  46. // v13 -> v14:v0.9.87
  47. NewMigration("set comment updated with created", setCommentUpdatedWithCreated),
  48. // v14 -> v15:v0.9.147
  49. NewMigration("generate and migrate Git hooks", generateAndMigrateGitHooks),
  50. // v15 -> v16:v0.10.16
  51. NewMigration("update repository sizes", updateRepositorySizes),
  52. // v16 -> v17:v0.10.31
  53. NewMigration("remove invalid protect branch whitelist", removeInvalidProtectBranchWhitelist),
  54. }
  55. // Migrate database to current version
  56. func Migrate(x *xorm.Engine) error {
  57. if err := x.Sync(new(Version)); err != nil {
  58. return fmt.Errorf("sync: %v", err)
  59. }
  60. currentVersion := &Version{ID: 1}
  61. has, err := x.Get(currentVersion)
  62. if err != nil {
  63. return fmt.Errorf("get: %v", err)
  64. } else if !has {
  65. // If the version record does not exist we think
  66. // it is a fresh installation and we can skip all migrations.
  67. currentVersion.ID = 0
  68. currentVersion.Version = int64(_MIN_DB_VER + len(migrations))
  69. if _, err = x.InsertOne(currentVersion); err != nil {
  70. return fmt.Errorf("insert: %v", err)
  71. }
  72. }
  73. v := currentVersion.Version
  74. if _MIN_DB_VER > v {
  75. log.Fatal(0, `
  76. Hi there, thank you for using Gogs for so long!
  77. However, Gogs has stopped supporting auto-migration from your previously installed version.
  78. But the good news is, it's very easy to fix this problem!
  79. You can migrate your older database using a previous release, then you can upgrade to the newest version.
  80. Please save following instructions to somewhere and start working:
  81. - If you were using below 0.6.0 (e.g. 0.5.x), download last supported archive from following link:
  82. https://notabug.org/hp/gogs/releases/tag/v0.7.33
  83. - If you were using below 0.7.0 (e.g. 0.6.x), download last supported archive from following link:
  84. https://notabug.org/hp/gogs/releases/tag/v0.9.141
  85. Once finished downloading,
  86. 1. Extract the archive and to upgrade steps as usual.
  87. 2. Run it once. To verify, you should see some migration traces.
  88. 3. Once it starts web server successfully, stop it.
  89. 4. Now it's time to put back the release archive you originally intent to upgrade.
  90. 5. Enjoy!
  91. In case you're stilling getting this notice, go through instructions again until it disappears.`)
  92. return nil
  93. }
  94. if int(v-_MIN_DB_VER) > len(migrations) {
  95. // User downgraded Gogs.
  96. currentVersion.Version = int64(len(migrations) + _MIN_DB_VER)
  97. _, err = x.Id(1).Update(currentVersion)
  98. return err
  99. }
  100. for i, m := range migrations[v-_MIN_DB_VER:] {
  101. log.Info("Migration: %s", m.Description())
  102. if err = m.Migrate(x); err != nil {
  103. return fmt.Errorf("do migrate: %v", err)
  104. }
  105. currentVersion.Version = v + int64(i) + 1
  106. if _, err = x.Id(1).Update(currentVersion); err != nil {
  107. return err
  108. }
  109. }
  110. return nil
  111. }
  112. func sessionRelease(sess *xorm.Session) {
  113. // @bug
  114. // if !sess.IsCommitedOrRollbacked {
  115. // sess.Rollback()
  116. // }
  117. sess.Close()
  118. }
  119. func generateOrgRandsAndSalt(x *xorm.Engine) (err error) {
  120. type User struct {
  121. ID int64 `xorm:"pk autoincr"`
  122. Rands string `xorm:"VARCHAR(10)"`
  123. Salt string `xorm:"VARCHAR(10)"`
  124. }
  125. orgs := make([]*User, 0, 10)
  126. if err = x.Where("type=1").And("rands=''").Find(&orgs); err != nil {
  127. return fmt.Errorf("select all organizations: %v", err)
  128. }
  129. sess := x.NewSession()
  130. defer sess.Close()
  131. if err = sess.Begin(); err != nil {
  132. return err
  133. }
  134. for _, org := range orgs {
  135. if org.Rands, err = tool.RandomString(10); err != nil {
  136. return err
  137. }
  138. if org.Salt, err = tool.RandomString(10); err != nil {
  139. return err
  140. }
  141. if _, err = sess.Id(org.ID).Update(org); err != nil {
  142. return err
  143. }
  144. }
  145. return sess.Commit()
  146. }
  147. type TAction struct {
  148. ID int64 `xorm:"pk autoincr"`
  149. CreatedUnix int64
  150. }
  151. func (t *TAction) TableName() string { return "action" }
  152. type TNotice struct {
  153. ID int64 `xorm:"pk autoincr"`
  154. CreatedUnix int64
  155. }
  156. func (t *TNotice) TableName() string { return "notice" }
  157. type TComment struct {
  158. ID int64 `xorm:"pk autoincr"`
  159. CreatedUnix int64
  160. }
  161. func (t *TComment) TableName() string { return "comment" }
  162. type TIssue struct {
  163. ID int64 `xorm:"pk autoincr"`
  164. DeadlineUnix int64
  165. CreatedUnix int64
  166. UpdatedUnix int64
  167. }
  168. func (t *TIssue) TableName() string { return "issue" }
  169. type TMilestone struct {
  170. ID int64 `xorm:"pk autoincr"`
  171. DeadlineUnix int64
  172. ClosedDateUnix int64
  173. }
  174. func (t *TMilestone) TableName() string { return "milestone" }
  175. type TAttachment struct {
  176. ID int64 `xorm:"pk autoincr"`
  177. CreatedUnix int64
  178. }
  179. func (t *TAttachment) TableName() string { return "attachment" }
  180. type TLoginSource struct {
  181. ID int64 `xorm:"pk autoincr"`
  182. CreatedUnix int64
  183. UpdatedUnix int64
  184. }
  185. func (t *TLoginSource) TableName() string { return "login_source" }
  186. type TPull struct {
  187. ID int64 `xorm:"pk autoincr"`
  188. MergedUnix int64
  189. }
  190. func (t *TPull) TableName() string { return "pull_request" }
  191. type TRelease struct {
  192. ID int64 `xorm:"pk autoincr"`
  193. CreatedUnix int64
  194. }
  195. func (t *TRelease) TableName() string { return "release" }
  196. type TRepo struct {
  197. ID int64 `xorm:"pk autoincr"`
  198. CreatedUnix int64
  199. UpdatedUnix int64
  200. }
  201. func (t *TRepo) TableName() string { return "repository" }
  202. type TMirror struct {
  203. ID int64 `xorm:"pk autoincr"`
  204. UpdatedUnix int64
  205. NextUpdateUnix int64
  206. }
  207. func (t *TMirror) TableName() string { return "mirror" }
  208. type TPublicKey struct {
  209. ID int64 `xorm:"pk autoincr"`
  210. CreatedUnix int64
  211. UpdatedUnix int64
  212. }
  213. func (t *TPublicKey) TableName() string { return "public_key" }
  214. type TDeployKey struct {
  215. ID int64 `xorm:"pk autoincr"`
  216. CreatedUnix int64
  217. UpdatedUnix int64
  218. }
  219. func (t *TDeployKey) TableName() string { return "deploy_key" }
  220. type TAccessToken struct {
  221. ID int64 `xorm:"pk autoincr"`
  222. CreatedUnix int64
  223. UpdatedUnix int64
  224. }
  225. func (t *TAccessToken) TableName() string { return "access_token" }
  226. type TUser struct {
  227. ID int64 `xorm:"pk autoincr"`
  228. CreatedUnix int64
  229. UpdatedUnix int64
  230. }
  231. func (t *TUser) TableName() string { return "user" }
  232. type TWebhook struct {
  233. ID int64 `xorm:"pk autoincr"`
  234. CreatedUnix int64
  235. UpdatedUnix int64
  236. }
  237. func (t *TWebhook) TableName() string { return "webhook" }
  238. func convertDateToUnix(x *xorm.Engine) (err error) {
  239. log.Info("This migration could take up to minutes, please be patient.")
  240. type Bean struct {
  241. ID int64 `xorm:"pk autoincr"`
  242. Created time.Time
  243. Updated time.Time
  244. Merged time.Time
  245. Deadline time.Time
  246. ClosedDate time.Time
  247. NextUpdate time.Time
  248. }
  249. var tables = []struct {
  250. name string
  251. cols []string
  252. bean interface{}
  253. }{
  254. {"action", []string{"created"}, new(TAction)},
  255. {"notice", []string{"created"}, new(TNotice)},
  256. {"comment", []string{"created"}, new(TComment)},
  257. {"issue", []string{"deadline", "created", "updated"}, new(TIssue)},
  258. {"milestone", []string{"deadline", "closed_date"}, new(TMilestone)},
  259. {"attachment", []string{"created"}, new(TAttachment)},
  260. {"login_source", []string{"created", "updated"}, new(TLoginSource)},
  261. {"pull_request", []string{"merged"}, new(TPull)},
  262. {"release", []string{"created"}, new(TRelease)},
  263. {"repository", []string{"created", "updated"}, new(TRepo)},
  264. {"mirror", []string{"updated", "next_update"}, new(TMirror)},
  265. {"public_key", []string{"created", "updated"}, new(TPublicKey)},
  266. {"deploy_key", []string{"created", "updated"}, new(TDeployKey)},
  267. {"access_token", []string{"created", "updated"}, new(TAccessToken)},
  268. {"user", []string{"created", "updated"}, new(TUser)},
  269. {"webhook", []string{"created", "updated"}, new(TWebhook)},
  270. }
  271. for _, table := range tables {
  272. log.Info("Converting table: %s", table.name)
  273. if err = x.Sync2(table.bean); err != nil {
  274. return fmt.Errorf("Sync [table: %s]: %v", table.name, err)
  275. }
  276. offset := 0
  277. for {
  278. beans := make([]*Bean, 0, 100)
  279. if err = x.Sql(fmt.Sprintf("SELECT * FROM `%s` ORDER BY id ASC LIMIT 100 OFFSET %d",
  280. table.name, offset)).Find(&beans); err != nil {
  281. return fmt.Errorf("select beans [table: %s, offset: %d]: %v", table.name, offset, err)
  282. }
  283. log.Trace("Table [%s]: offset: %d, beans: %d", table.name, offset, len(beans))
  284. if len(beans) == 0 {
  285. break
  286. }
  287. offset += 100
  288. baseSQL := "UPDATE `" + table.name + "` SET "
  289. for _, bean := range beans {
  290. valSQLs := make([]string, 0, len(table.cols))
  291. for _, col := range table.cols {
  292. fieldSQL := ""
  293. fieldSQL += col + "_unix = "
  294. switch col {
  295. case "deadline":
  296. if bean.Deadline.IsZero() {
  297. continue
  298. }
  299. fieldSQL += com.ToStr(bean.Deadline.Unix())
  300. case "created":
  301. fieldSQL += com.ToStr(bean.Created.Unix())
  302. case "updated":
  303. fieldSQL += com.ToStr(bean.Updated.Unix())
  304. case "closed_date":
  305. fieldSQL += com.ToStr(bean.ClosedDate.Unix())
  306. case "merged":
  307. fieldSQL += com.ToStr(bean.Merged.Unix())
  308. case "next_update":
  309. fieldSQL += com.ToStr(bean.NextUpdate.Unix())
  310. }
  311. valSQLs = append(valSQLs, fieldSQL)
  312. }
  313. if len(valSQLs) == 0 {
  314. continue
  315. }
  316. if _, err = x.Exec(baseSQL + strings.Join(valSQLs, ",") + " WHERE id = " + com.ToStr(bean.ID)); err != nil {
  317. return fmt.Errorf("update bean [table: %s, id: %d]: %v", table.name, bean.ID, err)
  318. }
  319. }
  320. }
  321. }
  322. return nil
  323. }