|
| 1 | +// |
| 2 | +// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | +// Licensed under the Amazon Software License |
| 4 | +// http://aws.amazon.com/asl/ |
| 5 | +// |
| 6 | + |
| 7 | +import Foundation |
| 8 | +import SQLite |
| 9 | + |
| 10 | +@available(*, deprecated, message: "This utility will be removed when the databaseURL parameter is removed from AWSAppSyncClientConfiguration") |
| 11 | +public struct AWSAppSyncCacheConfigurationMigration { |
| 12 | + public enum Error: Swift.Error { |
| 13 | + /// The destination file specified by the cache configuration already exists |
| 14 | + case destinationFileExists |
| 15 | + } |
| 16 | + |
| 17 | + /// A UserDefaults key that will be set once any successful migration has been completed |
| 18 | + public static let userDefaultsKey = "AWSAppSyncCacheConfigurationMigration.success" |
| 19 | + |
| 20 | + private enum TableName: String, CaseIterable { |
| 21 | + case offlineMutations = "mutation_records" |
| 22 | + case queries = "records" |
| 23 | + case subscriptionMetadata = "subscription_metadata" |
| 24 | + } |
| 25 | + |
| 26 | + /// A convenience method to migrate data from caches created prior to AWSAppSync 2.10.0. Once this migration |
| 27 | + /// completes, the old cache should be deleted. This method is safe to call multiple times: it stores a key in |
| 28 | + /// UserDefaults to indicate that the migration was successfully completed, and will only migrate if that flag is |
| 29 | + /// not set. That makes this method safe to call whenever you need to configure a new AppSyncClient (e.g., app |
| 30 | + /// startup or user login). |
| 31 | + /// |
| 32 | + /// **Usage** |
| 33 | + /// Invoke `migrate` before setting up the AWSAppSyncClientConfiguration: |
| 34 | + /// |
| 35 | + /// // Given `databaseURL` is the old consolidated databaseURL... |
| 36 | + /// |
| 37 | + /// // Create a default cacheConfiguration with cache files stored in the app's Caches directory |
| 38 | + /// let cacheConfiguration = try AWSAppSyncCacheConfiguration() |
| 39 | + /// try? AWSAppSyncCacheConfigurationMigration.migrate(from: databaseURL, using: cacheConfiguration) |
| 40 | + /// let clientConfig = try AWSAppSyncClientConfiguration(appSyncServiceConfig: serviceConfig, |
| 41 | + /// cacheConfiguration: cacheConfiguration) |
| 42 | + /// let appSyncClient = AWSAppSyncClient(appSyncConfig: clientConfig) |
| 43 | + /// |
| 44 | + /// **How it works** |
| 45 | + /// Internally, this method copies the database file from the source URL to the destination, and then drops |
| 46 | + /// unneeded tables. This results in higher disk usage on device, but is ultimately faster and safer than |
| 47 | + /// performing queries or data exports.creates a new connection to both the source and destination databases. |
| 48 | + /// |
| 49 | + /// **Multiple calls** |
| 50 | + /// A successful migration to a cache configuration with at least one persistent store will write a flag to |
| 51 | + /// UserDefaults to prevent any future migrations from occurring. Migrating to an in-memory cache (such as is |
| 52 | + /// configured by passing no `AWSAppSyncCacheConfiguration` to the `AWSAppSyncClientConfiguration` constructor, |
| 53 | + /// or by passing `AWSAppSyncCacheConfiguration.inMemory`) will __not__ set the flag. That also means this method |
| 54 | + /// will not populate an in-memory copy of a previously existing on-disk database. |
| 55 | + /// |
| 56 | + /// **Warning** |
| 57 | + /// This migration operates by copying the file from `databaseURL` to the URL of the individual cache. |
| 58 | + /// This would destroy any data at the destination cache, so the destination URL must not have a file present at |
| 59 | + /// the time the migration begins. |
| 60 | + /// |
| 61 | + /// - Parameters: |
| 62 | + /// - databaseURL: The URL of the consolidated cache |
| 63 | + /// - cacheConfiguration: The AWSAppSyncCacheConfiguration specifying the individual destination cache |
| 64 | + /// locations to migrate to |
| 65 | + /// - Throws: If the migration encounters an file system error, or if any of the cache files in |
| 66 | + /// `cacheConfiguration` already exists. |
| 67 | + public static func migrate(from databaseURL: URL, using cacheConfiguration: AWSAppSyncCacheConfiguration) throws { |
| 68 | + guard !hasSuccessfullyMigrated() else { |
| 69 | + AppSyncLog.info("Migration has already been completed, aborting") |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + guard cacheConfiguration.offlineMutations != nil || |
| 74 | + cacheConfiguration.queries != nil || |
| 75 | + cacheConfiguration.subscriptionMetadataCache != nil else { |
| 76 | + AppSyncLog.info("At least one cacheConfiguration must be non-nil") |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + try migrate(.offlineMutations, |
| 81 | + from: databaseURL, |
| 82 | + to: cacheConfiguration.offlineMutations) |
| 83 | + |
| 84 | + try migrate(.queries, |
| 85 | + from: databaseURL, |
| 86 | + to: cacheConfiguration.queries) |
| 87 | + |
| 88 | + try migrate(.subscriptionMetadata, |
| 89 | + from: databaseURL, |
| 90 | + to: cacheConfiguration.subscriptionMetadataCache) |
| 91 | + |
| 92 | + recordSuccessfulMigration() |
| 93 | + } |
| 94 | + |
| 95 | + private static func hasSuccessfullyMigrated() -> Bool { |
| 96 | + let hasMigrated = UserDefaults.standard.bool(forKey: userDefaultsKey) |
| 97 | + return hasMigrated |
| 98 | + } |
| 99 | + |
| 100 | + private static func migrate(_ tableName: TableName, |
| 101 | + from databaseURL: URL, |
| 102 | + to destinationURL: URL?) throws { |
| 103 | + guard let destinationURL = destinationURL else { |
| 104 | + return |
| 105 | + } |
| 106 | + |
| 107 | + try copyIfDestinationNotExists(from: databaseURL, to: destinationURL) |
| 108 | + |
| 109 | + let destinationDB = try Connection(.uri(destinationURL.absoluteString)) |
| 110 | + |
| 111 | + try deleteOtherTables(than: tableName, in: destinationDB) |
| 112 | + } |
| 113 | + |
| 114 | + private static func copyIfDestinationNotExists(from sourceURL: URL, to destinationURL: URL) throws { |
| 115 | + let fileManager = FileManager.default |
| 116 | + |
| 117 | + guard !fileManager.fileExists(atPath: destinationURL.path) else { |
| 118 | + throw Error.destinationFileExists |
| 119 | + } |
| 120 | + |
| 121 | + try fileManager.copyItem(at: sourceURL, to: destinationURL) |
| 122 | + } |
| 123 | + |
| 124 | + private static func deleteOtherTables(than tableName: TableName, in db: Connection) throws { |
| 125 | + for tableToDelete in TableName.allCases where tableToDelete != tableName { |
| 126 | + try db.run("DROP TABLE IF EXISTS \(tableToDelete.rawValue)") |
| 127 | + } |
| 128 | + |
| 129 | + try db.run("VACUUM") |
| 130 | + } |
| 131 | + |
| 132 | + private static func recordSuccessfulMigration() { |
| 133 | + UserDefaults.standard.set(true, forKey: userDefaultsKey) |
| 134 | + } |
| 135 | + |
| 136 | +} |
0 commit comments