@@ -43,7 +43,7 @@ public class SchemaChanger: CustomStringConvertible {
4343
4444 public enum Operation {
4545 case addColumn( ColumnDefinition )
46- case addIndex( IndexDefinition )
46+ case addIndex( IndexDefinition , ifNotExists : Bool )
4747 case dropColumn( String )
4848 case renameColumn( String , String )
4949 case renameTable( String )
@@ -54,12 +54,8 @@ public class SchemaChanger: CustomStringConvertible {
5454 switch self {
5555 case . addColumn( let definition) :
5656 return " ALTER TABLE \( table. quote ( ) ) ADD COLUMN \( definition. toSQL ( ) ) "
57- case . addIndex( let definition) :
58- let unique = definition. unique ? " UNIQUE " : " "
59- let columns = definition. columns. joined ( separator: " , " )
60- let `where` = definition. where. map { " WHERE " + $0 } ?? " "
61-
62- return " CREATE \( unique) INDEX \( definition. name) ON \( definition. table) ( \( columns) ) \( `where`) "
57+ case . addIndex( let definition, let ifNotExists) :
58+ return definition. toSQL ( ifNotExists: ifNotExists)
6359 case . renameColumn( let from, let to) where SQLiteFeature . renameColumn. isSupported ( by: version) :
6460 return " ALTER TABLE \( table. quote ( ) ) RENAME COLUMN \( from. quote ( ) ) TO \( to. quote ( ) ) "
6561 case . dropColumn( let column) where SQLiteFeature . dropColumn. isSupported ( by: version) :
@@ -152,7 +148,7 @@ public class SchemaChanger: CustomStringConvertible {
152148 precondition ( !columnDefinitions. isEmpty)
153149 return [
154150 . createTable( columns: columnDefinitions, ifNotExists: ifNotExists)
155- ] + indexDefinitions. map { . addIndex( $0) }
151+ ] + indexDefinitions. map { . addIndex( $0, ifNotExists : ifNotExists ) }
156152 }
157153
158154 private func columnName< T> ( for expression: Expression < T > ) -> String {
0 commit comments