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