]> source.dussan.org Git - sonarqube.git/blob
37851839f4f07a2d788c212323791f8d67cd7de7
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2021 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 3 of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19  */
20 package org.sonar.server.platform.db.migration.version.v00;
21
22 import java.sql.SQLException;
23 import org.sonar.db.Database;
24 import org.sonar.server.platform.db.migration.def.BigIntegerColumnDef;
25 import org.sonar.server.platform.db.migration.def.BooleanColumnDef;
26 import org.sonar.server.platform.db.migration.def.ColumnDef;
27 import org.sonar.server.platform.db.migration.def.IntegerColumnDef;
28 import org.sonar.server.platform.db.migration.def.TimestampColumnDef;
29 import org.sonar.server.platform.db.migration.def.TinyIntColumnDef;
30 import org.sonar.server.platform.db.migration.def.VarcharColumnDef;
31 import org.sonar.server.platform.db.migration.sql.CreateIndexBuilder;
32 import org.sonar.server.platform.db.migration.sql.CreateTableBuilder;
33 import org.sonar.server.platform.db.migration.step.DdlChange;
34
35 import static java.util.Arrays.stream;
36 import static java.util.stream.Stream.concat;
37 import static java.util.stream.Stream.of;
38 import static org.sonar.server.platform.db.migration.def.BigIntegerColumnDef.newBigIntegerColumnDefBuilder;
39 import static org.sonar.server.platform.db.migration.def.BlobColumnDef.newBlobColumnDefBuilder;
40 import static org.sonar.server.platform.db.migration.def.BooleanColumnDef.newBooleanColumnDefBuilder;
41 import static org.sonar.server.platform.db.migration.def.ClobColumnDef.newClobColumnDefBuilder;
42 import static org.sonar.server.platform.db.migration.def.DecimalColumnDef.newDecimalColumnDefBuilder;
43 import static org.sonar.server.platform.db.migration.def.IntegerColumnDef.newIntegerColumnDefBuilder;
44 import static org.sonar.server.platform.db.migration.def.TimestampColumnDef.newTimestampColumnDefBuilder;
45 import static org.sonar.server.platform.db.migration.def.TinyIntColumnDef.newTinyIntColumnDefBuilder;
46 import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.MAX_SIZE;
47 import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.USER_UUID_SIZE;
48 import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.UUID_SIZE;
49 import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.UUID_VARCHAR_SIZE;
50 import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder;
51
52 public class CreateInitialSchema extends DdlChange {
53
54   /**
55    * Initially, UUID columns were created with size 50 when only 40 is needed. {@link VarcharColumnDef#UUID_SIZE}
56    * should be used instead of this constant whenever reducing the column size is possible.
57    */
58   private static final int OLD_UUID_VARCHAR_SIZE = 50;
59
60   // keep column name constants in alphabetic order
61   private static final String ANALYSIS_UUID_COL_NAME = "analysis_uuid";
62   private static final String COMPONENT_UUID_COL_NAME = "component_uuid";
63   private static final String CREATED_AT_COL_NAME = "created_at";
64   private static final String DESCRIPTION_COL_NAME = "description";
65   private static final String GROUP_UUID_COL_NAME = "group_uuid";
66   private static final String LANGUAGE_COL_NAME = "language";
67   private static final String METRIC_UUID_COL_NAME = "metric_uuid";
68   private static final String PROJECT_UUID_COL_NAME = "project_uuid";
69   private static final String RULE_UUID_COL_NAME = "rule_uuid";
70   private static final String STATUS_COL_NAME = "status";
71   private static final String TASK_UUID_COL_NAME = "task_uuid";
72   private static final String TEMPLATE_UUID_COL_NAME = "template_uuid";
73   private static final String TEXT_VALUE_COL_NAME = "text_value";
74   private static final String UPDATED_AT_COL_NAME = "updated_at";
75   private static final String USER_UUID_COL_NAME = "user_uuid";
76   private static final String VALUE_COL_NAME = "value";
77   private static final String QPROFILE_UUID_COL_NAME = "qprofile_uuid";
78
79   private static final String UNIQUE_INDEX_SUFFIX = "_unique";
80
81   // usual technical columns
82   private static final VarcharColumnDef UUID_COL = newVarcharColumnDefBuilder().setColumnName("uuid").setIsNullable(false).setLimit(UUID_SIZE).build();
83
84   private static final BigIntegerColumnDef TECHNICAL_CREATED_AT_COL = newBigIntegerColumnDefBuilder().setColumnName(CREATED_AT_COL_NAME).setIsNullable(false).build();
85   private static final BigIntegerColumnDef NULLABLE_TECHNICAL_CREATED_AT_COL = newBigIntegerColumnDefBuilder().setColumnName(CREATED_AT_COL_NAME).setIsNullable(true).build();
86   private static final BigIntegerColumnDef TECHNICAL_UPDATED_AT_COL = newBigIntegerColumnDefBuilder().setColumnName(UPDATED_AT_COL_NAME).setIsNullable(false).build();
87   private static final BigIntegerColumnDef NULLABLE_TECHNICAL_UPDATED_AT_COL = newBigIntegerColumnDefBuilder().setColumnName(UPDATED_AT_COL_NAME).setIsNullable(true).build();
88   private static final TimestampColumnDef DEPRECATED_TECHNICAL_CREATED_AT_COL = newTimestampColumnDefBuilder().setColumnName(CREATED_AT_COL_NAME).setIsNullable(true).build();
89   private static final TimestampColumnDef DEPRECATED_TECHNICAL_UPDATED_AT_COL = newTimestampColumnDefBuilder().setColumnName(UPDATED_AT_COL_NAME).setIsNullable(true).build();
90
91   public CreateInitialSchema(Database db) {
92     super(db);
93   }
94
95   @Override
96   public void execute(Context context) throws SQLException {
97     createActiveRuleParameters(context);
98     createActiveRules(context);
99     createAlmPats(context);
100     createAlmSettings(context);
101     createProjectAlmSettings(context);
102     createAnalysisProperties(context);
103     createAppBranchProjectBranch(context);
104     createAppProjects(context);
105     createCeActivity(context);
106     createCeQueue(context);
107     createCeScannerContext(context);
108     createCeTaskCharacteristics(context);
109     createCeTaskInput(context);
110     createCeTaskMessage(context);
111     createComponents(context);
112     createDefaultQProfiles(context);
113     createDeprecatedRuleKeys(context);
114     createDuplicationsIndex(context);
115     createEsQueue(context);
116     createEventComponentChanges(context);
117     createEvents(context);
118     createFileSources(context);
119     createGroupRoles(context);
120     createGroups(context);
121     createGroupsUsers(context);
122     createInternalComponentProps(context);
123     createInternalProperties(context);
124     createIssueChanges(context);
125     createIssues(context);
126     createLiveMeasures(context);
127     createManualMeasures(context);
128     createMetrics(context);
129     createNewCodePeriods(context);
130     createNotifications(context);
131     createOrgQProfiles(context);
132     createPermTemplatesGroups(context);
133     createPermTemplatesUsers(context);
134     createPermTemplatesCharacteristics(context);
135     createPermissionTemplates(context);
136     createPlugins(context);
137     createProjectBranches(context);
138     createProjectLinks(context);
139     createProjectMappings(context);
140     createProjectMeasures(context);
141     createProjectQprofiles(context);
142     createProjects(context);
143     createProjectQGates(context);
144     createProperties(context);
145     createQProfileChanges(context);
146     createQProfileEditGroups(context);
147     createQProfileEditUsers(context);
148     createQualityGateConditions(context);
149     createQualityGates(context);
150     createSessionTokens(context);
151     createRulesRepository(context);
152     createRules(context);
153     createRulesMetadata(context);
154     createRulesParameters(context);
155     createRulesProfiles(context);
156     createSamlMessageIds(context);
157     createSnapshots(context);
158     createUserProperties(context);
159     createUserRoles(context);
160     createUserDismissedMessage(context);
161     createUserTokens(context);
162     createUsers(context);
163     createWebhookDeliveries(context);
164     createWebhooks(context);
165   }
166
167   private void createActiveRuleParameters(Context context) {
168     String tableName = "active_rule_parameters";
169     VarcharColumnDef activeRuleUuidColumnDef = newVarcharColumnBuilder("active_rule_uuid").setLimit(UUID_SIZE).setIsNullable(false).build();
170     VarcharColumnDef rulesParameterUuidColumnDef = newVarcharColumnBuilder("rules_parameter_uuid").setLimit(UUID_SIZE).setIsNullable(false).build();
171     context.execute(
172       newTableBuilder(tableName)
173         .addPkColumn(UUID_COL)
174         .addColumn(newVarcharColumnBuilder(VALUE_COL_NAME).setLimit(MAX_SIZE).build())
175         .addColumn(newVarcharColumnBuilder("rules_parameter_key").setLimit(128).build())
176         .addColumn(activeRuleUuidColumnDef)
177         .addColumn(rulesParameterUuidColumnDef)
178         .build());
179     addIndex(context, tableName, "arp_active_rule_uuid", false, activeRuleUuidColumnDef);
180   }
181
182   private void createActiveRules(Context context) {
183     VarcharColumnDef profileUuidCol = newVarcharColumnBuilder("profile_uuid").setLimit(UUID_SIZE).setIsNullable(false).build();
184     VarcharColumnDef ruleUuidCol = newVarcharColumnBuilder(RULE_UUID_COL_NAME).setLimit(UUID_SIZE).setIsNullable(false).build();
185     context.execute(
186       newTableBuilder("active_rules")
187         .addPkColumn(UUID_COL)
188         .addColumn(newIntegerColumnDefBuilder().setColumnName("failure_level").setIsNullable(false).build())
189         .addColumn(newVarcharColumnBuilder("inheritance").setLimit(10).build())
190         .addColumn(NULLABLE_TECHNICAL_CREATED_AT_COL)
191         .addColumn(NULLABLE_TECHNICAL_UPDATED_AT_COL)
192         .addColumn(profileUuidCol)
193         .addColumn(ruleUuidCol)
194         .build());
195     addIndex(context, "active_rules", "uniq_profile_rule_uuids", true, profileUuidCol, ruleUuidCol);
196   }
197
198   private void createAlmPats(Context context) {
199     String tableName = "alm_pats";
200     VarcharColumnDef patCol = newVarcharColumnBuilder("pat").setIsNullable(false).setLimit(2000).build();
201     VarcharColumnDef userUuidCol = newVarcharColumnBuilder(USER_UUID_COL_NAME).setIsNullable(false).setLimit(256).build();
202     VarcharColumnDef almSettingUuidCol = newVarcharColumnBuilder("alm_setting_uuid").setIsNullable(false).setLimit(UUID_SIZE).build();
203
204     context.execute(newTableBuilder(tableName)
205       .addPkColumn(UUID_COL)
206       .addColumn(patCol)
207       .addColumn(userUuidCol)
208       .addColumn(almSettingUuidCol)
209       .addColumn(TECHNICAL_UPDATED_AT_COL)
210       .addColumn(TECHNICAL_CREATED_AT_COL)
211       .build());
212     addIndex(context, tableName, "uniq_alm_pats", true, userUuidCol, almSettingUuidCol);
213   }
214
215   private void createAlmSettings(Context context) {
216     String tableName = "alm_settings";
217     VarcharColumnDef keeCol = newVarcharColumnBuilder("kee").setIsNullable(false).setLimit(200).build();
218
219     context.execute(newTableBuilder(tableName)
220       .addPkColumn(UUID_COL)
221       .addColumn(newVarcharColumnBuilder("alm_id").setIsNullable(false).setLimit(UUID_SIZE).build())
222       .addColumn(keeCol)
223       .addColumn(newVarcharColumnBuilder("url").setIsNullable(true).setLimit(2000).build())
224       .addColumn(newVarcharColumnBuilder("app_id").setIsNullable(true).setLimit(80).build())
225       .addColumn(newVarcharColumnBuilder("private_key").setIsNullable(true).setLimit(2000).build())
226       .addColumn(newVarcharColumnBuilder("pat").setIsNullable(true).setLimit(2000).build())
227       .addColumn(TECHNICAL_UPDATED_AT_COL)
228       .addColumn(TECHNICAL_CREATED_AT_COL)
229       .addColumn(newVarcharColumnBuilder("client_id").setIsNullable(true).setLimit(80).build())
230       .addColumn(newVarcharColumnBuilder("client_secret").setIsNullable(true).setLimit(80).build())
231       .build());
232     addIndex(context, tableName, "uniq_alm_settings", true, keeCol);
233   }
234
235   private void createProjectAlmSettings(Context context) {
236     String tableName = "project_alm_settings";
237     VarcharColumnDef almSettingUuidCol = newVarcharColumnBuilder("alm_setting_uuid").setIsNullable(false).setLimit(UUID_SIZE).build();
238     VarcharColumnDef projectUuidCol = newVarcharColumnBuilder(PROJECT_UUID_COL_NAME).setIsNullable(false).setLimit(UUID_VARCHAR_SIZE).build();
239     VarcharColumnDef almRepoCol = newVarcharColumnBuilder("alm_repo").setIsNullable(true).setLimit(256).build();
240     VarcharColumnDef almSlugCol = newVarcharColumnBuilder("alm_slug").setIsNullable(true).setLimit(256).build();
241     BooleanColumnDef summaryCommentEnabledCol = newBooleanColumnBuilder("summary_comment_enabled").setIsNullable(true).build();
242     BooleanColumnDef monorepoCol = newBooleanColumnBuilder("monorepo").setIsNullable(false).build();
243
244     context.execute(newTableBuilder(tableName)
245       .addPkColumn(UUID_COL)
246       .addColumn(almSettingUuidCol)
247       .addColumn(projectUuidCol)
248       .addColumn(almRepoCol)
249       .addColumn(almSlugCol)
250       .addColumn(TECHNICAL_UPDATED_AT_COL)
251       .addColumn(TECHNICAL_CREATED_AT_COL)
252       .addColumn(summaryCommentEnabledCol)
253       .addColumn(monorepoCol)
254       .build());
255     addIndex(context, tableName, "uniq_project_alm_settings", true, projectUuidCol);
256     addIndex(context, tableName, "project_alm_settings_alm", false, almSettingUuidCol);
257     addIndex(context, tableName, "project_alm_settings_slug", false, almSlugCol);
258   }
259
260   private void createAnalysisProperties(Context context) {
261     String tableName = "analysis_properties";
262     VarcharColumnDef snapshotUuidColumn = newVarcharColumnBuilder(ANALYSIS_UUID_COL_NAME)
263       .setIsNullable(false)
264       .setLimit(UUID_SIZE)
265       .build();
266     context.execute(
267       newTableBuilder(tableName)
268         .addPkColumn(UUID_COL)
269         .addColumn(snapshotUuidColumn)
270         .addColumn(newVarcharColumnBuilder("kee").setIsNullable(false).setLimit(512).build())
271         .addColumn(newVarcharColumnBuilder(TEXT_VALUE_COL_NAME).setIsNullable(true).setLimit(MAX_SIZE).build())
272         .addColumn(newClobColumnDefBuilder().setColumnName("clob_value").setIsNullable(true).build())
273         .addColumn(newBooleanColumnDefBuilder().setColumnName("is_empty").setIsNullable(false).build())
274         .addColumn(TECHNICAL_CREATED_AT_COL)
275         .build());
276     addIndex(context, tableName, "analysis_properties_analysis", false, snapshotUuidColumn);
277   }
278
279   private void createAppBranchProjectBranch(Context context) {
280     String tableName = "app_branch_project_branch";
281     VarcharColumnDef applicationBranchUuid = newVarcharColumnBuilder("application_branch_uuid").setIsNullable(false).setLimit(UUID_SIZE).build();
282     VarcharColumnDef projectBranchUuid = newVarcharColumnBuilder("project_branch_uuid").setIsNullable(false).setLimit(UUID_SIZE).build();
283     VarcharColumnDef applicationUuid = newVarcharColumnBuilder("application_uuid").setIsNullable(false).setLimit(UUID_SIZE).build();
284     VarcharColumnDef projectUuid = newVarcharColumnBuilder(PROJECT_UUID_COL_NAME).setIsNullable(false).setLimit(UUID_SIZE).build();
285     context.execute(
286       newTableBuilder(tableName)
287         .addPkColumn(UUID_COL)
288         .addColumn(applicationUuid)
289         .addColumn(applicationBranchUuid)
290         .addColumn(projectUuid)
291         .addColumn(projectBranchUuid)
292         .addColumn(TECHNICAL_CREATED_AT_COL)
293         .build());
294     addIndex(context, tableName, "uniq_app_branch_proj", true, applicationBranchUuid, projectBranchUuid);
295     addIndex(context, tableName, "idx_abpb_app_uuid", false, applicationUuid);
296     addIndex(context, tableName, "idx_abpb_app_branch_uuid", false, applicationBranchUuid);
297     addIndex(context, tableName, "idx_abpb_proj_uuid", false, projectUuid);
298     addIndex(context, tableName, "idx_abpb_proj_branch_uuid", false, projectBranchUuid);
299   }
300
301   private void createAppProjects(Context context) {
302     String tableName = "app_projects";
303     VarcharColumnDef applicationUuid = newVarcharColumnBuilder("application_uuid").setIsNullable(false).setLimit(UUID_SIZE).build();
304     VarcharColumnDef projectUuid = newVarcharColumnBuilder(PROJECT_UUID_COL_NAME).setIsNullable(false).setLimit(UUID_SIZE).build();
305     context.execute(
306       newTableBuilder(tableName)
307         .addPkColumn(UUID_COL)
308         .addColumn(applicationUuid)
309         .addColumn(projectUuid)
310         .addColumn(TECHNICAL_CREATED_AT_COL)
311         .build());
312
313     addIndex(context, tableName, "uniq_app_projects", true, applicationUuid, projectUuid);
314     addIndex(context, tableName, "idx_app_proj_application_uuid", false, applicationUuid);
315     addIndex(context, tableName, "idx_app_proj_project_uuid", false, projectUuid);
316   }
317
318   private void createCeActivity(Context context) {
319     String tableName = "ce_activity";
320     VarcharColumnDef uuidCol = UUID_COL;
321     VarcharColumnDef mainComponentUuidCol = newVarcharColumnBuilder("main_component_uuid").setLimit(UUID_SIZE).setIsNullable(true).build();
322     VarcharColumnDef componentUuidCol = newVarcharColumnBuilder(COMPONENT_UUID_COL_NAME).setLimit(UUID_SIZE).setIsNullable(true).build();
323     VarcharColumnDef statusCol = newVarcharColumnBuilder(STATUS_COL_NAME).setLimit(15).setIsNullable(false).build();
324     BooleanColumnDef mainIsLastCol = newBooleanColumnDefBuilder().setColumnName("main_is_last").setIsNullable(false).build();
325     VarcharColumnDef mainIsLastKeyCol = newVarcharColumnBuilder("main_is_last_key").setLimit(55).setIsNullable(false).build();
326     BooleanColumnDef isLastCol = newBooleanColumnDefBuilder().setColumnName("is_last").setIsNullable(false).build();
327     VarcharColumnDef isLastKeyCol = newVarcharColumnBuilder("is_last_key").setLimit(55).setIsNullable(false).build();
328     context.execute(
329       newTableBuilder(tableName)
330         .addPkColumn(uuidCol)
331         .addColumn(newVarcharColumnBuilder("task_type").setLimit(15).setIsNullable(false).build())
332         .addColumn(mainComponentUuidCol)
333         .addColumn(componentUuidCol)
334         .addColumn(statusCol)
335         .addColumn(mainIsLastCol)
336         .addColumn(mainIsLastKeyCol)
337         .addColumn(isLastCol)
338         .addColumn(isLastKeyCol)
339         .addColumn(newVarcharColumnBuilder("submitter_uuid").setLimit(USER_UUID_SIZE).setIsNullable(true).build())
340         .addColumn(newBigIntegerColumnDefBuilder().setColumnName("submitted_at").setIsNullable(false).build())
341         .addColumn(newBigIntegerColumnDefBuilder().setColumnName("started_at").setIsNullable(true).build())
342         .addColumn(newBigIntegerColumnDefBuilder().setColumnName("executed_at").setIsNullable(true).build())
343         .addColumn(newIntegerColumnDefBuilder().setColumnName("execution_count").setIsNullable(false).build())
344         .addColumn(newBigIntegerColumnDefBuilder().setColumnName("execution_time_ms").setIsNullable(true).build())
345         .addColumn(newVarcharColumnBuilder(ANALYSIS_UUID_COL_NAME).setLimit(OLD_UUID_VARCHAR_SIZE).setIsNullable(true).build())
346         .addColumn(newVarcharColumnBuilder("error_message").setLimit(1_000).setIsNullable(true).build())
347         .addColumn(newClobColumnDefBuilder().setColumnName("error_stacktrace").setIsNullable(true).build())
348         .addColumn(newVarcharColumnBuilder("error_type").setLimit(20).setIsNullable(true).build())
349         .addColumn(newVarcharColumnBuilder("worker_uuid").setLimit(UUID_SIZE).setIsNullable(true).build())
350         .addColumn(TECHNICAL_CREATED_AT_COL)
351         .addColumn(TECHNICAL_UPDATED_AT_COL)
352         .build());
353     addIndex(context, tableName, "ce_activity_component", false, componentUuidCol);
354     addIndex(context, tableName, "ce_activity_islast", false, isLastCol, statusCol);
355     addIndex(context, tableName, "ce_activity_islast_key", false, isLastKeyCol);
356     addIndex(context, tableName, "ce_activity_main_component", false, mainComponentUuidCol);
357     addIndex(context, tableName, "ce_activity_main_islast", false, mainIsLastCol, statusCol);
358     addIndex(context, tableName, "ce_activity_main_islast_key", false, mainIsLastKeyCol);
359     addIndex(context, tableName, "ce_activity_uuid", true, uuidCol);
360   }
361
362   private void createCeQueue(Context context) {
363     String tableName = "ce_queue";
364     VarcharColumnDef uuidCol = UUID_COL;
365     VarcharColumnDef mainComponentUuidCol = newVarcharColumnBuilder("main_component_uuid").setLimit(UUID_SIZE).setIsNullable(true).build();
366     VarcharColumnDef componentUuidCol = newVarcharColumnBuilder(COMPONENT_UUID_COL_NAME).setLimit(UUID_SIZE).setIsNullable(true).build();
367     context.execute(
368       newTableBuilder(tableName)
369         .addPkColumn(uuidCol)
370         .addColumn(newVarcharColumnBuilder("task_type").setLimit(15).setIsNullable(false).build())
371         .addColumn(mainComponentUuidCol)
372         .addColumn(componentUuidCol)
373         .addColumn(newVarcharColumnBuilder(STATUS_COL_NAME).setLimit(15).setIsNullable(true).build())
374         .addColumn(newVarcharColumnBuilder("submitter_uuid").setLimit(USER_UUID_SIZE).setIsNullable(true).build())
375         .addColumn(newBigIntegerColumnDefBuilder().setColumnName("started_at").setIsNullable(true).build())
376         .addColumn(newVarcharColumnBuilder("worker_uuid").setLimit(UUID_SIZE).setIsNullable(true).build())
377         .addColumn(newIntegerColumnDefBuilder().setColumnName("execution_count").setIsNullable(false).build())
378         .addColumn(TECHNICAL_CREATED_AT_COL)
379         .addColumn(TECHNICAL_UPDATED_AT_COL)
380         .build());
381     addIndex(context, tableName, "ce_queue_main_component", false, mainComponentUuidCol);
382     addIndex(context, tableName, "ce_queue_component", false, componentUuidCol);
383   }
384
385   private void createCeScannerContext(Context context) {
386     context.execute(
387       newTableBuilder("ce_scanner_context")
388         .addPkColumn(newVarcharColumnBuilder(TASK_UUID_COL_NAME).setLimit(UUID_SIZE).setIsNullable(false).build())
389         .addColumn(newBlobColumnDefBuilder().setColumnName("context_data").setIsNullable(false).build())
390         .addColumn(TECHNICAL_CREATED_AT_COL)
391         .addColumn(TECHNICAL_UPDATED_AT_COL)
392         .build());
393   }
394
395   private void createCeTaskCharacteristics(Context context) {
396     String tableName = "ce_task_characteristics";
397     VarcharColumnDef ceTaskUuidColumn = newVarcharColumnBuilder(TASK_UUID_COL_NAME)
398       .setLimit(UUID_SIZE)
399       .setIsNullable(false)
400       .build();
401
402     context.execute(
403       newTableBuilder(tableName)
404         .addPkColumn(UUID_COL)
405         .addColumn(ceTaskUuidColumn)
406         .addColumn(newVarcharColumnBuilder("kee").setLimit(512).setIsNullable(false).build())
407         .addColumn(newVarcharColumnBuilder(TEXT_VALUE_COL_NAME).setLimit(512).setIsNullable(true).build())
408         .build());
409     addIndex(context, tableName, "ce_characteristics_" + ceTaskUuidColumn.getName(), false, ceTaskUuidColumn);
410   }
411
412   private void createCeTaskInput(Context context) {
413     context.execute(
414       newTableBuilder("ce_task_input")
415         .addPkColumn(newVarcharColumnBuilder(TASK_UUID_COL_NAME).setLimit(UUID_SIZE).setIsNullable(false).build())
416         .addColumn(newBlobColumnDefBuilder().setColumnName("input_data").setIsNullable(true).build())
417         .addColumn(TECHNICAL_CREATED_AT_COL)
418         .addColumn(TECHNICAL_UPDATED_AT_COL)
419         .build());
420   }
421
422   private void createCeTaskMessage(Context context) {
423     String tableName = "ce_task_message";
424     VarcharColumnDef taskUuidCol = newVarcharColumnBuilder(TASK_UUID_COL_NAME).setIsNullable(false).setLimit(UUID_SIZE).build();
425     VarcharColumnDef messageTypeCol = newVarcharColumnBuilder("message_type").setIsNullable(false).setLimit(255).build();
426     context.execute(newTableBuilder(tableName)
427       .addPkColumn(UUID_COL)
428       .addColumn(taskUuidCol)
429       .addColumn(newVarcharColumnBuilder("message").setIsNullable(false).setLimit(MAX_SIZE).build())
430       .addColumn(TECHNICAL_CREATED_AT_COL)
431       .addColumn(messageTypeCol)
432       .build());
433     addIndex(context, tableName, tableName + "_task", false, taskUuidCol);
434     addIndex(context, tableName, "ctm_message_type", false, messageTypeCol);
435   }
436
437   private void createComponents(Context context) {
438     String tableName = "components";
439     VarcharColumnDef keeCol = newVarcharColumnBuilder("kee").setIsNullable(true).setLimit(400).build();
440     VarcharColumnDef moduleUuidCol = newVarcharColumnBuilder("module_uuid").setIsNullable(true).setLimit(50).build();
441     VarcharColumnDef projectUuidCol = newVarcharColumnBuilder(PROJECT_UUID_COL_NAME).setIsNullable(false).setLimit(50).build();
442     VarcharColumnDef qualifierCol = newVarcharColumnBuilder("qualifier").setIsNullable(true).setLimit(10).build();
443     VarcharColumnDef rootUuidCol = newVarcharColumnBuilder("root_uuid").setIsNullable(false).setLimit(50).build();
444     VarcharColumnDef uuidCol = newVarcharColumnBuilder("uuid").setIsNullable(false).setLimit(50).build();
445     VarcharColumnDef mainBranchProjectUuidCol = newVarcharColumnBuilder("main_branch_project_uuid").setIsNullable(true).setLimit(50).build();
446
447     context.execute(newTableBuilder(tableName)
448       .addColumn(uuidCol)
449       .addColumn(keeCol)
450       .addColumn(newVarcharColumnBuilder("deprecated_kee").setIsNullable(true).setLimit(400).build())
451       .addColumn(newVarcharColumnBuilder("name").setIsNullable(true).setLimit(2000).build())
452       .addColumn(newVarcharColumnBuilder("long_name").setIsNullable(true).setLimit(2000).build())
453       .addColumn(newVarcharColumnBuilder(DESCRIPTION_COL_NAME).setIsNullable(true).setLimit(2000).build())
454       .addColumn(newBooleanColumnDefBuilder().setColumnName("enabled").setIsNullable(false).setDefaultValue(true).build())
455       .addColumn(newVarcharColumnBuilder("scope").setIsNullable(true).setLimit(3).build())
456       .addColumn(qualifierCol)
457       .addColumn(newBooleanColumnDefBuilder().setColumnName("private").setIsNullable(false).build())
458       .addColumn(rootUuidCol)
459       .addColumn(newVarcharColumnBuilder(LANGUAGE_COL_NAME).setIsNullable(true).setLimit(20).build())
460       .addColumn(newVarcharColumnBuilder("copy_component_uuid").setIsNullable(true).setLimit(50).build())
461       .addColumn(newVarcharColumnBuilder("path").setIsNullable(true).setLimit(2000).build())
462       .addColumn(newVarcharColumnBuilder("uuid_path").setIsNullable(false).setLimit(1500).build())
463       .addColumn(projectUuidCol)
464       .addColumn(moduleUuidCol)
465       .addColumn(newVarcharColumnBuilder("module_uuid_path").setIsNullable(true).setLimit(1500).build())
466       .addColumn(mainBranchProjectUuidCol)
467       .addColumn(newBooleanColumnDefBuilder().setColumnName("b_changed").setIsNullable(true).build())
468       .addColumn(newVarcharColumnBuilder("b_name").setIsNullable(true).setLimit(500).build())
469       .addColumn(newVarcharColumnBuilder("b_long_name").setIsNullable(true).setLimit(500).build())
470       .addColumn(newVarcharColumnBuilder("b_description").setIsNullable(true).setLimit(2000).build())
471       .addColumn(newBooleanColumnDefBuilder().setColumnName("b_enabled").setIsNullable(true).build())
472       .addColumn(newVarcharColumnBuilder("b_qualifier").setIsNullable(true).setLimit(10).build())
473       .addColumn(newVarcharColumnBuilder("b_language").setIsNullable(true).setLimit(20).build())
474       .addColumn(newVarcharColumnBuilder("b_copy_component_uuid").setIsNullable(true).setLimit(50).build())
475       .addColumn(newVarcharColumnBuilder("b_path").setIsNullable(true).setLimit(2000).build())
476       .addColumn(newVarcharColumnBuilder("b_uuid_path").setIsNullable(true).setLimit(1500).build())
477       .addColumn(newVarcharColumnBuilder("b_module_uuid").setIsNullable(true).setLimit(50).build())
478       .addColumn(newVarcharColumnBuilder("b_module_uuid_path").setIsNullable(true).setLimit(1500).build())
479       .addColumn(newTimestampColumnDefBuilder().setColumnName(CREATED_AT_COL_NAME).setIsNullable(true).build())
480       .build());
481
482     addIndex(context, tableName, "projects_kee", true, keeCol);
483     addIndex(context, tableName, "projects_module_uuid", false, moduleUuidCol);
484     addIndex(context, tableName, "projects_project_uuid", false, projectUuidCol);
485     addIndex(context, tableName, "projects_qualifier", false, qualifierCol);
486     addIndex(context, tableName, "projects_root_uuid", false, rootUuidCol);
487     addIndex(context, tableName, "projects_uuid", false, uuidCol);
488     addIndex(context, tableName, "idx_main_branch_prj_uuid", false, mainBranchProjectUuidCol);
489   }
490
491   private void createDefaultQProfiles(Context context) {
492     String tableName = "default_qprofiles";
493     VarcharColumnDef profileUuidColumn = newVarcharColumnBuilder(QPROFILE_UUID_COL_NAME)
494       .setLimit(255)
495       .setIsNullable(false)
496       .build();
497
498     context.execute(
499       newTableBuilder(tableName)
500         .addPkColumn(newVarcharColumnBuilder(LANGUAGE_COL_NAME).setLimit(20).setIsNullable(false).build())
501         .addColumn(profileUuidColumn)
502         .addColumn(TECHNICAL_CREATED_AT_COL)
503         .addColumn(TECHNICAL_UPDATED_AT_COL)
504         .build());
505     addIndex(context, tableName, "uniq_default_qprofiles_uuid", true, profileUuidColumn);
506   }
507
508   private void createDeprecatedRuleKeys(Context context) {
509     String tableName = "deprecated_rule_keys";
510     VarcharColumnDef ruleUuidCol = newVarcharColumnBuilder(RULE_UUID_COL_NAME).setIsNullable(false).setLimit(UUID_SIZE).build();
511     VarcharColumnDef oldRepositoryKeyCol = newVarcharColumnBuilder("old_repository_key").setIsNullable(false).setLimit(255).build();
512     VarcharColumnDef oldRuleKeyCol = newVarcharColumnBuilder("old_rule_key").setIsNullable(false).setLimit(200).build();
513     context.execute(newTableBuilder(tableName)
514       .addPkColumn(UUID_COL)
515       .addColumn(oldRepositoryKeyCol)
516       .addColumn(oldRuleKeyCol)
517       .addColumn(TECHNICAL_CREATED_AT_COL)
518       .addColumn(ruleUuidCol)
519       .build());
520     addIndex(context, tableName, "uniq_deprecated_rule_keys", true, oldRepositoryKeyCol, oldRuleKeyCol);
521     addIndex(context, tableName, "rule_uuid_deprecated_rule_keys", false, ruleUuidCol);
522   }
523
524   private void createDuplicationsIndex(Context context) {
525     String tableName = "duplications_index";
526     VarcharColumnDef hashCol = newVarcharColumnBuilder("hash").setLimit(50).setIsNullable(false).build();
527     VarcharColumnDef analysisUuidCol = newVarcharColumnBuilder(ANALYSIS_UUID_COL_NAME).setLimit(OLD_UUID_VARCHAR_SIZE).setIsNullable(false).build();
528     VarcharColumnDef componentUuidCol = newVarcharColumnBuilder(COMPONENT_UUID_COL_NAME).setLimit(OLD_UUID_VARCHAR_SIZE).setIsNullable(false).build();
529     context.execute(
530       newTableBuilder(tableName)
531         .addPkColumn(UUID_COL)
532         .addColumn(analysisUuidCol)
533         .addColumn(componentUuidCol)
534         .addColumn(hashCol)
535         .addColumn(newIntegerColumnDefBuilder().setColumnName("index_in_file").setIsNullable(false).build())
536         .addColumn(newIntegerColumnDefBuilder().setColumnName("start_line").setIsNullable(false).build())
537         .addColumn(newIntegerColumnDefBuilder().setColumnName("end_line").setIsNullable(false).build())
538         .build());
539
540     addIndex(context, tableName, "duplications_index_hash", false, hashCol);
541     addIndex(context, tableName, "duplication_analysis_component", false, analysisUuidCol, componentUuidCol);
542   }
543
544   private void createEsQueue(Context context) {
545     String tableName = "es_queue";
546     BigIntegerColumnDef createdAtCol = TECHNICAL_CREATED_AT_COL;
547     context.execute(
548       newTableBuilder(tableName)
549         .addPkColumn(UUID_COL)
550         .addColumn(newVarcharColumnBuilder("doc_type").setIsNullable(false).setLimit(40).build())
551         .addColumn(newVarcharColumnBuilder("doc_id").setIsNullable(false).setLimit(MAX_SIZE).build())
552         .addColumn(newVarcharColumnBuilder("doc_id_type").setIsNullable(true).setLimit(20).build())
553         .addColumn(newVarcharColumnBuilder("doc_routing").setIsNullable(true).setLimit(MAX_SIZE).build())
554         .addColumn(createdAtCol)
555         .build());
556     addIndex(context, tableName, "es_queue_created_at", false, createdAtCol);
557   }
558
559   private void createEventComponentChanges(Context context) {
560     String tableName = "event_component_changes";
561     VarcharColumnDef eventUuidCol = newVarcharColumnBuilder("event_uuid").setIsNullable(false).setLimit(UUID_SIZE).build();
562     VarcharColumnDef eventComponentUuidCol = newVarcharColumnBuilder("event_component_uuid").setIsNullable(false).setLimit(OLD_UUID_VARCHAR_SIZE).build();
563     VarcharColumnDef eventAnalysisUuidCol = newVarcharColumnBuilder("event_analysis_uuid").setIsNullable(false).setLimit(OLD_UUID_VARCHAR_SIZE).build();
564     VarcharColumnDef changeCategoryCol = newVarcharColumnBuilder("change_category").setIsNullable(false).setLimit(12).build();
565     VarcharColumnDef componentUuidCol = newVarcharColumnBuilder(COMPONENT_UUID_COL_NAME).setIsNullable(false).setLimit(OLD_UUID_VARCHAR_SIZE).build();
566     context.execute(newTableBuilder(tableName)
567       .addPkColumn(UUID_COL)
568       .addColumn(eventUuidCol)
569       .addColumn(eventComponentUuidCol)
570       .addColumn(eventAnalysisUuidCol)
571       .addColumn(changeCategoryCol)
572       .addColumn(componentUuidCol)
573       .addColumn(newVarcharColumnBuilder("component_key").setIsNullable(false).setLimit(400).build())
574       .addColumn(newVarcharColumnBuilder("component_name").setIsNullable(false).setLimit(2000).build())
575       .addColumn(newVarcharColumnBuilder("component_branch_key").setIsNullable(true).setLimit(255).build())
576       .addColumn(TECHNICAL_CREATED_AT_COL)
577       .build());
578     addIndex(context, tableName, tableName + UNIQUE_INDEX_SUFFIX, true, eventUuidCol, changeCategoryCol, componentUuidCol);
579     addIndex(context, tableName, "event_cpnt_changes_cpnt", false, eventComponentUuidCol);
580     addIndex(context, tableName, "event_cpnt_changes_analysis", false, eventAnalysisUuidCol);
581   }
582
583   private void createEvents(Context context) {
584     String tableName = "events";
585     VarcharColumnDef uuidCol = UUID_COL;
586     VarcharColumnDef analysisUuidCol = newVarcharColumnBuilder(ANALYSIS_UUID_COL_NAME).setLimit(OLD_UUID_VARCHAR_SIZE).setIsNullable(false).build();
587     VarcharColumnDef componentUuid = newVarcharColumnBuilder(COMPONENT_UUID_COL_NAME).setLimit(OLD_UUID_VARCHAR_SIZE).setIsNullable(false).build();
588     context.execute(
589       newTableBuilder(tableName)
590         .addPkColumn(uuidCol)
591         .addColumn(analysisUuidCol)
592         .addColumn(newVarcharColumnBuilder("name").setLimit(400).setIsNullable(true).build())
593         .addColumn(newVarcharColumnBuilder("category").setLimit(50).build())
594         .addColumn(newVarcharColumnBuilder(DESCRIPTION_COL_NAME).setLimit(MAX_SIZE).build())
595         .addColumn(newVarcharColumnBuilder("event_data").setLimit(MAX_SIZE).build())
596         .addColumn(newBigIntegerColumnDefBuilder().setColumnName("event_date").setIsNullable(false).build())
597         .addColumn(TECHNICAL_CREATED_AT_COL)
598         .addColumn(componentUuid)
599         .build());
600     addIndex(context, tableName, "events_uuid", true, uuidCol);
601     addIndex(context, tableName, "events_analysis", false, analysisUuidCol);
602     addIndex(context, tableName, "events_component_uuid", false, componentUuid);
603   }
604
605   private void createFileSources(Context context) {
606     String tableName = "file_sources";
607     VarcharColumnDef projectUuidCol = newVarcharColumnBuilder(PROJECT_UUID_COL_NAME).setLimit(OLD_UUID_VARCHAR_SIZE).setIsNullable(false).build();
608     BigIntegerColumnDef updatedAtCol = TECHNICAL_UPDATED_AT_COL;
609     VarcharColumnDef fileUuidCol = newVarcharColumnBuilder("file_uuid").setLimit(OLD_UUID_VARCHAR_SIZE).setIsNullable(false).build();
610     context.execute(
611       newTableBuilder(tableName)
612         .addPkColumn(UUID_COL)
613         .addColumn(projectUuidCol)
614         .addColumn(fileUuidCol)
615         .addColumn(newClobColumnDefBuilder().setColumnName("line_hashes").setIsNullable(true).build())
616         .addColumn(newIntegerColumnDefBuilder().setColumnName("line_hashes_version").setIsNullable(true).build())
617         .addColumn(newVarcharColumnBuilder("data_hash").setLimit(50).setIsNullable(true).build())
618         .addColumn(newVarcharColumnBuilder("src_hash").setLimit(50).setIsNullable(true).build())
619         .addColumn(newVarcharColumnBuilder("revision").setLimit(100).setIsNullable(true).build())
620         .addColumn(newIntegerColumnDefBuilder().setColumnName("line_count").setIsNullable(false).build())
621         .addColumn(newBlobColumnDefBuilder().setColumnName("binary_data").setIsNullable(true).build())
622         .addColumn(TECHNICAL_CREATED_AT_COL)
623         .addColumn(updatedAtCol)
624         .build());
625     addIndex(context, tableName, "file_sources_file_uuid", true, fileUuidCol);
626     addIndex(context, tableName, "file_sources_project_uuid", false, projectUuidCol);
627     addIndex(context, tableName, "file_sources_updated_at", false, updatedAtCol);
628   }
629
630   private void createGroupRoles(Context context) {
631     String tableName = "group_roles";
632     VarcharColumnDef roleCol = newVarcharColumnBuilder("role").setLimit(64).setIsNullable(false).build();
633     VarcharColumnDef componentUuidCol = newVarcharColumnBuilder(COMPONENT_UUID_COL_NAME).setIsNullable(true).setLimit(UUID_SIZE).build();
634     VarcharColumnDef groupUuidCol = newVarcharColumnBuilder(GROUP_UUID_COL_NAME).setIsNullable(true).setLimit(UUID_SIZE).build();
635     context.execute(
636       newTableBuilder(tableName)
637         .addPkColumn(UUID_COL)
638         .addColumn(roleCol)
639         .addColumn(componentUuidCol)
640         .addColumn(groupUuidCol)
641         .build());
642     addIndex(context, tableName, "group_roles_component_uuid", false, componentUuidCol);
643     addIndex(context, tableName, "uniq_group_roles", true, groupUuidCol, componentUuidCol, roleCol);
644   }
645
646   private void createGroups(Context context) {
647     String tableName = "groups";
648     VarcharColumnDef nameCol = newVarcharColumnBuilder("name").setLimit(500).setIsNullable(false).build();
649     context.execute(
650       newTableBuilder(tableName)
651         .addPkColumn(UUID_COL)
652         .addColumn(nameCol)
653         .addColumn(newVarcharColumnBuilder(DESCRIPTION_COL_NAME).setLimit(200).setIsNullable(true).build())
654         .addColumn(DEPRECATED_TECHNICAL_CREATED_AT_COL)
655         .addColumn(DEPRECATED_TECHNICAL_UPDATED_AT_COL)
656         .build());
657     addIndex(context, tableName, "uniq_groups_name", true, nameCol);
658   }
659
660   private void createGroupsUsers(Context context) {
661     String tableName = "groups_users";
662     VarcharColumnDef groupUuidCol = newVarcharColumnBuilder(GROUP_UUID_COL_NAME).setLimit(40).setIsNullable(false).build();
663     VarcharColumnDef userUuidCol = newVarcharColumnBuilder(USER_UUID_COL_NAME).setLimit(255).setIsNullable(false).build();
664     context.execute(
665       newTableBuilder(tableName)
666         .addColumn(groupUuidCol)
667         .addColumn(userUuidCol)
668         .build());
669     addIndex(context, tableName, "index_groups_users_group_uuid", false, groupUuidCol);
670     addIndex(context, tableName, "index_groups_users_user_uuid", false, userUuidCol);
671     addIndex(context, tableName, "groups_users_unique", true, userUuidCol, groupUuidCol);
672   }
673
674   private void createInternalComponentProps(Context context) {
675     String tableName = "internal_component_props";
676     VarcharColumnDef componentUuidCol = newVarcharColumnBuilder(COMPONENT_UUID_COL_NAME).setIsNullable(false).setLimit(OLD_UUID_VARCHAR_SIZE).build();
677     VarcharColumnDef keeCol = newVarcharColumnBuilder("kee").setIsNullable(false).setLimit(512).build();
678     context.execute(newTableBuilder(tableName)
679       .addPkColumn(UUID_COL)
680       .addColumn(componentUuidCol)
681       .addColumn(keeCol)
682       .addColumn(newVarcharColumnBuilder(VALUE_COL_NAME).setIsNullable(true).setLimit(MAX_SIZE).build())
683       .addColumn(TECHNICAL_UPDATED_AT_COL)
684       .addColumn(TECHNICAL_CREATED_AT_COL)
685       .build());
686     addIndex(context, tableName, "unique_component_uuid_kee", true, componentUuidCol, keeCol);
687   }
688
689   private void createInternalProperties(Context context) {
690     context.execute(
691       newTableBuilder("internal_properties")
692         .addPkColumn(newVarcharColumnBuilder("kee").setLimit(20).setIsNullable(false).build())
693         .addColumn(newBooleanColumnDefBuilder().setColumnName("is_empty").setIsNullable(false).build())
694         .addColumn(newVarcharColumnDefBuilder().setColumnName(TEXT_VALUE_COL_NAME).setLimit(MAX_SIZE).setIgnoreOracleUnit(true).build())
695         .addColumn(newClobColumnDefBuilder().setColumnName("clob_value").setIsNullable(true).build())
696         .addColumn(TECHNICAL_CREATED_AT_COL)
697         .build());
698   }
699
700   private void createIssueChanges(Context context) {
701     String tableName = "issue_changes";
702     VarcharColumnDef issueKeyCol = newVarcharColumnBuilder("issue_key").setLimit(50).setIsNullable(false).build();
703     VarcharColumnDef keeCol = newVarcharColumnBuilder("kee").setLimit(50).build();
704     VarcharColumnDef projectUuidCol = newVarcharColumnBuilder(PROJECT_UUID_COL_NAME).setLimit(50).setIsNullable(false).build();
705     context.execute(
706       newTableBuilder(tableName)
707         .addPkColumn(UUID_COL)
708         .addColumn(keeCol)
709         .addColumn(issueKeyCol)
710         .addColumn(newVarcharColumnBuilder("user_login").setLimit(USER_UUID_SIZE).build())
711         .addColumn(newVarcharColumnBuilder("change_type").setLimit(20).build())
712         .addColumn(newClobColumnDefBuilder().setColumnName("change_data").build())
713         .addColumn(NULLABLE_TECHNICAL_CREATED_AT_COL)
714         .addColumn(NULLABLE_TECHNICAL_UPDATED_AT_COL)
715         .addColumn(newBigIntegerColumnDefBuilder().setColumnName("issue_change_creation_date").build())
716         .addColumn(projectUuidCol)
717         .build());
718     addIndex(context, tableName, "issue_changes_issue_key", false, issueKeyCol);
719     addIndex(context, tableName, "issue_changes_kee", false, keeCol);
720     addIndex(context, tableName, "issue_changes_project_uuid", false, projectUuidCol);
721   }
722
723   private void createIssues(Context context) {
724     var tableName = "issues";
725     VarcharColumnDef assigneeCol = newVarcharColumnBuilder("assignee").setLimit(USER_UUID_SIZE).build();
726     VarcharColumnDef componentUuidCol = newVarcharColumnBuilder(COMPONENT_UUID_COL_NAME).setLimit(50).build();
727     BigIntegerColumnDef issueCreationDateCol = newBigIntegerColumnDefBuilder().setColumnName("issue_creation_date").build();
728     VarcharColumnDef keeCol = newVarcharColumnBuilder("kee").setLimit(50).setIsNullable(false).build();
729     VarcharColumnDef projectUuidCol = newVarcharColumnBuilder(PROJECT_UUID_COL_NAME).setLimit(50).build();
730     VarcharColumnDef resolutionCol = newVarcharColumnBuilder("resolution").setLimit(20).build();
731     VarcharColumnDef ruleUuidCol = newVarcharColumnBuilder(RULE_UUID_COL_NAME).setLimit(40).setIsNullable(true).build();
732     BigIntegerColumnDef updatedAtCol = NULLABLE_TECHNICAL_UPDATED_AT_COL;
733     context.execute(
734       newTableBuilder(tableName)
735         .addPkColumn(keeCol)
736         .addColumn(ruleUuidCol)
737         .addColumn(newVarcharColumnBuilder("severity").setLimit(10).build())
738         .addColumn(newBooleanColumnDefBuilder().setColumnName("manual_severity").setIsNullable(false).build())
739         // unit has been fixed in SonarQube 5.6 (see migration 1151, SONAR-7493)
740         .addColumn(newVarcharColumnBuilder("message").setLimit(MAX_SIZE).build())
741         .addColumn(newIntegerColumnDefBuilder().setColumnName("line").build())
742         .addColumn(newDecimalColumnDefBuilder().setColumnName("gap").setPrecision(30).setScale(20).build())
743         .addColumn(newVarcharColumnBuilder(STATUS_COL_NAME).setLimit(20).build())
744         .addColumn(resolutionCol)
745         .addColumn(newVarcharColumnBuilder("checksum").setLimit(1000).build())
746         .addColumn(newVarcharColumnBuilder("reporter").setLimit(USER_UUID_SIZE).build())
747         .addColumn(assigneeCol)
748         .addColumn(newVarcharColumnBuilder("author_login").setLimit(255).build())
749         .addColumn(newVarcharColumnBuilder("action_plan_key").setLimit(50).build())
750         .addColumn(newVarcharColumnBuilder("issue_attributes").setLimit(MAX_SIZE).build())
751         .addColumn(newIntegerColumnDefBuilder().setColumnName("effort").build())
752         .addColumn(NULLABLE_TECHNICAL_CREATED_AT_COL)
753         .addColumn(updatedAtCol)
754         .addColumn(issueCreationDateCol)
755         .addColumn(newBigIntegerColumnDefBuilder().setColumnName("issue_update_date").build())
756         .addColumn(newBigIntegerColumnDefBuilder().setColumnName("issue_close_date").build())
757         .addColumn(newVarcharColumnBuilder("tags").setLimit(MAX_SIZE).build())
758         .addColumn(componentUuidCol)
759         .addColumn(projectUuidCol)
760         .addColumn(newBlobColumnDefBuilder().setColumnName("locations").build())
761         .addColumn(new TinyIntColumnDef.Builder().setColumnName("issue_type").build())
762         .addColumn(newBooleanColumnDefBuilder().setColumnName("from_hotspot").setIsNullable(true).build())
763         .build());
764
765     addIndex(context, tableName, "issues_assignee", false, assigneeCol);
766     addIndex(context, tableName, "issues_component_uuid", false, componentUuidCol);
767     addIndex(context, tableName, "issues_creation_date", false, issueCreationDateCol);
768     addIndex(context, tableName, "issues_kee", true, keeCol);
769     addIndex(context, tableName, "issues_project_uuid", false, projectUuidCol);
770     addIndex(context, tableName, "issues_resolution", false, resolutionCol);
771     addIndex(context, tableName, "issues_updated_at", false, updatedAtCol);
772     addIndex(context, tableName, "issues_rule_uuid", false, ruleUuidCol);
773   }
774
775   private void createLiveMeasures(Context context) {
776     String tableName = "live_measures";
777     VarcharColumnDef projectUuidCol = newVarcharColumnBuilder(PROJECT_UUID_COL_NAME).setIsNullable(false).setLimit(OLD_UUID_VARCHAR_SIZE).build();
778     VarcharColumnDef componentUuidCol = newVarcharColumnBuilder(COMPONENT_UUID_COL_NAME).setIsNullable(false).setLimit(OLD_UUID_VARCHAR_SIZE).build();
779     VarcharColumnDef metricUuidCol = newVarcharColumnBuilder(METRIC_UUID_COL_NAME).setLimit(UUID_SIZE).setIsNullable(false).build();
780     context.execute(newTableBuilder(tableName)
781       .addPkColumn(UUID_COL)
782       .addColumn(projectUuidCol)
783       .addColumn(componentUuidCol)
784       .addColumn(metricUuidCol)
785       .addColumn(newDecimalColumnDefBuilder().setColumnName(VALUE_COL_NAME).setPrecision(38).setScale(20).build())
786       .addColumn(newVarcharColumnBuilder(TEXT_VALUE_COL_NAME).setIsNullable(true).setLimit(MAX_SIZE).build())
787       .addColumn(newDecimalColumnDefBuilder().setColumnName("variation").setPrecision(38).setScale(20).build())
788       .addColumn(newBlobColumnDefBuilder().setColumnName("measure_data").setIsNullable(true).build())
789       .addColumn(newVarcharColumnBuilder("update_marker").setIsNullable(true).setLimit(UUID_SIZE).build())
790       .addColumn(TECHNICAL_CREATED_AT_COL)
791       .addColumn(TECHNICAL_UPDATED_AT_COL)
792       .build());
793     addIndex(context, tableName, "live_measures_project", false, projectUuidCol);
794     addIndex(context, tableName, "live_measures_component", true, componentUuidCol, metricUuidCol);
795   }
796
797   private void createManualMeasures(Context context) {
798     String tableName = "manual_measures";
799     VarcharColumnDef componentUuidCol = newVarcharColumnBuilder(COMPONENT_UUID_COL_NAME).setLimit(OLD_UUID_VARCHAR_SIZE).setIsNullable(false).build();
800     context.execute(
801       newTableBuilder(tableName)
802         .addPkColumn(UUID_COL)
803         .addColumn(newDecimalColumnDefBuilder().setColumnName(VALUE_COL_NAME).setPrecision(38).setScale(20).build())
804         .addColumn(newVarcharColumnBuilder(TEXT_VALUE_COL_NAME).setLimit(MAX_SIZE).build())
805         .addColumn(newVarcharColumnBuilder(USER_UUID_COL_NAME).setLimit(USER_UUID_SIZE).build())
806         .addColumn(newVarcharColumnBuilder(DESCRIPTION_COL_NAME).setLimit(MAX_SIZE).build())
807         .addColumn(NULLABLE_TECHNICAL_CREATED_AT_COL)
808         .addColumn(NULLABLE_TECHNICAL_UPDATED_AT_COL)
809         .addColumn(componentUuidCol)
810         .addColumn(newVarcharColumnBuilder(METRIC_UUID_COL_NAME).setLimit(UUID_SIZE).setIsNullable(false).build())
811         .build());
812     addIndex(context, tableName, "manual_measures_component_uuid", false, componentUuidCol);
813   }
814
815   private void createMetrics(Context context) {
816     String tableName = "metrics";
817     VarcharColumnDef nameCol = newVarcharColumnBuilder("name").setLimit(64).setIsNullable(false).build();
818     context.execute(
819       newTableBuilder(tableName)
820         .addPkColumn(UUID_COL)
821         .addColumn(nameCol)
822         .addColumn(newVarcharColumnBuilder(DESCRIPTION_COL_NAME).setLimit(255).build())
823         .addColumn(newIntegerColumnDefBuilder().setColumnName("direction").setIsNullable(false).setDefaultValue(0).build())
824         .addColumn(newVarcharColumnBuilder("domain").setLimit(64).build())
825         .addColumn(newVarcharColumnBuilder("short_name").setLimit(64).build())
826         .addColumn(newBooleanColumnDefBuilder().setColumnName("qualitative").setDefaultValue(false).setIsNullable(false).build())
827         .addColumn(newVarcharColumnBuilder("val_type").setLimit(8).build())
828         .addColumn(newBooleanColumnDefBuilder().setColumnName("user_managed").setDefaultValue(false).build())
829         .addColumn(newBooleanColumnDefBuilder().setColumnName("enabled").setDefaultValue(true).build())
830         .addColumn(newDecimalColumnDefBuilder().setColumnName("worst_value").setPrecision(38).setScale(20).build())
831         .addColumn(newDecimalColumnDefBuilder().setColumnName("best_value").setPrecision(38).setScale(20).build())
832         .addColumn(newBooleanColumnDefBuilder().setColumnName("optimized_best_value").build())
833         .addColumn(newBooleanColumnDefBuilder().setColumnName("hidden").build())
834         .addColumn(newBooleanColumnDefBuilder().setColumnName("delete_historical_data").build())
835         .addColumn(newIntegerColumnDefBuilder().setColumnName("decimal_scale").build())
836         .build());
837     addIndex(context, tableName, "metrics_unique_name", true, nameCol);
838   }
839
840   private void createNewCodePeriods(Context context) {
841     String tableName = "new_code_periods";
842     VarcharColumnDef projectUuidCol = newVarcharColumnBuilder(PROJECT_UUID_COL_NAME).setLimit(UUID_SIZE).setIsNullable(true).build();
843     VarcharColumnDef branchUuidCol = newVarcharColumnBuilder("branch_uuid").setLimit(UUID_SIZE).setIsNullable(true).build();
844     VarcharColumnDef typeCol = newVarcharColumnBuilder("type").setLimit(30).setIsNullable(false).build();
845     VarcharColumnDef valueCol = newVarcharColumnBuilder(VALUE_COL_NAME).setLimit(255).setIsNullable(true).build();
846     context.execute(
847       newTableBuilder(tableName)
848         .addPkColumn(UUID_COL)
849         .addColumn(projectUuidCol)
850         .addColumn(branchUuidCol)
851         .addColumn(typeCol)
852         .addColumn(valueCol)
853         .addColumn(TECHNICAL_UPDATED_AT_COL)
854         .addColumn(TECHNICAL_CREATED_AT_COL)
855         .build());
856
857     addIndex(context, tableName, "uniq_new_code_periods", true, projectUuidCol, branchUuidCol);
858     addIndex(context, tableName, "idx_ncp_type", false, typeCol);
859     addIndex(context, tableName, "idx_ncp_value", false, valueCol);
860   }
861
862   private void createNotifications(Context context) {
863     context.execute(
864       newTableBuilder("notifications")
865         .addPkColumn(UUID_COL)
866         .addColumn(newBlobColumnDefBuilder().setColumnName("data").build())
867         .addColumn(TECHNICAL_CREATED_AT_COL)
868         .build());
869   }
870
871   private void createOrgQProfiles(Context context) {
872     String tableName = "org_qprofiles";
873     int profileUuidSize = 255;
874     VarcharColumnDef rulesProfileUuidCol = newVarcharColumnBuilder("rules_profile_uuid").setLimit(profileUuidSize).setIsNullable(false).build();
875     VarcharColumnDef parentUuidCol = newVarcharColumnBuilder("parent_uuid").setLimit(profileUuidSize).setIsNullable(true).build();
876     context.execute(
877       newTableBuilder(tableName)
878         .addPkColumn(newVarcharColumnDefBuilder().setColumnName("uuid").setIsNullable(false).setLimit(255).build())
879         .addColumn(rulesProfileUuidCol)
880         .addColumn(parentUuidCol)
881         .addColumn(newBigIntegerColumnDefBuilder().setColumnName("last_used").setIsNullable(true).build())
882         .addColumn(newBigIntegerColumnDefBuilder().setColumnName("user_updated_at").setIsNullable(true).build())
883         .addColumn(TECHNICAL_CREATED_AT_COL)
884         .addColumn(TECHNICAL_UPDATED_AT_COL)
885         .build());
886     addIndex(context, tableName, "qprofiles_rp_uuid", false, rulesProfileUuidCol);
887     addIndex(context, tableName, "org_qprofiles_parent_uuid", false, parentUuidCol);
888   }
889
890   private void createPermTemplatesGroups(Context context) {
891     context.execute(
892       newTableBuilder("perm_templates_groups")
893         .addPkColumn(UUID_COL)
894         .addColumn(newVarcharColumnBuilder("permission_reference").setLimit(64).setIsNullable(false).build())
895         .addColumn(DEPRECATED_TECHNICAL_CREATED_AT_COL)
896         .addColumn(DEPRECATED_TECHNICAL_UPDATED_AT_COL)
897         .addColumn(newVarcharColumnBuilder(TEMPLATE_UUID_COL_NAME).setLimit(UUID_SIZE).setIsNullable(false).build())
898         .addColumn(newVarcharColumnBuilder(GROUP_UUID_COL_NAME).setLimit(UUID_SIZE).setIsNullable(true).build())
899         .build());
900   }
901
902   private void createPermTemplatesUsers(Context context) {
903     context.execute(
904       newTableBuilder("perm_templates_users")
905         .addPkColumn(UUID_COL)
906         .addColumn(newVarcharColumnBuilder("permission_reference").setLimit(64).setIsNullable(false).build())
907         .addColumn(DEPRECATED_TECHNICAL_CREATED_AT_COL)
908         .addColumn(DEPRECATED_TECHNICAL_UPDATED_AT_COL)
909         .addColumn(newVarcharColumnBuilder(TEMPLATE_UUID_COL_NAME).setLimit(UUID_SIZE).setIsNullable(false).build())
910         .addColumn(newVarcharColumnBuilder(USER_UUID_COL_NAME).setLimit(USER_UUID_SIZE).setIsNullable(false).build())
911         .build());
912   }
913
914   private void createPermTemplatesCharacteristics(Context context) {
915     String tableName = "perm_tpl_characteristics";
916     VarcharColumnDef permissionKeyColumn = newVarcharColumnBuilder("permission_key").setLimit(64).setIsNullable(false).build();
917     VarcharColumnDef templateUuidColumn = newVarcharColumnBuilder(TEMPLATE_UUID_COL_NAME).setLimit(UUID_SIZE).setIsNullable(false).build();
918     context.execute(
919       newTableBuilder(tableName)
920         .addPkColumn(UUID_COL)
921         .addColumn(permissionKeyColumn)
922         .addColumn(newBooleanColumnDefBuilder().setColumnName("with_project_creator").setIsNullable(false).setDefaultValue(false).build())
923         .addColumn(TECHNICAL_CREATED_AT_COL)
924         .addColumn(TECHNICAL_UPDATED_AT_COL)
925         .addColumn(templateUuidColumn)
926         .build());
927
928     addIndex(context, tableName, "uniq_perm_tpl_charac", true, templateUuidColumn, permissionKeyColumn);
929   }
930
931   private void createPermissionTemplates(Context context) {
932     context.execute(
933       newTableBuilder("permission_templates")
934         .addPkColumn(UUID_COL)
935         .addColumn(newVarcharColumnBuilder("name").setLimit(100).setIsNullable(false).build())
936         .addColumn(newVarcharColumnBuilder(DESCRIPTION_COL_NAME).setLimit(MAX_SIZE).build())
937         .addColumn(DEPRECATED_TECHNICAL_CREATED_AT_COL)
938         .addColumn(DEPRECATED_TECHNICAL_UPDATED_AT_COL)
939         .addColumn(newVarcharColumnBuilder("key_pattern").setLimit(500).build())
940         .build());
941   }
942
943   private void createPlugins(Context context) {
944     int pluginKeyMaxSize = 200;
945     String tableName = "plugins";
946     VarcharColumnDef keyColumn = newVarcharColumnBuilder("kee").setLimit(pluginKeyMaxSize).setIsNullable(false).build();
947     context.execute(
948       newTableBuilder(tableName)
949         .addPkColumn(UUID_COL)
950         .addColumn(keyColumn)
951         .addColumn(newVarcharColumnBuilder("base_plugin_key").setLimit(pluginKeyMaxSize).setIsNullable(true).build())
952         .addColumn(newVarcharColumnBuilder("file_hash").setLimit(200).setIsNullable(false).build())
953         .addColumn(TECHNICAL_CREATED_AT_COL)
954         .addColumn(TECHNICAL_UPDATED_AT_COL)
955         .addColumn(newVarcharColumnBuilder("type").setLimit(10).setIsNullable(false).build())
956         .build());
957     addIndex(context, tableName, "plugins_key", true, keyColumn);
958   }
959
960   private void createProjectBranches(Context context) {
961     String tableName = "project_branches";
962     VarcharColumnDef projectUuidCol = newVarcharColumnBuilder(PROJECT_UUID_COL_NAME).setIsNullable(false).setLimit(OLD_UUID_VARCHAR_SIZE).build();
963     VarcharColumnDef keeCol = newVarcharColumnBuilder("kee").setIsNullable(false).setLimit(255).build();
964     VarcharColumnDef branchTypeCol = newVarcharColumnBuilder("branch_type").setIsNullable(false).setLimit(12).build();
965     context.execute(
966       newTableBuilder(tableName)
967         .addPkColumn(newVarcharColumnBuilder("uuid").setIsNullable(false).setLimit(OLD_UUID_VARCHAR_SIZE).build())
968         .addColumn(projectUuidCol)
969         .addColumn(keeCol)
970         .addColumn(branchTypeCol)
971         .addColumn(newVarcharColumnBuilder("merge_branch_uuid").setIsNullable(true).setLimit(OLD_UUID_VARCHAR_SIZE).build())
972         .addColumn(newBlobColumnDefBuilder().setColumnName("pull_request_binary").setIsNullable(true).build())
973         .addColumn(newVarcharColumnBuilder("manual_baseline_analysis_uuid").setIsNullable(true).setLimit(UUID_SIZE).build())
974         .addColumn(TECHNICAL_CREATED_AT_COL)
975         .addColumn(TECHNICAL_UPDATED_AT_COL)
976         .addColumn(newBooleanColumnBuilder("exclude_from_purge").setDefaultValue(false).setIsNullable(false).build())
977         .addColumn(newBooleanColumnBuilder("need_issue_sync").setIsNullable(false).build())
978         .build());
979     addIndex(context, tableName, "uniq_project_branches", true, branchTypeCol, projectUuidCol, keeCol);
980   }
981
982   private void createProjectLinks(Context context) {
983     String tableName = "project_links";
984     VarcharColumnDef projectUuidCol = newVarcharColumnBuilder(PROJECT_UUID_COL_NAME).setLimit(UUID_SIZE).setIsNullable(false).build();
985     context.execute(newTableBuilder(tableName)
986       .addPkColumn(UUID_COL)
987       .addColumn(projectUuidCol)
988       .addColumn(newVarcharColumnBuilder("link_type").setLimit(20).setIsNullable(false).build())
989       .addColumn(newVarcharColumnBuilder("name").setLimit(128).setIsNullable(true).build())
990       .addColumn(newVarcharColumnBuilder("href").setLimit(2048).setIsNullable(false).build())
991       .addColumn(TECHNICAL_CREATED_AT_COL)
992       .addColumn(TECHNICAL_UPDATED_AT_COL)
993       .build());
994     addIndex(context, tableName, "project_links_project", false, projectUuidCol);
995   }
996
997   private void createProjectMappings(Context context) {
998     String tableName = "project_mappings";
999     VarcharColumnDef keyTypeCol = newVarcharColumnBuilder("key_type").setIsNullable(false).setLimit(200).build();
1000     VarcharColumnDef keyCol = newVarcharColumnBuilder("kee").setIsNullable(false).setLimit(MAX_SIZE).build();
1001     VarcharColumnDef projectUuidCol = newVarcharColumnBuilder(PROJECT_UUID_COL_NAME).setIsNullable(false).setLimit(UUID_SIZE).build();
1002     context.execute(newTableBuilder(tableName)
1003       .addPkColumn(UUID_COL)
1004       .addColumn(keyTypeCol)
1005       .addColumn(keyCol)
1006       .addColumn(projectUuidCol)
1007       .addColumn(TECHNICAL_CREATED_AT_COL)
1008       .build());
1009     addIndex(context, tableName, "key_type_kee", true, keyTypeCol, keyCol);
1010     addIndex(context, tableName, PROJECT_UUID_COL_NAME, false, projectUuidCol);
1011   }
1012
1013   private void createProjectMeasures(Context context) {
1014     String tableName = "project_measures";
1015     IntegerColumnDef personIdCol = newIntegerColumnDefBuilder().setColumnName("person_id").build();
1016     VarcharColumnDef metricUuidCol = newVarcharColumnBuilder(METRIC_UUID_COL_NAME).setLimit(UUID_SIZE).setIsNullable(false).build();
1017     VarcharColumnDef analysisUuidCol = newVarcharColumnBuilder(ANALYSIS_UUID_COL_NAME).setLimit(OLD_UUID_VARCHAR_SIZE).setIsNullable(false).build();
1018     VarcharColumnDef componentUuidCol = newVarcharColumnBuilder(COMPONENT_UUID_COL_NAME).setLimit(OLD_UUID_VARCHAR_SIZE).setIsNullable(false).build();
1019     context.execute(
1020       newTableBuilder(tableName)
1021         .addPkColumn(UUID_COL)
1022         .addColumn(newDecimalColumnDefBuilder().setColumnName(VALUE_COL_NAME).setPrecision(38).setScale(20).build())
1023         .addColumn(analysisUuidCol)
1024         .addColumn(componentUuidCol)
1025         .addColumn(newVarcharColumnBuilder(TEXT_VALUE_COL_NAME).setLimit(MAX_SIZE).build())
1026         .addColumn(newVarcharColumnBuilder("alert_status").setLimit(5).build())
1027         .addColumn(newVarcharColumnBuilder("alert_text").setLimit(MAX_SIZE).build())
1028         .addColumn(personIdCol)
1029         .addColumn(newDecimalColumnDefBuilder().setColumnName("variation_value_1").setPrecision(38).setScale(20).build())
1030         .addColumn(newBlobColumnDefBuilder().setColumnName("measure_data").build())
1031         .addColumn(metricUuidCol)
1032         .build());
1033     addIndex(context, tableName, "measures_component_uuid", false, componentUuidCol);
1034     addIndex(context, tableName, "measures_analysis_metric", false, analysisUuidCol, metricUuidCol);
1035     addIndex(context, tableName, "project_measures_metric", false, metricUuidCol);
1036   }
1037
1038   private void createProjectQprofiles(Context context) {
1039     String tableName = "project_qprofiles";
1040     VarcharColumnDef projectUuid = newVarcharColumnBuilder(PROJECT_UUID_COL_NAME).setLimit(50).setIsNullable(false).build();
1041     VarcharColumnDef profileKey = newVarcharColumnBuilder("profile_key").setLimit(50).setIsNullable(false).build();
1042     context.execute(
1043       newTableBuilder(tableName)
1044         .addPkColumn(UUID_COL)
1045         .addColumn(projectUuid)
1046         .addColumn(profileKey)
1047         .build());
1048     addIndex(context, tableName, "uniq_project_qprofiles", true, projectUuid, profileKey);
1049   }
1050
1051   private void createProjects(Context context) {
1052     String tableName = "projects";
1053     VarcharColumnDef uuidCol = UUID_COL;
1054     VarcharColumnDef keeCol = newVarcharColumnBuilder("kee").setLimit(400).setIsNullable(false).build();
1055     VarcharColumnDef qualifierCol = newVarcharColumnBuilder("qualifier").setLimit(10).setIsNullable(false).build();
1056     context.execute(
1057       newTableBuilder(tableName)
1058         .addPkColumn(uuidCol)
1059         .addColumn(keeCol)
1060         .addColumn(qualifierCol)
1061         .addColumn(newVarcharColumnBuilder("name").setLimit(2_000).setIsNullable(true).build())
1062         .addColumn(newVarcharColumnBuilder(DESCRIPTION_COL_NAME).setLimit(2_000).setIsNullable(true).build())
1063         .addColumn(newBooleanColumnDefBuilder().setColumnName("private").setIsNullable(false).build())
1064         .addColumn(newVarcharColumnBuilder("tags").setLimit(500).setIsNullable(true).build())
1065         .addColumn(NULLABLE_TECHNICAL_CREATED_AT_COL)
1066         .addColumn(TECHNICAL_UPDATED_AT_COL)
1067         .withPkConstraintName("pk_new_projects")
1068         .build());
1069     addIndex(context, tableName, "uniq_projects_kee", true, keeCol);
1070     addIndex(context, tableName, "idx_qualifier", false, qualifierCol);
1071   }
1072
1073   private void createProjectQGates(Context context) {
1074     String tableName = "project_qgates";
1075     VarcharColumnDef projectUuidCol = newVarcharColumnBuilder(PROJECT_UUID_COL_NAME).setLimit(UUID_SIZE).setIsNullable(false).build();
1076     VarcharColumnDef qualityGateUuidCol = newVarcharColumnBuilder("quality_gate_uuid").setLimit(UUID_SIZE).setIsNullable(false).build();
1077     context.execute(
1078       newTableBuilder(tableName)
1079         .addPkColumn(projectUuidCol)
1080         .addColumn(qualityGateUuidCol)
1081         .build());
1082     addIndex(context, tableName, "uniq_project_qgates", true, projectUuidCol, qualityGateUuidCol);
1083   }
1084
1085   private void createProperties(Context context) {
1086     String tableName = "properties";
1087     VarcharColumnDef propKey = newVarcharColumnBuilder("prop_key").setLimit(512).setIsNullable(false).build();
1088     context.execute(newTableBuilder(tableName)
1089       .addPkColumn(UUID_COL)
1090       .addColumn(propKey)
1091       .addColumn(newBooleanColumnDefBuilder().setColumnName("is_empty").setIsNullable(false).build())
1092       .addColumn(newVarcharColumnBuilder(TEXT_VALUE_COL_NAME).setLimit(MAX_SIZE).build())
1093       .addColumn(newClobColumnDefBuilder().setColumnName("clob_value").setIsNullable(true).build())
1094       .addColumn(TECHNICAL_CREATED_AT_COL)
1095       .addColumn(newVarcharColumnDefBuilder().setColumnName(COMPONENT_UUID_COL_NAME).setIsNullable(true).setLimit(UUID_SIZE).build())
1096       .addColumn(newVarcharColumnDefBuilder().setColumnName(USER_UUID_COL_NAME).setIsNullable(true).setLimit(USER_UUID_SIZE).build())
1097       // table with be renamed to properties in following migration, use final constraint name right away
1098       .withPkConstraintName("pk_properties")
1099       .build());
1100     addIndex(context, tableName, "properties_key", false, propKey);
1101   }
1102
1103   private void createQProfileChanges(Context context) {
1104     String tableName = "qprofile_changes";
1105     VarcharColumnDef rulesProfileUuidCol = newVarcharColumnBuilder("rules_profile_uuid").setLimit(255).setIsNullable(false).build();
1106     context.execute(newTableBuilder(tableName)
1107       .addPkColumn(newVarcharColumnBuilder("kee").setLimit(UUID_SIZE).setIsNullable(false).build())
1108       .addColumn(rulesProfileUuidCol)
1109       .addColumn(newVarcharColumnBuilder("change_type").setLimit(20).setIsNullable(false).build())
1110       .addColumn(newVarcharColumnBuilder(USER_UUID_COL_NAME).setLimit(USER_UUID_SIZE).setIsNullable(true).build())
1111       .addColumn(newClobColumnDefBuilder().setColumnName("change_data").setIsNullable(true).build())
1112       .addColumn(TECHNICAL_CREATED_AT_COL)
1113       .build());
1114     addIndex(context, tableName, "qp_changes_rules_profile_uuid", false, rulesProfileUuidCol);
1115   }
1116
1117   private void createQProfileEditGroups(Context context) {
1118     String tableName = "qprofile_edit_groups";
1119     VarcharColumnDef qProfileUuidCol = newVarcharColumnBuilder(QPROFILE_UUID_COL_NAME).setIsNullable(false).setLimit(255).build();
1120     VarcharColumnDef groupUuidCol = newVarcharColumnBuilder(GROUP_UUID_COL_NAME).setLimit(UUID_SIZE).setIsNullable(false).build();
1121     context.execute(newTableBuilder(tableName)
1122       .addPkColumn(UUID_COL)
1123       .addColumn(qProfileUuidCol)
1124       .addColumn(TECHNICAL_CREATED_AT_COL)
1125       .addColumn(groupUuidCol)
1126       .build());
1127     addIndex(context, tableName, tableName + "_qprofile", false, qProfileUuidCol);
1128     addIndex(context, tableName, tableName + UNIQUE_INDEX_SUFFIX, true, groupUuidCol, qProfileUuidCol);
1129   }
1130
1131   private void createQProfileEditUsers(Context context) {
1132     String tableName = "qprofile_edit_users";
1133     VarcharColumnDef qProfileUuidCol = newVarcharColumnBuilder(QPROFILE_UUID_COL_NAME).setLimit(255).setIsNullable(false).build();
1134     VarcharColumnDef userUuidCol = newVarcharColumnBuilder(USER_UUID_COL_NAME).setLimit(255).setIsNullable(false).build();
1135     context.execute(newTableBuilder(tableName)
1136       .addPkColumn(UUID_COL)
1137       .addColumn(qProfileUuidCol)
1138       .addColumn(TECHNICAL_CREATED_AT_COL)
1139       .addColumn(userUuidCol)
1140       .build());
1141     addIndex(context, tableName, tableName + "_qprofile", false, qProfileUuidCol);
1142     addIndex(context, tableName, tableName + UNIQUE_INDEX_SUFFIX, true, userUuidCol, qProfileUuidCol);
1143   }
1144
1145   private void createQualityGateConditions(Context context) {
1146     context.execute(
1147       newTableBuilder("quality_gate_conditions")
1148         .addPkColumn(UUID_COL)
1149         .addColumn(newVarcharColumnBuilder("operator").setLimit(3).setIsNullable(true).build())
1150         .addColumn(newVarcharColumnBuilder("value_error").setLimit(64).setIsNullable(true).build())
1151         .addColumn(DEPRECATED_TECHNICAL_CREATED_AT_COL)
1152         .addColumn(DEPRECATED_TECHNICAL_UPDATED_AT_COL)
1153         .addColumn(newVarcharColumnBuilder(METRIC_UUID_COL_NAME).setLimit(UUID_SIZE).setIsNullable(false).build())
1154         .addColumn(newVarcharColumnBuilder("qgate_uuid").setLimit(UUID_SIZE).setIsNullable(false).build())
1155         .build());
1156   }
1157
1158   private void createQualityGates(Context context) {
1159     context.execute(
1160       newTableBuilder("quality_gates")
1161         .addPkColumn(UUID_COL)
1162         .addColumn(newVarcharColumnBuilder("name").setLimit(100).setIsNullable(false).build())
1163         .addColumn(newBooleanColumnDefBuilder().setColumnName("is_built_in").setIsNullable(false).build())
1164         .addColumn(DEPRECATED_TECHNICAL_CREATED_AT_COL)
1165         .addColumn(DEPRECATED_TECHNICAL_UPDATED_AT_COL)
1166         .build());
1167   }
1168
1169   private void createSessionTokens(Context context) {
1170     String tableName = "session_tokens";
1171     VarcharColumnDef userUuidCol = newVarcharColumnBuilder(USER_UUID_COL_NAME).setLimit(255).setIsNullable(false).build();
1172     context.execute(
1173       newTableBuilder(tableName)
1174         .addPkColumn(UUID_COL)
1175         .addColumn(userUuidCol)
1176         .addColumn(newBigIntegerColumnDefBuilder().setColumnName("expiration_date").setIsNullable(false).build())
1177         .addColumn(TECHNICAL_CREATED_AT_COL)
1178         .addColumn(TECHNICAL_UPDATED_AT_COL)
1179         .build());
1180
1181     addIndex(context, tableName, "session_tokens_user_uuid", false, userUuidCol);
1182   }
1183
1184   private void createRulesRepository(Context context) {
1185     String tableName = "rule_repositories";
1186     context.execute(newTableBuilder(tableName)
1187       .addPkColumn(newVarcharColumnBuilder("kee").setLimit(200).setIsNullable(false).build())
1188       .addColumn(newVarcharColumnBuilder(LANGUAGE_COL_NAME).setLimit(20).setIsNullable(false).build())
1189       .addColumn(newVarcharColumnBuilder("name").setLimit(4_000).setIsNullable(false).build())
1190       .addColumn(TECHNICAL_CREATED_AT_COL)
1191       .build());
1192   }
1193
1194   private void createRules(Context context) {
1195     VarcharColumnDef pluginRuleKeyCol = newVarcharColumnBuilder("plugin_rule_key").setLimit(200).setIsNullable(false).build();
1196     VarcharColumnDef pluginNameCol = newVarcharColumnBuilder("plugin_name").setLimit(255).setIsNullable(false).build();
1197     context.execute(
1198       newTableBuilder("rules")
1199         .addPkColumn(UUID_COL)
1200         .addColumn(newVarcharColumnBuilder("name").setLimit(200).setIsNullable(true).build())
1201         .addColumn(pluginRuleKeyCol)
1202         .addColumn(newVarcharColumnBuilder("plugin_key").setLimit(200).setIsNullable(true).build())
1203         .addColumn(newVarcharColumnBuilder("plugin_config_key").setLimit(200).setIsNullable(true).build())
1204         .addColumn(pluginNameCol)
1205         .addColumn(newVarcharColumnBuilder("scope").setLimit(20).setIsNullable(false).build())
1206         .addColumn(newClobColumnDefBuilder().setColumnName(DESCRIPTION_COL_NAME).setIsNullable(true).build())
1207         .addColumn(newIntegerColumnDefBuilder().setColumnName("priority").setIsNullable(true).build())
1208         .addColumn(newVarcharColumnBuilder(STATUS_COL_NAME).setLimit(40).setIsNullable(true).build())
1209         .addColumn(newVarcharColumnBuilder(LANGUAGE_COL_NAME).setLimit(20).setIsNullable(true).build())
1210         .addColumn(newVarcharColumnBuilder("def_remediation_function").setLimit(20).setIsNullable(true).build())
1211         .addColumn(newVarcharColumnBuilder("def_remediation_gap_mult").setLimit(20).setIsNullable(true).build())
1212         .addColumn(newVarcharColumnBuilder("def_remediation_base_effort").setLimit(20).setIsNullable(true).build())
1213         .addColumn(newVarcharColumnBuilder("gap_description").setLimit(MAX_SIZE).setIsNullable(true).build())
1214         .addColumn(newVarcharColumnBuilder("system_tags").setLimit(MAX_SIZE).setIsNullable(true).build())
1215         .addColumn(newBooleanColumnDefBuilder().setColumnName("is_template").setIsNullable(false).setDefaultValue(false).build())
1216         .addColumn(newVarcharColumnBuilder("description_format").setLimit(20).setIsNullable(true).build())
1217         .addColumn(new TinyIntColumnDef.Builder().setColumnName("rule_type").setIsNullable(true).build())
1218         .addColumn(newVarcharColumnBuilder("security_standards").setIsNullable(true).setLimit(4_000).build())
1219         .addColumn(newBooleanColumnDefBuilder().setColumnName("is_ad_hoc").setIsNullable(false).build())
1220         .addColumn(newBooleanColumnDefBuilder().setColumnName("is_external").setIsNullable(false).build())
1221         .addColumn(NULLABLE_TECHNICAL_CREATED_AT_COL)
1222         .addColumn(NULLABLE_TECHNICAL_UPDATED_AT_COL)
1223         .addColumn(newVarcharColumnBuilder(TEMPLATE_UUID_COL_NAME).setIsNullable(true).setLimit(UUID_SIZE).build())
1224         .build());
1225     addIndex(context, "rules", "rules_repo_key", true, pluginRuleKeyCol, pluginNameCol);
1226   }
1227
1228   private void createRulesMetadata(Context context) {
1229     String tableName = "rules_metadata";
1230     context.execute(newTableBuilder(tableName)
1231       .addPkColumn(newVarcharColumnBuilder(RULE_UUID_COL_NAME).setLimit(UUID_SIZE).setIsNullable(false).build())
1232       .addColumn(newClobColumnDefBuilder().setColumnName("note_data").setIsNullable(true).build())
1233       .addColumn(newVarcharColumnBuilder("note_user_uuid").setLimit(USER_UUID_SIZE).setIsNullable(true).build())
1234       .addColumn(newBigIntegerColumnDefBuilder().setColumnName("note_created_at").setIsNullable(true).build())
1235       .addColumn(newBigIntegerColumnDefBuilder().setColumnName("note_updated_at").setIsNullable(true).build())
1236       .addColumn(newVarcharColumnBuilder("remediation_function").setLimit(20).setIsNullable(true).build())
1237       .addColumn(newVarcharColumnBuilder("remediation_gap_mult").setLimit(20).setIsNullable(true).build())
1238       .addColumn(newVarcharColumnBuilder("remediation_base_effort").setLimit(20).setIsNullable(true).build())
1239       .addColumn(newVarcharColumnBuilder("tags").setLimit(4_000).setIsNullable(true).build())
1240       .addColumn(newVarcharColumnBuilder("ad_hoc_name").setLimit(200).setIsNullable(true).build())
1241       .addColumn(newClobColumnDefBuilder().setColumnName("ad_hoc_description").setIsNullable(true).build())
1242       .addColumn(newVarcharColumnBuilder("ad_hoc_severity").setLimit(10).setIsNullable(true).build())
1243       .addColumn(newTinyIntColumnDefBuilder().setColumnName("ad_hoc_type").setIsNullable(true).build())
1244       .addColumn(TECHNICAL_CREATED_AT_COL)
1245       .addColumn(TECHNICAL_UPDATED_AT_COL)
1246       .withPkConstraintName("pk_rules_metadata")
1247       .build());
1248   }
1249
1250   private void createRulesParameters(Context context) {
1251     String tableName = "rules_parameters";
1252     VarcharColumnDef ruleUuidCol = newVarcharColumnBuilder(RULE_UUID_COL_NAME).setLimit(UUID_SIZE).setIsNullable(false).build();
1253     VarcharColumnDef nameCol = newVarcharColumnBuilder("name").setLimit(128).setIsNullable(false).build();
1254     context.execute(
1255       newTableBuilder(tableName)
1256         .addPkColumn(UUID_COL)
1257         .addColumn(nameCol)
1258         .addColumn(newVarcharColumnBuilder(DESCRIPTION_COL_NAME).setLimit(MAX_SIZE).setIsNullable(true).build())
1259         .addColumn(newVarcharColumnBuilder("param_type").setLimit(512).setIsNullable(false).build())
1260         .addColumn(newVarcharColumnBuilder("default_value").setLimit(MAX_SIZE).setIsNullable(true).build())
1261         .addColumn(ruleUuidCol)
1262         .build());
1263     addIndex(context, tableName, "rules_parameters_rule_uuid", false, ruleUuidCol);
1264     addIndex(context, tableName, "rules_parameters_unique", true, ruleUuidCol, nameCol);
1265   }
1266
1267   private void createRulesProfiles(Context context) {
1268     String tableName = "rules_profiles";
1269     context.execute(
1270       newTableBuilder(tableName)
1271         .addPkColumn(UUID_COL)
1272         .addColumn(newVarcharColumnBuilder("name").setLimit(100).setIsNullable(false).build())
1273         .addColumn(newVarcharColumnBuilder(LANGUAGE_COL_NAME).setLimit(20).setIsNullable(true).build())
1274         .addColumn(newBooleanColumnDefBuilder().setColumnName("is_built_in").setIsNullable(false).build())
1275         .addColumn(newVarcharColumnBuilder("rules_updated_at").setLimit(100).setIsNullable(true).build())
1276         .addColumn(DEPRECATED_TECHNICAL_CREATED_AT_COL)
1277         .addColumn(DEPRECATED_TECHNICAL_UPDATED_AT_COL)
1278         .build());
1279   }
1280
1281   private void createSamlMessageIds(Context context) {
1282     String tableName = "saml_message_ids";
1283     VarcharColumnDef messageIdCol = newVarcharColumnBuilder("message_id").setLimit(255).setIsNullable(false).build();
1284     context.execute(
1285       newTableBuilder(tableName)
1286         .addPkColumn(UUID_COL)
1287         .addColumn(messageIdCol)
1288         .addColumn(newBigIntegerColumnDefBuilder().setColumnName("expiration_date").setIsNullable(false).build())
1289         .addColumn(TECHNICAL_CREATED_AT_COL)
1290         .build());
1291     addIndex(context, tableName, "saml_message_ids_unique", true, messageIdCol);
1292   }
1293
1294   private void createSnapshots(Context context) {
1295     String tableName = "snapshots";
1296     VarcharColumnDef uuidCol = newVarcharColumnBuilder("uuid").setLimit(OLD_UUID_VARCHAR_SIZE).setIsNullable(false).build();
1297     VarcharColumnDef componentUuidCol = newVarcharColumnBuilder(COMPONENT_UUID_COL_NAME).setLimit(OLD_UUID_VARCHAR_SIZE).setIsNullable(false).build();
1298     context.execute(
1299       newTableBuilder(tableName)
1300         .addPkColumn(uuidCol)
1301         .addColumn(componentUuidCol)
1302         .addColumn(newVarcharColumnBuilder(STATUS_COL_NAME).setLimit(4).setIsNullable(false).setDefaultValue("U").build())
1303         .addColumn(newBooleanColumnDefBuilder().setColumnName("islast").setIsNullable(false).setDefaultValue(false).build())
1304         .addColumn(newVarcharColumnBuilder("version").setLimit(500).setIsNullable(true).build())
1305         .addColumn(newIntegerColumnDefBuilder().setColumnName("purge_status").setIsNullable(true).build())
1306         .addColumn(newVarcharColumnBuilder("build_string").setLimit(100).setIsNullable(true).build())
1307         .addColumn(newVarcharColumnBuilder("revision").setLimit(100).setIsNullable(true).build())
1308         .addColumn(newBigIntegerColumnDefBuilder().setColumnName("build_date").setIsNullable(true).build())
1309         .addColumn(newVarcharColumnBuilder("period1_mode").setLimit(100).setIsNullable(true).build())
1310         .addColumn(newVarcharColumnBuilder("period1_param").setLimit(100).setIsNullable(true).build())
1311         .addColumn(newBigIntegerColumnDefBuilder().setColumnName("period1_date").setIsNullable(true).build())
1312         .addColumn(NULLABLE_TECHNICAL_CREATED_AT_COL)
1313         .build());
1314     addIndex(context, tableName, "analyses_uuid", true, uuidCol);
1315     addIndex(context, tableName, "snapshot_component", false, componentUuidCol);
1316   }
1317
1318   private void createUserProperties(Context context) {
1319     String tableName = "user_properties";
1320     VarcharColumnDef userUuidCol = newVarcharColumnBuilder(USER_UUID_COL_NAME).setLimit(USER_UUID_SIZE).setIsNullable(false).build();
1321     VarcharColumnDef keyCol = newVarcharColumnBuilder("kee").setLimit(100).setIsNullable(false).build();
1322     context.execute(newTableBuilder(tableName)
1323       .addPkColumn(UUID_COL)
1324       .addColumn(userUuidCol)
1325       .addColumn(keyCol)
1326       .addColumn(newVarcharColumnBuilder(TEXT_VALUE_COL_NAME).setLimit(4_000).setIsNullable(false).build())
1327       .addColumn(TECHNICAL_CREATED_AT_COL)
1328       .addColumn(TECHNICAL_UPDATED_AT_COL)
1329       .build());
1330     addIndex(context, tableName, "user_properties_user_uuid_kee", true, userUuidCol, keyCol);
1331   }
1332
1333   private void createUserRoles(Context context) {
1334     String tableName = "user_roles";
1335     VarcharColumnDef componentUuidCol = newVarcharColumnBuilder(COMPONENT_UUID_COL_NAME).setLimit(UUID_SIZE).setIsNullable(true).build();
1336     VarcharColumnDef userUuidCol = newVarcharColumnBuilder(USER_UUID_COL_NAME).setLimit(USER_UUID_SIZE).setIsNullable(true).build();
1337     context.execute(
1338       newTableBuilder(tableName)
1339         .addPkColumn(UUID_COL)
1340         .addColumn(newVarcharColumnBuilder("role").setLimit(64).setIsNullable(false).build())
1341         .addColumn(componentUuidCol)
1342         .addColumn(userUuidCol)
1343         .build());
1344     addIndex(context, tableName, "user_roles_component_uuid", false, componentUuidCol);
1345     addIndex(context, tableName, "user_roles_user", false, userUuidCol);
1346   }
1347
1348   private void createUserDismissedMessage(Context context) {
1349     String tableName = "user_dismissed_messages";
1350     VarcharColumnDef userUuidCol = newVarcharColumnBuilder(USER_UUID_COL_NAME).setLimit(USER_UUID_SIZE).setIsNullable(false).build();
1351     VarcharColumnDef projectUuidCol = newVarcharColumnBuilder(PROJECT_UUID_COL_NAME).setLimit(UUID_SIZE).setIsNullable(false).build();
1352     VarcharColumnDef messageTypeCol = newVarcharColumnBuilder("message_type").setLimit(255).setIsNullable(false).build();
1353     context.execute(
1354       newTableBuilder(tableName)
1355         .addPkColumn(UUID_COL)
1356         .addColumn(userUuidCol)
1357         .addColumn(projectUuidCol)
1358         .addColumn(messageTypeCol)
1359         .addColumn(TECHNICAL_CREATED_AT_COL)
1360         .build());
1361     addIndex(context, tableName, "uniq_user_dismissed_messages", true, userUuidCol, projectUuidCol, messageTypeCol);
1362     addIndex(context, tableName, "udm_project_uuid", false, projectUuidCol);
1363     addIndex(context, tableName, "udm_message_type", false, messageTypeCol);
1364   }
1365
1366   private void createUserTokens(Context context) {
1367     String tableName = "user_tokens";
1368     VarcharColumnDef userUuidCol = newVarcharColumnBuilder(USER_UUID_COL_NAME).setLimit(USER_UUID_SIZE).setIsNullable(false).build();
1369     VarcharColumnDef nameCol = newVarcharColumnBuilder("name").setLimit(100).setIsNullable(false).build();
1370     VarcharColumnDef tokenHashCol = newVarcharColumnBuilder("token_hash").setLimit(255).setIsNullable(false).build();
1371     context.execute(
1372       newTableBuilder(tableName)
1373         .addPkColumn(UUID_COL)
1374         .addColumn(userUuidCol)
1375         .addColumn(nameCol)
1376         .addColumn(tokenHashCol)
1377         .addColumn(newBigIntegerColumnDefBuilder().setColumnName("last_connection_date").setIsNullable(true).build())
1378         .addColumn(TECHNICAL_CREATED_AT_COL)
1379         .build());
1380     addIndex(context, tableName, "user_tokens_user_uuid_name", true, userUuidCol, nameCol);
1381     addIndex(context, tableName, "user_tokens_token_hash", true, tokenHashCol);
1382   }
1383
1384   private void createUsers(Context context) {
1385     String tableName = "users";
1386     VarcharColumnDef loginCol = newVarcharColumnBuilder("login").setLimit(255).setIsNullable(false).build();
1387     VarcharColumnDef externalLoginCol = newVarcharColumnBuilder("external_login").setLimit(255).setIsNullable(false).build();
1388     VarcharColumnDef externalIdentityProviderCol = newVarcharColumnBuilder("external_identity_provider").setLimit(100).setIsNullable(false).build();
1389     VarcharColumnDef externalIdCol = newVarcharColumnBuilder("external_id").setLimit(255).setIsNullable(false).build();
1390     context.execute(
1391       newTableBuilder(tableName)
1392         .addPkColumn(newVarcharColumnBuilder("uuid").setLimit(USER_UUID_SIZE).setIsNullable(false).build())
1393         .addColumn(loginCol)
1394         .addColumn(newVarcharColumnBuilder("name").setLimit(200).setIsNullable(true).build())
1395         .addColumn(newVarcharColumnBuilder("email").setLimit(100).setIsNullable(true).build())
1396         .addColumn(newVarcharColumnBuilder("crypted_password").setLimit(100).setIsNullable(true).build())
1397         .addColumn(newVarcharColumnBuilder("salt").setLimit(40).setIsNullable(true).build())
1398         .addColumn(newVarcharColumnBuilder("hash_method").setLimit(10).setIsNullable(true).build())
1399         .addColumn(newBooleanColumnDefBuilder().setColumnName("active").setDefaultValue(true).setIsNullable(true).build())
1400         .addColumn(newVarcharColumnBuilder("scm_accounts").setLimit(MAX_SIZE).build())
1401         .addColumn(externalLoginCol)
1402         .addColumn(externalIdentityProviderCol)
1403         .addColumn(externalIdCol)
1404         .addColumn(newBooleanColumnDefBuilder().setColumnName("is_root").setIsNullable(false).build())
1405         .addColumn(newBooleanColumnDefBuilder().setColumnName("user_local").setIsNullable(true).build())
1406         .addColumn(newBooleanColumnDefBuilder().setColumnName("onboarded").setIsNullable(false).build())
1407         .addColumn(newVarcharColumnBuilder("homepage_type").setLimit(40).setIsNullable(true).build())
1408         .addColumn(newVarcharColumnBuilder("homepage_parameter").setLimit(40).setIsNullable(true).build())
1409         .addColumn(newBigIntegerColumnDefBuilder().setColumnName("last_connection_date").setIsNullable(true).build())
1410         .addColumn(NULLABLE_TECHNICAL_CREATED_AT_COL)
1411         .addColumn(NULLABLE_TECHNICAL_UPDATED_AT_COL)
1412         .addColumn(newBooleanColumnDefBuilder().setColumnName("reset_password").setIsNullable(false).build())
1413         .addColumn(newBigIntegerColumnDefBuilder().setColumnName("last_sonarlint_connection").setIsNullable(true).build())
1414         .build());
1415     addIndex(context, tableName, "users_login", true, loginCol);
1416     addIndex(context, tableName, "users_updated_at", false, NULLABLE_TECHNICAL_UPDATED_AT_COL);
1417     addIndex(context, tableName, "uniq_external_id", true, externalIdentityProviderCol, externalIdCol);
1418     addIndex(context, tableName, "uniq_external_login", true, externalIdentityProviderCol, externalLoginCol);
1419   }
1420
1421   private void createWebhookDeliveries(Context context) {
1422     String tableName = "webhook_deliveries";
1423     VarcharColumnDef componentUuidColumn = newVarcharColumnBuilder(COMPONENT_UUID_COL_NAME).setLimit(UUID_SIZE).setIsNullable(false).build();
1424     VarcharColumnDef ceTaskUuidColumn = newVarcharColumnBuilder("ce_task_uuid").setLimit(UUID_SIZE).setIsNullable(true).build();
1425     VarcharColumnDef webhookUuidColumn = newVarcharColumnBuilder("webhook_uuid").setLimit(UUID_SIZE).setIsNullable(false).build();
1426     context.execute(newTableBuilder(tableName)
1427       .addPkColumn(UUID_COL)
1428       .addColumn(webhookUuidColumn)
1429       .addColumn(componentUuidColumn)
1430       .addColumn(ceTaskUuidColumn)
1431       .addColumn(newVarcharColumnBuilder(ANALYSIS_UUID_COL_NAME).setLimit(UUID_SIZE).setIsNullable(true).build())
1432       .addColumn(newVarcharColumnBuilder("name").setLimit(100).setIsNullable(false).build())
1433       .addColumn(newVarcharColumnBuilder("url").setLimit(2_000).setIsNullable(false).build())
1434       .addColumn(newBooleanColumnDefBuilder().setColumnName("success").setIsNullable(false).build())
1435       .addColumn(newIntegerColumnDefBuilder().setColumnName("http_status").setIsNullable(true).build())
1436       .addColumn(newBigIntegerColumnDefBuilder().setColumnName("duration_ms").setIsNullable(false).build())
1437       .addColumn(newClobColumnDefBuilder().setColumnName("payload").setIsNullable(false).build())
1438       .addColumn(newClobColumnDefBuilder().setColumnName("error_stacktrace").setIsNullable(true).build())
1439       .addColumn(TECHNICAL_CREATED_AT_COL)
1440       .build());
1441     addIndex(context, tableName, COMPONENT_UUID_COL_NAME, false, componentUuidColumn);
1442     addIndex(context, tableName, "ce_task_uuid", false, ceTaskUuidColumn);
1443     addIndex(context, tableName, "idx_wbhk_dlvrs_wbhk_uuid", false, webhookUuidColumn);
1444   }
1445
1446   private void createWebhooks(Context context) {
1447     String tableName = "webhooks";
1448     VarcharColumnDef projectUuidCol = newVarcharColumnBuilder(PROJECT_UUID_COL_NAME).setLimit(UUID_SIZE).setIsNullable(true).build();
1449     context.execute(newTableBuilder(tableName)
1450       .addPkColumn(UUID_COL)
1451       .addColumn(projectUuidCol)
1452       .addColumn(newVarcharColumnBuilder("name").setLimit(100).setIsNullable(false).build())
1453       .addColumn(newVarcharColumnBuilder("url").setLimit(2_000).setIsNullable(false).build())
1454       .addColumn(newVarcharColumnBuilder("secret").setLimit(200).setIsNullable(true).build())
1455       .addColumn(TECHNICAL_CREATED_AT_COL)
1456       .addColumn(NULLABLE_TECHNICAL_UPDATED_AT_COL)
1457       .build());
1458   }
1459
1460   private static void addIndex(Context context, String table, String index, boolean unique, ColumnDef firstColumn, ColumnDef... otherColumns) {
1461     CreateIndexBuilder builder = new CreateIndexBuilder()
1462       .setTable(table)
1463       .setName(index)
1464       .setUnique(unique);
1465     concat(of(firstColumn), stream(otherColumns)).forEach(builder::addColumn);
1466     context.execute(builder.build());
1467   }
1468
1469   private static VarcharColumnDef.Builder newVarcharColumnBuilder(String column) {
1470     return newVarcharColumnDefBuilder().setColumnName(column);
1471   }
1472
1473   private static BooleanColumnDef.Builder newBooleanColumnBuilder(String column) {
1474     return newBooleanColumnDefBuilder().setColumnName(column);
1475   }
1476
1477   private CreateTableBuilder newTableBuilder(String tableName) {
1478     return new CreateTableBuilder(getDialect(), tableName);
1479   }
1480 }