performMigration backs up current .dnote data, performs migration, and restores or cleans backups depending on if there is an error
(ctx context.DnoteCtx, migrationID int)
| 117 | // performMigration backs up current .dnote data, performs migration, and |
| 118 | // restores or cleans backups depending on if there is an error |
| 119 | func performMigration(ctx context.DnoteCtx, migrationID int) error { |
| 120 | // legacyMigrationV8 is the final migration of the legacy JSON Dnote migration |
| 121 | // migrate to sqlite and return |
| 122 | if migrationID == legacyMigrationV8 { |
| 123 | if err := migrateToV8(ctx); err != nil { |
| 124 | return errors.Wrap(err, "migrating to sqlite") |
| 125 | } |
| 126 | |
| 127 | return nil |
| 128 | } |
| 129 | |
| 130 | if err := backupDnoteDir(ctx); err != nil { |
| 131 | return errors.Wrap(err, "Failed to back up dnote directory") |
| 132 | } |
| 133 | |
| 134 | var migrationError error |
| 135 | |
| 136 | switch migrationID { |
| 137 | case legacyMigrationV1: |
| 138 | migrationError = migrateToV1(ctx) |
| 139 | case legacyMigrationV2: |
| 140 | migrationError = migrateToV2(ctx) |
| 141 | case legacyMigrationV3: |
| 142 | migrationError = migrateToV3(ctx) |
| 143 | case legacyMigrationV4: |
| 144 | migrationError = migrateToV4(ctx) |
| 145 | case legacyMigrationV5: |
| 146 | migrationError = migrateToV5(ctx) |
| 147 | case legacyMigrationV6: |
| 148 | migrationError = migrateToV6(ctx) |
| 149 | case legacyMigrationV7: |
| 150 | migrationError = migrateToV7(ctx) |
| 151 | default: |
| 152 | return errors.Errorf("Unrecognized migration id %d", migrationID) |
| 153 | } |
| 154 | |
| 155 | if migrationError != nil { |
| 156 | if err := restoreBackup(ctx); err != nil { |
| 157 | panic(errors.Wrap(err, "Failed to restore backup for a failed migration")) |
| 158 | } |
| 159 | |
| 160 | return errors.Wrapf(migrationError, "Failed to perform migration #%d", migrationID) |
| 161 | } |
| 162 | |
| 163 | if err := clearBackup(ctx); err != nil { |
| 164 | return errors.Wrap(err, "Failed to clear backup") |
| 165 | } |
| 166 | |
| 167 | if err := updateSchemaVersion(ctx, migrationID); err != nil { |
| 168 | return errors.Wrap(err, "Failed to update schema version") |
| 169 | } |
| 170 | |
| 171 | return nil |
| 172 | } |
| 173 | |
| 174 | // backupDnoteDir backs up the dnote directory to a temporary backup directory |
| 175 | func backupDnoteDir(ctx context.DnoteCtx) error { |
no test coverage detected