@@ -47,7 +47,7 @@ public class SchemaChanger: CustomStringConvertible {
47
47
case dropColumn( String )
48
48
case renameColumn( String , String )
49
49
case renameTable( String )
50
- case createTable( columns: [ ColumnDefinition ] )
50
+ case createTable( columns: [ ColumnDefinition ] , ifNotExists : Bool )
51
51
52
52
/// Returns non-nil if the operation can be executed with a simple SQL statement
53
53
func toSQL( _ table: String , version: SQLiteVersion ) -> String ? {
@@ -64,8 +64,8 @@ public class SchemaChanger: CustomStringConvertible {
64
64
return " ALTER TABLE \( table. quote ( ) ) RENAME COLUMN \( from. quote ( ) ) TO \( to. quote ( ) ) "
65
65
case . dropColumn( let column) where SQLiteFeature . dropColumn. isSupported ( by: version) :
66
66
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 ( ) ) ( " +
69
69
columns. map { $0. toSQL ( ) } . joined ( separator: " , " ) +
70
70
" ) "
71
71
default : return nil
@@ -125,9 +125,11 @@ public class SchemaChanger: CustomStringConvertible {
125
125
fileprivate var indexDefinitions : [ IndexDefinition ] = [ ]
126
126
127
127
let name : String
128
+ let ifNotExists : Bool
128
129
129
- init ( name: String ) {
130
+ init ( name: String , ifNotExists : Bool ) {
130
131
self . name = name
132
+ self . ifNotExists = ifNotExists
131
133
}
132
134
133
135
public func add( column: ColumnDefinition ) {
@@ -141,7 +143,7 @@ public class SchemaChanger: CustomStringConvertible {
141
143
var operations : [ Operation ] {
142
144
precondition ( !columnDefinitions. isEmpty)
143
145
return [
144
- . createTable( columns: columnDefinitions)
146
+ . createTable( columns: columnDefinitions, ifNotExists : ifNotExists )
145
147
] + indexDefinitions. map { . addIndex( $0) }
146
148
}
147
149
}
@@ -181,7 +183,7 @@ public class SchemaChanger: CustomStringConvertible {
181
183
}
182
184
183
185
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 )
185
187
block ( createTableDefinition)
186
188
187
189
for operation in createTableDefinition. operations {
0 commit comments