168 lines
5.1 KiB
C#
Executable File
168 lines
5.1 KiB
C#
Executable File
#!/usr/bin/env dotnet-script
|
|
#r "nuget: MongoDB.Driver, 2.23.1"
|
|
#r "nuget: Microsoft.EntityFrameworkCore.SqlServer, 8.0.0"
|
|
#r "nuget: Microsoft.EntityFrameworkCore, 8.0.0"
|
|
#r "nuget: System.Text.Json, 8.0.0"
|
|
|
|
using MongoDB.Driver;
|
|
using MongoDB.Bson;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System.Text.Json;
|
|
|
|
Console.WriteLine("==============================================");
|
|
Console.WriteLine("MongoDB to SQL Server Migration Script");
|
|
Console.WriteLine("==============================================\n");
|
|
|
|
// Connection strings
|
|
var mongoConnectionString = "mongodb://localhost:27017";
|
|
var mongoDatabaseName = "SkyArtShopDB";
|
|
var sqlConnectionString = "Server=localhost,1433;Database=SkyArtShop;User Id=sa;Password=Str0ngP@ssw0rd!2025;TrustServerCertificate=True;";
|
|
|
|
Console.WriteLine($"MongoDB: {mongoConnectionString}/{mongoDatabaseName}");
|
|
Console.WriteLine($"SQL Server: Server=localhost,1433;Database=SkyArtShop\n");
|
|
|
|
// Connect to MongoDB
|
|
var mongoClient = new MongoClient(mongoConnectionString);
|
|
var mongoDatabase = mongoClient.GetDatabase(mongoDatabaseName);
|
|
|
|
Console.WriteLine("✓ Connected to MongoDB");
|
|
|
|
// Test SQL connection
|
|
try
|
|
{
|
|
using (var connection = new Microsoft.Data.SqlClient.SqlConnection(sqlConnectionString))
|
|
{
|
|
connection.Open();
|
|
Console.WriteLine("✓ Connected to SQL Server\n");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"✗ Failed to connect to SQL Server: {ex.Message}");
|
|
return 1;
|
|
}
|
|
|
|
Console.WriteLine("Starting data migration...\n");
|
|
|
|
// Migration statistics
|
|
int totalCollections = 0;
|
|
int totalDocuments = 0;
|
|
var errors = new List<string>();
|
|
|
|
try
|
|
{
|
|
// Get all collections
|
|
var collections = new[]
|
|
{
|
|
"Pages", "PortfolioCategories", "PortfolioProjects",
|
|
"Products", "BlogPosts", "SiteSettings", "MenuItems",
|
|
"AdminUsers", "UserRoles", "Orders", "ProductViews",
|
|
"HomepageSections"
|
|
};
|
|
|
|
foreach (var collectionName in collections)
|
|
{
|
|
try
|
|
{
|
|
Console.WriteLine($"Migrating {collectionName}...");
|
|
var collection = mongoDatabase.GetCollection<BsonDocument>(collectionName);
|
|
var documents = await collection.Find(new BsonDocument()).ToListAsync();
|
|
|
|
if (documents.Count == 0)
|
|
{
|
|
Console.WriteLine($" → 0 documents (skipped)\n");
|
|
continue;
|
|
}
|
|
|
|
// Export to JSON file for SQL import
|
|
var jsonData = new List<string>();
|
|
foreach (var doc in documents)
|
|
{
|
|
// Convert ObjectId to string
|
|
if (doc.Contains("_id"))
|
|
{
|
|
doc["_id"] = doc["_id"].ToString();
|
|
}
|
|
|
|
// Handle nested ObjectIds
|
|
ConvertObjectIdsToStrings(doc);
|
|
|
|
jsonData.Add(doc.ToJson(new MongoDB.Bson.IO.JsonWriterSettings { OutputMode = MongoDB.Bson.IO.JsonOutputMode.Strict }));
|
|
}
|
|
|
|
// Save to file
|
|
var outputPath = $"/tmp/migration_{collectionName}.json";
|
|
await File.WriteAllTextAsync(outputPath, $"[{string.Join(",", jsonData)}]");
|
|
|
|
Console.WriteLine($" ✓ Exported {documents.Count} documents to {outputPath}");
|
|
totalDocuments += documents.Count;
|
|
totalCollections++;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
errors.Add($"{collectionName}: {ex.Message}");
|
|
Console.WriteLine($" ✗ Error: {ex.Message}");
|
|
}
|
|
|
|
Console.WriteLine();
|
|
}
|
|
|
|
Console.WriteLine("==============================================");
|
|
Console.WriteLine("Migration Export Complete");
|
|
Console.WriteLine("==============================================");
|
|
Console.WriteLine($"Collections exported: {totalCollections}");
|
|
Console.WriteLine($"Total documents: {totalDocuments}");
|
|
|
|
if (errors.Count > 0)
|
|
{
|
|
Console.WriteLine($"\nErrors ({errors.Count}):");
|
|
foreach (var error in errors)
|
|
{
|
|
Console.WriteLine($" - {error}");
|
|
}
|
|
}
|
|
|
|
Console.WriteLine("\n✓ JSON files created in /tmp/migration_*.json");
|
|
Console.WriteLine("✓ Next step: Run the C# import program to load into SQL Server");
|
|
|
|
return 0;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"\n✗ Fatal error: {ex.Message}");
|
|
Console.WriteLine(ex.StackTrace);
|
|
return 1;
|
|
}
|
|
|
|
void ConvertObjectIdsToStrings(BsonDocument doc)
|
|
{
|
|
var keysToConvert = new List<string>();
|
|
|
|
foreach (var element in doc)
|
|
{
|
|
if (element.Value.IsObjectId)
|
|
{
|
|
keysToConvert.Add(element.Name);
|
|
}
|
|
else if (element.Value.IsBsonDocument)
|
|
{
|
|
ConvertObjectIdsToStrings(element.Value.AsBsonDocument);
|
|
}
|
|
else if (element.Value.IsBsonArray)
|
|
{
|
|
foreach (var item in element.Value.AsBsonArray)
|
|
{
|
|
if (item.IsBsonDocument)
|
|
{
|
|
ConvertObjectIdsToStrings(item.AsBsonDocument);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach (var key in keysToConvert)
|
|
{
|
|
doc[key] = doc[key].ToString();
|
|
}
|
|
}
|