Skip to content

Commit

Permalink
Merge pull request #36 from gibachan/fix-database-name-conflict
Browse files Browse the repository at this point in the history
Fix to prevent conflicts between database and table names
  • Loading branch information
helje5 authored Oct 22, 2024
2 parents cc09c1d + b4fff57 commit 28450e5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public extension DatabaseInfo {
var entities = [ EntityInfo ]()
entities.reserveCapacity(schema.tables.count + schema.views.count)

let dbName = schema.tables.contains(where: { $0.name == name }) ? "\(name)DB" : name
for table in schema.tables {
let isWithoutRowID = table.isTableWithoutRowID
let indices = schema.indices [table.name] ?? []
Expand Down Expand Up @@ -56,7 +57,7 @@ public extension DatabaseInfo {
entities.append(entity)
}

self.init(name: name, userVersion: schema.userVersion, entities: entities)
self.init(name: dbName, userVersion: schema.userVersion, entities: entities)
}
}

Expand Down
7 changes: 7 additions & 0 deletions Tests/EntityGenTests/ASTDatabaseStructGenerationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,11 @@ final class ASTDatabaseStructGenerationTests: XCTestCase {
XCTAssertTrue(source.contains(
"[ Person.self, Address.self, AFancyTestTable.self ]"))
}

func testResolveConflictedDatabaseName() throws {
let name = try XCTUnwrap(Fixtures.addressSchema.tables.first?.name)
let dbInfo = DatabaseInfo(name: name, schema: Fixtures.addressSchema)

XCTAssertEqual(dbInfo.name, "\(name)DB")
}
}

0 comments on commit 28450e5

Please sign in to comment.