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 26KB

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