/src/Raven.Server/Storage/Schema/SchemaUpgrader.cs

https://github.com/fitzchak/ravendb
C# | 108 lines | 94 code | 14 blank | 0 comment | 6 complexity | 395eb0191a6de734d21c2547b280da83 MD5 | raw file
  1. using System;
  2. using System.Linq;
  3. using Raven.Server.Documents;
  4. using Voron;
  5. using Voron.Impl;
  6. namespace Raven.Server.Storage.Schema
  7. {
  8. public static class SchemaUpgrader
  9. {
  10. internal class CurrentVersion
  11. {
  12. public const int ServerVersion = 11;
  13. public const int ConfigurationVersion = 10;
  14. public const int DocumentsVersion = 15;
  15. public const int IndexVersion = 12;
  16. }
  17. private static readonly int[] SkippedDocumentsVersion = { 12 };
  18. public enum StorageType
  19. {
  20. Server,
  21. Configuration,
  22. Documents,
  23. Index,
  24. }
  25. private class InternalUpgrader
  26. {
  27. private readonly StorageType _storageType;
  28. private readonly ConfigurationStorage _configurationStorage;
  29. private readonly DocumentsStorage _documentsStorage;
  30. internal InternalUpgrader(StorageType storageType, ConfigurationStorage configurationStorage, DocumentsStorage documentsStorage)
  31. {
  32. _storageType = storageType;
  33. _configurationStorage = configurationStorage;
  34. _documentsStorage = documentsStorage;
  35. }
  36. internal bool Upgrade(Transaction readTx, Transaction writeTx, int currentVersion, out int versionAfterUpgrade)
  37. {
  38. switch (_storageType)
  39. {
  40. case StorageType.Server:
  41. break;
  42. case StorageType.Configuration:
  43. break;
  44. case StorageType.Documents:
  45. if (SkippedDocumentsVersion.Contains(currentVersion))
  46. {
  47. throw new NotSupportedException($"Documents schema upgrade from version {currentVersion} is not supported, use the recovery tool to dump the data and then import it into a new database");
  48. }
  49. break;
  50. case StorageType.Index:
  51. break;
  52. default:
  53. throw new ArgumentOutOfRangeException(nameof(_storageType), _storageType, null);
  54. }
  55. versionAfterUpgrade = currentVersion;
  56. var name = $"Raven.Server.Storage.Schema.Updates.{_storageType.ToString()}.From{currentVersion}";
  57. var schemaUpdateType = typeof(SchemaUpgrader).Assembly.GetType(name);
  58. if (schemaUpdateType == null)
  59. return false;
  60. versionAfterUpgrade++;
  61. switch (_storageType)
  62. {
  63. case StorageType.Server:
  64. break;
  65. case StorageType.Configuration:
  66. break;
  67. case StorageType.Documents:
  68. while (SkippedDocumentsVersion.Contains(versionAfterUpgrade))
  69. {
  70. versionAfterUpgrade++;
  71. }
  72. break;
  73. case StorageType.Index:
  74. break;
  75. default:
  76. throw new ArgumentOutOfRangeException(nameof(_storageType), _storageType, null);
  77. }
  78. var schemaUpdate = (ISchemaUpdate)Activator.CreateInstance(schemaUpdateType);
  79. return schemaUpdate.Update(new UpdateStep
  80. {
  81. ReadTx = readTx,
  82. WriteTx = writeTx,
  83. ConfigurationStorage = _configurationStorage,
  84. DocumentsStorage = _documentsStorage,
  85. });
  86. }
  87. }
  88. public static UpgraderDelegate Upgrader(StorageType storageType, ConfigurationStorage configurationStorage, DocumentsStorage documentsStorage)
  89. {
  90. var upgrade = new InternalUpgrader(storageType, configurationStorage, documentsStorage);
  91. return upgrade.Upgrade;
  92. }
  93. }
  94. }