-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
142 lines (123 loc) · 4.94 KB
/
Program.cs
File metadata and controls
142 lines (123 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Threading;
using Raven.Abstractions.Data;
using Raven.Abstractions.Replication;
using Raven.Abstractions.Smuggler;
using Raven.Client;
using Raven.Client.Connection;
using Raven.Client.Document;
using Raven.Smuggler;
namespace RavenMigration
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("So. are you sure you want to start? Yes/No");
using (var store = new DocumentStore
{
ConnectionStringName = "RavenDB"
}.Initialize())
{
if (Console.ReadLine() == "Yes")
{
Console.WriteLine("create master database");
var fileMaster = Directory.GetFiles(@".\Master").First();
CreateDatabase(store, GetDatabaseName(fileMaster));
CreateData(store, GetDatabaseName(fileMaster), fileMaster);
var backupFiles = Directory.GetFiles(@".\Tenants");
foreach (var file in backupFiles)
{
var tenantId = GetDatabaseName(file);
if (!store.DatabaseExists(tenantId))
{
CreateDatabase(store, tenantId);
CreateReplication(store, tenantId);
CreateData(store, tenantId, file);
CreateIndexes(store, tenantId);
}
}
}
}
}
private static void CreateIndexes(IDocumentStore store, string tenantId)
{
//?????
}
private static void CreateData(IDocumentStore store, string tenantId, string file)
{
Console.WriteLine("import data: {0}", tenantId);
var connectionStringOptions = new RavenConnectionStringOptions
{
DefaultDatabase = tenantId,
Url = ConfigurationManager.AppSettings["app:master"]
};
var smugglerApi = new SmugglerApi(new SmugglerOptions { }, connectionStringOptions);
smugglerApi.ImportData(new SmugglerOptions
{
OperateOnTypes = ItemType.Documents,
BackupPath = file,
TransformScript = TransformScript(tenantId)
}).Wait(TimeSpan.FromSeconds(15));
WaitForNonStaleIndexes(store.DatabaseCommands.ForDatabase(tenantId));
}
private static string TransformScript(string tenantId)
{
if (tenantId.Contains("Global"))
{
return @"function(doc) {
var type = doc['@metadata']['Raven-Entity-Name'];
if(type === 'Songs' || type === 'Users')
return doc;
return null;
}";
}
return @"function(doc) {
var type = doc['@metadata']['Raven-Entity-Name'];
if(type === 'Songs' || type === 'Users')
return null;
return doc;
}";
}
private static void CreateReplication(IDocumentStore store, string tenantId)
{
Console.WriteLine("Setup replication: {0}", tenantId);
using (var session = store.OpenSession("MusicMind.Global"))
{
var replicationDocument = session.Load<ReplicationDocument>("Raven/Replication/Destinations") ?? new ReplicationDocument();
replicationDocument.Destinations = new List<ReplicationDestination>(replicationDocument.Destinations)
{
new ReplicationDestination
{
Url = ConfigurationManager.AppSettings["app:master"],
Database = tenantId
}
};
session.Store(replicationDocument);
session.SaveChanges();
}
WaitForNonStaleIndexes(store.DatabaseCommands.ForDatabase(tenantId));
}
private static void CreateDatabase(IDocumentStore store, string tenantId)
{
Console.WriteLine("Create database: {0}", tenantId);
store.CreateDataBase(tenantId);
WaitForNonStaleIndexes(store.DatabaseCommands.ForDatabase(tenantId));
}
private static void WaitForNonStaleIndexes(IDatabaseCommands databaseCommands)
{
while (databaseCommands.GetStatistics().StaleIndexes.Any())
{
Thread.Sleep(25);
}
}
private static string GetDatabaseName(string file)
{
return Path.GetFileNameWithoutExtension(file).Replace(".ravendump", "");
}
}
}