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.

migrations.go 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2017 The Gitea Authors. All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. package migrations
  5. import (
  6. "context"
  7. "fmt"
  8. "os"
  9. "code.gitea.io/gitea/models/migrations/v1_10"
  10. "code.gitea.io/gitea/models/migrations/v1_11"
  11. "code.gitea.io/gitea/models/migrations/v1_12"
  12. "code.gitea.io/gitea/models/migrations/v1_13"
  13. "code.gitea.io/gitea/models/migrations/v1_14"
  14. "code.gitea.io/gitea/models/migrations/v1_15"
  15. "code.gitea.io/gitea/models/migrations/v1_16"
  16. "code.gitea.io/gitea/models/migrations/v1_17"
  17. "code.gitea.io/gitea/models/migrations/v1_18"
  18. "code.gitea.io/gitea/models/migrations/v1_19"
  19. "code.gitea.io/gitea/models/migrations/v1_6"
  20. "code.gitea.io/gitea/models/migrations/v1_7"
  21. "code.gitea.io/gitea/models/migrations/v1_8"
  22. "code.gitea.io/gitea/models/migrations/v1_9"
  23. "code.gitea.io/gitea/modules/git"
  24. "code.gitea.io/gitea/modules/log"
  25. "code.gitea.io/gitea/modules/setting"
  26. "xorm.io/xorm"
  27. "xorm.io/xorm/names"
  28. )
  29. const minDBVersion = 70 // Gitea 1.5.3
  30. // Migration describes on migration from lower version to high version
  31. type Migration interface {
  32. Description() string
  33. Migrate(*xorm.Engine) error
  34. }
  35. type migration struct {
  36. description string
  37. migrate func(*xorm.Engine) error
  38. }
  39. // NewMigration creates a new migration
  40. func NewMigration(desc string, fn func(*xorm.Engine) error) Migration {
  41. return &migration{desc, fn}
  42. }
  43. // Description returns the migration's description
  44. func (m *migration) Description() string {
  45. return m.description
  46. }
  47. // Migrate executes the migration
  48. func (m *migration) Migrate(x *xorm.Engine) error {
  49. return m.migrate(x)
  50. }
  51. // Version describes the version table. Should have only one row with id==1
  52. type Version struct {
  53. ID int64 `xorm:"pk autoincr"`
  54. Version int64
  55. }
  56. // Use noopMigration when there is a migration that has been no-oped
  57. var noopMigration = func(_ *xorm.Engine) error { return nil }
  58. // This is a sequence of migrations. Add new migrations to the bottom of the list.
  59. // If you want to "retire" a migration, remove it from the top of the list and
  60. // update minDBVersion accordingly
  61. var migrations = []Migration{
  62. // Gitea 1.5.0 ends at v69
  63. // v70 -> v71
  64. NewMigration("add issue_dependencies", v1_6.AddIssueDependencies),
  65. // v71 -> v72
  66. NewMigration("protect each scratch token", v1_6.AddScratchHash),
  67. // v72 -> v73
  68. NewMigration("add review", v1_6.AddReview),
  69. // Gitea 1.6.0 ends at v73
  70. // v73 -> v74
  71. NewMigration("add must_change_password column for users table", v1_7.AddMustChangePassword),
  72. // v74 -> v75
  73. NewMigration("add approval whitelists to protected branches", v1_7.AddApprovalWhitelistsToProtectedBranches),
  74. // v75 -> v76
  75. NewMigration("clear nonused data which not deleted when user was deleted", v1_7.ClearNonusedData),
  76. // Gitea 1.7.0 ends at v76
  77. // v76 -> v77
  78. NewMigration("add pull request rebase with merge commit", v1_8.AddPullRequestRebaseWithMerge),
  79. // v77 -> v78
  80. NewMigration("add theme to users", v1_8.AddUserDefaultTheme),
  81. // v78 -> v79
  82. NewMigration("rename repo is_bare to repo is_empty", v1_8.RenameRepoIsBareToIsEmpty),
  83. // v79 -> v80
  84. NewMigration("add can close issues via commit in any branch", v1_8.AddCanCloseIssuesViaCommitInAnyBranch),
  85. // v80 -> v81
  86. NewMigration("add is locked to issues", v1_8.AddIsLockedToIssues),
  87. // v81 -> v82
  88. NewMigration("update U2F counter type", v1_8.ChangeU2FCounterType),
  89. // Gitea 1.8.0 ends at v82
  90. // v82 -> v83
  91. NewMigration("hot fix for wrong release sha1 on release table", v1_9.FixReleaseSha1OnReleaseTable),
  92. // v83 -> v84
  93. NewMigration("add uploader id for table attachment", v1_9.AddUploaderIDForAttachment),
  94. // v84 -> v85
  95. NewMigration("add table to store original imported gpg keys", v1_9.AddGPGKeyImport),
  96. // v85 -> v86
  97. NewMigration("hash application token", v1_9.HashAppToken),
  98. // v86 -> v87
  99. NewMigration("add http method to webhook", v1_9.AddHTTPMethodToWebhook),
  100. // v87 -> v88
  101. NewMigration("add avatar field to repository", v1_9.AddAvatarFieldToRepository),
  102. // Gitea 1.9.0 ends at v88
  103. // v88 -> v89
  104. NewMigration("add commit status context field to commit_status", v1_10.AddCommitStatusContext),
  105. // v89 -> v90
  106. NewMigration("add original author/url migration info to issues, comments, and repo ", v1_10.AddOriginalMigrationInfo),
  107. // v90 -> v91
  108. NewMigration("change length of some repository columns", v1_10.ChangeSomeColumnsLengthOfRepo),
  109. // v91 -> v92
  110. NewMigration("add index on owner_id of repository and type, review_id of comment", v1_10.AddIndexOnRepositoryAndComment),
  111. // v92 -> v93
  112. NewMigration("remove orphaned repository index statuses", v1_10.RemoveLingeringIndexStatus),
  113. // v93 -> v94
  114. NewMigration("add email notification enabled preference to user", v1_10.AddEmailNotificationEnabledToUser),
  115. // v94 -> v95
  116. NewMigration("add enable_status_check, status_check_contexts to protected_branch", v1_10.AddStatusCheckColumnsForProtectedBranches),
  117. // v95 -> v96
  118. NewMigration("add table columns for cross referencing issues", v1_10.AddCrossReferenceColumns),
  119. // v96 -> v97
  120. NewMigration("delete orphaned attachments", v1_10.DeleteOrphanedAttachments),
  121. // v97 -> v98
  122. NewMigration("add repo_admin_change_team_access to user", v1_10.AddRepoAdminChangeTeamAccessColumnForUser),
  123. // v98 -> v99
  124. NewMigration("add original author name and id on migrated release", v1_10.AddOriginalAuthorOnMigratedReleases),
  125. // v99 -> v100
  126. NewMigration("add task table and status column for repository table", v1_10.AddTaskTable),
  127. // v100 -> v101
  128. NewMigration("update migration repositories' service type", v1_10.UpdateMigrationServiceTypes),
  129. // v101 -> v102
  130. NewMigration("change length of some external login users columns", v1_10.ChangeSomeColumnsLengthOfExternalLoginUser),
  131. // Gitea 1.10.0 ends at v102
  132. // v102 -> v103
  133. NewMigration("update migration repositories' service type", v1_11.DropColumnHeadUserNameOnPullRequest),
  134. // v103 -> v104
  135. NewMigration("Add WhitelistDeployKeys to protected branch", v1_11.AddWhitelistDeployKeysToBranches),
  136. // v104 -> v105
  137. NewMigration("remove unnecessary columns from label", v1_11.RemoveLabelUneededCols),
  138. // v105 -> v106
  139. NewMigration("add includes_all_repositories to teams", v1_11.AddTeamIncludesAllRepositories),
  140. // v106 -> v107
  141. NewMigration("add column `mode` to table watch", v1_11.AddModeColumnToWatch),
  142. // v107 -> v108
  143. NewMigration("Add template options to repository", v1_11.AddTemplateToRepo),
  144. // v108 -> v109
  145. NewMigration("Add comment_id on table notification", v1_11.AddCommentIDOnNotification),
  146. // v109 -> v110
  147. NewMigration("add can_create_org_repo to team", v1_11.AddCanCreateOrgRepoColumnForTeam),
  148. // v110 -> v111
  149. NewMigration("change review content type to text", v1_11.ChangeReviewContentToText),
  150. // v111 -> v112
  151. NewMigration("update branch protection for can push and whitelist enable", v1_11.AddBranchProtectionCanPushAndEnableWhitelist),
  152. // v112 -> v113
  153. NewMigration("remove release attachments which repository deleted", v1_11.RemoveAttachmentMissedRepo),
  154. // v113 -> v114
  155. NewMigration("new feature: change target branch of pull requests", v1_11.FeatureChangeTargetBranch),
  156. // v114 -> v115
  157. NewMigration("Remove authentication credentials from stored URL", v1_11.SanitizeOriginalURL),
  158. // v115 -> v116
  159. NewMigration("add user_id prefix to existing user avatar name", v1_11.RenameExistingUserAvatarName),
  160. // v116 -> v117
  161. NewMigration("Extend TrackedTimes", v1_11.ExtendTrackedTimes),
  162. // Gitea 1.11.0 ends at v117
  163. // v117 -> v118
  164. NewMigration("Add block on rejected reviews branch protection", v1_12.AddBlockOnRejectedReviews),
  165. // v118 -> v119
  166. NewMigration("Add commit id and stale to reviews", v1_12.AddReviewCommitAndStale),
  167. // v119 -> v120
  168. NewMigration("Fix migrated repositories' git service type", v1_12.FixMigratedRepositoryServiceType),
  169. // v120 -> v121
  170. NewMigration("Add owner_name on table repository", v1_12.AddOwnerNameOnRepository),
  171. // v121 -> v122
  172. NewMigration("add is_restricted column for users table", v1_12.AddIsRestricted),
  173. // v122 -> v123
  174. NewMigration("Add Require Signed Commits to ProtectedBranch", v1_12.AddRequireSignedCommits),
  175. // v123 -> v124
  176. NewMigration("Add original information for reactions", v1_12.AddReactionOriginals),
  177. // v124 -> v125
  178. NewMigration("Add columns to user and repository", v1_12.AddUserRepoMissingColumns),
  179. // v125 -> v126
  180. NewMigration("Add some columns on review for migration", v1_12.AddReviewMigrateInfo),
  181. // v126 -> v127
  182. NewMigration("Fix topic repository count", v1_12.FixTopicRepositoryCount),
  183. // v127 -> v128
  184. NewMigration("add repository code language statistics", v1_12.AddLanguageStats),
  185. // v128 -> v129
  186. NewMigration("fix merge base for pull requests", v1_12.FixMergeBase),
  187. // v129 -> v130
  188. NewMigration("remove dependencies from deleted repositories", v1_12.PurgeUnusedDependencies),
  189. // v130 -> v131
  190. NewMigration("Expand webhooks for more granularity", v1_12.ExpandWebhooks),
  191. // v131 -> v132
  192. NewMigration("Add IsSystemWebhook column to webhooks table", v1_12.AddSystemWebhookColumn),
  193. // v132 -> v133
  194. NewMigration("Add Branch Protection Protected Files Column", v1_12.AddBranchProtectionProtectedFilesColumn),
  195. // v133 -> v134
  196. NewMigration("Add EmailHash Table", v1_12.AddEmailHashTable),
  197. // v134 -> v135
  198. NewMigration("Refix merge base for merged pull requests", v1_12.RefixMergeBase),
  199. // v135 -> v136
  200. NewMigration("Add OrgID column to Labels table", v1_12.AddOrgIDLabelColumn),
  201. // v136 -> v137
  202. NewMigration("Add CommitsAhead and CommitsBehind Column to PullRequest Table", v1_12.AddCommitDivergenceToPulls),
  203. // v137 -> v138
  204. NewMigration("Add Branch Protection Block Outdated Branch", v1_12.AddBlockOnOutdatedBranch),
  205. // v138 -> v139
  206. NewMigration("Add ResolveDoerID to Comment table", v1_12.AddResolveDoerIDCommentColumn),
  207. // v139 -> v140
  208. NewMigration("prepend refs/heads/ to issue refs", v1_12.PrependRefsHeadsToIssueRefs),
  209. // Gitea 1.12.0 ends at v140
  210. // v140 -> v141
  211. NewMigration("Save detected language file size to database instead of percent", v1_13.FixLanguageStatsToSaveSize),
  212. // v141 -> v142
  213. NewMigration("Add KeepActivityPrivate to User table", v1_13.AddKeepActivityPrivateUserColumn),
  214. // v142 -> v143
  215. NewMigration("Ensure Repository.IsArchived is not null", v1_13.SetIsArchivedToFalse),
  216. // v143 -> v144
  217. NewMigration("recalculate Stars number for all user", v1_13.RecalculateStars),
  218. // v144 -> v145
  219. NewMigration("update Matrix Webhook http method to 'PUT'", v1_13.UpdateMatrixWebhookHTTPMethod),
  220. // v145 -> v146
  221. NewMigration("Increase Language field to 50 in LanguageStats", v1_13.IncreaseLanguageField),
  222. // v146 -> v147
  223. NewMigration("Add projects info to repository table", v1_13.AddProjectsInfo),
  224. // v147 -> v148
  225. NewMigration("create review for 0 review id code comments", v1_13.CreateReviewsForCodeComments),
  226. // v148 -> v149
  227. NewMigration("remove issue dependency comments who refer to non existing issues", v1_13.PurgeInvalidDependenciesComments),
  228. // v149 -> v150
  229. NewMigration("Add Created and Updated to Milestone table", v1_13.AddCreatedAndUpdatedToMilestones),
  230. // v150 -> v151
  231. NewMigration("add primary key to repo_topic", v1_13.AddPrimaryKeyToRepoTopic),
  232. // v151 -> v152
  233. NewMigration("set default password algorithm to Argon2", v1_13.SetDefaultPasswordToArgon2),
  234. // v152 -> v153
  235. NewMigration("add TrustModel field to Repository", v1_13.AddTrustModelToRepository),
  236. // v153 > v154
  237. NewMigration("add Team review request support", v1_13.AddTeamReviewRequestSupport),
  238. // v154 > v155
  239. NewMigration("add timestamps to Star, Label, Follow, Watch and Collaboration", v1_13.AddTimeStamps),
  240. // Gitea 1.13.0 ends at v155
  241. // v155 -> v156
  242. NewMigration("add changed_protected_files column for pull_request table", v1_14.AddChangedProtectedFilesPullRequestColumn),
  243. // v156 -> v157
  244. NewMigration("fix publisher ID for tag releases", v1_14.FixPublisherIDforTagReleases),
  245. // v157 -> v158
  246. NewMigration("ensure repo topics are up-to-date", v1_14.FixRepoTopics),
  247. // v158 -> v159
  248. NewMigration("code comment replies should have the commitID of the review they are replying to", v1_14.UpdateCodeCommentReplies),
  249. // v159 -> v160
  250. NewMigration("update reactions constraint", v1_14.UpdateReactionConstraint),
  251. // v160 -> v161
  252. NewMigration("Add block on official review requests branch protection", v1_14.AddBlockOnOfficialReviewRequests),
  253. // v161 -> v162
  254. NewMigration("Convert task type from int to string", v1_14.ConvertTaskTypeToString),
  255. // v162 -> v163
  256. NewMigration("Convert webhook task type from int to string", v1_14.ConvertWebhookTaskTypeToString),
  257. // v163 -> v164
  258. NewMigration("Convert topic name from 25 to 50", v1_14.ConvertTopicNameFrom25To50),
  259. // v164 -> v165
  260. NewMigration("Add scope and nonce columns to oauth2_grant table", v1_14.AddScopeAndNonceColumnsToOAuth2Grant),
  261. // v165 -> v166
  262. NewMigration("Convert hook task type from char(16) to varchar(16) and trim the column", v1_14.ConvertHookTaskTypeToVarcharAndTrim),
  263. // v166 -> v167
  264. NewMigration("Where Password is Valid with Empty String delete it", v1_14.RecalculateUserEmptyPWD),
  265. // v167 -> v168
  266. NewMigration("Add user redirect", v1_14.AddUserRedirect),
  267. // v168 -> v169
  268. NewMigration("Recreate user table to fix default values", v1_14.RecreateUserTableToFixDefaultValues),
  269. // v169 -> v170
  270. NewMigration("Update DeleteBranch comments to set the old_ref to the commit_sha", v1_14.CommentTypeDeleteBranchUseOldRef),
  271. // v170 -> v171
  272. NewMigration("Add Dismissed to Review table", v1_14.AddDismissedReviewColumn),
  273. // v171 -> v172
  274. NewMigration("Add Sorting to ProjectBoard table", v1_14.AddSortingColToProjectBoard),
  275. // v172 -> v173
  276. NewMigration("Add sessions table for go-chi/session", v1_14.AddSessionTable),
  277. // v173 -> v174
  278. NewMigration("Add time_id column to Comment", v1_14.AddTimeIDCommentColumn),
  279. // v174 -> v175
  280. NewMigration("Create repo transfer table", v1_14.AddRepoTransfer),
  281. // v175 -> v176
  282. NewMigration("Fix Postgres ID Sequences broken by recreate-table", v1_14.FixPostgresIDSequences),
  283. // v176 -> v177
  284. NewMigration("Remove invalid labels from comments", v1_14.RemoveInvalidLabels),
  285. // v177 -> v178
  286. NewMigration("Delete orphaned IssueLabels", v1_14.DeleteOrphanedIssueLabels),
  287. // Gitea 1.14.0 ends at v178
  288. // v178 -> v179
  289. NewMigration("Add LFS columns to Mirror", v1_15.AddLFSMirrorColumns),
  290. // v179 -> v180
  291. NewMigration("Convert avatar url to text", v1_15.ConvertAvatarURLToText),
  292. // v180 -> v181
  293. NewMigration("Delete credentials from past migrations", v1_15.DeleteMigrationCredentials),
  294. // v181 -> v182
  295. NewMigration("Always save primary email on email address table", v1_15.AddPrimaryEmail2EmailAddress),
  296. // v182 -> v183
  297. NewMigration("Add issue resource index table", v1_15.AddIssueResourceIndexTable),
  298. // v183 -> v184
  299. NewMigration("Create PushMirror table", v1_15.CreatePushMirrorTable),
  300. // v184 -> v185
  301. NewMigration("Rename Task errors to message", v1_15.RenameTaskErrorsToMessage),
  302. // v185 -> v186
  303. NewMigration("Add new table repo_archiver", v1_15.AddRepoArchiver),
  304. // v186 -> v187
  305. NewMigration("Create protected tag table", v1_15.CreateProtectedTagTable),
  306. // v187 -> v188
  307. NewMigration("Drop unneeded webhook related columns", v1_15.DropWebhookColumns),
  308. // v188 -> v189
  309. NewMigration("Add key is verified to gpg key", v1_15.AddKeyIsVerified),
  310. // Gitea 1.15.0 ends at v189
  311. // v189 -> v190
  312. NewMigration("Unwrap ldap.Sources", v1_16.UnwrapLDAPSourceCfg),
  313. // v190 -> v191
  314. NewMigration("Add agit flow pull request support", v1_16.AddAgitFlowPullRequest),
  315. // v191 -> v192
  316. NewMigration("Alter issue/comment table TEXT fields to LONGTEXT", v1_16.AlterIssueAndCommentTextFieldsToLongText),
  317. // v192 -> v193
  318. NewMigration("RecreateIssueResourceIndexTable to have a primary key instead of an unique index", v1_16.RecreateIssueResourceIndexTable),
  319. // v193 -> v194
  320. NewMigration("Add repo id column for attachment table", v1_16.AddRepoIDForAttachment),
  321. // v194 -> v195
  322. NewMigration("Add Branch Protection Unprotected Files Column", v1_16.AddBranchProtectionUnprotectedFilesColumn),
  323. // v195 -> v196
  324. NewMigration("Add table commit_status_index", v1_16.AddTableCommitStatusIndex),
  325. // v196 -> v197
  326. NewMigration("Add Color to ProjectBoard table", v1_16.AddColorColToProjectBoard),
  327. // v197 -> v198
  328. NewMigration("Add renamed_branch table", v1_16.AddRenamedBranchTable),
  329. // v198 -> v199
  330. NewMigration("Add issue content history table", v1_16.AddTableIssueContentHistory),
  331. // v199 -> v200
  332. NewMigration("No-op (remote version is using AppState now)", noopMigration),
  333. // v200 -> v201
  334. NewMigration("Add table app_state", v1_16.AddTableAppState),
  335. // v201 -> v202
  336. NewMigration("Drop table remote_version (if exists)", v1_16.DropTableRemoteVersion),
  337. // v202 -> v203
  338. NewMigration("Create key/value table for user settings", v1_16.CreateUserSettingsTable),
  339. // v203 -> v204
  340. NewMigration("Add Sorting to ProjectIssue table", v1_16.AddProjectIssueSorting),
  341. // v204 -> v205
  342. NewMigration("Add key is verified to ssh key", v1_16.AddSSHKeyIsVerified),
  343. // v205 -> v206
  344. NewMigration("Migrate to higher varchar on user struct", v1_16.MigrateUserPasswordSalt),
  345. // v206 -> v207
  346. NewMigration("Add authorize column to team_unit table", v1_16.AddAuthorizeColForTeamUnit),
  347. // v207 -> v208
  348. NewMigration("Add webauthn table and migrate u2f data to webauthn - NO-OPED", v1_16.AddWebAuthnCred),
  349. // v208 -> v209
  350. NewMigration("Use base32.HexEncoding instead of base64 encoding for cred ID as it is case insensitive - NO-OPED", v1_16.UseBase32HexForCredIDInWebAuthnCredential),
  351. // v209 -> v210
  352. NewMigration("Increase WebAuthentication CredentialID size to 410 - NO-OPED", v1_16.IncreaseCredentialIDTo410),
  353. // v210 -> v211
  354. NewMigration("v208 was completely broken - remigrate", v1_16.RemigrateU2FCredentials),
  355. // Gitea 1.16.2 ends at v211
  356. // v211 -> v212
  357. NewMigration("Create ForeignReference table", v1_17.CreateForeignReferenceTable),
  358. // v212 -> v213
  359. NewMigration("Add package tables", v1_17.AddPackageTables),
  360. // v213 -> v214
  361. NewMigration("Add allow edits from maintainers to PullRequest table", v1_17.AddAllowMaintainerEdit),
  362. // v214 -> v215
  363. NewMigration("Add auto merge table", v1_17.AddAutoMergeTable),
  364. // v215 -> v216
  365. NewMigration("allow to view files in PRs", v1_17.AddReviewViewedFiles),
  366. // v216 -> v217
  367. NewMigration("No-op (Improve Action table indices v1)", noopMigration),
  368. // v217 -> v218
  369. NewMigration("Alter hook_task table TEXT fields to LONGTEXT", v1_17.AlterHookTaskTextFieldsToLongText),
  370. // v218 -> v219
  371. NewMigration("Improve Action table indices v2", v1_17.ImproveActionTableIndices),
  372. // v219 -> v220
  373. NewMigration("Add sync_on_commit column to push_mirror table", v1_17.AddSyncOnCommitColForPushMirror),
  374. // v220 -> v221
  375. NewMigration("Add container repository property", v1_17.AddContainerRepositoryProperty),
  376. // v221 -> v222
  377. NewMigration("Store WebAuthentication CredentialID as bytes and increase size to at least 1024", v1_17.StoreWebauthnCredentialIDAsBytes),
  378. // v222 -> v223
  379. NewMigration("Drop old CredentialID column", v1_17.DropOldCredentialIDColumn),
  380. // v223 -> v224
  381. NewMigration("Rename CredentialIDBytes column to CredentialID", v1_17.RenameCredentialIDBytes),
  382. // Gitea 1.17.0 ends at v224
  383. // v224 -> v225
  384. NewMigration("Add badges to users", v1_18.CreateUserBadgesTable),
  385. // v225 -> v226
  386. NewMigration("Alter gpg_key/public_key content TEXT fields to MEDIUMTEXT", v1_18.AlterPublicGPGKeyContentFieldsToMediumText),
  387. // v226 -> v227
  388. NewMigration("Conan and generic packages do not need to be semantically versioned", v1_18.FixPackageSemverField),
  389. // v227 -> v228
  390. NewMigration("Create key/value table for system settings", v1_18.CreateSystemSettingsTable),
  391. // v228 -> v229
  392. NewMigration("Add TeamInvite table", v1_18.AddTeamInviteTable),
  393. // v229 -> v230
  394. NewMigration("Update counts of all open milestones", v1_18.UpdateOpenMilestoneCounts),
  395. // v230 -> v231
  396. NewMigration("Add ConfidentialClient column (default true) to OAuth2Application table", v1_18.AddConfidentialClientColumnToOAuth2ApplicationTable),
  397. // Gitea 1.18.0 ends at v231
  398. // v231 -> v232
  399. NewMigration("Add index for hook_task", v1_19.AddIndexForHookTask),
  400. // v232 -> v233
  401. NewMigration("Alter package_version.metadata_json to LONGTEXT", v1_19.AlterPackageVersionMetadataToLongText),
  402. // v233 -> v234
  403. NewMigration("Add header_authorization_encrypted column to webhook table", v1_19.AddHeaderAuthorizationEncryptedColWebhook),
  404. // v234 -> v235
  405. NewMigration("Add package cleanup rule table", v1_19.CreatePackageCleanupRuleTable),
  406. // v235 -> v236
  407. NewMigration("Add index for access_token", v1_19.AddIndexForAccessToken),
  408. // v236 -> v237
  409. NewMigration("Create secrets table", v1_19.CreateSecretsTable),
  410. // v237 -> v238
  411. NewMigration("Drop ForeignReference table", v1_19.DropForeignReferenceTable),
  412. // v238 -> v239
  413. NewMigration("Add updated unix to LFSMetaObject", v1_19.AddUpdatedUnixToLFSMetaObject),
  414. // v239 -> v240
  415. NewMigration("Add scope for access_token", v1_19.AddScopeForAccessTokens),
  416. // v240 -> v241
  417. NewMigration("Add actions tables", v1_19.AddActionsTables),
  418. }
  419. // GetCurrentDBVersion returns the current db version
  420. func GetCurrentDBVersion(x *xorm.Engine) (int64, error) {
  421. if err := x.Sync(new(Version)); err != nil {
  422. return -1, fmt.Errorf("sync: %w", err)
  423. }
  424. currentVersion := &Version{ID: 1}
  425. has, err := x.Get(currentVersion)
  426. if err != nil {
  427. return -1, fmt.Errorf("get: %w", err)
  428. }
  429. if !has {
  430. return -1, nil
  431. }
  432. return currentVersion.Version, nil
  433. }
  434. // ExpectedVersion returns the expected db version
  435. func ExpectedVersion() int64 {
  436. return int64(minDBVersion + len(migrations))
  437. }
  438. // EnsureUpToDate will check if the db is at the correct version
  439. func EnsureUpToDate(x *xorm.Engine) error {
  440. currentDB, err := GetCurrentDBVersion(x)
  441. if err != nil {
  442. return err
  443. }
  444. if currentDB < 0 {
  445. return fmt.Errorf("Database has not been initialized")
  446. }
  447. if minDBVersion > currentDB {
  448. return fmt.Errorf("DB version %d (<= %d) is too old for auto-migration. Upgrade to Gitea 1.6.4 first then upgrade to this version", currentDB, minDBVersion)
  449. }
  450. expected := ExpectedVersion()
  451. if currentDB != expected {
  452. return fmt.Errorf(`Current database version %d is not equal to the expected version %d. Please run "gitea [--config /path/to/app.ini] migrate" to update the database version`, currentDB, expected)
  453. }
  454. return nil
  455. }
  456. // Migrate database to current version
  457. func Migrate(x *xorm.Engine) error {
  458. // Set a new clean the default mapper to GonicMapper as that is the default for Gitea.
  459. x.SetMapper(names.GonicMapper{})
  460. if err := x.Sync(new(Version)); err != nil {
  461. return fmt.Errorf("sync: %w", err)
  462. }
  463. currentVersion := &Version{ID: 1}
  464. has, err := x.Get(currentVersion)
  465. if err != nil {
  466. return fmt.Errorf("get: %w", err)
  467. } else if !has {
  468. // If the version record does not exist we think
  469. // it is a fresh installation and we can skip all migrations.
  470. currentVersion.ID = 0
  471. currentVersion.Version = int64(minDBVersion + len(migrations))
  472. if _, err = x.InsertOne(currentVersion); err != nil {
  473. return fmt.Errorf("insert: %w", err)
  474. }
  475. }
  476. v := currentVersion.Version
  477. if minDBVersion > v {
  478. log.Fatal(`Gitea no longer supports auto-migration from your previously installed version.
  479. Please try upgrading to a lower version first (suggested v1.6.4), then upgrade to this version.`)
  480. return nil
  481. }
  482. // Downgrading Gitea's database version not supported
  483. if int(v-minDBVersion) > len(migrations) {
  484. msg := fmt.Sprintf("Your database (migration version: %d) is for a newer Gitea, you can not use the newer database for this old Gitea release (%d).", v, minDBVersion+len(migrations))
  485. msg += "\nGitea will exit to keep your database safe and unchanged. Please use the correct Gitea release, do not change the migration version manually (incorrect manual operation may lose data)."
  486. if !setting.IsProd {
  487. msg += fmt.Sprintf("\nIf you are in development and really know what you're doing, you can force changing the migration version by executing: UPDATE version SET version=%d WHERE id=1;", minDBVersion+len(migrations))
  488. }
  489. _, _ = fmt.Fprintln(os.Stderr, msg)
  490. log.Fatal(msg)
  491. return nil
  492. }
  493. // Some migration tasks depend on the git command
  494. if git.DefaultContext == nil {
  495. if err = git.InitSimple(context.Background()); err != nil {
  496. return err
  497. }
  498. }
  499. // Migrate
  500. for i, m := range migrations[v-minDBVersion:] {
  501. log.Info("Migration[%d]: %s", v+int64(i), m.Description())
  502. // Reset the mapper between each migration - migrations are not supposed to depend on each other
  503. x.SetMapper(names.GonicMapper{})
  504. if err = m.Migrate(x); err != nil {
  505. return fmt.Errorf("migration[%d]: %s failed: %w", v+int64(i), m.Description(), err)
  506. }
  507. currentVersion.Version = v + int64(i) + 1
  508. if _, err = x.ID(1).Update(currentVersion); err != nil {
  509. return err
  510. }
  511. }
  512. return nil
  513. }