Skip to content

Commit 4cc8b89

Browse files
foscomputerservicestanner0101
authored andcommitted
Add delete column support (#217)
* Add delete column support Added deleteColumns to MySQLAlterTable This change allows columns to be deleted (DROP). I will build on this with a pull request to fluent-mysql to allow MySQLDatabase:SchemaSupporting to bind FluentMySQLSchema.deleteColumns and thus allowing full column ADD/DROP support. * Fixed test class name Aligned MySQLMigrationTests name with the file name Sadly only caught this in Linux build… * DROP column refinements Updates as per code review. * Changed DROP specification to include only the column name Since MySql 8.0 will not accept the table name in the column name specification of DROP [COLUMN] (where as 5.7 would) I changed the code to only include the identifier and not the table name.
1 parent 70f65ef commit 4cc8b89

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

Sources/MySQL/SQL/MySQLAlterTable.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ public struct MySQLAlterTable: SQLAlterTable {
33
/// See `SQLAlterTable`.
44
public typealias ColumnDefinition = MySQLColumnDefinition
55

6+
/// See `SQLAlterTable`.
7+
public typealias ColumnIdentifier = MySQLColumnIdentifier
8+
69
/// See `SQLAlterTable`.
710
public typealias TableIdentifier = MySQLTableIdentifier
811

@@ -20,6 +23,9 @@ public struct MySQLAlterTable: SQLAlterTable {
2023
/// See `SQLAlterTable`.
2124
public var columns: [ColumnDefinition]
2225

26+
/// See `FluentSQLSchema`.
27+
public var deleteColumns: [ColumnIdentifier]
28+
2329
/// See `SQLAlterTable`.
2430
public var constraints: [TableConstraint]
2531

@@ -50,6 +56,7 @@ public struct MySQLAlterTable: SQLAlterTable {
5056
public init(table: TableIdentifier) {
5157
self.table = table
5258
self.columns = []
59+
self.deleteColumns = []
5360
self.constraints = []
5461
self.columnPositions = [:]
5562
}
@@ -67,7 +74,10 @@ public struct MySQLAlterTable: SQLAlterTable {
6774
return sql
6875
}
6976
} + constraints.map { "ADD " + $0.serialize(&binds) }
70-
sql.append(actions.joined(separator: ", "))
77+
let deleteActions = deleteColumns.map {
78+
return "DROP " + $0.identifier.serialize(&binds)
79+
}
80+
sql.append((actions + deleteActions).joined(separator: ", "))
7181
return sql.joined(separator: " ")
7282
}
7383
}

Tests/MySQLTests/MySQLTests.swift

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,69 @@ class MySQLTests: XCTestCase {
362362
}
363363
}
364364

365+
func testDropOneColumn() throws {
366+
let client = try MySQLConnection.makeTest()
367+
defer { client.close(done: nil) }
368+
let dropResults = try client.simpleQuery("DROP TABLE IF EXISTS foos;").wait()
369+
XCTAssertEqual(dropResults.count, 0)
370+
let createResults = try client.simpleQuery("CREATE TABLE foos (id INT SIGNED, name VARCHAR(64));").wait()
371+
XCTAssertEqual(createResults.count, 0)
372+
let insertResults = try client.raw("INSERT INTO foos VALUES (?, ?);").bind(-1).bind("vapor").all().wait()
373+
XCTAssertEqual(insertResults.count, 0)
374+
375+
let preSelectResults = try client.raw("SELECT * FROM foos;").all().wait()
376+
XCTAssertEqual(preSelectResults.count, 1)
377+
XCTAssertEqual(preSelectResults[0].count, 2) // 2 columns
378+
379+
let tableId = MySQLTableIdentifier(stringLiteral: "foos");
380+
var migration = MySQLAlterTable(table: tableId)
381+
migration.deleteColumns = [
382+
MySQLColumnIdentifier.column(MySQLTableIdentifier(stringLiteral: "foos"), MySQLIdentifier.init("name"))
383+
]
384+
385+
var binds: [Encodable] = []
386+
let sql = migration.serialize(&binds)
387+
let migrationResults = try client.raw(sql).all().wait()
388+
XCTAssertEqual(migrationResults.count, 0)
389+
390+
let postSelectResults = try client.raw("SELECT * FROM foos;").all().wait()
391+
XCTAssertEqual(postSelectResults.count, 1)
392+
XCTAssertEqual(postSelectResults[0].count, 1) // 1 column
393+
XCTAssertEqual(postSelectResults[0].keys.first!, MySQLColumn(table: "foos", name: "id"))
394+
}
395+
396+
func testDropColumnMultipleColumns() throws {
397+
let client = try MySQLConnection.makeTest()
398+
defer { client.close(done: nil) }
399+
let dropResults = try client.simpleQuery("DROP TABLE IF EXISTS foos;").wait()
400+
XCTAssertEqual(dropResults.count, 0)
401+
let createResults = try client.simpleQuery("CREATE TABLE foos (id INT SIGNED, name1 VARCHAR(64), name2 VARCHAR(64));").wait()
402+
XCTAssertEqual(createResults.count, 0)
403+
let insertResults = try client.raw("INSERT INTO foos VALUES (?, ?, ?);").bind(-1).bind("vapor1").bind("vapor2").all().wait()
404+
XCTAssertEqual(insertResults.count, 0)
405+
406+
let preSelectResults = try client.raw("SELECT * FROM foos;").all().wait()
407+
XCTAssertEqual(preSelectResults.count, 1)
408+
XCTAssertEqual(preSelectResults[0].count, 3) // 3 columns
409+
410+
let tableId = MySQLTableIdentifier(stringLiteral: "foos");
411+
var migration = MySQLAlterTable(table: tableId)
412+
migration.deleteColumns = [
413+
MySQLColumnIdentifier.column(MySQLTableIdentifier(stringLiteral: "foos"), MySQLIdentifier.init("name1")),
414+
MySQLColumnIdentifier.column(MySQLTableIdentifier(stringLiteral: "foos"), MySQLIdentifier.init("name2"))
415+
]
416+
417+
var binds: [Encodable] = []
418+
let sql = migration.serialize(&binds)
419+
let migrationResults = try client.raw(sql).all().wait()
420+
XCTAssertEqual(migrationResults.count, 0)
421+
422+
let postSelectResults = try client.raw("SELECT * FROM foos;").all().wait()
423+
XCTAssertEqual(postSelectResults.count, 1)
424+
XCTAssertEqual(postSelectResults[0].count, 1) // 1 column
425+
XCTAssertEqual(postSelectResults[0].keys.first!, MySQLColumn(table: "foos", name: "id"))
426+
}
427+
365428
static let allTests = [
366429
("testBenchmark", testBenchmark),
367430
("testSimpleQuery", testSimpleQuery),
@@ -383,6 +446,8 @@ class MySQLTests: XCTestCase {
383446
("testZeroLengthArray", testZeroLengthArray),
384447
("testMySQLTimeDescription", testMySQLTimeDescription),
385448
("testGH223", testGH223),
449+
("testDropOneColumn", testDropOneColumn),
450+
("testDropColumnMultipleColumns", testDropColumnMultipleColumns),
386451
]
387452
}
388453

0 commit comments

Comments
 (0)