using System.Data;
using Bit.Seeder.Migration.Models;
using Bit.Seeder.Migration.Utils;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Logging;
namespace Bit.Seeder.Migration.Databases;
///
/// SQL Server database importer that handles schema creation, data import, and constraint management.
///
public class SqlServerImporter(DatabaseConfig config, ILogger logger) : IDatabaseImporter
{
private readonly ILogger _logger = logger;
private readonly string _host = config.Host;
private readonly int _port = config.Port;
private readonly string _database = config.Database;
private readonly string _username = config.Username;
private readonly string _password = config.Password;
private SqlConnection? _connection;
private bool _disposed = false;
private const string _trackingTableName = "[dbo].[_MigrationDisabledConstraint]";
///
/// Connects to the SQL Server database.
///
public bool Connect()
{
try
{
var safeConnectionString = $"Server={_host},{_port};Database={_database};" +
$"User Id={_username};Password={DbSeederConstants.REDACTED_PASSWORD};" +
$"TrustServerCertificate=True;" +
$"Connection Timeout={DbSeederConstants.DEFAULT_CONNECTION_TIMEOUT};";
var actualConnectionString = safeConnectionString.Replace(DbSeederConstants.REDACTED_PASSWORD, _password);
_connection = new SqlConnection(actualConnectionString);
_connection.Open();
_logger.LogInformation("Connected to SQL Server: {Host}/{Database}", _host, _database);
return true;
}
catch (Exception ex)
{
_logger.LogError("Failed to connect to SQL Server: {Message}", ex.Message);
return false;
}
}
///
/// Disconnects from the SQL Server database.
///
public void Disconnect()
{
if (_connection != null)
{
_connection.Close();
_connection.Dispose();
_connection = null;
_logger.LogInformation("Disconnected from SQL Server");
}
}
///
/// Gets the list of columns for a table.
///
public List GetTableColumns(string tableName)
{
if (_connection == null)
throw new InvalidOperationException("Not connected to database");
IdentifierValidator.ValidateOrThrow(tableName, "table name");
try
{
var query = @"
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @TableName
ORDER BY ORDINAL_POSITION";
using var command = new SqlCommand(query, _connection);
command.Parameters.AddWithValue("@TableName", tableName);
var columns = new List();
using var reader = command.ExecuteReader();
while (reader.Read())
{
var colName = reader.GetString(0);
// Validate column name immediately to prevent second-order SQL injection
IdentifierValidator.ValidateOrThrow(colName, "column name");
columns.Add(colName);
}
return columns;
}
catch (Exception ex)
{
_logger.LogError("Error getting columns for table {TableName}: {Message}", tableName, ex.Message);
return [];
}
}
///
/// Gets the column types for a table.
///
private Dictionary GetTableColumnTypes(string tableName)
{
if (_connection == null)
throw new InvalidOperationException("Not connected to database");
IdentifierValidator.ValidateOrThrow(tableName, "table name");
try
{
var query = @"
SELECT COLUMN_NAME, DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @TableName";
using var command = new SqlCommand(query, _connection);
command.Parameters.AddWithValue("@TableName", tableName);
var columnTypes = new Dictionary(StringComparer.OrdinalIgnoreCase);
using var reader = command.ExecuteReader();
while (reader.Read())
{
var colName = reader.GetString(0);
// Validate column name immediately to prevent second-order SQL injection
IdentifierValidator.ValidateOrThrow(colName, "column name");
columnTypes[colName] = reader.GetString(1);
}
return columnTypes;
}
catch (Exception ex)
{
_logger.LogError("Error getting column types for table {TableName}: {Message}", tableName, ex.Message);
return new Dictionary();
}
}
///
/// Checks if a table exists in the database.
///
public bool TableExists(string tableName)
{
if (_connection == null)
throw new InvalidOperationException("Not connected to database");
IdentifierValidator.ValidateOrThrow(tableName, "table name");
try
{
var query = @"
SELECT COUNT(*)
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = @TableName AND TABLE_TYPE = 'BASE TABLE'";
using var command = new SqlCommand(query, _connection);
command.Parameters.AddWithValue("@TableName", tableName);
var count = command.GetScalarValue(0, _logger, $"table existence check for {tableName}");
return count > 0;
}
catch (Exception ex)
{
_logger.LogError("Error checking if table {TableName} exists: {Message}", tableName, ex.Message);
return false;
}
}
///
/// Gets the row count for a specific table.
///
public int GetTableRowCount(string tableName)
{
if (_connection == null)
throw new InvalidOperationException("Not connected to database");
IdentifierValidator.ValidateOrThrow(tableName, "table name");
try
{
var query = $"SELECT COUNT(*) FROM [{tableName}]";
using var command = new SqlCommand(query, _connection);
var count = command.GetScalarValue(0, _logger, $"row count for {tableName}");
_logger.LogDebug("Row count for {TableName}: {Count}", tableName, count);
return count;
}
catch (Exception ex)
{
_logger.LogError("Error getting row count for {TableName}: {Message}", tableName, ex.Message);
return 0;
}
}
///
/// Drops a table from the database.
///
public bool DropTable(string tableName)
{
if (_connection == null)
throw new InvalidOperationException("Not connected to database");
IdentifierValidator.ValidateOrThrow(tableName, "table name");
try
{
var query = $"DROP TABLE IF EXISTS [{tableName}]";
using var command = new SqlCommand(query, _connection);
command.ExecuteNonQuery();
_logger.LogInformation("Dropped table {TableName}", tableName);
return true;
}
catch (Exception ex)
{
_logger.LogError("Error dropping table {TableName}: {Message}", tableName, ex.Message);
return false;
}
}
private bool DropTrackingTable()
{
if (_connection == null)
throw new InvalidOperationException("Not connected to database");
try
{
var dropSql = $"DROP TABLE IF EXISTS {_trackingTableName}";
using var command = new SqlCommand(dropSql, _connection);
command.ExecuteNonQuery();
_logger.LogDebug("Dropped tracking table {TrackingTableName}", _trackingTableName);
return true;
}
catch (Exception ex)
{
_logger.LogWarning("Error dropping tracking table: {Message}", ex.Message);
return false;
}
}
///
/// Gets the list of constraints that need to be re-enabled from the tracking table.
///
private List<(string Schema, string Table, string Constraint)> GetConstraintsToReEnable()
{
if (_connection == null)
throw new InvalidOperationException("Not connected to database");
var constraints = new List<(string Schema, string Table, string Constraint)>();
try
{
// Check if tracking table exists
var checkSql = "SELECT COUNT(*) FROM sys.tables WHERE name = '_MigrationDisabledConstraint' AND schema_id = SCHEMA_ID('dbo')";
using var checkCommand = new SqlCommand(checkSql, _connection);
var count = checkCommand.GetScalarValue(0, _logger, "tracking table existence check");
var tableExists = count > 0;
if (!tableExists)
{
_logger.LogDebug("Tracking table does not exist, no constraints to re-enable");
return constraints;
}
// Get only constraints that we disabled (PreExistingDisabled = 0)
var querySql = $@"
SELECT SchemaName, TableName, ConstraintName
FROM {_trackingTableName}
WHERE PreExistingDisabled = 0";
using var command = new SqlCommand(querySql, _connection);
using var reader = command.ExecuteReader();
while (reader.Read())
{
var schema = reader.GetString(0);
var table = reader.GetString(1);
var constraint = reader.GetString(2);
// Validate all identifiers immediately to prevent second-order SQL injection
IdentifierValidator.ValidateOrThrow(schema, "schema name");
IdentifierValidator.ValidateOrThrow(table, "table name");
IdentifierValidator.ValidateOrThrow(constraint, "constraint name");
constraints.Add((schema, table, constraint));
}
_logger.LogDebug("Found {Count} constraints to re-enable from tracking table", constraints.Count);
}
catch (Exception ex)
{
_logger.LogWarning("Error reading tracking table: {Message}", ex.Message);
}
return constraints;
}
///
/// Disables all foreign key constraints and tracks them for re-enabling.
///
public bool DisableForeignKeys()
{
if (_connection == null)
throw new InvalidOperationException("Not connected to database");
try
{
_logger.LogInformation("Disabling foreign key constraints for SQL Server");
// Check if tracking table already exists
var checkSql = "SELECT COUNT(*) FROM sys.tables WHERE name = '_MigrationDisabledConstraint' AND schema_id = SCHEMA_ID('dbo')";
using (var checkCommand = new SqlCommand(checkSql, _connection))
{
var count = checkCommand.GetScalarValue(0, _logger, "tracking table existence check");
var tableExists = count > 0;
if (tableExists)
{
// Table exists - this means we're resuming from an interrupted run
// Constraints are already disabled and tracked
_logger.LogInformation("Tracking table already exists - resuming from previous interrupted run");
_logger.LogInformation("Foreign key constraints are already disabled");
return true;
}
}
// Table doesn't exist - this is a fresh run
// Create table and disable constraints in a transaction for atomicity
using var transaction = _connection.BeginTransaction();
try
{
// Create tracking table
var createSql = $@"
CREATE TABLE {_trackingTableName} (
SchemaName NVARCHAR(128) NOT NULL,
TableName NVARCHAR(128) NOT NULL,
ConstraintName NVARCHAR(128) NOT NULL,
PreExistingDisabled BIT NOT NULL,
DisabledAt DATETIME2 NOT NULL DEFAULT GETDATE()
)";
using (var createCommand = new SqlCommand(createSql, _connection, transaction))
{
createCommand.ExecuteNonQuery();
}
_logger.LogDebug("Created tracking table {TrackingTableName}", _trackingTableName);
// First, get all PRE-EXISTING disabled foreign key constraints
var preExistingQuery = @"
SELECT
OBJECT_SCHEMA_NAME(parent_object_id) AS schema_name,
OBJECT_NAME(parent_object_id) AS table_name,
name AS constraint_name
FROM sys.foreign_keys
WHERE is_disabled = 1";
var preExistingConstraints = new List<(string Schema, string Table, string Constraint)>();
using (var preCommand = new SqlCommand(preExistingQuery, _connection, transaction))
using (var preReader = preCommand.ExecuteReader())
{
while (preReader.Read())
{
preExistingConstraints.Add((
preReader.GetString(0),
preReader.GetString(1),
preReader.GetString(2)
));
}
}
// Store pre-existing disabled constraints
foreach (var (schema, table, constraint) in preExistingConstraints)
{
var insertSql = $@"
INSERT INTO {_trackingTableName} (SchemaName, TableName, ConstraintName, PreExistingDisabled)
VALUES (@Schema, @Table, @Constraint, 1)";
using var insertCommand = new SqlCommand(insertSql, _connection, transaction);
insertCommand.Parameters.AddWithValue("@Schema", schema);
insertCommand.Parameters.AddWithValue("@Table", table);
insertCommand.Parameters.AddWithValue("@Constraint", constraint);
insertCommand.ExecuteNonQuery();
}
if (preExistingConstraints.Count > 0)
{
_logger.LogInformation("Found {Count} pre-existing disabled constraints", preExistingConstraints.Count);
}
// Now get all ENABLED foreign key constraints
var enabledQuery = @"
SELECT
OBJECT_SCHEMA_NAME(parent_object_id) AS schema_name,
OBJECT_NAME(parent_object_id) AS table_name,
name AS constraint_name
FROM sys.foreign_keys
WHERE is_disabled = 0";
var constraints = new List<(string Schema, string Table, string Constraint)>();
using (var command = new SqlCommand(enabledQuery, _connection, transaction))
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
var schema = reader.GetString(0);
var table = reader.GetString(1);
var constraint = reader.GetString(2);
// Validate all identifiers immediately to prevent second-order SQL injection
IdentifierValidator.ValidateOrThrow(schema, "schema name");
IdentifierValidator.ValidateOrThrow(table, "table name");
IdentifierValidator.ValidateOrThrow(constraint, "constraint name");
constraints.Add((schema, table, constraint));
}
}
// Disable each enabled constraint and track it
var disabledCount = 0;
foreach (var (schema, table, constraint) in constraints)
{
try
{
// Validate identifiers to prevent SQL injection
IdentifierValidator.ValidateOrThrow(schema, "schema name");
IdentifierValidator.ValidateOrThrow(table, "table name");
IdentifierValidator.ValidateOrThrow(constraint, "constraint name");
// Disable the constraint
var disableSql = $"ALTER TABLE [{schema}].[{table}] NOCHECK CONSTRAINT [{constraint}]";
using var disableCommand = new SqlCommand(disableSql, _connection, transaction);
disableCommand.ExecuteNonQuery();
// Store in tracking table with PreExistingDisabled = false
var insertSql = $@"
INSERT INTO {_trackingTableName} (SchemaName, TableName, ConstraintName, PreExistingDisabled)
VALUES (@Schema, @Table, @Constraint, 0)";
using var insertCommand = new SqlCommand(insertSql, _connection, transaction);
insertCommand.Parameters.AddWithValue("@Schema", schema);
insertCommand.Parameters.AddWithValue("@Table", table);
insertCommand.Parameters.AddWithValue("@Constraint", constraint);
insertCommand.ExecuteNonQuery();
disabledCount++;
_logger.LogDebug("Disabled constraint: {Constraint} on {Schema}.{Table}", constraint, schema, table);
}
catch (Exception ex)
{
_logger.LogWarning("Could not disable constraint {Constraint}: {Message}", constraint, ex.Message);
}
}
// Commit the transaction - this makes everything atomic
transaction.Commit();
_logger.LogInformation("Disabled {Count} foreign key constraints", disabledCount);
return true;
}
catch
{
// If anything fails, rollback the transaction
// This ensures the tracking table doesn't exist with incomplete data
// Safely rollback transaction, preserving original exception
transaction.SafeRollback(_connection, _logger, "foreign key constraint disabling");
throw;
}
}
catch (Exception ex)
{
_logger.LogError("Error disabling foreign key constraints: {Message}", ex.Message);
return false;
}
}
///
/// Re-enables all foreign key constraints that were disabled.
///
public bool EnableForeignKeys()
{
if (_connection == null)
throw new InvalidOperationException("Not connected to database");
try
{
_logger.LogInformation("Re-enabling foreign key constraints for SQL Server");
// Get constraints that we disabled (PreExistingDisabled = 0) from tracking table
var constraintsToReEnable = GetConstraintsToReEnable();
if (constraintsToReEnable.Count == 0)
{
_logger.LogInformation("No constraints to re-enable");
// Still drop tracking table to clean up
DropTrackingTable();
return true;
}
var enabledCount = 0;
foreach (var (schema, table, constraint) in constraintsToReEnable)
{
try
{
IdentifierValidator.ValidateOrThrow(schema, "schema name");
IdentifierValidator.ValidateOrThrow(table, "table name");
IdentifierValidator.ValidateOrThrow(constraint, "constraint name");
var enableSql = $"ALTER TABLE [{schema}].[{table}] CHECK CONSTRAINT [{constraint}]";
using var command = new SqlCommand(enableSql, _connection);
command.ExecuteNonQuery();
enabledCount++;
_logger.LogDebug("Re-enabled constraint: {Constraint} on {Schema}.{Table}", constraint, schema, table);
}
catch (Exception ex)
{
_logger.LogWarning("Could not re-enable constraint {Constraint}: {Message}", constraint, ex.Message);
}
}
_logger.LogInformation("Re-enabled {EnabledCount}/{TotalCount} foreign key constraints", enabledCount, constraintsToReEnable.Count);
// Drop tracking table to clean up
DropTrackingTable();
return true;
}
catch (Exception ex)
{
_logger.LogError("Error re-enabling foreign key constraints: {Message}", ex.Message);
return false;
}
}
///
/// Creates a table from the provided schema definition.
///
public bool CreateTableFromSchema(
string tableName,
List columns,
Dictionary columnTypes,
List? specialColumns = null)
{
specialColumns ??= [];
if (_connection == null)
throw new InvalidOperationException("Not connected to database");
IdentifierValidator.ValidateOrThrow(tableName, "table name");
try
{
// Build column definitions
var sqlServerColumns = new List();
foreach (var colName in columns)
{
IdentifierValidator.ValidateOrThrow(colName, "column name");
var colType = columnTypes.GetValueOrDefault(colName, "NVARCHAR(MAX)");
// If it's a special JSON column, ensure it's a large text type
if (specialColumns.Contains(colName) &&
!colType.ToUpper().Contains("VARCHAR(MAX)") &&
!colType.ToUpper().Contains("TEXT"))
{
colType = "NVARCHAR(MAX)";
}
sqlServerColumns.Add($"[{colName}] {colType}");
}
// Build CREATE TABLE statement
var createSql = $@"
CREATE TABLE [{tableName}] (
{string.Join(",\n ", sqlServerColumns)}
)";
_logger.LogInformation("Creating table {TableName} in SQL Server", tableName);
_logger.LogDebug("CREATE TABLE SQL: {CreateSql}", createSql);
using var command = new SqlCommand(createSql, _connection);
command.ExecuteNonQuery();
_logger.LogInformation("Successfully created table {TableName}", tableName);
return true;
}
catch (Exception ex)
{
_logger.LogError("Error creating table {TableName}: {Message}", tableName, ex.Message);
return false;
}
}
///
/// Gets the list of identity columns for a table.
///
public List GetIdentityColumns(string tableName)
{
if (_connection == null)
throw new InvalidOperationException("Not connected to database");
IdentifierValidator.ValidateOrThrow(tableName, "table name");
try
{
var query = @"
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @TableName
AND COLUMNPROPERTY(OBJECT_ID(TABLE_SCHEMA + '.' + TABLE_NAME), COLUMN_NAME, 'IsIdentity') = 1";
using var command = new SqlCommand(query, _connection);
command.Parameters.AddWithValue("@TableName", tableName);
var columns = new List();
using var reader = command.ExecuteReader();
while (reader.Read())
{
columns.Add(reader.GetString(0));
}
return columns;
}
catch (Exception ex)
{
_logger.LogError("Error getting identity columns for table {TableName}: {Message}", tableName, ex.Message);
return [];
}
}
///
/// Enables IDENTITY_INSERT for a table to allow explicit identity values.
///
public bool EnableIdentityInsert(string tableName)
{
if (_connection == null)
throw new InvalidOperationException("Not connected to database");
IdentifierValidator.ValidateOrThrow(tableName, "table name");
try
{
var query = $"SET IDENTITY_INSERT [{tableName}] ON";
using var command = new SqlCommand(query, _connection);
command.ExecuteNonQuery();
_logger.LogDebug("Enabled IDENTITY_INSERT for {TableName}", tableName);
return true;
}
catch (Exception ex)
{
_logger.LogError("Error enabling IDENTITY_INSERT for {TableName}: {Message}", tableName, ex.Message);
return false;
}
}
///
/// Disables IDENTITY_INSERT for a table.
///
public bool DisableIdentityInsert(string tableName)
{
if (_connection == null)
throw new InvalidOperationException("Not connected to database");
IdentifierValidator.ValidateOrThrow(tableName, "table name");
try
{
var query = $"SET IDENTITY_INSERT [{tableName}] OFF";
using var command = new SqlCommand(query, _connection);
command.ExecuteNonQuery();
_logger.LogDebug("Disabled IDENTITY_INSERT for {TableName}", tableName);
return true;
}
catch (Exception ex)
{
_logger.LogError("Error disabling IDENTITY_INSERT for {TableName}: {Message}", tableName, ex.Message);
return false;
}
}
///
/// Imports data into a table using batch insert statements.
///
public bool ImportData(
string tableName,
List columns,
List