public void UpdateSchemaIfNecessary() { if (storage.SchemaVersion == SchemaVersion) return; using (var ticker = new OutputTicker(TimeSpan.FromSeconds(3), () => { log.Info("."); Console.Write("."); }, null, () => { log.Info("OK"); Console.Write("OK"); Console.WriteLine(); })) { bool lockTaken = false; try { Monitor.TryEnter(UpdateLocker, TimeSpan.FromSeconds(15), ref lockTaken); if (lockTaken == false) throw new TimeoutException("Could not take upgrade lock after 15 seconds, probably another database is upgrading itself and we can't interupt it midway. Please try again later"); do { var updater = Updaters.FirstOrDefault(update => update.Value.FromSchemaVersion == storage.SchemaVersion); if (updater == null) throw new InvalidOperationException( string.Format( "The version on disk ({0}) is different that the version supported by this library: {1}{2}You need to migrate the disk version to the library version, alternatively, if the data isn't important, you can delete the file and it will be re-created (with no data) with the library version.", storage.SchemaVersion, SchemaVersion, Environment.NewLine)); log.Info("Updating schema from version {0}: ", storage.SchemaVersion); Console.WriteLine("Updating schema from version {0}: ", storage.SchemaVersion); ticker.Start(); updater.Value.Update(storage, output); updater.Value.UpdateSchemaVersion(storage, output); ticker.Stop(); } while (storage.SchemaVersion != SchemaVersion); } finally { if (lockTaken) Monitor.Exit(UpdateLocker); } } }
private void SetIdFromDb() { try { instance.WithDatabase(database, (session, dbid, tx) => { using (var details = new Table(session, dbid, "details", OpenTableGrbit.ReadOnly)) { Api.JetMove(session, details, JET_Move.First, MoveGrbit.None); var columnids = Api.GetColumnDictionary(session, details); var column = Api.RetrieveColumn(session, details, columnids["id"]); Id = new Guid(column); var schemaVersion = Api.RetrieveColumnAsString(session, details, columnids["schema_version"]); if (configuration.Storage.PreventSchemaUpdate || schemaVersion == SchemaCreator.SchemaVersion) return tx; using (var ticker = new OutputTicker(TimeSpan.FromSeconds(3), () => { log.Info("."); Console.Write("."); }, null, () => { log.Info("OK"); Console.Write("OK"); Console.WriteLine(); })) { bool lockTaken = false; try { Monitor.TryEnter(UpdateLocker, TimeSpan.FromSeconds(15), ref lockTaken); if (lockTaken == false) throw new TimeoutException("Could not take upgrade lock after 15 seconds, probably another database is upgrading itself and we can't interrupt it midway. Please try again later"); do { var updater = Updaters.FirstOrDefault(update => update.Value.FromSchemaVersion == schemaVersion); if (updater == null) throw new InvalidOperationException( string.Format( "The version on disk ({0}) is different that the version supported by this library: {1}{2}You need to migrate the disk version to the library version, alternatively, if the data isn't important, you can delete the file and it will be re-created (with no data) with the library version.", schemaVersion, SchemaCreator.SchemaVersion, Environment.NewLine)); log.Info("Updating schema from version {0}: ", schemaVersion); Console.WriteLine("Updating schema from version {0}: ", schemaVersion); ticker.Start(); updater.Value.Init(generator, configuration); updater.Value.Update(session, dbid, Output); tx.Commit(CommitTransactionGrbit.LazyFlush); tx = new Transaction(session); schemaVersion = Api.RetrieveColumnAsString(session, details, columnids["schema_version"]); ticker.Stop(); } while (schemaVersion != SchemaCreator.SchemaVersion); return tx; } finally { if (lockTaken) Monitor.Exit(UpdateLocker); } } } }); } catch (EsentVersionStoreOutOfMemoryException esentOutOfMemoryException) { var message = "Schema Update Process for " + database + " Failed due to Esent Out Of Memory Exception!" + Environment.NewLine + "This might be caused by exceptionally large files, Consider enlarging the value of Raven/Esent/MaxVerPages (default:512)"; log.Error(message); Console.WriteLine(message); throw new InvalidOperationException(message, esentOutOfMemoryException); } catch (Exception e) { var message = "Could not read db details from disk. It is likely that there is a version difference between the library and the db on the disk." + Environment.NewLine + "You need to migrate the disk version to the library version, alternatively, if the data isn't important, you can delete the file and it will be re-created (with no data) with the library version."; log.Error(message); Console.WriteLine(message); throw new InvalidOperationException( message, e); } }
private void SetIdFromDb() { try { instance.WithDatabase(database, (session, dbid) => { using (var details = new Table(session, dbid, "details", OpenTableGrbit.ReadOnly)) { Api.JetMove(session, details, JET_Move.First, MoveGrbit.None); var columnids = Api.GetColumnDictionary(session, details); var column = Api.RetrieveColumn(session, details, columnids["id"]); Id = new Guid(column); var schemaVersion = Api.RetrieveColumnAsString(session, details, columnids["schema_version"]); if (schemaVersion == SchemaCreator.SchemaVersion) return; using (var ticker = new OutputTicker(TimeSpan.FromSeconds(3), () => { log.Info("."); Console.Write("."); }, null, () => { log.Info("OK"); Console.Write("OK"); Console.WriteLine(); })) { do { var updater = Updaters.FirstOrDefault(update => update.Value.FromSchemaVersion == schemaVersion); if (updater == null) throw new InvalidOperationException( string.Format( "The version on disk ({0}) is different that the version supported by this library: {1}{2}You need to migrate the disk version to the library version, alternatively, if the data isn't important, you can delete the file and it will be re-created (with no data) with the library version.", schemaVersion, SchemaCreator.SchemaVersion, Environment.NewLine)); log.Info("Updating schema from version {0}: ", schemaVersion); Console.WriteLine("Updating schema from version {0}: ", schemaVersion); ticker.Start(); updater.Value.Init(generator); updater.Value.Update(session, dbid, Output); schemaVersion = Api.RetrieveColumnAsString(session, details, columnids["schema_version"]); ticker.Stop(); } while (schemaVersion != SchemaCreator.SchemaVersion); } } }); } catch (Exception e) { throw new InvalidOperationException( "Could not read db details from disk. It is likely that there is a version difference between the library and the db on the disk." + Environment.NewLine + "You need to migrate the disk version to the library version, alternatively, if the data isn't important, you can delete the file and it will be re-created (with no data) with the library version.", e); } }