Skip to content

Commit c06d706

Browse files
committed
Add site entity to database
1 parent b9a8181 commit c06d706

File tree

2 files changed

+297
-85
lines changed

2 files changed

+297
-85
lines changed

Modules/Sources/Storage/GRDB/Migrations/V001InitialSchema.swift

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ struct V001InitialSchema {
77
// TODO: Mark this as final when we enable the pointOfSaleLocalCatalogi1 feature flag
88

99
static func migrate(_ db: Database) throws {
10+
try createSiteTable(db)
1011
try createProductTable(db)
1112
try createProductAttributeTable(db)
1213
try createProductImageTable(db)
@@ -15,15 +16,19 @@ struct V001InitialSchema {
1516
try createProductVariationImageTable(db)
1617
}
1718

19+
static func createSiteTable(_ db: Database) throws {
20+
try db.create(table: "site") { siteTable in
21+
siteTable.primaryKey("id", .integer).notNull()
22+
}
23+
}
24+
1825
static func createProductTable(_ db: Database) throws {
1926
// Note that it's best to use strings for names in the database
2027
// not derive them from a Swift class.
2128
// https://swiftpackageindex.com/groue/grdb.swift/v7.6.1/documentation/grdb/migrations#Good-Practices-for-Defining-Migrations
2229
try db.create(table: "product") { productTable in
23-
productTable.primaryKey(["siteID", "productID"])
24-
25-
productTable.column("siteID", .integer).notNull()
26-
productTable.column("productID", .integer).notNull()
30+
productTable.primaryKey("id", .integer).notNull()
31+
productTable.belongsTo("site", onDelete: .cascade).notNull()
2732

2833
productTable.column("name", .text).notNull()
2934
productTable.column("productTypeKey", .text).notNull()
@@ -43,92 +48,70 @@ struct V001InitialSchema {
4348

4449
private static func createProductAttributeTable(_ db: Database) throws {
4550
try db.create(table: "productAttribute") { productAttributeTable in
46-
productAttributeTable.primaryKey(["siteID", "attributeID"])
47-
48-
productAttributeTable.column("siteID", .integer).notNull()
49-
productAttributeTable.column("attributeID", .integer).notNull()
50-
productAttributeTable.column("productID", .integer).notNull()
51+
// This table holds local product attributes only. Global attributes belong to a site.
52+
productAttributeTable.autoIncrementedPrimaryKey("id").notNull()
53+
productAttributeTable.belongsTo("product", onDelete: .cascade).notNull()
5154

5255
productAttributeTable.column("name", .text).notNull()
5356
productAttributeTable.column("position", .integer).notNull()
5457
productAttributeTable.column("visible", .boolean).notNull()
5558
productAttributeTable.column("variation", .boolean).notNull()
5659
productAttributeTable.column("options", .jsonText).notNull()
57-
58-
productAttributeTable.foreignKey(["siteID", "productID"], references: "product")
5960
}
6061
}
6162

6263
private static func createProductImageTable(_ db: Database) throws {
6364
try db.create(table: "productImage") { productImageTable in
64-
productImageTable.primaryKey(["siteID", "imageID"])
65-
66-
productImageTable.column("siteID", .integer).notNull()
67-
productImageTable.column("imageID", .integer).notNull()
68-
productImageTable.column("productID", .integer).notNull()
65+
productImageTable.primaryKey("id", .integer).notNull()
66+
productImageTable.belongsTo("product", onDelete: .cascade).notNull()
6967

7068
productImageTable.column("dateCreated", .datetime).notNull()
7169
productImageTable.column("dateModified", .datetime)
7270

7371
productImageTable.column("src", .text).notNull()
7472
productImageTable.column("name", .text)
7573
productImageTable.column("alt", .text)
76-
77-
productImageTable.foreignKey(["siteID", "productID"], references: "product")
7874
}
7975
}
8076

8177
private static func createProductVariationTable(_ db: Database) throws {
8278
try db.create(table: "productVariation") { productVariationTable in
83-
productVariationTable.primaryKey(["siteID", "productVariationID"])
84-
85-
productVariationTable.column("siteID", .integer).notNull()
86-
productVariationTable.column("productVariationID", .integer).notNull()
87-
productVariationTable.column("productID", .integer).notNull()
79+
productVariationTable.primaryKey("id", .integer).notNull()
80+
productVariationTable.belongsTo("site", onDelete: .cascade).notNull()
81+
productVariationTable.belongsTo("product", onDelete: .cascade).notNull()
8882

8983
productVariationTable.column("sku", .text)
9084
productVariationTable.column("globalUniqueID", .text)
9185
productVariationTable.column("price", .text).notNull()
9286

9387
productVariationTable.column("downloadable", .boolean).notNull()
9488

95-
productVariationTable.column("description", .text)
96-
97-
productVariationTable.foreignKey(["siteID", "productID"], references: "product")
89+
productVariationTable.column("fullDescription", .text)
9890
}
9991
}
10092

10193
private static func createProductVariationAttributeTable(_ db: Database) throws {
10294
try db.create(table: "productVariationAttribute") { productVariationAttributeTable in
103-
productVariationAttributeTable.primaryKey(["siteID", "attributeID"])
104-
105-
productVariationAttributeTable.column("siteID", .integer).notNull()
106-
productVariationAttributeTable.column("productVariationID", .integer).notNull()
107-
productVariationAttributeTable.column("attributeID", .integer).notNull()
95+
// This table holds local variation attributes only. Global attributes belong to a site.
96+
productVariationAttributeTable.autoIncrementedPrimaryKey("id").notNull()
97+
productVariationAttributeTable.belongsTo("productVariation", onDelete: .cascade).notNull()
10898

10999
productVariationAttributeTable.column("name", .text).notNull()
110100
productVariationAttributeTable.column("option", .text).notNull()
111-
112-
productVariationAttributeTable.foreignKey(["siteID", "productVariationID"], references: "productVariation")
113101
}
114102
}
115103

116104
private static func createProductVariationImageTable(_ db: Database) throws {
117105
try db.create(table: "productVariationImage") { productVariationImageTable in
118-
productVariationImageTable.primaryKey(["siteID", "imageID"])
119-
120-
productVariationImageTable.column("siteID", .integer).notNull()
121-
productVariationImageTable.column("imageID", .integer).notNull()
122-
productVariationImageTable.column("productVariationID", .integer).notNull()
106+
productVariationImageTable.primaryKey("id", .integer).notNull()
107+
productVariationImageTable.belongsTo("productVariation").notNull()
123108

124109
productVariationImageTable.column("dateCreated", .datetime).notNull()
125110
productVariationImageTable.column("dateModified", .datetime)
126111

127112
productVariationImageTable.column("src", .text).notNull()
128113
productVariationImageTable.column("name", .text)
129114
productVariationImageTable.column("alt", .text)
130-
131-
productVariationImageTable.foreignKey(["siteID", "productVariationID"], references: "productVariation")
132115
}
133116
}
134117
}

0 commit comments

Comments
 (0)