Skip to content

Commit d051c95

Browse files
authored
[Core Data] Make new Core Data migration path. Remove models below 30 (#15987)
2 parents 3e89dae + 644059f commit d051c95

File tree

42 files changed

+163
-15740
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+163
-15740
lines changed

Modules/Sources/Storage/CoreData/CoreDataIterativeMigrator.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ final class CoreDataIterativeMigrator {
5959
// Find the current model used by the store.
6060
let sourceModel = try model(for: sourceMetadata)
6161

62+
// Check if we should nuke the DB entirely based on the oldest supported data model.
63+
// We will attempt to either move the merchant to the latest model version, or perform an iterative migration
64+
// The persistent store coordinator will automatically create a fresh store with the current model as needed.
65+
if shouldDestroyPersistentStore(for: sourceModel) {
66+
do {
67+
try persistentStoreCoordinator.destroyPersistentStore(at: sourceStoreURL, ofType: storeType, options: nil)
68+
DDLogInfo("[CoreDataIterativeMigrator] Database at \(sourceStoreURL) destroyed successfully.")
69+
} catch {
70+
DDLogError("[CoreDataIterativeMigrator] Database destruction failed. Error: \(error). Falling back to iterative migration.")
71+
}
72+
}
73+
6274
// Get the steps to perform the migration.
6375
let steps = try MigrationStep.steps(using: modelsInventory, source: sourceModel, target: targetModel)
6476
guard !steps.isEmpty else {
@@ -176,4 +188,18 @@ private extension CoreDataIterativeMigrator {
176188
.appendingPathComponent("migration_\(UUID().uuidString)")
177189
.appendingPathExtension("sqlite")
178190
}
191+
192+
func shouldDestroyPersistentStore(for sourceModel: NSManagedObjectModel) -> Bool {
193+
guard let sourceVersion = CoreDataMigratorUtils.findSourceVersion(for: sourceModel, in: modelsInventory) else {
194+
// If the model is not found in inventory it's from a deleted model version
195+
// The database can be destroyed for a fresh start
196+
DDLogInfo("Source model not found in available model versions. Will destroy database for fresh start.")
197+
return true
198+
}
199+
200+
// Model found in inventory, so it's a supported version. Use iterative migration
201+
DDLogInfo("Source model \(sourceVersion.name) found in available versions. Will use iterative migration.")
202+
return false
203+
}
204+
179205
}

Modules/Sources/Storage/CoreData/CoreDataManager.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ public final class CoreDataManager: StorageManagerType {
242242
}
243243

244244
/// Migrates the current persistent store to the latest data model if needed.
245-
/// - Returns: an array of debug messages for logging. Please feel free to remove when #2371 is resolved.
245+
/// - Returns: an array of debug messages for logging.
246246
private static func migrateDataModelIfNecessary(using coordinator: NSPersistentStoreCoordinator,
247247
storeURL: URL,
248248
modelsInventory: ManagedObjectModelsInventory) -> [String] {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import CoreData
2+
3+
struct CoreDataMigratorUtils {
4+
/// Finds the ModelVersion that corresponds to the given NSManagedObjectModel
5+
static func findSourceVersion(for sourceModel: NSManagedObjectModel,
6+
in modelsInventory: ManagedObjectModelsInventory) -> ManagedObjectModelsInventory.ModelVersion? {
7+
do {
8+
let allModels = try modelsInventory.models(for: modelsInventory.versions)
9+
for (index, model) in allModels.enumerated() {
10+
if model.isEqual(sourceModel) {
11+
return modelsInventory.versions[index]
12+
}
13+
}
14+
} catch {
15+
DDLogError("[CoreDataMigratorUtils] Error loading models for version detection: \(error)")
16+
}
17+
return nil
18+
}
19+
}

Modules/Sources/Storage/Model/MIGRATIONS.md

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -459,50 +459,50 @@ This file documents changes in the WCiOS Storage data model. Please explain any
459459
- Delete `OrderStats` entity
460460
- Delete `OrderStatsItem` entity
461461

462-
## Model 29 (Release 4.7.0.0)
462+
## Model 29 (Release 4.7.0.0) - Deleted
463463
- @pmusolino 2020-06-29
464464
- Add `siteID` attribute to `ProductTag` entity
465465
- Update `ProductTag`'s `product` relationship to `products`
466466
- Update `Product`'s `tags` relationship with `nullify` as delete rule
467467
- Used mapping model: `WooCommerceModelV28toV29.xcmappingmodel` to remove product tags without `siteID`
468468

469-
## Model 28 (Release 4.5.0.0)
469+
## Model 28 (Release 4.5.0.0) - Deleted
470470
- @jaclync 2020-06-05
471471
- Add `buttonText` attribute to `Product` entity
472472

473-
## Model 27 (Release 3.9.0.1)
473+
## Model 27 (Release 3.9.0.1) - Deleted
474474
- @ecarrion 2020-03-30
475475
- Update `ProductCategory`'s `product` relationship to `products`
476476
- Add `siteID` and `parentID` to `ProductCategory` entity
477477
- Used mapping model: `WooCommerceModelV26toV27.xcmappingmodel` to remove product categories without `siteID`
478478

479-
## Model 26 (Release 3.5.0.0)
479+
## Model 26 (Release 3.5.0.0) - Deleted
480480
- @jaclync 2019-01-14
481481
- Update `Product`'s `images` relationship to be ordered
482482

483-
## Model 25 (Release 3.4.0.0)
483+
## Model 25 (Release 3.4.0.0) - Deleted
484484
- @pmusolino 2019-01-7
485485
- Add `gmtOffset` attribute to `Site` entity
486486

487-
## Model 24 (Release 3.3.0.0)
487+
## Model 24 (Release 3.3.0.0) - Deleted
488488
- @jaclync 2019-12-2
489489
- New `ProductShippingClass` entity
490490
- Add `dateOnSaleStart` attribute to `Product` entity
491491
- Add `dateOnSaleEnd` attribute to `Product` entity
492492
- New `TaxClass` entity
493493

494-
## Model 23 (Release 3.2.0.0)
494+
## Model 23 (Release 3.2.0.0) - Deleted
495495
- @jaclync 2019-11-15
496496
- New `Attribute` entity
497497
- New `ProductVariation` entity
498498
- New `Product.productVariations` relationship
499499

500-
## Model 22 (Release 3.1.0.0)
500+
## Model 22 (Release 3.1.0.0) - Deleted
501501
- @pmusolino 2019-11-4
502502
- New `ShippingLine` entity
503503
- New `Order.shippingLines` relationship
504504

505-
## Model 21 (Release 2.9.0.0)
505+
## Model 21 (Release 2.9.0.0) - Deleted
506506
- @mindgraffiti 2019-10-11
507507
- New `OrderItemTax` entity
508508
- New `OrderItemTaxRefund` entity
@@ -512,7 +512,7 @@ This file documents changes in the WCiOS Storage data model. Please explain any
512512
- New `Refund` entity
513513
- New `Refund.items` relationship
514514

515-
## Model 20 (Release 2.8.0.0)
515+
## Model 20 (Release 2.8.0.0) - Deleted
516516
- @jaclync 2019-09-17
517517
- New `ProductSearchResults` entity
518518
- New `Product.searchResults` relationship
@@ -524,7 +524,7 @@ This file documents changes in the WCiOS Storage data model. Please explain any
524524
- New `OrderRefundCondensed` entity
525525
- New `Order.refunds` relationship
526526

527-
## Model 19 (Release 2.6.0.0)
527+
## Model 19 (Release 2.6.0.0) - Deleted
528528
- @ctarda 2019-08-21
529529
- Add `ProductReview` entity
530530

@@ -534,33 +534,33 @@ This file documents changes in the WCiOS Storage data model. Please explain any
534534
- @jaclync 2019-08-06
535535
- Add `timeRange` attribute to `OrderStatsV4` entity
536536

537-
## Model 18 (Release 2.5.0.0)
537+
## Model 18 (Release 2.5.0.0) - Deleted
538538
- @ctarda 2019-07-30
539539
- Add `OrderCount` entity
540540
- Add `OrderCountItem` entity
541541

542-
## Model 17 (Release 2.3.0.0)
542+
## Model 17 (Release 2.3.0.0) - Deleted
543543
- @ctarda 2019-07-10
544544
- Add `OrderStatsV4` entity
545545
- Add `OrderStatsV4Totals` entity
546546
- Add `OrderStatsV4Interval` entity
547547

548-
## Model 16 (Release 2.0.0.0)
548+
## Model 16 (Release 2.0.0.0) - Deleted
549549
- @mindgraffiti 2019-05-29
550550
- Add `ProductDownload` entity
551551

552-
## Model 15 (Release 1.9.0.0)
552+
## Model 15 (Release 1.9.0.0) - Deleted
553553
- @mindgraffiti 2019-05-03
554554
- Delete `ProductVariation` entity
555555
- Delete `ProductVariationAttribute` entity
556556
- Delete `ProductVariationDimensions` entity
557557
- Delete `ProductVariationImage` entity
558558

559-
## Model 14 (Release 1.8.0.0)
559+
## Model 14 (Release 1.8.0.0) - Deleted
560560
- @astralbodies 2019-04-22
561561
- New `AccountSettings` entity with `tracksOptOut` attribute.
562562

563-
## Model 13 (Release 1.6.0.0)
563+
## Model 13 (Release 1.6.0.0) - Deleted
564564
- @bummytime 2019-03-28
565565
- Added `settingGroupKey` attribute on `SiteSetting` entity
566566

@@ -570,7 +570,7 @@ This file documents changes in the WCiOS Storage data model. Please explain any
570570
- New `ProductVariationDimensions` entity
571571
- New `ProductVariationImage` entity
572572

573-
## Model 12 (Release 1.5.0.0)
573+
## Model 12 (Release 1.5.0.0) - Deleted
574574
- @bummytime 2019-03-20
575575
- New `Product` entity
576576
- New `ProductDefaultAttribute` entity
@@ -583,13 +583,13 @@ This file documents changes in the WCiOS Storage data model. Please explain any
583583
- @ctarda 2019-03-14
584584
- Adds `ShipmentTrackingProvider` and `ShipmentTrackingProviderGroup`
585585

586-
## Model 11 (Release 1.4.0.0)
586+
## Model 11 (Release 1.4.0.0) - Deleted
587587

588588
- @mindgraffiti 2019-02-27
589589
- Adds `siteID` and `total` attributes to `OrderStatus`
590590
- Changes `name` and `total` on `OrderStatus` to be optional
591591

592-
## Model 10 (Release 1.3.0.0)
592+
## Model 10 (Release 1.3.0.0) - Deleted
593593
Used mapping model: `WooCommerceModelV9toV10.xcmappingmodel`
594594

595595
- @astralbodies 2019-02-08
@@ -602,29 +602,29 @@ Used mapping model: `WooCommerceModelV9toV10.xcmappingmodel`
602602
- Changes `status` attribute on `Order` to `statusKey`
603603
- New `OrderStatus` entity
604604

605-
## Model 9 (Release 1.0.0.1)
605+
## Model 9 (Release 1.0.0.1) - Deleted
606606
- @bummytime 2019-01-11
607607
- Added `price` attribute on `OrderItem` entity
608608

609609
Note: the 1.0.0 model 9 never made it to our users so we are not reving the version #.
610610

611-
## Model 9 (Release 1.0.0)
611+
## Model 9 (Release 1.0.0) - Deleted
612612
- @jleandroperez 2018-12-26
613613
- New `Order.exclusiveForSearch` property
614614
- New `OrderSearchResults` entity
615615

616-
## Model 8 (Release 0.13)
616+
## Model 8 (Release 0.13) - Deleted
617617
- @jleandroperez 2018-12-14
618618
- Removed `Site.isJetpackInstalled` attribute.
619619

620620
- @bummytime 2018-12-11
621621
- New `OrderNote.author` attribute
622622

623-
## Model 7
623+
## Model 7 - Deleted
624624
- @bummytime 2018-11-26
625625
- New `Note.deleteInProgress` property
626626

627-
## Model 6
627+
## Model 6 - Deleted
628628
- @jleandroperez 2018-11-15
629629
- New `Note.siteID` property
630630

@@ -635,26 +635,26 @@ Note: the 1.0.0 model 9 never made it to our users so we are not reving the vers
635635
- Added new attribute: `isJetpackInstalled`, to site entity
636636
- Added new attribute: `plan`, to site entity
637637

638-
## Model 5
638+
## Model 5 - Deleted
639639
- @bummytime 2018-10-26
640640
- Added new entity: `Note`, to encapsulate all things notifications
641641

642642
- @bummytime 2018-10-23
643643
- Added new entity: `SiteSetting`, to encapsulate all of the site settings
644644

645-
## Model 4
645+
## Model 4 - Deleted
646646
- @bummytime 2018-10-09
647647
- Added new entity: `SiteVisitStats`, to encapsulate all of the visitor stats for a given site & granularity
648648
- Added new entity: `SiteVisitStatsItem`, to encapsulate all the visitor stats for a specific period
649649
- Added new entity: `OrderStats`, to encapsulate all of the order stats for a given site & granularity
650650
- Added new entity: `OrderStatsItem`, to encapsulate all the order stats for a specific period
651651

652-
## Model 3
652+
## Model 3 - Deleted
653653
- @bummytime 2018-09-19
654654
- Widened `quantity` attribute on `OrderItem` from Int16 to Int64
655655
- Widened `quantity` attribute on `TopEarnerStatsItem` from Int16 to Int64
656656

657-
## Model 2
657+
## Model 2 - Deleted
658658
- @bummytime 2018-09-05
659659
- Added new entity: `TopEarnerStats`, to encapsulate all of the top earner stats for a given site & granularity
660660
- Added new entity: `TopEarnerStatsItem`, to encapsulate all the top earner stats for a specific product

0 commit comments

Comments
 (0)