Skip to content

add support for ALTER TABLE drop actions #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions Sources/FluentPostgreSQL/PostgreSQLDatabase+QuerySupporting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,25 +115,4 @@ extension PostgreSQLDatabase: QuerySupporting {

return conn.future(model)
}

/// See `SchemaSupporting`.
public static func schemaExecute(_ fluent: FluentPostgreSQLSchema, on conn: PostgreSQLConnection) -> Future<Void> {
let query: PostgreSQLQuery
switch fluent.statement {
case ._createTable:
var createTable: PostgreSQLCreateTable = .createTable(fluent.table)
createTable.columns = fluent.columns
createTable.tableConstraints = fluent.constraints
query = ._createTable(createTable)
case ._alterTable:
var alterTable: PostgreSQLAlterTable = .alterTable(fluent.table)
alterTable.columns = fluent.columns
alterTable.constraints = fluent.constraints
query = ._alterTable(alterTable)
case ._dropTable:
let dropTable: PostgreSQLDropTable = .dropTable(fluent.table)
query = ._dropTable(dropTable)
}
return conn.query(query).transform(to: ())
}
}
28 changes: 28 additions & 0 deletions Sources/FluentPostgreSQL/PostgreSQLDatabase+SchemaSupporting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,34 @@ extension PostgreSQLDatabase: SchemaSupporting {
/// See `SchemaSupporting`.
public typealias SchemaReferenceAction = PostgreSQLForeignKeyAction

/// See `SchemaSupporting`.
public static func schemaExecute(_ fluent: FluentPostgreSQLSchema, on conn: PostgreSQLConnection) -> Future<Void> {
let query: PostgreSQLQuery
switch fluent.statement {
case ._createTable:
var createTable: PostgreSQLCreateTable = .createTable(fluent.table)
createTable.columns = fluent.columns
createTable.tableConstraints = fluent.constraints
query = ._createTable(createTable)
case ._alterTable:
var alterTable: PostgreSQLAlterTable = .alterTable(fluent.table)
alterTable.columns = fluent.columns
alterTable.constraints = fluent.constraints
alterTable.dropActions += fluent.deleteColumns.map { .init(.column, $0.identifier) }
alterTable.dropActions += fluent.deleteConstraints.map {
guard let id = $0.identifier else {
fatalError("Cannot drop constraint without identifier: \($0).")
}
return .init(.constraint, id)
}
query = ._alterTable(alterTable)
case ._dropTable:
let dropTable: PostgreSQLDropTable = .dropTable(fluent.table)
query = ._dropTable(dropTable)
}
return conn.query(query).transform(to: ())
}

/// See `SchemaSupporting`.
public static func schemaField(for type: Any.Type, isIdentifier: Bool, _ column: PostgreSQLColumnIdentifier) -> PostgreSQLColumnDefinition {
var constraints: [PostgreSQLColumnConstraint] = []
Expand Down
53 changes: 53 additions & 0 deletions Tests/FluentPostgreSQLTests/FluentPostgreSQLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,59 @@ class FluentPostgreSQLTests: XCTestCase {
XCTAssertEqual(b.id, 1)
}

func testAlterDrop() throws {
struct A: PostgreSQLMigration {
static func prepare(on conn: PostgreSQLConnection) -> Future<Void> {
return PostgreSQLDatabase.create(Planet.self, on: conn) { builder in
builder.field(for: \.id)
}
}

static func revert(on conn: PostgreSQLConnection) -> Future<Void> {
return PostgreSQLDatabase.delete(Planet.self, on: conn)
}
}
struct B: PostgreSQLMigration {
static func prepare(on conn: PostgreSQLConnection) -> Future<Void> {
return PostgreSQLDatabase.update(Planet.self, on: conn) { builder in
builder.field(for: \.name)
builder.deleteField(for: \.id)
}
}

static func revert(on conn: PostgreSQLConnection) -> Future<Void> {
return PostgreSQLDatabase.update(Planet.self, on: conn) { builder in
builder.deleteField(for: \.name)
builder.field(for: \.id)
}
}
}
struct C: PostgreSQLMigration {
static func prepare(on conn: PostgreSQLConnection) -> Future<Void> {
return PostgreSQLDatabase.update(Planet.self, on: conn) { builder in
builder.unique(on: \.name)
}
}

static func revert(on conn: PostgreSQLConnection) -> Future<Void> {
return PostgreSQLDatabase.update(Planet.self, on: conn) { builder in
builder.deleteUnique(from: \.name)
}
}
}

let conn = try benchmarker.pool.requestConnection().wait()
conn.logger = DatabaseLogger(database: .psql, handler: PrintLogHandler())
defer { benchmarker.pool.releaseConnection(conn) }

try A.prepare(on: conn).wait()
defer { try? A.revert(on: conn).wait() }
try B.prepare(on: conn).wait()
defer { try? B.revert(on: conn).wait() }
try C.prepare(on: conn).wait()
defer { try? C.revert(on: conn).wait() }
}

static let allTests = [
("testBenchmark", testBenchmark),
("testNestedStruct", testNestedStruct),
Expand Down