entity 오류로인해 제거해야해서 . 제거전 백업
This commit is contained in:
123
DBMigration/Services/DatabaseService.cs
Normal file
123
DBMigration/Services/DatabaseService.cs
Normal file
@@ -0,0 +1,123 @@
|
||||
using Microsoft.Data.SqlClient;
|
||||
using Microsoft.SqlServer.Management.Smo;
|
||||
using DBMigration.Models;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DBMigration.Services
|
||||
{
|
||||
public class DatabaseService
|
||||
{
|
||||
public async IAsyncEnumerable<DatabaseObject> GetTables(ConnectionInfo connection)
|
||||
{
|
||||
await foreach (var obj in GetDatabaseObjectsAsync(connection, "TABLE"))
|
||||
{
|
||||
yield return obj;
|
||||
}
|
||||
}
|
||||
|
||||
public async IAsyncEnumerable<DatabaseObject> GetViews(ConnectionInfo connection)
|
||||
{
|
||||
await foreach (var obj in GetDatabaseObjectsAsync(connection, "VIEW"))
|
||||
{
|
||||
yield return obj;
|
||||
}
|
||||
}
|
||||
|
||||
public async IAsyncEnumerable<DatabaseObject> GetProcedures(ConnectionInfo connection)
|
||||
{
|
||||
await foreach (var obj in GetDatabaseObjectsAsync(connection, "PROCEDURE"))
|
||||
{
|
||||
yield return obj;
|
||||
}
|
||||
}
|
||||
|
||||
private async IAsyncEnumerable<DatabaseObject> GetDatabaseObjectsAsync(ConnectionInfo connection, string objectType)
|
||||
{
|
||||
using (var conn = new SqlConnection(connection.GetConnectionString()))
|
||||
{
|
||||
await conn.OpenAsync();
|
||||
var server = new Server(new Microsoft.SqlServer.Management.Common.ServerConnection(conn));
|
||||
var database = server.Databases[connection.DatabaseName];
|
||||
|
||||
switch (objectType)
|
||||
{
|
||||
case "TABLE":
|
||||
foreach (Table table in database.Tables)
|
||||
{
|
||||
if (table.IsSystemObject || table.Name.StartsWith("_")) continue;
|
||||
yield return CreateDatabaseObject(table);
|
||||
await Task.Yield();
|
||||
}
|
||||
break;
|
||||
|
||||
case "VIEW":
|
||||
foreach (Microsoft.SqlServer.Management.Smo.View view in database.Views)
|
||||
{
|
||||
if (view.IsSystemObject) continue;
|
||||
yield return CreateDatabaseObject(view);
|
||||
await Task.Yield();
|
||||
}
|
||||
break;
|
||||
|
||||
case "PROCEDURE":
|
||||
foreach (StoredProcedure sp in database.StoredProcedures)
|
||||
{
|
||||
if (sp.IsSystemObject) continue;
|
||||
yield return CreateDatabaseObject(sp);
|
||||
await Task.Yield();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private DatabaseObject CreateDatabaseObject(Table table)
|
||||
{
|
||||
return new DatabaseObject
|
||||
{
|
||||
Name = table.Name,
|
||||
Schema = table.Schema,
|
||||
Type = "TABLE",
|
||||
Definition = GetTableDefinition(table)
|
||||
};
|
||||
}
|
||||
|
||||
private DatabaseObject CreateDatabaseObject(Microsoft.SqlServer.Management.Smo.View view)
|
||||
{
|
||||
return new DatabaseObject
|
||||
{
|
||||
Name = view.Name,
|
||||
Schema = view.Schema,
|
||||
Type = "VIEW",
|
||||
Definition = view.TextBody
|
||||
};
|
||||
}
|
||||
|
||||
private DatabaseObject CreateDatabaseObject(StoredProcedure sp)
|
||||
{
|
||||
return new DatabaseObject
|
||||
{
|
||||
Name = sp.Name,
|
||||
Schema = sp.Schema,
|
||||
Type = "PROCEDURE",
|
||||
Definition = sp.TextBody
|
||||
};
|
||||
}
|
||||
|
||||
private string GetTableDefinition(Table table)
|
||||
{
|
||||
var options = new ScriptingOptions
|
||||
{
|
||||
IncludeIfNotExists = true,
|
||||
ScriptDrops = false,
|
||||
WithDependencies = true,
|
||||
Indexes = true,
|
||||
Triggers = true,
|
||||
ClusteredIndexes = true,
|
||||
NonClusteredIndexes = true
|
||||
};
|
||||
|
||||
return table.Script(options).ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
92
DBMigration/Services/MigrationService.cs
Normal file
92
DBMigration/Services/MigrationService.cs
Normal file
@@ -0,0 +1,92 @@
|
||||
using Microsoft.Data.SqlClient;
|
||||
using Microsoft.SqlServer.Management.Smo;
|
||||
using DBMigration.Models;
|
||||
|
||||
namespace DBMigration.Services
|
||||
{
|
||||
public class MigrationService
|
||||
{
|
||||
private readonly DatabaseService _databaseService;
|
||||
|
||||
public MigrationService()
|
||||
{
|
||||
_databaseService = new DatabaseService();
|
||||
}
|
||||
|
||||
public void MigrateTable(DatabaseObject table, ConnectionInfo source, ConnectionInfo target)
|
||||
{
|
||||
using (var sourceConn = new SqlConnection(source.GetConnectionString()))
|
||||
using (var targetConn = new SqlConnection(target.GetConnectionString()))
|
||||
{
|
||||
sourceConn.Open();
|
||||
targetConn.Open();
|
||||
|
||||
// 1. 테이블 생성 (인덱스 포함)
|
||||
ExecuteScript(table.Definition, targetConn);
|
||||
|
||||
// 2. IDENTITY와 트리거 비활성화
|
||||
DisableIdentityAndTriggers(table, targetConn);
|
||||
|
||||
// 3. 데이터 복사
|
||||
CopyData(table, sourceConn, targetConn);
|
||||
|
||||
// 4. IDENTITY와 트리거 재활성화
|
||||
EnableIdentityAndTriggers(table, targetConn);
|
||||
|
||||
// 5. 통계 업데이트
|
||||
UpdateStatistics(table, targetConn);
|
||||
}
|
||||
}
|
||||
|
||||
private void ExecuteScript(string script, SqlConnection connection)
|
||||
{
|
||||
using (var cmd = new SqlCommand(script, connection))
|
||||
{
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
|
||||
private void DisableIdentityAndTriggers(DatabaseObject table, SqlConnection connection)
|
||||
{
|
||||
var disableScript = $@"
|
||||
-- IDENTITY 비활성화
|
||||
SET IDENTITY_INSERT {table.FullName} ON;
|
||||
|
||||
-- 트리거 비활성화
|
||||
DISABLE TRIGGER ALL ON {table.FullName};";
|
||||
|
||||
ExecuteScript(disableScript, connection);
|
||||
}
|
||||
|
||||
private void EnableIdentityAndTriggers(DatabaseObject table, SqlConnection connection)
|
||||
{
|
||||
var enableScript = $@"
|
||||
-- IDENTITY 활성화
|
||||
SET IDENTITY_INSERT {table.FullName} OFF;
|
||||
|
||||
-- 트리거 활성화
|
||||
ENABLE TRIGGER ALL ON {table.FullName};";
|
||||
|
||||
ExecuteScript(enableScript, connection);
|
||||
}
|
||||
|
||||
private void CopyData(DatabaseObject table, SqlConnection source, SqlConnection target)
|
||||
{
|
||||
using (var cmd = new SqlCommand($"SELECT * FROM {table.FullName}", source))
|
||||
using (var reader = cmd.ExecuteReader())
|
||||
{
|
||||
using (var bulkCopy = new SqlBulkCopy(target))
|
||||
{
|
||||
bulkCopy.DestinationTableName = table.FullName;
|
||||
bulkCopy.WriteToServer(reader);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateStatistics(DatabaseObject table, SqlConnection connection)
|
||||
{
|
||||
var updateStatsScript = $"UPDATE STATISTICS {table.FullName} WITH FULLSCAN;";
|
||||
ExecuteScript(updateStatsScript, connection);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user