Created
January 30, 2019 06:29
-
-
Save lahma/b13a38918fec5dadaebad2e4d3b67066 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.IO; | |
| using System.Net.Http; | |
| using Raven.Client.Documents.Conventions; | |
| using Raven.Client.Documents.Operations; | |
| using Raven.Client.Http; | |
| using Sparrow.Json; | |
| namespace Testing | |
| { | |
| // RavenDB doesn't yet have public API for this. | |
| public class MigrateDatabaseOperation : IMaintenanceOperation<OperationIdResult> | |
| { | |
| private readonly string serverUrl; | |
| private readonly string apiKey; | |
| private readonly string sourceDatabase; | |
| private readonly string targetDatabase; | |
| public MigrateDatabaseOperation( | |
| string serverUrl, | |
| string apiKey, | |
| string sourceDatabase, | |
| string targetDatabase) | |
| { | |
| this.serverUrl = serverUrl; | |
| this.apiKey = apiKey; | |
| this.sourceDatabase = sourceDatabase; | |
| this.targetDatabase = targetDatabase; | |
| } | |
| public RavenCommand<OperationIdResult> GetCommand(DocumentConventions conventions, JsonOperationContext context) | |
| { | |
| return new MigrateDatabaseCommand(serverUrl, apiKey, sourceDatabase, targetDatabase); | |
| } | |
| private class MigrateDatabaseCommand : RavenCommand<OperationIdResult> | |
| { | |
| private readonly string serverUrl; | |
| private readonly string sourceDatabase; | |
| private readonly string targetDatabase; | |
| private readonly string apiKey; | |
| public MigrateDatabaseCommand( | |
| string serverUrl, | |
| string apiKey, | |
| string sourceDatabase, | |
| string targetDatabase) | |
| { | |
| this.serverUrl = serverUrl; | |
| this.sourceDatabase = sourceDatabase; | |
| this.targetDatabase = targetDatabase; | |
| this.apiKey = apiKey; | |
| } | |
| public override bool IsReadRequest => false; | |
| public override HttpRequestMessage CreateRequest(JsonOperationContext ctx, ServerNode node, out string url) | |
| { | |
| url = $"{node.Url}/databases/{targetDatabase}/admin/smuggler/migrate/ravendb"; | |
| Action<Stream> builder = stream => | |
| { | |
| using (var writer = new BlittableJsonTextWriter(ctx, stream)) | |
| { | |
| writer.WriteStartObject(); | |
| writer.WritePropertyName("ServerUrl"); | |
| writer.WriteString(serverUrl); | |
| writer.WriteComma(); | |
| writer.WritePropertyName("ApiKey"); | |
| writer.WriteString(apiKey); | |
| writer.WriteComma(); | |
| writer.WritePropertyName("BuildMajorVersion"); | |
| writer.WriteString("V4"); | |
| writer.WriteComma(); | |
| writer.WritePropertyName("BuildVersion"); | |
| writer.WriteInteger(35262); | |
| writer.WriteComma(); | |
| writer.WritePropertyName("SkipServerCertificateValidation"); | |
| writer.WriteBool(true); | |
| writer.WriteComma(); | |
| writer.WritePropertyName("MigrationSettings"); | |
| writer.WriteStartObject(); | |
| writer.WritePropertyName("DatabaseName"); | |
| writer.WriteString(sourceDatabase); | |
| writer.WriteComma(); | |
| writer.WritePropertyName("ImportRavenFs"); | |
| writer.WriteBool(false); | |
| writer.WriteComma(); | |
| writer.WritePropertyName("OperateOnTypes"); | |
| writer.WriteString("DatabaseRecord,Documents"); | |
| writer.WriteComma(); | |
| writer.WritePropertyName("RemoveAnalyzers"); | |
| writer.WriteBool(false); | |
| writer.WritePropertyName("TransformScript"); | |
| writer.WriteString(""); | |
| writer.WriteEndObject(); | |
| writer.WriteEndObject(); | |
| } | |
| }; | |
| var type = Type.GetType("Raven.Client.Json.BlittableJsonContent, Raven.Client", throwOnError: true); | |
| var content = (HttpContent) Activator.CreateInstance(type, builder); | |
| var request = new HttpRequestMessage | |
| { | |
| Method = HttpMethod.Post, | |
| Content = content | |
| }; | |
| return request; | |
| } | |
| public override void SetResponse(JsonOperationContext context, BlittableJsonReaderObject response, bool fromCache) | |
| { | |
| if (response == null) | |
| { | |
| ThrowInvalidResponse(); | |
| } | |
| Type type = Type.GetType("Raven.Client.Json.Converters.JsonDeserializationClient, Raven.Client"); | |
| var field = type.GetField("OperationIdResult"); | |
| var func = (Func<BlittableJsonReaderObject, OperationIdResult>) field.GetValue(null); | |
| Result = func(response); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment