7.0 KiB
source_files, generated_at, model, schema_version, sha256
| source_files | generated_at | model | schema_version | sha256 | |||
|---|---|---|---|---|---|---|---|
|
2026-04-17T15:55:24.798276+00:00 | zai-org/GLM-5-FP8 | 1 | 0f84be859c5d0bf5 |
DatabaseMigrator Module Documentation
1. Purpose
The DatabaseMigrator module is a Windows Forms application that provides a GUI utility for migrating DataPRO databases between versions. It handles SQL Server Express LocalDB instance management, database backup creation, and version upgrades by orchestrating calls to DbOperations.Connection.UpgradeVersionsIfNeeded. The module exists to allow developers or administrators to upgrade database schemas from older versions (minimum version 61) to the current database version.
2. Public Interface
MigrateDatabase (static class)
static void Main(string[] args)
- Entry point for the application. Marked with
[STAThread]. - Currently ignores command-line arguments (parsing logic is commented out).
- Sets
targetDirtoEnvironment.CurrentDirectory. - Initializes visual styles and launches
MigrationForm.
MigrationForm (class, inherits from Form)
public MigrationForm(string targetDir)
- Constructor accepting the target directory path where database files reside.
- Initializes the form components and stores
targetDirin a private readonly field.
Event Handlers (private, wired to UI controls):
-
void buttonOK_Click(object sender, EventArgs e)- Initiates the migration process. Queries current database version, validates against desired version, creates a backup viaCopyLocalDB(), and callsDbOperations.Connection.UpgradeVersionsIfNeeded(). Displays appropriate warnings if versions match or current version exceeds desired. -
void buttonCancel_Click(object sender, EventArgs e)- Closes the form without performing migration. -
void MigrationForm_Load(object sender, EventArgs e)- InitializesnumericUpDownDesiredVersionwith minimum 61, maximumDbOperations.CURRENT_DB_VERSION, and default value of current version. Checks for existing local database and populatesTbDatabasePath. -
void numericUpDownDesiredVersion_ValueChanged(object sender, EventArgs e)- Updates_desiredDatabaseVersionfield from the numeric control.
Helper Methods (private):
-
int GetDatabaseVersion(bool usingLocalDatabase)- Retrieves current database version by calling stored proceduresp_DbVersionGet. Returns 0 on failure. -
bool CopyLocalDB()- Creates timestamped backup copies of.mdfand_log.ldffiles. -
string InstallDatabase()- Configures SQL LocalDB instance by stopping, deleting, creating, and startingDataPROInstance, then attaches both DataPRO and ISO databases. -
static string ProcessSqlLocalDbCommand(string command)- Executes SQL LocalDB utility commands. -
static string AttachDatabase(string targetDir, string dbName, string sqlDbFileName, string sqlLogFileName)- Attaches a database usingsqlcmd.exevia batch file. -
static string GetSqlServerLocalDBPath()- Queries Windows Registry to find the highest installed SQL Server LocalDB version path. -
static string SqlCommandProcessor(string sqlLocalDbExeFileName, string command)- Executes a SQL command-line utility and captures output. -
static string BatchCommandProcessor(string batchFileName, string dbName, string sqlDbFileName, string sqlLogFileName, string fullSqlcmdPath)- Executes a batch file for database operations. -
bool LocalDataExists()- Checks for existence ofDataPRO.mdf(SQL LocalDB) ordatapro.db(SQLite) files.
3. Invariants
- Version bounds:
numericUpDownDesiredVersion.Minimumis always 61;numericUpDownDesiredVersion.Maximumis alwaysDbOperations.CURRENT_DB_VERSION. - Database name: The database name is hardcoded as
"DataPRO"throughout the module. - Form behavior: The form is always
TopMost = trueand centered on screen (StartPosition = CenterScreen). - Backup naming: Backup files are named with timestamp format
YYYY_MM_DD HH_MMappended before the file extension. - SQL LocalDB instance name: The instance name is
DataPROInstance(fromSettings.Default.LocalDbDataPROInstance). - Migration direction: Migration only proceeds if
currentDatabaseVersion < _desiredDatabaseVersion. Equal or greater versions result in user warnings and no migration.
4. Dependencies
This module depends on:
System.Windows.Forms- Windows Forms UI frameworkSystem.Data.SqlClient- SQL Server database connectivityMicrosoft.Win32- Registry access for SQL LocalDB path detectionDTS.Common.Storage- ContainsDbOperationsandDbOperationsEnumDTS.Common.Enums- ContainsMigrationResultenumDTS.Common.Utils.Database- ContainsGetODBCToolsPath()methodDatabaseMigrator.Properties.Settings- Application settings for paths, instance names, and message strings
External dependencies (inferred from usage):
DbOperations.Connection- Must provideUpgradeVersionsIfNeeded(),Server,DBName,DbVersionpropertiesDbOperations.CURRENT_DB_VERSION- Constant defining maximum supported versionDbOperations.GetSQLCommand()- Factory method for SQL commandsMigrationResultenum with values:OK,ExceptionThrown,WarningAllowStreamingModesWasNotMigrated
5. Gotchas
-
Dead code: Command-line argument parsing in
Main()is fully commented out. The application always usesEnvironment.CurrentDirectoryregardless of arguments passed. -
Hardcoded values with "fix this" comments: Multiple locations contain hardcoded values that were intended to be configurable:
GetDatabaseVersion(true)- parameter is ignored;trueis hardcoded- Database name
"DataPRO"is hardcoded instead of usingtbDBName.Text(commented out) UpgradeVersionsIfNeededreceives hardcoded strings"previousdir","targetdir","applicationSettings"with a "fix this" comment
-
SQLite support is incomplete: The code contains commented-out SQLite handling in
GetDatabaseVersion()andLocalDataExists(). ThelocalSQLiteDataExistsflag is set but never acted upon. -
Silent failures:
GetDatabaseVersion()catches all exceptions and returns 0 without logging or rethrowing. This could mask connectivity or schema issues. -
Static mutable state: The
StringBuilder sbfield is static and reused acrossSqlCommandProcessorandBatchCommandProcessor. While it's cleared before each use, this could cause issues if methods are called concurrently. -
Commented-out exit codes: Multiple
Environment.Exit()calls are commented out after error conditions, meaning errors display a message box but allow the application to continue. -
Process output handling asymmetry:
SqlCommandProcessorandBatchCommandProcessorredirect both stdout and stderr, but only stderr is read asynchronously viaOutputHandler, while stdout is read synchronously withReadToEnd().