Coverage Report - liquibase.change.core.DropPrimaryKeyChange
 
Classes in this File Line Coverage Branch Coverage Complexity
DropPrimaryKeyChange
65%
13/20
100%
2/2
1.214
DropPrimaryKeyChange$1
0%
0/7
0%
0/2
1.214
 
 1  
 package liquibase.change.core;
 2  
 
 3  
 import liquibase.change.AbstractChange;
 4  
 import liquibase.change.ChangeMetaData;
 5  
 import liquibase.change.ColumnConfig;
 6  
 import liquibase.database.Database;
 7  
 import liquibase.database.core.SQLiteDatabase;
 8  
 import liquibase.database.core.SQLiteDatabase.AlterTableVisitor;
 9  
 import liquibase.database.structure.Index;
 10  
 import liquibase.statement.SqlStatement;
 11  
 import liquibase.statement.core.DropPrimaryKeyStatement;
 12  
 import liquibase.util.StringUtils;
 13  
 
 14  
 import java.util.ArrayList;
 15  
 import java.util.List;
 16  
 
 17  
 /**
 18  
  * Removes an existing primary key.
 19  
  */
 20  
 public class DropPrimaryKeyChange extends AbstractChange {
 21  
     private String schemaName;
 22  
     private String tableName;
 23  
     private String constraintName;
 24  
 
 25  
     public DropPrimaryKeyChange() {
 26  17
         super("dropPrimaryKey", "Drop Primary Key", ChangeMetaData.PRIORITY_DEFAULT);
 27  17
     }
 28  
 
 29  
     public String getSchemaName() {
 30  47
         return schemaName;
 31  
     }
 32  
 
 33  
     public void setSchemaName(String schemaName) {
 34  3
         this.schemaName = StringUtils.trimToNull(schemaName);
 35  3
     }
 36  
 
 37  
     public String getTableName() {
 38  47
         return tableName;
 39  
     }
 40  
 
 41  
     public void setTableName(String tableName) {
 42  3
         this.tableName = tableName;
 43  3
     }
 44  
 
 45  
     public String getConstraintName() {
 46  46
         return constraintName;
 47  
     }
 48  
 
 49  
     public void setConstraintName(String constraintName) {
 50  3
         this.constraintName = constraintName;
 51  3
     }
 52  
 
 53  
     public SqlStatement[] generateStatements(Database database) {
 54  
 
 55  
         // todo if (database instanceof SQLiteDatabase) {
 56  
         // // return special statements for SQLite databases
 57  
         // return generateStatementsForSQLiteDatabase(database);
 58  
         // }
 59  
 
 60  46
         return new SqlStatement[] { new DropPrimaryKeyStatement(
 61  
                 getSchemaName() == null ? database.getDefaultSchemaName() : getSchemaName(), getTableName(),
 62  
                 getConstraintName()), };
 63  
     }
 64  
 
 65  
     private SqlStatement[] generateStatementsForSQLiteDatabase(Database database) {
 66  
 
 67  
         // SQLite does not support this ALTER TABLE operation until now.
 68  
         // For more information see: http://www.sqlite.org/omitted.html.
 69  
         // This is a small work around...
 70  
 
 71  
         // Note: The attribute "constraintName" is used to pass the column
 72  
         // name instead of the constraint name.
 73  
 
 74  0
         List<SqlStatement> statements = new ArrayList<SqlStatement>();
 75  
 
 76  
         // define alter table logic
 77  0
         AlterTableVisitor rename_alter_visitor = new AlterTableVisitor() {
 78  
             public ColumnConfig[] getColumnsToAdd() {
 79  0
                 return new ColumnConfig[0];
 80  
             }
 81  
 
 82  
             public boolean copyThisColumn(ColumnConfig column) {
 83  0
                 return true;
 84  
             }
 85  
 
 86  
             public boolean createThisColumn(ColumnConfig column) {
 87  0
                 if (column.getName().equals(getConstraintName())) {
 88  0
                     column.getConstraints().setPrimaryKey(false);
 89  
                 }
 90  0
                 return true;
 91  
             }
 92  
 
 93  
             public boolean createThisIndex(Index index) {
 94  0
                 return true;
 95  
             }
 96  
         };
 97  
 
 98  
         try {
 99  
             // alter table
 100  0
             statements.addAll(SQLiteDatabase.getAlterTableStatements(rename_alter_visitor, database, getSchemaName(),
 101  
                     getTableName()));
 102  0
         } catch (Exception e) {
 103  0
             e.printStackTrace();
 104  0
         }
 105  
 
 106  0
         return statements.toArray(new SqlStatement[statements.size()]);
 107  
     }
 108  
 
 109  
     public String getConfirmationMessage() {
 110  1
         return "Primary key dropped from " + getTableName();
 111  
     }
 112  
 }