View Javadoc

1   /*
2    * #%L
3    * Bitrepository Reference Pillar
4    * %%
5    * Copyright (C) 2010 - 2013 The State and University Library, The Royal Library and The State Archives, Denmark
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 2.1 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-2.1.html>.
20   * #L%
21   */
22  package org.bitrepository.pillar.cache;
23  
24  import java.util.Map;
25  import org.bitrepository.common.settings.Settings;
26  import org.bitrepository.service.database.DBConnector;
27  import org.bitrepository.service.database.DatabaseMigrator;
28  import org.bitrepository.service.database.DatabaseUtils;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  import static org.bitrepository.pillar.cache.database.DatabaseConstants.CHECKSUM_TABLE;
33  import static org.bitrepository.pillar.cache.database.DatabaseConstants.CS_COLLECTION_ID;
34  
35  /**
36   * Migration class for the ChecksumDatabase of the ReferencePillar and ChecksumPillar.
37   */
38  public class ChecksumDBMigrator extends DatabaseMigrator {
39      /** The log.*/
40      private static Logger log = LoggerFactory.getLogger(DatabaseUtils.class);
41      /** The settings.*/
42      private final Settings settings;
43      /** The current version of the database. */
44      private final Integer currentVersion = 2;
45      
46      
47      /**
48       * Constructor.
49       * @param connector
50       * @param settings
51       */
52      public ChecksumDBMigrator(DBConnector connector, Settings settings) {
53          super(connector);
54          this.settings = settings;
55      }
56      
57      @Override
58      public void migrate() {
59          Map<String, Integer> versions = getTableVersions();
60          
61          if(!versions.containsKey(CHECKSUM_TABLE)) {
62              throw new IllegalStateException("The database does not contain '" + CHECKSUM_TABLE 
63                      + "' table as required.");
64          }
65          if(versions.get(CHECKSUM_TABLE) == 1) {
66              migrateChecksumsTableFromVersion1To2();
67          }
68      }
69      
70      /**
71       * Migrate the ChecksumTable from version 1 to 2.
72       * Just adds the column 'collectionid', which is set to the current (or first) collection id.
73       */
74      private void migrateChecksumsTableFromVersion1To2() {
75          log.warn("Migrating the " + CHECKSUM_TABLE + " table from version 1 to 2 in the ChecksumDatabase.");
76          
77          String alterSql = "ALTER TABLE " + CHECKSUM_TABLE + " ADD COLUMN " + CS_COLLECTION_ID + " VARCHAR(255)";
78          updateTable(CHECKSUM_TABLE, 2, alterSql, new Object[0]);
79          
80          String updateAfterwards = "UPDATE " + CHECKSUM_TABLE + " SET " + CS_COLLECTION_ID + " = ? WHERE " 
81                  + CS_COLLECTION_ID + " IS NULL";
82          DatabaseUtils.executeStatement(connector, updateAfterwards, settings.getCollections().get(0).getID());
83      }
84  
85      @Override
86      public boolean needsMigration() {
87          Map<String, Integer> versions = getTableVersions();
88          
89          if(!versions.containsKey(CHECKSUM_TABLE)) {
90              throw new IllegalStateException("The database does not contain '" + CHECKSUM_TABLE 
91                      + "' table as required.");
92          }
93          
94          if(versions.get(CHECKSUM_TABLE) < currentVersion) {
95              return true;
96          } else {
97              return false;
98          }
99      }
100 }