11using Microsoft . Azure . SqlDatabase . ElasticScale . ShardManagement ;
2+ using Newtonsoft . Json ;
3+ using Newtonsoft . Json . Linq ;
24using System ;
35using System . Collections . Generic ;
46using System . Data . SqlClient ;
57using System . IO ;
68using System . Linq ;
9+ using System . Net ;
710using System . Runtime . Serialization . Formatters . Binary ;
811
912namespace SyncTokenGenerator
@@ -13,9 +16,9 @@ internal class Program
1316 static void Main ( string [ ] args )
1417 {
1518 Console . WriteLine ( "Please enter the connectionstring to the shardmapmanager" ) ;
16-
1719 var shardMapManagerConnectionString = Console . ReadLine ( ) ;
1820 Dictionary < string , long ? > shardChangeVersions = new Dictionary < string , long ? > ( ) ;
21+ string syncToken = "" ;
1922
2023 try
2124 {
@@ -56,16 +59,52 @@ static void Main(string[] args)
5659 {
5760 BinaryFormatter binaryFormatter = new BinaryFormatter ( ) ;
5861 binaryFormatter . Serialize ( memoryStream , shardChangeVersions ) ;
62+ syncToken = Convert . ToBase64String ( memoryStream . ToArray ( ) ) ;
5963 Console . WriteLine ( "The new sync token is:" ) ;
60- Console . WriteLine ( Convert . ToBase64String ( memoryStream . ToArray ( ) ) ) ;
64+ Console . WriteLine ( syncToken ) ;
6165 }
6266 }
63- catch ( Exception ex )
67+ catch ( Exception ex )
6468 {
65- Console . WriteLine ( "Unable to access the Shard Map Manager database" ) ;
66- Console . WriteLine ( ex . Message ) ;
69+ WriteError ( "Unable to access the Shard Map Manager database" ) ;
70+ WriteError ( ex . Message ) ;
71+ }
72+
73+ Console . WriteLine ( ) ;
74+ Console . WriteLine ( "Update XDB SOLR Index? (Y/N)" ) ;
75+ bool updateSolr = Console . ReadLine ( ) . ToLowerInvariant ( ) == "y" ;
76+ if ( updateSolr )
77+ {
78+ try
79+ {
80+ Console . WriteLine ( "Please enter the SOLR XDB connectionstring, for ex https://localhost:8983/solr/sitecore_xdb" ) ;
81+ var solrConnectionString = Console . ReadLine ( ) ;
82+ if ( ! solrConnectionString . EndsWith ( "/" ) )
83+ {
84+ solrConnectionString += "/" ;
85+ }
86+ solrConnectionString += "update?commit=true" ;
87+ using ( var webclient = new WebClient ( ) )
88+ {
89+ var postBody = $ "[{{ \" id\" :\" xdb-index-token\" ,\" xdbtokenbytes_s\" :\" { syncToken } \" }}]";
90+ webclient . Headers . Add ( "content-type" , "application/json" ) ;
91+ var response = webclient . UploadString ( solrConnectionString , "post" , postBody ) ;
92+ var jsonResponse = ! string . IsNullOrEmpty ( response ) ? JObject . Parse ( response ) : null ; ;
93+ if ( ! int . TryParse ( ( jsonResponse [ "responseHeader" ] ? [ "status" ] as JValue ) ? . Value ? . ToString ( ) , out var status ) || status != 0 )
94+ {
95+ WriteError ( "Invalid response recieved from SOLR" ) ;
96+ WriteError ( jsonResponse ? . ToString ( ) ) ;
97+ }
98+ }
99+ }
100+ catch ( Exception ex )
101+ {
102+ WriteError ( "Unable to update Solr" ) ;
103+ WriteError ( ex . Message ) ;
104+ }
67105 }
68-
106+
107+ Console . WriteLine ( "Done, press any key to exit" ) ;
69108 Console . ReadLine ( ) ;
70109 }
71110
@@ -84,5 +123,13 @@ static string Credentials(string connectionString)
84123 }
85124 return result . ToString ( ) ;
86125 }
126+
127+ static void WriteError ( string message )
128+ {
129+ var originalColor = Console . ForegroundColor ;
130+ Console . ForegroundColor = ConsoleColor . Red ;
131+ Console . Error . WriteLine ( message ) ;
132+ Console . ForegroundColor = originalColor ;
133+ }
87134 }
88135}
0 commit comments