1
0
mirror of https://github.com/bitwarden/server synced 2025-12-24 20:23:21 +00:00

Change dynamic importer variable to interface

This commit is contained in:
Mark Kincaid
2025-10-29 08:09:00 -07:00
parent e104fe6c5c
commit b91bf61c7b
6 changed files with 100 additions and 8 deletions

View File

@@ -0,0 +1,92 @@
namespace Bit.Seeder.Migration.Databases;
/// <summary>
/// Interface for database importers that migrate data from SQL Server to various database systems.
/// </summary>
public interface IDatabaseImporter : IDisposable
{
/// <summary>
/// Establishes a connection to the target database.
/// </summary>
/// <returns>True if connection was successful, false otherwise.</returns>
bool Connect();
/// <summary>
/// Closes the connection to the target database.
/// </summary>
void Disconnect();
/// <summary>
/// Creates a table in the target database from a schema definition.
/// </summary>
/// <param name="tableName">Name of the table to create.</param>
/// <param name="columns">List of column names.</param>
/// <param name="columnTypes">Dictionary mapping column names to their SQL Server data types.</param>
/// <param name="specialColumns">Optional list of columns that require special handling (e.g., JSON columns).</param>
/// <returns>True if table was created successfully, false otherwise.</returns>
bool CreateTableFromSchema(
string tableName,
List<string> columns,
Dictionary<string, string> columnTypes,
List<string>? specialColumns = null);
/// <summary>
/// Retrieves the list of column names for a table.
/// </summary>
/// <param name="tableName">Name of the table.</param>
/// <returns>List of column names.</returns>
List<string> GetTableColumns(string tableName);
/// <summary>
/// Imports data into a table.
/// </summary>
/// <param name="tableName">Name of the target table.</param>
/// <param name="columns">List of column names in the data.</param>
/// <param name="data">Data rows to import.</param>
/// <param name="batchSize">Number of rows to import per batch.</param>
/// <returns>True if import was successful, false otherwise.</returns>
bool ImportData(
string tableName,
List<string> columns,
List<object[]> data,
int batchSize = 1000);
/// <summary>
/// Checks if a table exists in the target database.
/// </summary>
/// <param name="tableName">Name of the table to check.</param>
/// <returns>True if table exists, false otherwise.</returns>
bool TableExists(string tableName);
/// <summary>
/// Gets the number of rows in a table.
/// </summary>
/// <param name="tableName">Name of the table.</param>
/// <returns>Number of rows in the table.</returns>
int GetTableRowCount(string tableName);
/// <summary>
/// Drops a table from the database.
/// </summary>
/// <param name="tableName">Name of the table to drop.</param>
/// <returns>True if table was dropped successfully, false otherwise.</returns>
bool DropTable(string tableName);
/// <summary>
/// Disables foreign key constraints to allow data import without referential integrity checks.
/// </summary>
/// <returns>True if foreign keys were disabled successfully, false otherwise.</returns>
bool DisableForeignKeys();
/// <summary>
/// Re-enables foreign key constraints after data import.
/// </summary>
/// <returns>True if foreign keys were enabled successfully, false otherwise.</returns>
bool EnableForeignKeys();
/// <summary>
/// Tests the connection to the database.
/// </summary>
/// <returns>True if connection test was successful, false otherwise.</returns>
bool TestConnection();
}

View File

@@ -5,7 +5,7 @@ using Microsoft.Extensions.Logging;
namespace Bit.Seeder.Migration.Databases;
public class MariaDbImporter(DatabaseConfig config, ILogger<MariaDbImporter> logger) : IDisposable
public class MariaDbImporter(DatabaseConfig config, ILogger<MariaDbImporter> logger) : IDatabaseImporter
{
private readonly ILogger<MariaDbImporter> _logger = logger;
private readonly string _host = config.Host;

View File

@@ -4,7 +4,7 @@ using Microsoft.Extensions.Logging;
namespace Bit.Seeder.Migration.Databases;
public class PostgresImporter(DatabaseConfig config, ILogger<PostgresImporter> logger) : IDisposable
public class PostgresImporter(DatabaseConfig config, ILogger<PostgresImporter> logger) : IDatabaseImporter
{
private readonly ILogger<PostgresImporter> _logger = logger;
private readonly string _host = config.Host;

View File

@@ -6,7 +6,7 @@ using System.Data;
namespace Bit.Seeder.Migration.Databases;
public class SqlServerImporter(DatabaseConfig config, ILogger<SqlServerImporter> logger) : IDisposable
public class SqlServerImporter(DatabaseConfig config, ILogger<SqlServerImporter> logger) : IDatabaseImporter
{
private readonly ILogger<SqlServerImporter> _logger = logger;
private readonly string _host = config.Host;

View File

@@ -5,7 +5,7 @@ using Microsoft.Extensions.Logging;
namespace Bit.Seeder.Migration.Databases;
public class SqliteImporter(DatabaseConfig config, ILogger<SqliteImporter> logger) : IDisposable
public class SqliteImporter(DatabaseConfig config, ILogger<SqliteImporter> logger) : IDatabaseImporter
{
private readonly ILogger<SqliteImporter> _logger = logger;
private readonly string _databasePath = config.Database;

View File

@@ -212,7 +212,7 @@ public class CsvMigrationRecipe(MigrationConfig config, ILoggerFactory loggerFac
return false;
}
dynamic? importer = CreateImporter(dbType, destConfig);
IDatabaseImporter? importer = CreateImporter(dbType, destConfig);
if (importer == null)
{
_logger.LogError("Failed to create importer for {DbType}", dbType);
@@ -354,7 +354,7 @@ public class CsvMigrationRecipe(MigrationConfig config, ILoggerFactory loggerFac
return false;
}
dynamic? importer = CreateImporter(dbType, destConfig);
IDatabaseImporter? importer = CreateImporter(dbType, destConfig);
if (importer == null)
{
_logger.LogError("Failed to create importer for {DbType}", dbType);
@@ -453,7 +453,7 @@ public class CsvMigrationRecipe(MigrationConfig config, ILoggerFactory loggerFac
return false;
}
dynamic? importer = CreateImporter(dbType, destConfig);
IDatabaseImporter? importer = CreateImporter(dbType, destConfig);
if (importer == null)
{
_logger.LogError("Failed to create importer for {DbType}", dbType);
@@ -481,7 +481,7 @@ public class CsvMigrationRecipe(MigrationConfig config, ILoggerFactory loggerFac
}
}
private dynamic? CreateImporter(string dbType, DatabaseConfig config) =>
private IDatabaseImporter? CreateImporter(string dbType, DatabaseConfig config) =>
dbType.ToLower() switch
{
"postgres" or "postgresql" => new PostgresImporter(config, _loggerFactory.CreateLogger<PostgresImporter>()),