@@ -47,7 +47,7 @@ public class SchemaChanger: CustomStringConvertible {
4747 case dropColumn( String )
4848 case renameColumn( String , String )
4949 case renameTable( String )
50- case createTable( columns: [ ColumnDefinition ] )
50+ case createTable( columns: [ ColumnDefinition ] , ifNotExists : Bool )
5151
5252 /// Returns non-nil if the operation can be executed with a simple SQL statement
5353 func toSQL( _ table: String , version: SQLiteVersion ) -> String ? {
@@ -64,8 +64,8 @@ public class SchemaChanger: CustomStringConvertible {
6464 return " ALTER TABLE \( table. quote ( ) ) RENAME COLUMN \( from. quote ( ) ) TO \( to. quote ( ) ) "
6565 case . dropColumn( let column) where SQLiteFeature . dropColumn. isSupported ( by: version) :
6666 return " ALTER TABLE \( table. quote ( ) ) DROP COLUMN \( column. quote ( ) ) "
67- case . createTable( let columns) :
68- return " CREATE TABLE \( table. quote ( ) ) ( " +
67+ case . createTable( let columns, let ifNotExists ) :
68+ return " CREATE TABLE \( ifNotExists ? " IF NOT EXISTS " : " " ) \( table. quote ( ) ) ( " +
6969 columns. map { $0. toSQL ( ) } . joined ( separator: " , " ) +
7070 " ) "
7171 default : return nil
@@ -125,9 +125,11 @@ public class SchemaChanger: CustomStringConvertible {
125125 fileprivate var indexDefinitions : [ IndexDefinition ] = [ ]
126126
127127 let name : String
128+ let ifNotExists : Bool
128129
129- init ( name: String ) {
130+ init ( name: String , ifNotExists : Bool ) {
130131 self . name = name
132+ self . ifNotExists = ifNotExists
131133 }
132134
133135 public func add( column: ColumnDefinition ) {
@@ -141,7 +143,7 @@ public class SchemaChanger: CustomStringConvertible {
141143 var operations : [ Operation ] {
142144 precondition ( !columnDefinitions. isEmpty)
143145 return [
144- . createTable( columns: columnDefinitions)
146+ . createTable( columns: columnDefinitions, ifNotExists : ifNotExists )
145147 ] + indexDefinitions. map { . addIndex( $0) }
146148 }
147149 }
@@ -181,7 +183,7 @@ public class SchemaChanger: CustomStringConvertible {
181183 }
182184
183185 public func create( table: String , ifNotExists: Bool = false , block: CreateTableDefinitionBlock ) throws {
184- let createTableDefinition = CreateTableDefinition ( name: table)
186+ let createTableDefinition = CreateTableDefinition ( name: table, ifNotExists : ifNotExists )
185187 block ( createTableDefinition)
186188
187189 for operation in createTableDefinition. operations {
0 commit comments