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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  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. "code.gitea.io/gitea/models/migrations/v1_10"
  9. "code.gitea.io/gitea/models/migrations/v1_11"
  10. "code.gitea.io/gitea/models/migrations/v1_12"
  11. "code.gitea.io/gitea/models/migrations/v1_13"
  12. "code.gitea.io/gitea/models/migrations/v1_14"
  13. "code.gitea.io/gitea/models/migrations/v1_15"
  14. "code.gitea.io/gitea/models/migrations/v1_16"
  15. "code.gitea.io/gitea/models/migrations/v1_17"
  16. "code.gitea.io/gitea/models/migrations/v1_18"
  17. "code.gitea.io/gitea/models/migrations/v1_19"
  18. "code.gitea.io/gitea/models/migrations/v1_20"
  19. "code.gitea.io/gitea/models/migrations/v1_21"
  20. "code.gitea.io/gitea/models/migrations/v1_22"
  21. "code.gitea.io/gitea/models/migrations/v1_23"
  22. "code.gitea.io/gitea/models/migrations/v1_6"
  23. "code.gitea.io/gitea/models/migrations/v1_7"
  24. "code.gitea.io/gitea/models/migrations/v1_8"
  25. "code.gitea.io/gitea/models/migrations/v1_9"
  26. "code.gitea.io/gitea/modules/git"
  27. "code.gitea.io/gitea/modules/log"
  28. "code.gitea.io/gitea/modules/setting"
  29. "xorm.io/xorm"
  30. "xorm.io/xorm/names"
  31. )
  32. const minDBVersion = 70 // Gitea 1.5.3
  33. // Migration describes on migration from lower version to high version
  34. type Migration interface {
  35. Description() string
  36. Migrate(*xorm.Engine) error
  37. }
  38. type migration struct {
  39. description string
  40. migrate func(*xorm.Engine) error
  41. }
  42. // NewMigration creates a new migration
  43. func NewMigration(desc string, fn func(*xorm.Engine) error) Migration {
  44. return &migration{desc, fn}
  45. }
  46. // Description returns the migration's description
  47. func (m *migration) Description() string {
  48. return m.description
  49. }
  50. // Migrate executes the migration
  51. func (m *migration) Migrate(x *xorm.Engine) error {
  52. return m.migrate(x)
  53. }
  54. // Version describes the version table. Should have only one row with id==1
  55. type Version struct {
  56. ID int64 `xorm:"pk autoincr"`
  57. Version int64
  58. }
  59. // Use noopMigration when there is a migration that has been no-oped
  60. var noopMigration = func(_ *xorm.Engine) error { return nil }
  61. // This is a sequence of migrations. Add new migrations to the bottom of the list.
  62. // If you want to "retire" a migration, remove it from the top of the list and
  63. // update minDBVersion accordingly
  64. var migrations = []Migration{
  65. // Gitea 1.5.0 ends at v69
  66. // v70 -> v71
  67. NewMigration("add issue_dependencies", v1_6.AddIssueDependencies),
  68. // v71 -> v72
  69. NewMigration("protect each scratch token", v1_6.AddScratchHash),
  70. // v72 -> v73
  71. NewMigration("add review", v1_6.AddReview),
  72. // Gitea 1.6.0 ends at v73
  73. // v73 -> v74
  74. NewMigration("add must_change_password column for users table", v1_7.AddMustChangePassword),
  75. // v74 -> v75
  76. NewMigration("add approval whitelists to protected branches", v1_7.AddApprovalWhitelistsToProtectedBranches),
  77. // v75 -> v76
  78. NewMigration("clear nonused data which not deleted when user was deleted", v1_7.ClearNonusedData),
  79. // Gitea 1.7.0 ends at v76
  80. // v76 -> v77
  81. NewMigration("add pull request rebase with merge commit", v1_8.AddPullRequestRebaseWithMerge),
  82. // v77 -> v78
  83. NewMigration("add theme to users", v1_8.AddUserDefaultTheme),
  84. // v78 -> v79
  85. NewMigration("rename repo is_bare to repo is_empty", v1_8.RenameRepoIsBareToIsEmpty),
  86. // v79 -> v80
  87. NewMigration("add can close issues via commit in any branch", v1_8.AddCanCloseIssuesViaCommitInAnyBranch),
  88. // v80 -> v81
  89. NewMigration("add is locked to issues", v1_8.AddIsLockedToIssues),
  90. // v81 -> v82
  91. NewMigration("update U2F counter type", v1_8.ChangeU2FCounterType),
  92. // Gitea 1.8.0 ends at v82
  93. // v82 -> v83
  94. NewMigration("hot fix for wrong release sha1 on release table", v1_9.FixReleaseSha1OnReleaseTable),
  95. // v83 -> v84
  96. NewMigration("add uploader id for table attachment", v1_9.AddUploaderIDForAttachment),
  97. // v84 -> v85
  98. NewMigration("add table to store original imported gpg keys", v1_9.AddGPGKeyImport),
  99. // v85 -> v86
  100. NewMigration("hash application token", v1_9.HashAppToken),
  101. // v86 -> v87
  102. NewMigration("add http method to webhook", v1_9.AddHTTPMethodToWebhook),
  103. // v87 -> v88
  104. NewMigration("add avatar field to repository", v1_9.AddAvatarFieldToRepository),
  105. // Gitea 1.9.0 ends at v88
  106. // v88 -> v89
  107. NewMigration("add commit status context field to commit_status", v1_10.AddCommitStatusContext),
  108. // v89 -> v90
  109. NewMigration("add original author/url migration info to issues, comments, and repo ", v1_10.AddOriginalMigrationInfo),
  110. // v90 -> v91
  111. NewMigration("change length of some repository columns", v1_10.ChangeSomeColumnsLengthOfRepo),
  112. // v91 -> v92
  113. NewMigration("add index on owner_id of repository and type, review_id of comment", v1_10.AddIndexOnRepositoryAndComment),
  114. // v92 -> v93
  115. NewMigration("remove orphaned repository index statuses", v1_10.RemoveLingeringIndexStatus),
  116. // v93 -> v94
  117. NewMigration("add email notification enabled preference to user", v1_10.AddEmailNotificationEnabledToUser),
  118. // v94 -> v95
  119. NewMigration("add enable_status_check, status_check_contexts to protected_branch", v1_10.AddStatusCheckColumnsForProtectedBranches),
  120. // v95 -> v96
  121. NewMigration("add table columns for cross referencing issues", v1_10.AddCrossReferenceColumns),
  122. // v96 -> v97
  123. NewMigration("delete orphaned attachments", v1_10.DeleteOrphanedAttachments),
  124. // v97 -> v98
  125. NewMigration("add repo_admin_change_team_access to user", v1_10.AddRepoAdminChangeTeamAccessColumnForUser),
  126. // v98 -> v99
  127. NewMigration("add original author name and id on migrated release", v1_10.AddOriginalAuthorOnMigratedReleases),
  128. // v99 -> v100
  129. NewMigration("add task table and status column for repository table", v1_10.AddTaskTable),
  130. // v100 -> v101
  131. NewMigration("update migration repositories' service type", v1_10.UpdateMigrationServiceTypes),
  132. // v101 -> v102
  133. NewMigration("change length of some external login users columns", v1_10.ChangeSomeColumnsLengthOfExternalLoginUser),
  134. // Gitea 1.10.0 ends at v102
  135. // v102 -> v103
  136. NewMigration("update migration repositories' service type", v1_11.DropColumnHeadUserNameOnPullRequest),
  137. // v103 -> v104
  138. NewMigration("Add WhitelistDeployKeys to protected branch", v1_11.AddWhitelistDeployKeysToBranches),
  139. // v104 -> v105
  140. NewMigration("remove unnecessary columns from label", v1_11.RemoveLabelUneededCols),
  141. // v105 -> v106
  142. NewMigration("add includes_all_repositories to teams", v1_11.AddTeamIncludesAllRepositories),
  143. // v106 -> v107
  144. NewMigration("add column `mode` to table watch", v1_11.AddModeColumnToWatch),
  145. // v107 -> v108
  146. NewMigration("Add template options to repository", v1_11.AddTemplateToRepo),
  147. // v108 -> v109
  148. NewMigration("Add comment_id on table notification", v1_11.AddCommentIDOnNotification),
  149. // v109 -> v110
  150. NewMigration("add can_create_org_repo to team", v1_11.AddCanCreateOrgRepoColumnForTeam),
  151. // v110 -> v111
  152. NewMigration("change review content type to text", v1_11.ChangeReviewContentToText),
  153. // v111 -> v112
  154. NewMigration("update branch protection for can push and whitelist enable", v1_11.AddBranchProtectionCanPushAndEnableWhitelist),
  155. // v112 -> v113
  156. NewMigration("remove release attachments which repository deleted", v1_11.RemoveAttachmentMissedRepo),
  157. // v113 -> v114
  158. NewMigration("new feature: change target branch of pull requests", v1_11.FeatureChangeTargetBranch),
  159. // v114 -> v115
  160. NewMigration("Remove authentication credentials from stored URL", v1_11.SanitizeOriginalURL),
  161. // v115 -> v116
  162. NewMigration("add user_id prefix to existing user avatar name", v1_11.RenameExistingUserAvatarName),
  163. // v116 -> v117
  164. NewMigration("Extend TrackedTimes", v1_11.ExtendTrackedTimes),
  165. // Gitea 1.11.0 ends at v117
  166. // v117 -> v118
  167. NewMigration("Add block on rejected reviews branch protection", v1_12.AddBlockOnRejectedReviews),
  168. // v118 -> v119
  169. NewMigration("Add commit id and stale to reviews", v1_12.AddReviewCommitAndStale),
  170. // v119 -> v120
  171. NewMigration("Fix migrated repositories' git service type", v1_12.FixMigratedRepositoryServiceType),
  172. // v120 -> v121
  173. NewMigration("Add owner_name on table repository", v1_12.AddOwnerNameOnRepository),
  174. // v121 -> v122
  175. NewMigration("add is_restricted column for users table", v1_12.AddIsRestricted),
  176. // v122 -> v123
  177. NewMigration("Add Require Signed Commits to ProtectedBranch", v1_12.AddRequireSignedCommits),
  178. // v123 -> v124
  179. NewMigration("Add original information for reactions", v1_12.AddReactionOriginals),
  180. // v124 -> v125
  181. NewMigration("Add columns to user and repository", v1_12.AddUserRepoMissingColumns),
  182. // v125 -> v126
  183. NewMigration("Add some columns on review for migration", v1_12.AddReviewMigrateInfo),
  184. // v126 -> v127
  185. NewMigration("Fix topic repository count", v1_12.FixTopicRepositoryCount),
  186. // v127 -> v128
  187. NewMigration("add repository code language statistics", v1_12.AddLanguageStats),
  188. // v128 -> v129
  189. NewMigration("fix merge base for pull requests", v1_12.FixMergeBase),
  190. // v129 -> v130
  191. NewMigration("remove dependencies from deleted repositories", v1_12.PurgeUnusedDependencies),
  192. // v130 -> v131
  193. NewMigration("Expand webhooks for more granularity", v1_12.ExpandWebhooks),
  194. // v131 -> v132
  195. NewMigration("Add IsSystemWebhook column to webhooks table", v1_12.AddSystemWebhookColumn),
  196. // v132 -> v133
  197. NewMigration("Add Branch Protection Protected Files Column", v1_12.AddBranchProtectionProtectedFilesColumn),
  198. // v133 -> v134
  199. NewMigration("Add EmailHash Table", v1_12.AddEmailHashTable),
  200. // v134 -> v135
  201. NewMigration("Refix merge base for merged pull requests", v1_12.RefixMergeBase),
  202. // v135 -> v136
  203. NewMigration("Add OrgID column to Labels table", v1_12.AddOrgIDLabelColumn),
  204. // v136 -> v137
  205. NewMigration("Add CommitsAhead and CommitsBehind Column to PullRequest Table", v1_12.AddCommitDivergenceToPulls),
  206. // v137 -> v138
  207. NewMigration("Add Branch Protection Block Outdated Branch", v1_12.AddBlockOnOutdatedBranch),
  208. // v138 -> v139
  209. NewMigration("Add ResolveDoerID to Comment table", v1_12.AddResolveDoerIDCommentColumn),
  210. // v139 -> v140
  211. NewMigration("prepend refs/heads/ to issue refs", v1_12.PrependRefsHeadsToIssueRefs),
  212. // Gitea 1.12.0 ends at v140
  213. // v140 -> v141
  214. NewMigration("Save detected language file size to database instead of percent", v1_13.FixLanguageStatsToSaveSize),
  215. // v141 -> v142
  216. NewMigration("Add KeepActivityPrivate to User table", v1_13.AddKeepActivityPrivateUserColumn),
  217. // v142 -> v143
  218. NewMigration("Ensure Repository.IsArchived is not null", v1_13.SetIsArchivedToFalse),
  219. // v143 -> v144
  220. NewMigration("recalculate Stars number for all user", v1_13.RecalculateStars),
  221. // v144 -> v145
  222. NewMigration("update Matrix Webhook http method to 'PUT'", v1_13.UpdateMatrixWebhookHTTPMethod),
  223. // v145 -> v146
  224. NewMigration("Increase Language field to 50 in LanguageStats", v1_13.IncreaseLanguageField),
  225. // v146 -> v147
  226. NewMigration("Add projects info to repository table", v1_13.AddProjectsInfo),
  227. // v147 -> v148
  228. NewMigration("create review for 0 review id code comments", v1_13.CreateReviewsForCodeComments),
  229. // v148 -> v149
  230. NewMigration("remove issue dependency comments who refer to non existing issues", v1_13.PurgeInvalidDependenciesComments),
  231. // v149 -> v150
  232. NewMigration("Add Created and Updated to Milestone table", v1_13.AddCreatedAndUpdatedToMilestones),
  233. // v150 -> v151
  234. NewMigration("add primary key to repo_topic", v1_13.AddPrimaryKeyToRepoTopic),
  235. // v151 -> v152
  236. NewMigration("set default password algorithm to Argon2", v1_13.SetDefaultPasswordToArgon2),
  237. // v152 -> v153
  238. NewMigration("add TrustModel field to Repository", v1_13.AddTrustModelToRepository),
  239. // v153 > v154
  240. NewMigration("add Team review request support", v1_13.AddTeamReviewRequestSupport),
  241. // v154 > v155
  242. NewMigration("add timestamps to Star, Label, Follow, Watch and Collaboration", v1_13.AddTimeStamps),
  243. // Gitea 1.13.0 ends at v155
  244. // v155 -> v156
  245. NewMigration("add changed_protected_files column for pull_request table", v1_14.AddChangedProtectedFilesPullRequestColumn),
  246. // v156 -> v157
  247. NewMigration("fix publisher ID for tag releases", v1_14.FixPublisherIDforTagReleases),
  248. // v157 -> v158
  249. NewMigration("ensure repo topics are up-to-date", v1_14.FixRepoTopics),
  250. // v158 -> v159
  251. NewMigration("code comment replies should have the commitID of the review they are replying to", v1_14.UpdateCodeCommentReplies),
  252. // v159 -> v160
  253. NewMigration("update reactions constraint", v1_14.UpdateReactionConstraint),
  254. // v160 -> v161
  255. NewMigration("Add block on official review requests branch protection", v1_14.AddBlockOnOfficialReviewRequests),
  256. // v161 -> v162
  257. NewMigration("Convert task type from int to string", v1_14.ConvertTaskTypeToString),
  258. // v162 -> v163
  259. NewMigration("Convert webhook task type from int to string", v1_14.ConvertWebhookTaskTypeToString),
  260. // v163 -> v164
  261. NewMigration("Convert topic name from 25 to 50", v1_14.ConvertTopicNameFrom25To50),
  262. // v164 -> v165
  263. NewMigration("Add scope and nonce columns to oauth2_grant table", v1_14.AddScopeAndNonceColumnsToOAuth2Grant),
  264. // v165 -> v166
  265. NewMigration("Convert hook task type from char(16) to varchar(16) and trim the column", v1_14.ConvertHookTaskTypeToVarcharAndTrim),
  266. // v166 -> v167
  267. NewMigration("Where Password is Valid with Empty String delete it", v1_14.RecalculateUserEmptyPWD),
  268. // v167 -> v168
  269. NewMigration("Add user redirect", v1_14.AddUserRedirect),
  270. // v168 -> v169
  271. NewMigration("Recreate user table to fix default values", v1_14.RecreateUserTableToFixDefaultValues),
  272. // v169 -> v170
  273. NewMigration("Update DeleteBranch comments to set the old_ref to the commit_sha", v1_14.CommentTypeDeleteBranchUseOldRef),
  274. // v170 -> v171
  275. NewMigration("Add Dismissed to Review table", v1_14.AddDismissedReviewColumn),
  276. // v171 -> v172
  277. NewMigration("Add Sorting to ProjectBoard table", v1_14.AddSortingColToProjectBoard),
  278. // v172 -> v173
  279. NewMigration("Add sessions table for go-chi/session", v1_14.AddSessionTable),
  280. // v173 -> v174
  281. NewMigration("Add time_id column to Comment", v1_14.AddTimeIDCommentColumn),
  282. // v174 -> v175
  283. NewMigration("Create repo transfer table", v1_14.AddRepoTransfer),
  284. // v175 -> v176
  285. NewMigration("Fix Postgres ID Sequences broken by recreate-table", v1_14.FixPostgresIDSequences),
  286. // v176 -> v177
  287. NewMigration("Remove invalid labels from comments", v1_14.RemoveInvalidLabels),
  288. // v177 -> v178
  289. NewMigration("Delete orphaned IssueLabels", v1_14.DeleteOrphanedIssueLabels),
  290. // Gitea 1.14.0 ends at v178
  291. // v178 -> v179
  292. NewMigration("Add LFS columns to Mirror", v1_15.AddLFSMirrorColumns),
  293. // v179 -> v180
  294. NewMigration("Convert avatar url to text", v1_15.ConvertAvatarURLToText),
  295. // v180 -> v181
  296. NewMigration("Delete credentials from past migrations", v1_15.DeleteMigrationCredentials),
  297. // v181 -> v182
  298. NewMigration("Always save primary email on email address table", v1_15.AddPrimaryEmail2EmailAddress),
  299. // v182 -> v183
  300. NewMigration("Add issue resource index table", v1_15.AddIssueResourceIndexTable),
  301. // v183 -> v184
  302. NewMigration("Create PushMirror table", v1_15.CreatePushMirrorTable),
  303. // v184 -> v185
  304. NewMigration("Rename Task errors to message", v1_15.RenameTaskErrorsToMessage),
  305. // v185 -> v186
  306. NewMigration("Add new table repo_archiver", v1_15.AddRepoArchiver),
  307. // v186 -> v187
  308. NewMigration("Create protected tag table", v1_15.CreateProtectedTagTable),
  309. // v187 -> v188
  310. NewMigration("Drop unneeded webhook related columns", v1_15.DropWebhookColumns),
  311. // v188 -> v189
  312. NewMigration("Add key is verified to gpg key", v1_15.AddKeyIsVerified),
  313. // Gitea 1.15.0 ends at v189
  314. // v189 -> v190
  315. NewMigration("Unwrap ldap.Sources", v1_16.UnwrapLDAPSourceCfg),
  316. // v190 -> v191
  317. NewMigration("Add agit flow pull request support", v1_16.AddAgitFlowPullRequest),
  318. // v191 -> v192
  319. NewMigration("Alter issue/comment table TEXT fields to LONGTEXT", v1_16.AlterIssueAndCommentTextFieldsToLongText),
  320. // v192 -> v193
  321. NewMigration("RecreateIssueResourceIndexTable to have a primary key instead of an unique index", v1_16.RecreateIssueResourceIndexTable),
  322. // v193 -> v194
  323. NewMigration("Add repo id column for attachment table", v1_16.AddRepoIDForAttachment),
  324. // v194 -> v195
  325. NewMigration("Add Branch Protection Unprotected Files Column", v1_16.AddBranchProtectionUnprotectedFilesColumn),
  326. // v195 -> v196
  327. NewMigration("Add table commit_status_index", v1_16.AddTableCommitStatusIndex),
  328. // v196 -> v197
  329. NewMigration("Add Color to ProjectBoard table", v1_16.AddColorColToProjectBoard),
  330. // v197 -> v198
  331. NewMigration("Add renamed_branch table", v1_16.AddRenamedBranchTable),
  332. // v198 -> v199
  333. NewMigration("Add issue content history table", v1_16.AddTableIssueContentHistory),
  334. // v199 -> v200
  335. NewMigration("No-op (remote version is using AppState now)", noopMigration),
  336. // v200 -> v201
  337. NewMigration("Add table app_state", v1_16.AddTableAppState),
  338. // v201 -> v202
  339. NewMigration("Drop table remote_version (if exists)", v1_16.DropTableRemoteVersion),
  340. // v202 -> v203
  341. NewMigration("Create key/value table for user settings", v1_16.CreateUserSettingsTable),
  342. // v203 -> v204
  343. NewMigration("Add Sorting to ProjectIssue table", v1_16.AddProjectIssueSorting),
  344. // v204 -> v205
  345. NewMigration("Add key is verified to ssh key", v1_16.AddSSHKeyIsVerified),
  346. // v205 -> v206
  347. NewMigration("Migrate to higher varchar on user struct", v1_16.MigrateUserPasswordSalt),
  348. // v206 -> v207
  349. NewMigration("Add authorize column to team_unit table", v1_16.AddAuthorizeColForTeamUnit),
  350. // v207 -> v208
  351. NewMigration("Add webauthn table and migrate u2f data to webauthn - NO-OPED", v1_16.AddWebAuthnCred),
  352. // v208 -> v209
  353. NewMigration("Use base32.HexEncoding instead of base64 encoding for cred ID as it is case insensitive - NO-OPED", v1_16.UseBase32HexForCredIDInWebAuthnCredential),
  354. // v209 -> v210
  355. NewMigration("Increase WebAuthentication CredentialID size to 410 - NO-OPED", v1_16.IncreaseCredentialIDTo410),
  356. // v210 -> v211
  357. NewMigration("v208 was completely broken - remigrate", v1_16.RemigrateU2FCredentials),
  358. // Gitea 1.16.2 ends at v211
  359. // v211 -> v212
  360. NewMigration("Create ForeignReference table", v1_17.CreateForeignReferenceTable),
  361. // v212 -> v213
  362. NewMigration("Add package tables", v1_17.AddPackageTables),
  363. // v213 -> v214
  364. NewMigration("Add allow edits from maintainers to PullRequest table", v1_17.AddAllowMaintainerEdit),
  365. // v214 -> v215
  366. NewMigration("Add auto merge table", v1_17.AddAutoMergeTable),
  367. // v215 -> v216
  368. NewMigration("allow to view files in PRs", v1_17.AddReviewViewedFiles),
  369. // v216 -> v217
  370. NewMigration("No-op (Improve Action table indices v1)", noopMigration),
  371. // v217 -> v218
  372. NewMigration("Alter hook_task table TEXT fields to LONGTEXT", v1_17.AlterHookTaskTextFieldsToLongText),
  373. // v218 -> v219
  374. NewMigration("Improve Action table indices v2", v1_17.ImproveActionTableIndices),
  375. // v219 -> v220
  376. NewMigration("Add sync_on_commit column to push_mirror table", v1_17.AddSyncOnCommitColForPushMirror),
  377. // v220 -> v221
  378. NewMigration("Add container repository property", v1_17.AddContainerRepositoryProperty),
  379. // v221 -> v222
  380. NewMigration("Store WebAuthentication CredentialID as bytes and increase size to at least 1024", v1_17.StoreWebauthnCredentialIDAsBytes),
  381. // v222 -> v223
  382. NewMigration("Drop old CredentialID column", v1_17.DropOldCredentialIDColumn),
  383. // v223 -> v224
  384. NewMigration("Rename CredentialIDBytes column to CredentialID", v1_17.RenameCredentialIDBytes),
  385. // Gitea 1.17.0 ends at v224
  386. // v224 -> v225
  387. NewMigration("Add badges to users", v1_18.CreateUserBadgesTable),
  388. // v225 -> v226
  389. NewMigration("Alter gpg_key/public_key content TEXT fields to MEDIUMTEXT", v1_18.AlterPublicGPGKeyContentFieldsToMediumText),
  390. // v226 -> v227
  391. NewMigration("Conan and generic packages do not need to be semantically versioned", v1_18.FixPackageSemverField),
  392. // v227 -> v228
  393. NewMigration("Create key/value table for system settings", v1_18.CreateSystemSettingsTable),
  394. // v228 -> v229
  395. NewMigration("Add TeamInvite table", v1_18.AddTeamInviteTable),
  396. // v229 -> v230
  397. NewMigration("Update counts of all open milestones", v1_18.UpdateOpenMilestoneCounts),
  398. // v230 -> v231
  399. NewMigration("Add ConfidentialClient column (default true) to OAuth2Application table", v1_18.AddConfidentialClientColumnToOAuth2ApplicationTable),
  400. // Gitea 1.18.0 ends at v231
  401. // v231 -> v232
  402. NewMigration("Add index for hook_task", v1_19.AddIndexForHookTask),
  403. // v232 -> v233
  404. NewMigration("Alter package_version.metadata_json to LONGTEXT", v1_19.AlterPackageVersionMetadataToLongText),
  405. // v233 -> v234
  406. NewMigration("Add header_authorization_encrypted column to webhook table", v1_19.AddHeaderAuthorizationEncryptedColWebhook),
  407. // v234 -> v235
  408. NewMigration("Add package cleanup rule table", v1_19.CreatePackageCleanupRuleTable),
  409. // v235 -> v236
  410. NewMigration("Add index for access_token", v1_19.AddIndexForAccessToken),
  411. // v236 -> v237
  412. NewMigration("Create secrets table", v1_19.CreateSecretsTable),
  413. // v237 -> v238
  414. NewMigration("Drop ForeignReference table", v1_19.DropForeignReferenceTable),
  415. // v238 -> v239
  416. NewMigration("Add updated unix to LFSMetaObject", v1_19.AddUpdatedUnixToLFSMetaObject),
  417. // v239 -> v240
  418. NewMigration("Add scope for access_token", v1_19.AddScopeForAccessTokens),
  419. // v240 -> v241
  420. NewMigration("Add actions tables", v1_19.AddActionsTables),
  421. // v241 -> v242
  422. NewMigration("Add card_type column to project table", v1_19.AddCardTypeToProjectTable),
  423. // v242 -> v243
  424. NewMigration("Alter gpg_key_import content TEXT field to MEDIUMTEXT", v1_19.AlterPublicGPGKeyImportContentFieldToMediumText),
  425. // v243 -> v244
  426. NewMigration("Add exclusive label", v1_19.AddExclusiveLabel),
  427. // Gitea 1.19.0 ends at v244
  428. // v244 -> v245
  429. NewMigration("Add NeedApproval to actions tables", v1_20.AddNeedApprovalToActionRun),
  430. // v245 -> v246
  431. NewMigration("Rename Webhook org_id to owner_id", v1_20.RenameWebhookOrgToOwner),
  432. // v246 -> v247
  433. NewMigration("Add missed column owner_id for project table", v1_20.AddNewColumnForProject),
  434. // v247 -> v248
  435. NewMigration("Fix incorrect project type", v1_20.FixIncorrectProjectType),
  436. // v248 -> v249
  437. NewMigration("Add version column to action_runner table", v1_20.AddVersionToActionRunner),
  438. // v249 -> v250
  439. NewMigration("Improve Action table indices v3", v1_20.ImproveActionTableIndices),
  440. // v250 -> v251
  441. NewMigration("Change Container Metadata", v1_20.ChangeContainerMetadataMultiArch),
  442. // v251 -> v252
  443. NewMigration("Fix incorrect owner team unit access mode", v1_20.FixIncorrectOwnerTeamUnitAccessMode),
  444. // v252 -> v253
  445. NewMigration("Fix incorrect admin team unit access mode", v1_20.FixIncorrectAdminTeamUnitAccessMode),
  446. // v253 -> v254
  447. NewMigration("Fix ExternalTracker and ExternalWiki accessMode in owner and admin team", v1_20.FixExternalTrackerAndExternalWikiAccessModeInOwnerAndAdminTeam),
  448. // v254 -> v255
  449. NewMigration("Add ActionTaskOutput table", v1_20.AddActionTaskOutputTable),
  450. // v255 -> v256
  451. NewMigration("Add ArchivedUnix Column", v1_20.AddArchivedUnixToRepository),
  452. // v256 -> v257
  453. NewMigration("Add is_internal column to package", v1_20.AddIsInternalColumnToPackage),
  454. // v257 -> v258
  455. NewMigration("Add Actions Artifact table", v1_20.CreateActionArtifactTable),
  456. // v258 -> v259
  457. NewMigration("Add PinOrder Column", v1_20.AddPinOrderToIssue),
  458. // v259 -> v260
  459. NewMigration("Convert scoped access tokens", v1_20.ConvertScopedAccessTokens),
  460. // Gitea 1.20.0 ends at 260
  461. // v260 -> v261
  462. NewMigration("Drop custom_labels column of action_runner table", v1_21.DropCustomLabelsColumnOfActionRunner),
  463. // v261 -> v262
  464. NewMigration("Add variable table", v1_21.CreateVariableTable),
  465. // v262 -> v263
  466. NewMigration("Add TriggerEvent to action_run table", v1_21.AddTriggerEventToActionRun),
  467. // v263 -> v264
  468. NewMigration("Add git_size and lfs_size columns to repository table", v1_21.AddGitSizeAndLFSSizeToRepositoryTable),
  469. // v264 -> v265
  470. NewMigration("Add branch table", v1_21.AddBranchTable),
  471. // v265 -> v266
  472. NewMigration("Alter Actions Artifact table", v1_21.AlterActionArtifactTable),
  473. // v266 -> v267
  474. NewMigration("Reduce commit status", v1_21.ReduceCommitStatus),
  475. // v267 -> v268
  476. NewMigration("Add action_tasks_version table", v1_21.CreateActionTasksVersionTable),
  477. // v268 -> v269
  478. NewMigration("Update Action Ref", v1_21.UpdateActionsRefIndex),
  479. // v269 -> v270
  480. NewMigration("Drop deleted branch table", v1_21.DropDeletedBranchTable),
  481. // v270 -> v271
  482. NewMigration("Fix PackageProperty typo", v1_21.FixPackagePropertyTypo),
  483. // v271 -> v272
  484. NewMigration("Allow archiving labels", v1_21.AddArchivedUnixColumInLabelTable),
  485. // v272 -> v273
  486. NewMigration("Add Version to ActionRun table", v1_21.AddVersionToActionRunTable),
  487. // v273 -> v274
  488. NewMigration("Add Action Schedule Table", v1_21.AddActionScheduleTable),
  489. // v274 -> v275
  490. NewMigration("Add Actions artifacts expiration date", v1_21.AddExpiredUnixColumnInActionArtifactTable),
  491. // v275 -> v276
  492. NewMigration("Add ScheduleID for ActionRun", v1_21.AddScheduleIDForActionRun),
  493. // v276 -> v277
  494. NewMigration("Add RemoteAddress to mirrors", v1_21.AddRemoteAddressToMirrors),
  495. // v277 -> v278
  496. NewMigration("Add Index to issue_user.issue_id", v1_21.AddIndexToIssueUserIssueID),
  497. // v278 -> v279
  498. NewMigration("Add Index to comment.dependent_issue_id", v1_21.AddIndexToCommentDependentIssueID),
  499. // v279 -> v280
  500. NewMigration("Add Index to action.user_id", v1_21.AddIndexToActionUserID),
  501. // Gitea 1.21.0 ends at 280
  502. // v280 -> v281
  503. NewMigration("Rename user themes", v1_22.RenameUserThemes),
  504. // v281 -> v282
  505. NewMigration("Add auth_token table", v1_22.CreateAuthTokenTable),
  506. // v282 -> v283
  507. NewMigration("Add Index to pull_auto_merge.doer_id", v1_22.AddIndexToPullAutoMergeDoerID),
  508. // v283 -> v284
  509. NewMigration("Add combined Index to issue_user.uid and issue_id", v1_22.AddCombinedIndexToIssueUser),
  510. // v284 -> v285
  511. NewMigration("Add ignore stale approval column on branch table", v1_22.AddIgnoreStaleApprovalsColumnToProtectedBranchTable),
  512. // v285 -> v286
  513. NewMigration("Add PreviousDuration to ActionRun", v1_22.AddPreviousDurationToActionRun),
  514. // v286 -> v287
  515. NewMigration("Add support for SHA256 git repositories", v1_22.AdjustDBForSha256),
  516. // v287 -> v288
  517. NewMigration("Use Slug instead of ID for Badges", v1_22.UseSlugInsteadOfIDForBadges),
  518. // v288 -> v289
  519. NewMigration("Add user_blocking table", v1_22.AddUserBlockingTable),
  520. // v289 -> v290
  521. NewMigration("Add default_wiki_branch to repository table", v1_22.AddDefaultWikiBranch),
  522. // v290 -> v291
  523. NewMigration("Add PayloadVersion to HookTask", v1_22.AddPayloadVersionToHookTaskTable),
  524. // v291 -> v292
  525. NewMigration("Add Index to attachment.comment_id", v1_22.AddCommentIDIndexofAttachment),
  526. // v292 -> v293
  527. NewMigration("Ensure every project has exactly one default column - No Op", noopMigration),
  528. // v293 -> v294
  529. NewMigration("Ensure every project has exactly one default column", v1_22.CheckProjectColumnsConsistency),
  530. // Gitea 1.22.0 ends at 294
  531. // v294 -> v295
  532. NewMigration("Add unique index for project issue table", v1_23.AddUniqueIndexForProjectIssue),
  533. // v295 -> v296
  534. NewMigration("Add commit status summary table", v1_23.AddCommitStatusSummary),
  535. // v296 -> v297
  536. NewMigration("Add missing field of commit status summary table", v1_23.AddCommitStatusSummary2),
  537. // v297 -> v298
  538. NewMigration("Add everyone_access_mode for repo_unit", v1_23.AddRepoUnitEveryoneAccessMode),
  539. // v298 -> v299
  540. NewMigration("Drop wrongly created table o_auth2_application", v1_23.DropWronglyCreatedTable),
  541. // v300 -> v301
  542. NewMigration("Add TimeEstimate to issue table", v1_23.AddTimeEstimateColumnToIssueTable),
  543. }
  544. // GetCurrentDBVersion returns the current db version
  545. func GetCurrentDBVersion(x *xorm.Engine) (int64, error) {
  546. if err := x.Sync(new(Version)); err != nil {
  547. return -1, fmt.Errorf("sync: %w", err)
  548. }
  549. currentVersion := &Version{ID: 1}
  550. has, err := x.Get(currentVersion)
  551. if err != nil {
  552. return -1, fmt.Errorf("get: %w", err)
  553. }
  554. if !has {
  555. return -1, nil
  556. }
  557. return currentVersion.Version, nil
  558. }
  559. // ExpectedVersion returns the expected db version
  560. func ExpectedVersion() int64 {
  561. return int64(minDBVersion + len(migrations))
  562. }
  563. // EnsureUpToDate will check if the db is at the correct version
  564. func EnsureUpToDate(x *xorm.Engine) error {
  565. currentDB, err := GetCurrentDBVersion(x)
  566. if err != nil {
  567. return err
  568. }
  569. if currentDB < 0 {
  570. return fmt.Errorf("Database has not been initialized")
  571. }
  572. if minDBVersion > currentDB {
  573. 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)
  574. }
  575. expected := ExpectedVersion()
  576. if currentDB != expected {
  577. 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)
  578. }
  579. return nil
  580. }
  581. // Migrate database to current version
  582. func Migrate(x *xorm.Engine) error {
  583. // Set a new clean the default mapper to GonicMapper as that is the default for Gitea.
  584. x.SetMapper(names.GonicMapper{})
  585. if err := x.Sync(new(Version)); err != nil {
  586. return fmt.Errorf("sync: %w", err)
  587. }
  588. currentVersion := &Version{ID: 1}
  589. has, err := x.Get(currentVersion)
  590. if err != nil {
  591. return fmt.Errorf("get: %w", err)
  592. } else if !has {
  593. // If the version record does not exist we think
  594. // it is a fresh installation and we can skip all migrations.
  595. currentVersion.ID = 0
  596. currentVersion.Version = int64(minDBVersion + len(migrations))
  597. if _, err = x.InsertOne(currentVersion); err != nil {
  598. return fmt.Errorf("insert: %w", err)
  599. }
  600. }
  601. v := currentVersion.Version
  602. if minDBVersion > v {
  603. log.Fatal(`Gitea no longer supports auto-migration from your previously installed version.
  604. Please try upgrading to a lower version first (suggested v1.6.4), then upgrade to this version.`)
  605. return nil
  606. }
  607. // Downgrading Gitea's database version not supported
  608. if int(v-minDBVersion) > len(migrations) {
  609. 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))
  610. 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)."
  611. if !setting.IsProd {
  612. 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))
  613. }
  614. log.Fatal("Migration Error: %s", msg)
  615. return nil
  616. }
  617. // Some migration tasks depend on the git command
  618. if git.DefaultContext == nil {
  619. if err = git.InitSimple(context.Background()); err != nil {
  620. return err
  621. }
  622. }
  623. // Migrate
  624. for i, m := range migrations[v-minDBVersion:] {
  625. log.Info("Migration[%d]: %s", v+int64(i), m.Description())
  626. // Reset the mapper between each migration - migrations are not supposed to depend on each other
  627. x.SetMapper(names.GonicMapper{})
  628. if err = m.Migrate(x); err != nil {
  629. return fmt.Errorf("migration[%d]: %s failed: %w", v+int64(i), m.Description(), err)
  630. }
  631. currentVersion.Version = v + int64(i) + 1
  632. if _, err = x.ID(1).Update(currentVersion); err != nil {
  633. return err
  634. }
  635. }
  636. return nil
  637. }