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.

PurgeMapper.xml 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd">
  3. <mapper namespace="org.sonar.db.purge.PurgeMapper">
  4. <select id="selectAnalysisIdsAndUuids" parameterType="map" resultType="IdUuidPair">
  5. select
  6. s.id as id, s.uuid as uuid
  7. from
  8. snapshots s
  9. where
  10. s.component_uuid=#{componentUuid,jdbcType=VARCHAR}
  11. <if test="islast != null">
  12. and s.islast=#{islast}
  13. </if>
  14. <if test="notPurged != null and notPurged">
  15. and (s.purge_status is null or s.purge_status=0)
  16. </if>
  17. <if test="status != null">
  18. and s.status in
  19. <foreach item="s" index="index" collection="status" open="(" separator="," close=")">#{s}</foreach>
  20. </if>
  21. </select>
  22. <select id="selectPurgeableAnalyses" parameterType="String" resultType="PurgeableAnalysis">
  23. select
  24. s.id as "analysisId",
  25. s.uuid as "analysisUuid",
  26. s.created_at as "date",
  27. case when ve.analysis_uuid is not null then ${_true} else ${_false} end as "hasEvents",
  28. islast as "isLast",
  29. case when ve.category='Version' then ve.name else null end as "version"
  30. from snapshots s
  31. left outer join events ve on ve.analysis_uuid=s.uuid
  32. where
  33. s.component_uuid=#{componentUuid,jdbcType=VARCHAR}
  34. and s.status='P'
  35. </select>
  36. <select id="selectSpecificAnalysisNewCodePeriod" parameterType="String" resultType="String">
  37. select
  38. value
  39. from
  40. new_code_periods ncp
  41. where
  42. ncp.type='SPECIFIC_ANALYSIS'
  43. AND ncp.branch_uuid=#{projectUuid,jdbcType=VARCHAR}
  44. </select>
  45. <select id="selectStaleBranchesAndPullRequests" parameterType="map" resultType="String">
  46. select
  47. pb.uuid
  48. from
  49. project_branches pb
  50. where
  51. pb.project_uuid=#{projectUuid,jdbcType=VARCHAR}
  52. and pb.exclude_from_purge = ${_false}
  53. and pb.updated_at &lt; #{toDate}
  54. </select>
  55. <select id="selectRootAndModulesOrSubviewsByProjectUuid" resultType="IdUuidPair" parameterType="String">
  56. select
  57. p.id, p.uuid
  58. from
  59. components p
  60. where
  61. (
  62. p.project_uuid=#{rootUuid,jdbcType=VARCHAR}
  63. and p.scope = 'PRJ' and p.qualifier in ('SVW','BRC')
  64. )
  65. or (
  66. uuid=#{rootUuid,jdbcType=VARCHAR}
  67. and p.scope = 'PRJ' and p.qualifier in ('APP', 'VW','TRK')
  68. )
  69. </select>
  70. <select id="selectDisabledComponentsWithFileSource" parameterType="map" resultType="String">
  71. select
  72. file_uuid
  73. from file_sources fs
  74. inner join components p on
  75. p.uuid = fs.file_uuid
  76. and p.enabled = ${_false}
  77. and p.project_uuid=#{projectUuid,jdbcType=VARCHAR}
  78. </select>
  79. <select id="selectDisabledComponentsWithUnresolvedIssues" parameterType="map" resultType="String">
  80. select
  81. i.component_uuid
  82. from issues i
  83. inner join components p on
  84. p.uuid = i.component_uuid
  85. and p.enabled = ${_false}
  86. and p.project_uuid=#{projectUuid,jdbcType=VARCHAR}
  87. where
  88. resolution is null
  89. </select>
  90. <select id="selectDisabledComponentsWithLiveMeasures" parameterType="map" resultType="String">
  91. select
  92. lm.component_uuid
  93. from live_measures lm
  94. inner join components p on
  95. p.uuid = lm.component_uuid
  96. and p.enabled = ${_false}
  97. and p.project_uuid=#{projectUuid,jdbcType=VARCHAR}
  98. </select>
  99. <delete id="deleteAnalysisMeasures" parameterType="map">
  100. delete from project_measures
  101. where
  102. analysis_uuid in
  103. <foreach collection="analysisUuids" open="(" close=")" item="analysisUuid" separator=",">
  104. #{analysisUuid,jdbcType=VARCHAR}
  105. </foreach>
  106. </delete>
  107. <delete id="fullDeleteComponentMeasures" parameterType="map">
  108. delete from project_measures
  109. where
  110. component_uuid in
  111. <foreach collection="componentUuids" open="(" close=")" item="componentUuid" separator=",">
  112. #{componentUuid,jdbcType=VARCHAR}
  113. </foreach>
  114. </delete>
  115. <delete id="deleteAnalysisDuplications" parameterType="map">
  116. delete from duplications_index
  117. where
  118. analysis_uuid in
  119. <foreach collection="analysisUuids" open="(" close=")" item="analysisUuid" separator=",">
  120. #{analysisUuid,jdbcType=VARCHAR}
  121. </foreach>
  122. </delete>
  123. <delete id="deleteAnalysisEvents" parameterType="map">
  124. delete from events
  125. where
  126. analysis_uuid in
  127. <foreach collection="analysisUuids" open="(" close=")" item="analysisUuid" separator=",">
  128. #{analysisUuid,jdbcType=VARCHAR}
  129. </foreach>
  130. </delete>
  131. <delete id="deleteAnalysisEventComponentChanges" parameterType="map">
  132. delete from event_component_changes
  133. where
  134. event_analysis_uuid in
  135. <foreach collection="analysisUuids" open="(" close=")" item="analysisUuid" separator=",">
  136. #{analysisUuid,jdbcType=VARCHAR}
  137. </foreach>
  138. </delete>
  139. <delete id="deleteAnalyses" parameterType="map">
  140. delete from snapshots
  141. where
  142. uuid in
  143. <foreach collection="analysisUuids" open="(" close=")" item="analysisUuid" separator=",">
  144. #{analysisUuid,jdbcType=VARCHAR}
  145. </foreach>
  146. </delete>
  147. <delete id="deleteAnalysisProperties" parameterType="map">
  148. DELETE FROM analysis_properties
  149. WHERE
  150. analysis_uuid IN
  151. <foreach collection="analysisUuids" open="(" close=")" item="analysisUuid" separator=",">
  152. #{analysisUuid,jdbcType=VARCHAR}
  153. </foreach>
  154. </delete>
  155. <update id="updatePurgeStatusToOne" parameterType="map">
  156. update
  157. snapshots
  158. set
  159. purge_status = 1
  160. where
  161. uuid in
  162. <foreach collection="analysisUuids" open="(" close=")" item="analysisUuid" separator=",">
  163. #{analysisUuid,jdbcType=VARCHAR}
  164. </foreach>
  165. </update>
  166. <update id="resolveComponentIssuesNotAlreadyResolved" parameterType="map">
  167. update
  168. issues
  169. set
  170. status='CLOSED',
  171. resolution='REMOVED',
  172. updated_at=#{dateAsLong},
  173. issue_close_date=#{dateAsLong},
  174. issue_update_date=#{dateAsLong}
  175. where
  176. component_uuid in
  177. <foreach collection="componentUuids" open="(" close=")" item="componentUuid" separator=",">
  178. #{componentUuid,jdbcType=VARCHAR}
  179. </foreach>
  180. and resolution is null
  181. </update>
  182. <delete id="deleteProjectLinksByProjectUuid" parameterType="map">
  183. delete from project_links
  184. where
  185. project_uuid = #{rootUuid,jdbcType=VARCHAR}
  186. </delete>
  187. <delete id="deletePropertiesByComponentIds" parameterType="map">
  188. delete from properties
  189. where
  190. resource_id in
  191. <foreach collection="componentIds" open="(" close=")" item="componentId" separator=",">
  192. #{componentId}
  193. </foreach>
  194. </delete>
  195. <delete id="deleteComponentsByProjectUuid" parameterType="map">
  196. delete from components
  197. where
  198. project_uuid = #{rootUuid,jdbcType=VARCHAR}
  199. </delete>
  200. <delete id="deleteProjectsByProjectUuid" parameterType="map">
  201. delete from projects
  202. where
  203. uuid = #{projectUuid,jdbcType=VARCHAR}
  204. </delete>
  205. <delete id="deleteComponentsByUuids" parameterType="map">
  206. delete from components
  207. where
  208. uuid in
  209. <foreach collection="componentUuids" open="(" close=")" item="componentUuid" separator=",">
  210. #{componentUuid,jdbcType=VARCHAR}
  211. </foreach>
  212. </delete>
  213. <delete id="deleteGroupRolesByComponentId" parameterType="map">
  214. delete from group_roles
  215. where
  216. resource_id = #{rootId,jdbcType=INTEGER}
  217. </delete>
  218. <delete id="deleteUserRolesByComponentId" parameterType="map">
  219. delete from user_roles
  220. where
  221. resource_id = #{rootId,jdbcType=INTEGER}
  222. </delete>
  223. <delete id="deleteManualMeasuresByComponentUuids" parameterType="map">
  224. delete from manual_measures where component_uuid in
  225. <foreach collection="componentUuids" open="(" close=")" item="componentUuid" separator=",">
  226. #{componentUuid,jdbcType=VARCHAR}
  227. </foreach>
  228. </delete>
  229. <delete id="deleteEventsByComponentUuid" parameterType="map">
  230. delete from events
  231. where
  232. component_uuid = #{componentUuid,jdbcType=VARCHAR}
  233. </delete>
  234. <delete id="deleteEventComponentChangesByComponentUuid" parameterType="map">
  235. delete from event_component_changes
  236. where
  237. event_component_uuid = #{componentUuid,jdbcType=VARCHAR}
  238. </delete>
  239. <delete id="deleteIssueChangesByProjectUuid" parameterType="map">
  240. delete from issue_changes ic
  241. where
  242. exists (select 1 from issues i where i.kee=ic.issue_key and i.project_uuid = #{projectUuid,jdbcType=VARCHAR})
  243. </delete>
  244. <!-- Mssql -->
  245. <delete id="deleteIssueChangesByProjectUuid" databaseId="mssql" parameterType="map">
  246. delete issue_changes from issue_changes
  247. inner join issues on
  248. issue_changes.issue_key=issues.kee
  249. where
  250. issues.project_uuid = #{projectUuid,jdbcType=VARCHAR}
  251. </delete>
  252. <delete id="deleteIssuesByProjectUuid" parameterType="map">
  253. delete from issues
  254. where project_uuid = #{projectUuid,jdbcType=VARCHAR}
  255. </delete>
  256. <delete id="deleteFileSourcesByProjectUuid">
  257. delete from file_sources where project_uuid=#{rootProjectUuid,jdbcType=VARCHAR}
  258. </delete>
  259. <delete id="deleteFileSourcesByFileUuid">
  260. delete from file_sources
  261. where
  262. file_uuid in
  263. <foreach collection="fileUuids" open="(" close=")" item="fileUuid" separator=",">
  264. #{fileUuid,jdbcType=VARCHAR}
  265. </foreach>
  266. </delete>
  267. <select id="selectOldClosedIssueKeys" parameterType="map" resultType="String">
  268. SELECT kee FROM issues
  269. WHERE project_uuid=#{projectUuid,jdbcType=VARCHAR}
  270. <choose>
  271. <when test="toDate == null">
  272. AND issue_close_date IS NOT NULL
  273. </when>
  274. <otherwise>
  275. AND issue_close_date &lt; #{toDate}
  276. </otherwise>
  277. </choose>
  278. </select>
  279. <select id="selectDisabledComponentsWithoutIssues" resultType="IdUuidPair" parameterType="String">
  280. SELECT
  281. p.id, p.uuid
  282. FROM
  283. components p
  284. WHERE
  285. p.enabled = ${_false}
  286. AND p.project_uuid=#{projectUuid,jdbcType=VARCHAR}
  287. AND NOT EXISTS (SELECT 1 FROM issues i WHERE i.component_uuid = p.uuid)
  288. </select>
  289. <delete id="deleteIssuesFromKeys" parameterType="map">
  290. DELETE FROM issues
  291. WHERE kee IN
  292. <foreach collection="keys" open="(" close=")" item="key" separator=",">
  293. #{key,jdbcType=VARCHAR}
  294. </foreach>
  295. </delete>
  296. <delete id="deleteIssueChangesFromIssueKeys" parameterType="map">
  297. DELETE FROM issue_changes
  298. WHERE issue_key IN
  299. <foreach collection="issueKeys" open="(" close=")" item="issueKey" separator=",">
  300. #{issueKey,jdbcType=VARCHAR}
  301. </foreach>
  302. </delete>
  303. <delete id="deleteCeScannerContextOfCeActivityByRootUuidOrBefore">
  304. delete from ce_scanner_context
  305. where
  306. task_uuid in (
  307. select
  308. uuid
  309. from ce_activity
  310. <include refid="whereClauseCeActivityByRootUuidOrBefore" />
  311. )
  312. </delete>
  313. <delete id="deleteCeTaskCharacteristicsOfCeActivityByRootUuidOrBefore">
  314. delete from ce_task_characteristics
  315. where
  316. task_uuid in (
  317. select
  318. uuid
  319. from ce_activity
  320. <include refid="whereClauseCeActivityByRootUuidOrBefore" />
  321. )
  322. </delete>
  323. <delete id="deleteCeTaskInputOfCeActivityByRootUuidOrBefore">
  324. delete from ce_task_input
  325. where
  326. task_uuid in (
  327. select
  328. uuid
  329. from ce_activity
  330. <include refid="whereClauseCeActivityByRootUuidOrBefore" />
  331. )
  332. </delete>
  333. <delete id="deleteCeTaskMessageOfCeActivityByRootUuidOrBefore">
  334. delete from ce_task_message
  335. where
  336. task_uuid in (
  337. select
  338. uuid
  339. from ce_activity
  340. <include refid="whereClauseCeActivityByRootUuidOrBefore" />
  341. )
  342. </delete>
  343. <delete id="deleteCeActivityByRootUuidOrBefore">
  344. delete from ce_activity
  345. <include refid="whereClauseCeActivityByRootUuidOrBefore" />
  346. </delete>
  347. <sql id="whereClauseCeActivityByRootUuidOrBefore">
  348. where
  349. <choose>
  350. <when test="rootUuid != null and createdAtBefore != null">
  351. created_at &lt; #{createdAtBefore,jdbcType=BIGINT}
  352. and (
  353. component_uuid=#{rootUuid,jdbcType=VARCHAR}
  354. or main_component_uuid=#{rootUuid,jdbcType=VARCHAR}
  355. )
  356. </when>
  357. <when test="createdAtBefore != null">
  358. created_at &lt; #{createdAtBefore,jdbcType=BIGINT}
  359. </when>
  360. <when test="rootUuid != null">
  361. component_uuid=#{rootUuid,jdbcType=VARCHAR}
  362. or main_component_uuid=#{rootUuid,jdbcType=VARCHAR}
  363. </when>
  364. <!-- safety net when both variables are null to never generate a
  365. delete statement deleting the whole table -->
  366. <otherwise>
  367. 1 = 2
  368. </otherwise>
  369. </choose>
  370. </sql>
  371. <delete id="deleteCeScannerContextOfCeQueueByRootUuid">
  372. delete from ce_scanner_context
  373. where
  374. task_uuid in (
  375. select
  376. uuid
  377. from ce_queue
  378. where
  379. component_uuid=#{rootUuid,jdbcType=VARCHAR}
  380. or main_component_uuid=#{rootUuid,jdbcType=VARCHAR}
  381. )
  382. </delete>
  383. <delete id="deleteCeTaskCharacteristicsOfCeQueueByRootUuid">
  384. delete from ce_task_characteristics
  385. where
  386. task_uuid in (
  387. select
  388. uuid
  389. from ce_queue
  390. where
  391. component_uuid=#{rootUuid,jdbcType=VARCHAR}
  392. or main_component_uuid=#{rootUuid,jdbcType=VARCHAR}
  393. )
  394. </delete>
  395. <delete id="deleteCeTaskInputOfCeQueueByRootUuid">
  396. delete from ce_task_input
  397. where
  398. task_uuid in (
  399. select
  400. uuid
  401. from ce_queue
  402. where
  403. component_uuid=#{rootUuid,jdbcType=VARCHAR}
  404. or main_component_uuid=#{rootUuid,jdbcType=VARCHAR}
  405. )
  406. </delete>
  407. <delete id="deleteCeTaskMessageOfCeQueueByRootUuid">
  408. delete from ce_task_message
  409. where
  410. task_uuid in (
  411. select
  412. uuid
  413. from ce_queue
  414. where
  415. component_uuid=#{rootUuid,jdbcType=VARCHAR}
  416. or main_component_uuid=#{rootUuid,jdbcType=VARCHAR}
  417. )
  418. </delete>
  419. <delete id="deleteCeQueueByRootUuid">
  420. delete from ce_queue
  421. where
  422. component_uuid=#{rootUuid,jdbcType=VARCHAR}
  423. or main_component_uuid=#{rootUuid,jdbcType=VARCHAR}
  424. </delete>
  425. <delete id="deleteNewCodePeriodsByRootUuid">
  426. DELETE FROM new_code_periods
  427. WHERE
  428. branch_uuid=#{rootUuid,jdbcType=VARCHAR}
  429. OR project_uuid=#{rootUuid,jdbcType=VARCHAR}
  430. </delete>
  431. <delete id="deleteWebhooksByProjectUuid">
  432. delete from webhooks where project_uuid=#{projectUuid,jdbcType=VARCHAR}
  433. </delete>
  434. <delete id="deleteWebhookDeliveriesByProjectUuid">
  435. delete from webhook_deliveries where component_uuid=#{projectUuid,jdbcType=VARCHAR}
  436. </delete>
  437. <delete id="deleteProjectMappingsByProjectUuid">
  438. delete from project_mappings where project_uuid=#{projectUuid,jdbcType=VARCHAR}
  439. </delete>
  440. <delete id="deleteProjectAlmBindingsByProjectUuid">
  441. delete from project_alm_bindings where project_uuid=#{projectUuid,jdbcType=VARCHAR}
  442. </delete>
  443. <delete id="deleteBranchByUuid">
  444. delete from project_branches where uuid=#{uuid,jdbcType=VARCHAR}
  445. </delete>
  446. <delete id="deleteLiveMeasuresByProjectUuid">
  447. delete from live_measures where project_uuid = #{projectUuid,jdbcType=VARCHAR}
  448. </delete>
  449. <delete id="deleteLiveMeasuresByComponentUuids">
  450. delete from live_measures where component_uuid in <foreach item="componentUuid" index="index" collection="componentUuids" open="(" separator="," close=")">#{componentUuid, jdbcType=VARCHAR}</foreach>
  451. </delete>
  452. </mapper>