From b4fff574ea1a91187f39780f48e090610121e296 Mon Sep 17 00:00:00 2001 From: gibachan Date: Tue, 22 Oct 2024 08:02:16 +0900 Subject: [PATCH] Fix to prevent conflicts between database and table names --- .../Libraries/LighterGeneration/GenModel/SchemaInit.swift | 3 ++- .../EntityGenTests/ASTDatabaseStructGenerationTests.swift | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Plugins/Libraries/LighterGeneration/GenModel/SchemaInit.swift b/Plugins/Libraries/LighterGeneration/GenModel/SchemaInit.swift index a3aeb96..5199bd1 100644 --- a/Plugins/Libraries/LighterGeneration/GenModel/SchemaInit.swift +++ b/Plugins/Libraries/LighterGeneration/GenModel/SchemaInit.swift @@ -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] ?? [] @@ -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) } } diff --git a/Tests/EntityGenTests/ASTDatabaseStructGenerationTests.swift b/Tests/EntityGenTests/ASTDatabaseStructGenerationTests.swift index 89c2557..040a711 100644 --- a/Tests/EntityGenTests/ASTDatabaseStructGenerationTests.swift +++ b/Tests/EntityGenTests/ASTDatabaseStructGenerationTests.swift @@ -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") + } }