|
| 1 | +import Foundation |
| 2 | +import GRDB |
| 3 | + |
| 4 | +// TODO: remove ignore when we start using this |
| 5 | +// periphery: ignore |
| 6 | +struct V001InitialSchema { |
| 7 | + // This migration is under development and not released yet. |
| 8 | + // It's still open for modification, until we ship. |
| 9 | + // TODO: Mark this as final when we enable the pointOfSaleLocalCatalogi1 feature flag |
| 10 | + |
| 11 | + static func migrate(_ db: Database) throws { |
| 12 | + try createSiteTable(db) |
| 13 | + try createProductTable(db) |
| 14 | + try createProductAttributeTable(db) |
| 15 | + try createProductImageTable(db) |
| 16 | + try createProductVariationTable(db) |
| 17 | + try createProductVariationAttributeTable(db) |
| 18 | + try createProductVariationImageTable(db) |
| 19 | + } |
| 20 | + |
| 21 | + static func createSiteTable(_ db: Database) throws { |
| 22 | + try db.create(table: "site") { siteTable in |
| 23 | + siteTable.primaryKey("id", .integer).notNull() |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + static func createProductTable(_ db: Database) throws { |
| 28 | + // Note that it's best to use strings for names in the database |
| 29 | + // not derive them from a Swift class. |
| 30 | + // https://swiftpackageindex.com/groue/grdb.swift/v7.6.1/documentation/grdb/migrations#Good-Practices-for-Defining-Migrations |
| 31 | + try db.create(table: "product") { productTable in |
| 32 | + productTable.primaryKey("id", .integer).notNull() |
| 33 | + productTable.belongsTo("site", onDelete: .cascade).notNull() |
| 34 | + |
| 35 | + productTable.column("name", .text).notNull() |
| 36 | + productTable.column("productTypeKey", .text).notNull() |
| 37 | + |
| 38 | + productTable.column("fullDescription", .text) |
| 39 | + productTable.column("shortDescription", .text) |
| 40 | + |
| 41 | + productTable.column("sku", .text) |
| 42 | + productTable.column("globalUniqueID", .text) |
| 43 | + productTable.column("price", .text).notNull() |
| 44 | + |
| 45 | + productTable.column("downloadable", .boolean).notNull() |
| 46 | + |
| 47 | + productTable.column("parentID", .integer).notNull() |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + private static func createProductAttributeTable(_ db: Database) throws { |
| 52 | + try db.create(table: "productAttribute") { productAttributeTable in |
| 53 | + // This table holds local product attributes only. Global attributes belong to a site. |
| 54 | + productAttributeTable.autoIncrementedPrimaryKey("id").notNull() |
| 55 | + productAttributeTable.belongsTo("product", onDelete: .cascade).notNull() |
| 56 | + |
| 57 | + productAttributeTable.column("name", .text).notNull() |
| 58 | + productAttributeTable.column("position", .integer).notNull() |
| 59 | + productAttributeTable.column("visible", .boolean).notNull() |
| 60 | + productAttributeTable.column("variation", .boolean).notNull() |
| 61 | + productAttributeTable.column("options", .jsonText).notNull() |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private static func createProductImageTable(_ db: Database) throws { |
| 66 | + try db.create(table: "productImage") { productImageTable in |
| 67 | + productImageTable.primaryKey("id", .integer).notNull() |
| 68 | + productImageTable.belongsTo("product", onDelete: .cascade).notNull() |
| 69 | + |
| 70 | + productImageTable.column("dateCreated", .datetime).notNull() |
| 71 | + productImageTable.column("dateModified", .datetime) |
| 72 | + |
| 73 | + productImageTable.column("src", .text).notNull() |
| 74 | + productImageTable.column("name", .text) |
| 75 | + productImageTable.column("alt", .text) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private static func createProductVariationTable(_ db: Database) throws { |
| 80 | + try db.create(table: "productVariation") { productVariationTable in |
| 81 | + productVariationTable.primaryKey("id", .integer).notNull() |
| 82 | + productVariationTable.belongsTo("site", onDelete: .cascade).notNull() |
| 83 | + productVariationTable.belongsTo("product", onDelete: .cascade).notNull() |
| 84 | + |
| 85 | + productVariationTable.column("sku", .text) |
| 86 | + productVariationTable.column("globalUniqueID", .text) |
| 87 | + productVariationTable.column("price", .text).notNull() |
| 88 | + |
| 89 | + productVariationTable.column("downloadable", .boolean).notNull() |
| 90 | + |
| 91 | + productVariationTable.column("fullDescription", .text) |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private static func createProductVariationAttributeTable(_ db: Database) throws { |
| 96 | + try db.create(table: "productVariationAttribute") { productVariationAttributeTable in |
| 97 | + // This table holds local variation attributes only. Global attributes belong to a site. |
| 98 | + productVariationAttributeTable.autoIncrementedPrimaryKey("id").notNull() |
| 99 | + productVariationAttributeTable.belongsTo("productVariation", onDelete: .cascade).notNull() |
| 100 | + |
| 101 | + productVariationAttributeTable.column("name", .text).notNull() |
| 102 | + productVariationAttributeTable.column("option", .text).notNull() |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private static func createProductVariationImageTable(_ db: Database) throws { |
| 107 | + try db.create(table: "productVariationImage") { productVariationImageTable in |
| 108 | + productVariationImageTable.primaryKey("id", .integer).notNull() |
| 109 | + productVariationImageTable.belongsTo("productVariation").notNull() |
| 110 | + |
| 111 | + productVariationImageTable.column("dateCreated", .datetime).notNull() |
| 112 | + productVariationImageTable.column("dateModified", .datetime) |
| 113 | + |
| 114 | + productVariationImageTable.column("src", .text).notNull() |
| 115 | + productVariationImageTable.column("name", .text) |
| 116 | + productVariationImageTable.column("alt", .text) |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments