Autopsy  4.19.3
Graphical digital forensics platform for The Sleuth Kit and other tools.
CentralRepoDbUpgrader13To14.java
Go to the documentation of this file.
1 /*
2  * Central Repository
3  *
4  * Copyright 2020 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.centralrepository.datamodel;
20 
21 import java.sql.Connection;
22 import java.sql.SQLException;
23 import java.sql.Statement;
24 import org.sleuthkit.datamodel.CaseDbSchemaVersionNumber;
25 
26 
34 
35  @Override
36  public void upgradeSchema(CaseDbSchemaVersionNumber dbSchemaVersion, Connection connection) throws CentralRepoException, SQLException {
37 
38  if (dbSchemaVersion.compareTo(new CaseDbSchemaVersionNumber(1, 4)) < 0) {
39 
40  try (Statement statement = connection.createStatement();) {
41 
43 
44  // Create account_types and accounts tables which are referred by X_instances tables
45  statement.execute(RdbmsCentralRepoFactory.getCreateAccountTypesTableStatement(selectedPlatform));
46  statement.execute(RdbmsCentralRepoFactory.getCreateAccountsTableStatement(selectedPlatform));
47 
49  String instance_type_dbname = CentralRepoDbUtil.correlationTypeToInstanceTableName(type);
50 
52 
53  // these are new Correlation types - new tables need to be created
54  statement.execute(String.format(RdbmsCentralRepoFactory.getCreateAccountInstancesTableTemplate(selectedPlatform), instance_type_dbname, instance_type_dbname));
55  statement.execute(String.format(RdbmsCentralRepoFactory.getAddCaseIdIndexTemplate(), instance_type_dbname, instance_type_dbname));
56  statement.execute(String.format(RdbmsCentralRepoFactory.getAddDataSourceIdIndexTemplate(), instance_type_dbname, instance_type_dbname));
57  statement.execute(String.format(RdbmsCentralRepoFactory.getAddValueIndexTemplate(), instance_type_dbname, instance_type_dbname));
58  statement.execute(String.format(RdbmsCentralRepoFactory.getAddKnownStatusIndexTemplate(), instance_type_dbname, instance_type_dbname));
59  statement.execute(String.format(RdbmsCentralRepoFactory.getAddObjectIdIndexTemplate(), instance_type_dbname, instance_type_dbname));
60 
61  // add new correlation type
62  CentralRepoDbUtil.insertCorrelationType(connection, type);
63 
64  } else if (type.getId() == CorrelationAttributeInstance.EMAIL_TYPE_ID || type.getId() == CorrelationAttributeInstance.PHONE_TYPE_ID) {
65  // Alter the existing _instance tables for Phone and Email attributes to add account_id column
66  String sqlStr = String.format(getAlterArtifactInstancesAddAccountIdTemplate(selectedPlatform), instance_type_dbname);
67  statement.execute(sqlStr);
68 
69  // SQLite does NOT allow adding a constraint with Alter Table statement.
70  // The alternative would be to create new tables, copy all data over, and delete old tables - potentially a time consuming process.
71  // We decided to not add this constraint for SQLite, since there likely aren't many users using SQLite based Central Repo.
72  if (selectedPlatform == CentralRepoPlatforms.POSTGRESQL) {
73  sqlStr = String.format(getAlterArtifactInstancesAddAccountIdConstraintTemplate(), instance_type_dbname);
74  statement.execute(sqlStr);
75  }
76  }
77  }
78 
79  // insert default accounts data
80  RdbmsCentralRepoFactory.insertDefaultAccountsTablesContent(connection, selectedPlatform);
81  }
82  }
83 
84  }
85 
94  static String getAlterArtifactInstancesAddAccountIdTemplate(CentralRepoPlatforms selectedPlatform) {
95  // Each "%s" will be replaced with the relevant TYPE_instances table name.
96  return "ALTER TABLE %s"
97  + " ADD account_id " + RdbmsCentralRepoFactory.getBigIntType(selectedPlatform) + " DEFAULT NULL";
98 
99  }
100 
107  static String getAlterArtifactInstancesAddAccountIdConstraintTemplate() {
108  // Each "%s" will be replaced with the relevant TYPE_instances table name.
109  return "ALTER TABLE %s"
110  + " ADD CONSTRAINT account_id_fk foreign key (account_id) references accounts(id)";
111  }
112 
113 
114 }
static void insertCorrelationType(Connection conn, CorrelationAttributeInstance.Type correlationType)
void upgradeSchema(CaseDbSchemaVersionNumber dbSchemaVersion, Connection connection)
static String correlationTypeToInstanceTableName(CorrelationAttributeInstance.Type type)

Copyright © 2012-2022 Basis Technology. Generated on: Wed Oct 5 2022
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.