Clover Coverage Report - Liquibase Core 2.0.3-SNAPSHOT
Coverage timestamp: Sat Aug 6 2011 11:33:15 EDT
../../../img/srcFileCovDistChart5.png 35% of files have more coverage
36   145   23   1.89
6   103   0.64   19
19     1.21  
1    
 
  RenameColumnChange       Line # 21 36 0% 23 35 42.6% 0.4262295
 
  (8)
 
1    package liquibase.change.core;
2   
3    import liquibase.change.AbstractChange;
4    import liquibase.change.Change;
5    import liquibase.change.ChangeMetaData;
6    import liquibase.change.ColumnConfig;
7    import liquibase.database.Database;
8    import liquibase.database.core.SQLiteDatabase;
9    import liquibase.database.core.SQLiteDatabase.AlterTableVisitor;
10    import liquibase.database.structure.Index;
11    import liquibase.statement.SqlStatement;
12    import liquibase.statement.core.RenameColumnStatement;
13    import liquibase.util.StringUtils;
14   
15    import java.util.ArrayList;
16    import java.util.List;
17   
18    /**
19    * Renames an existing column.
20    */
 
21    public class RenameColumnChange extends AbstractChange {
22   
23    private String schemaName;
24    private String tableName;
25    private String oldColumnName;
26    private String newColumnName;
27    private String columnDataType;
28   
 
29  20 toggle public RenameColumnChange() {
30  20 super("renameColumn", "Rename Column", ChangeMetaData.PRIORITY_DEFAULT);
31    }
32   
 
33  45 toggle public String getSchemaName() {
34  45 return schemaName;
35    }
36   
 
37  7 toggle public void setSchemaName(String schemaName) {
38  7 this.schemaName = StringUtils.trimToNull(schemaName);
39    }
40   
 
41  44 toggle public String getTableName() {
42  44 return tableName;
43    }
44   
 
45  7 toggle public void setTableName(String tableName) {
46  7 this.tableName = tableName;
47    }
48   
 
49  44 toggle public String getOldColumnName() {
50  44 return oldColumnName;
51    }
52   
 
53  7 toggle public void setOldColumnName(String oldColumnName) {
54  7 this.oldColumnName = oldColumnName;
55    }
56   
 
57  44 toggle public String getNewColumnName() {
58  44 return newColumnName;
59    }
60   
 
61  7 toggle public void setNewColumnName(String newColumnName) {
62  7 this.newColumnName = newColumnName;
63    }
64   
 
65  44 toggle public String getColumnDataType() {
66  44 return columnDataType;
67    }
68   
 
69  0 toggle public void setColumnDataType(String columnDataType) {
70  0 this.columnDataType = columnDataType;
71    }
72   
 
73  44 toggle public SqlStatement[] generateStatements(Database database) {
74    // todo if (database instanceof SQLiteDatabase) {
75    // // return special statements for SQLite databases
76    // return generateStatementsForSQLiteDatabase(database);
77    // }
78   
79  44 return new SqlStatement[] { new RenameColumnStatement(getSchemaName() == null ? database.getDefaultSchemaName()
80    : getSchemaName(), getTableName(), getOldColumnName(), getNewColumnName(), getColumnDataType()) };
81    }
82   
 
83  0 toggle private SqlStatement[] generateStatementsForSQLiteDatabase(Database database) {
84   
85    // SQLite does not support this ALTER TABLE operation until now.
86    // For more information see: http://www.sqlite.org/omitted.html.
87    // This is a small work around...
88   
89  0 List<SqlStatement> statements = new ArrayList<SqlStatement>();
90   
91    // define alter table logic
92  0 AlterTableVisitor rename_alter_visitor = new AlterTableVisitor() {
 
93  0 toggle public ColumnConfig[] getColumnsToAdd() {
94  0 return new ColumnConfig[0];
95    }
96   
 
97  0 toggle public boolean copyThisColumn(ColumnConfig column) {
98  0 return true;
99    }
100   
 
101  0 toggle public boolean createThisColumn(ColumnConfig column) {
102  0 if (column.getName().equals(getOldColumnName())) {
103  0 column.setName(getNewColumnName());
104    }
105  0 return true;
106    }
107   
 
108  0 toggle public boolean createThisIndex(Index index) {
109  0 if (index.getColumns().contains(getOldColumnName())) {
110  0 index.getColumns().remove(getOldColumnName());
111  0 index.getColumns().add(getNewColumnName());
112    }
113  0 return true;
114    }
115    };
116   
117  0 try {
118    // alter table
119  0 statements.addAll(SQLiteDatabase.getAlterTableStatements(rename_alter_visitor, database, getSchemaName(),
120    getTableName()));
121    } catch (Exception e) {
122  0 System.err.println(e);
123  0 e.printStackTrace();
124    }
125   
126  0 return statements.toArray(new SqlStatement[statements.size()]);
127    }
128   
 
129  0 toggle @Override
130    protected Change[] createInverses() {
131  0 RenameColumnChange inverse = new RenameColumnChange();
132  0 inverse.setSchemaName(getSchemaName());
133  0 inverse.setTableName(getTableName());
134  0 inverse.setOldColumnName(getNewColumnName());
135  0 inverse.setNewColumnName(getOldColumnName());
136  0 inverse.setColumnDataType(getColumnDataType());
137   
138  0 return new Change[] { inverse };
139    }
140   
 
141  1 toggle public String getConfirmationMessage() {
142  1 return "Column " + tableName + "." + oldColumnName + " renamed to " + newColumnName;
143    }
144   
145    }