Skip to content

Commit f9ea674

Browse files
authored
fix: Quote table names in CreateTableStatement (#1233)
* fix: Quote table names in CreateTableStatement * test: Correct model count assertion
1 parent c60bea0 commit f9ea674

File tree

6 files changed

+92
-16
lines changed

6 files changed

+92
-16
lines changed

Amplify.xcodeproj/project.pbxproj

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,8 @@
557557
FA31529C233D645400DE78E7 /* StorageListRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA31529B233D645400DE78E7 /* StorageListRequest.swift */; };
558558
FA31529E233D645800DE78E7 /* StorageUploadDataRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA31529D233D645800DE78E7 /* StorageUploadDataRequest.swift */; };
559559
FA3152A0233D645B00DE78E7 /* StorageRemoveRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA31529F233D645A00DE78E7 /* StorageRemoveRequest.swift */; };
560+
FA32C428264F35390057272F /* Transaction.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA32C427264F35390057272F /* Transaction.swift */; };
561+
FA32C42A264F35420057272F /* Transaction+Schema.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA32C429264F35410057272F /* Transaction+Schema.swift */; };
560562
FA47B8362350C2D60031A0E3 /* AutoUnsubscribeOperationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA47B8352350C2D60031A0E3 /* AutoUnsubscribeOperationTests.swift */; };
561563
FA47B8382350C58B0031A0E3 /* AutoUnsubscribeHubListenToOperationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA47B8372350C58B0031A0E3 /* AutoUnsubscribeHubListenToOperationTests.swift */; };
562564
FA4A955F239ADEBD008E876E /* MockResponder.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA4A955E239ADEBD008E876E /* MockResponder.swift */; };
@@ -1524,6 +1526,8 @@
15241526
FA31529D233D645800DE78E7 /* StorageUploadDataRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StorageUploadDataRequest.swift; sourceTree = "<group>"; };
15251527
FA31529F233D645A00DE78E7 /* StorageRemoveRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StorageRemoveRequest.swift; sourceTree = "<group>"; };
15261528
FA317107232AE8DF009BC140 /* SerialDispatcherPerformanceTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SerialDispatcherPerformanceTests.swift; sourceTree = "<group>"; };
1529+
FA32C427264F35390057272F /* Transaction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Transaction.swift; sourceTree = "<group>"; };
1530+
FA32C429264F35410057272F /* Transaction+Schema.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Transaction+Schema.swift"; sourceTree = "<group>"; };
15271531
FA47B8352350C2D60031A0E3 /* AutoUnsubscribeOperationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AutoUnsubscribeOperationTests.swift; sourceTree = "<group>"; };
15281532
FA47B8372350C58B0031A0E3 /* AutoUnsubscribeHubListenToOperationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AutoUnsubscribeHubListenToOperationTests.swift; sourceTree = "<group>"; };
15291533
FA4A955E239ADEBD008E876E /* MockResponder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockResponder.swift; sourceTree = "<group>"; };
@@ -2921,17 +2925,14 @@
29212925
B952182D237E21B900F53237 /* Models */ = {
29222926
isa = PBXGroup;
29232927
children = (
2928+
B952182F237E21B900F53237 /* schema.graphql */,
29242929
FAF512AD23986791001ADF4E /* AmplifyModels.swift */,
29252930
214F49CB24898E8400DA616C /* Article.swift */,
29262931
214F49CC24898E8500DA616C /* Article+Schema.swift */,
2927-
B9FAA10C23878BD6009414B4 /* Associations */,
2928-
217D5E992577F95D009F0639 /* Collection */,
29292932
B9521830237E21B900F53237 /* Comment.swift */,
29302933
B952182E237E21B900F53237 /* Comment+Schema.swift */,
29312934
21AD4255249BFFDF0016FE95 /* DeprecatedTodo.swift */,
2932-
6BE9D73025A67F5A00AB5C9A /* M2MPostEditorUser */,
29332935
FAA2E8BB239FFC7700E420EA /* MockModels.swift */,
2934-
216E45E9248E91420035E3CE /* NonModel */,
29352936
6BEE08222533D30800133961 /* OGCScenarioBMGroupPost.swift */,
29362937
6BEE08232533D30800133961 /* OGCScenarioBMGroupPost+Schema.swift */,
29372938
6BEE081B2533CCFA00133961 /* OGCScenarioBPost.swift */,
@@ -2942,22 +2943,26 @@
29422943
B9AA09F02473CA29000E6FBB /* PostStatus.swift */,
29432944
6B597A092565D3E50038C3E2 /* QPredGen.swift */,
29442945
6B597A082565D3E40038C3E2 /* QPredGen+Schema.swift */,
2945-
6B7743D625906F7E001469F5 /* Restaurant */,
2946-
21A9051E2616442000EC141D /* Scalar */,
2947-
6B9F7C542526864800F1F71C /* ScenarioATest6Post.swift */,
2948-
6B9F7C532526864800F1F71C /* ScenarioATest6Post+Schema.swift */,
29492946
762167CB261542F70033FCD2 /* Record.swift */,
29502947
762167D42615435C0033FCD2 /* Record+Schema.swift */,
29512948
762C978426210F6400798FA3 /* RecordCover.swift */,
29522949
762C978D26210FF100798FA3 /* RecordCover+Schema.swift */,
2953-
B952182F237E21B900F53237 /* schema.graphql */,
2954-
6BE9D6E725A6620B00AB5C9A /* TeamProject */,
2950+
6B9F7C542526864800F1F71C /* ScenarioATest6Post.swift */,
2951+
6B9F7C532526864800F1F71C /* ScenarioATest6Post+Schema.swift */,
29552952
214F49742486D8A200DA616C /* User.swift */,
29562953
214F49752486D8A200DA616C /* User+Schema.swift */,
29572954
214F49762486D8A200DA616C /* UserFollowers.swift */,
29582955
214F49712486D8A100DA616C /* UserFollowers+Schema.swift */,
29592956
214F49722486D8A200DA616C /* UserFollowing.swift */,
29602957
214F49732486D8A200DA616C /* UserFollowing+Schema.swift */,
2958+
B9FAA10C23878BD6009414B4 /* Associations */,
2959+
217D5E992577F95D009F0639 /* Collection */,
2960+
6BE9D73025A67F5A00AB5C9A /* M2MPostEditorUser */,
2961+
216E45E9248E91420035E3CE /* NonModel */,
2962+
FA32C42B264F35480057272F /* ReservedWords */,
2963+
6B7743D625906F7E001469F5 /* Restaurant */,
2964+
21A9051E2616442000EC141D /* Scalar */,
2965+
6BE9D6E725A6620B00AB5C9A /* TeamProject */,
29612966
);
29622967
path = Models;
29632968
sourceTree = "<group>";
@@ -3231,6 +3236,15 @@
32313236
path = Operation/Request;
32323237
sourceTree = "<group>";
32333238
};
3239+
FA32C42B264F35480057272F /* ReservedWords */ = {
3240+
isa = PBXGroup;
3241+
children = (
3242+
FA32C427264F35390057272F /* Transaction.swift */,
3243+
FA32C429264F35410057272F /* Transaction+Schema.swift */,
3244+
);
3245+
path = ReservedWords;
3246+
sourceTree = "<group>";
3247+
};
32343248
FA4E730F23282A3D003B8EEB /* Internal */ = {
32353249
isa = PBXGroup;
32363250
children = (
@@ -5374,6 +5388,7 @@
53745388
FACA361D2327FC84000E74F6 /* MockAPICategoryPlugin.swift in Sources */,
53755389
21A905602616484A00EC141D /* Scalar+Equatable.swift in Sources */,
53765390
B9FAA11023878C5E009414B4 /* UserProfile.swift in Sources */,
5391+
FA32C42A264F35420057272F /* Transaction+Schema.swift in Sources */,
53775392
21A905322616446F00EC141D /* ListStringContainer+Schema.swift in Sources */,
53785393
6BE9D6EE25A6622000AB5C9A /* Team.swift in Sources */,
53795394
B9AA09F12473CA29000E6FBB /* PostStatus.swift in Sources */,
@@ -5413,6 +5428,7 @@
54135428
21FDBB622587D7A30086FCDC /* Post6.swift in Sources */,
54145429
217D5EBA2577F9DF009F0639 /* Post4+Schema.swift in Sources */,
54155430
21A7C90225ACC4D1004355D6 /* MockDataStoreResponders.swift in Sources */,
5431+
FA32C428264F35390057272F /* Transaction.swift in Sources */,
54165432
217D5EBD2577F9DF009F0639 /* Post5.swift in Sources */,
54175433
6B7743E425906FD3001469F5 /* Restaurant.swift in Sources */,
54185434
FA1846EE23998E44009B9D01 /* MockAPIResponders.swift in Sources */,

AmplifyPlugins/DataStore/AWSDataStoreCategoryPlugin/Storage/SQLite/SQLStatement+CreateTable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ struct CreateTableStatement: SQLStatement {
1919

2020
var stringValue: String {
2121
let name = modelSchema.name
22-
var statement = "create table if not exists \(name) (\n"
22+
var statement = #"create table if not exists "\#(name)" (\#n"#
2323

2424
let columns = modelSchema.columns
2525
let foreignKeys = modelSchema.foreignKeys

AmplifyPlugins/DataStore/AWSDataStoreCategoryPluginIntegrationTests/DataStoreHubEventsTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class DataStoreHubEventTests: HubEventsIntegrationTestBase {
6262
XCTFail("Failed to cast payload data as SyncQueriesStartedEvent")
6363
return
6464
}
65-
XCTAssertEqual(syncQueriesStartedEvent.models.count, 16)
65+
XCTAssertEqual(syncQueriesStartedEvent.models.count, 21)
6666
syncQueriesStartedReceived.fulfill()
6767
}
6868

AmplifyPlugins/DataStore/AWSDataStoreCategoryPluginTests/Core/SQLStatementTests.swift

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,22 @@ class SQLStatementTests: XCTestCase {
3131
ModelRegistry.register(modelType: Book.self)
3232
ModelRegistry.register(modelType: BookAuthor.self)
3333

34+
// Reserved word
35+
ModelRegistry.register(modelType: Transaction.self)
3436
}
3537

3638
// MARK: - Create Table
3739

40+
func testCreateTableWithReservedWord() {
41+
let statement = CreateTableStatement(modelSchema: Transaction.schema)
42+
let expectedStatement = """
43+
create table if not exists "Transaction" (
44+
"id" text primary key not null
45+
);
46+
"""
47+
XCTAssertEqual(statement.stringValue, expectedStatement)
48+
}
49+
3850
/// - Given: a `Model` type
3951
/// - When:
4052
/// - the model is of type `Post`
@@ -44,7 +56,7 @@ class SQLStatementTests: XCTestCase {
4456
func testCreateTableFromSimpleModel() {
4557
let statement = CreateTableStatement(modelSchema: Post.schema)
4658
let expectedStatement = """
47-
create table if not exists Post (
59+
create table if not exists "Post" (
4860
"id" text primary key not null,
4961
"content" text not null,
5062
"createdAt" text not null,
@@ -68,7 +80,7 @@ class SQLStatementTests: XCTestCase {
6880
func testCreateTableFromModelWithForeignKey() {
6981
let statement = CreateTableStatement(modelSchema: Comment.schema)
7082
let expectedStatement = """
71-
create table if not exists Comment (
83+
create table if not exists "Comment" (
7284
"id" text primary key not null,
7385
"content" text not null,
7486
"createdAt" text not null,
@@ -91,7 +103,7 @@ class SQLStatementTests: XCTestCase {
91103
func testCreateTableFromModelWithOneToOneForeignKey() {
92104
let statement = CreateTableStatement(modelSchema: UserProfile.schema)
93105
let expectedStatement = """
94-
create table if not exists UserProfile (
106+
create table if not exists "UserProfile" (
95107
"id" text primary key not null,
96108
"accountId" text not null unique,
97109
foreign key("accountId") references UserAccount("id")
@@ -112,7 +124,7 @@ class SQLStatementTests: XCTestCase {
112124
func testCreateTableFromManyToManyAssociationModel() {
113125
let statement = CreateTableStatement(modelSchema: BookAuthor.schema)
114126
let expectedStatement = """
115-
create table if not exists BookAuthor (
127+
create table if not exists "BookAuthor" (
116128
"id" text primary key not null,
117129
"authorId" text not null,
118130
"bookId" text not null,
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
// swiftlint:disable all
9+
import Foundation
10+
import Amplify
11+
12+
extension Transaction {
13+
// MARK: - CodingKeys
14+
public enum CodingKeys: String, ModelKey {
15+
case id
16+
}
17+
18+
public static let keys = CodingKeys.self
19+
// MARK: - ModelSchema
20+
21+
public static let schema = defineSchema { model in
22+
let transaction = Transaction.keys
23+
24+
model.pluralName = "Transactions"
25+
26+
model.fields(
27+
.id()
28+
)
29+
}
30+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
// swiftlint:disable all
9+
import Foundation
10+
import Amplify
11+
12+
public struct Transaction: Model {
13+
public let id: String
14+
15+
public init(id: String = UUID().uuidString) {
16+
self.id = id
17+
}
18+
}

0 commit comments

Comments
 (0)