public void Close() { if (!IsOpen) { return; } ProjectsTableAdapter.Connection.Close(); MilestonesTableAdapter.Connection.Close(); TicketReportersTableAdapter.Connection.Close(); TicketAssigneesTableAdapter.Connection.Close(); TicketsTableAdapter.Connection.Close(); AttachmentsWithoutContentsTableAdapter.Connection.Close(); AttachmentsTableAdapter.Connection.Close(); TicketsHistoryTableAdapter.Connection.Close(); VersionTableAdapter.Connection.Close(); ProjectsTableAdapter = null; MilestonesTableAdapter = null; TicketReportersTableAdapter = null; TicketAssigneesTableAdapter = null; TicketsTableAdapter = null; AttachmentsWithoutContentsTableAdapter = null; AttachmentsTableAdapter = null; TicketsHistoryTableAdapter = null; VersionTableAdapter = null; CurrentDatabasePath = null; IsOpen = false; }
private static void ExecuteQuery(VersionTableAdapter tableAdapter, string query) { using (var cmd = tableAdapter.Connection.CreateCommand()) { cmd.CommandType = CommandType.Text; cmd.CommandText = query; cmd.ExecuteNonQuery(); } }
protected override void UpdatePrivate(Database db) { VersionTableAdapter tableAdapter = db.VersionTableAdapter; tableAdapter.Update(ID + 1, ID); ID++; }
public static Version Get(IDatabaseProvider db) { VersionTableAdapter tableAdapter = db.DB.VersionTableAdapter; var tableData = tableAdapter.GetData(); // This should never be the case outside of manual tampering // In which case, there's not really a suitable way to recover if (tableData.Count != 1) { throw new ManualTamperingException(Resources.String_VersionManualTampering); } return(new Version(tableData[0])); }
public static void UpdateIfAppropriate(Database db) { VersionTableAdapter tableAdapter = db.VersionTableAdapter; attempt : { try { var tableData = tableAdapter.GetData(); // This should never be the case outside of manual tampering // In which case, there's not really a suitable way to recover if (tableData.Count != 1) { throw new ManualTamperingException(Resources.String_VersionManualTampering); } var data = new Version(tableData[0]); int current = data.ID; // This should never be the case outside of manual tampering // In which case, there's not really a suitable way to recover if (current < 1) { throw new ManualTamperingException(Resources.String_VersionManualTampering); } // This DB is produced with a future version of the software // It may be backward-incompatible with this version but it also may not // For now, we'll play it safe and throw if (current > CurrentVersion) { throw new BackwardCompatibilityException(Resources.String_VersionBackwardCompatibility); } // Nothing to do here if (current == CurrentVersion) { return; } applyUpdate : { switch (current) { // Add migrations as appropriate // These are specified in terms of current -> next // So if I'm looking to update to version 2, I should be putting my query in case 1 case 1 : ExecuteQuery(tableAdapter, "ALTER TABLE `Projects` ADD State INT NULL"); ExecuteQuery(tableAdapter, "UPDATE `Projects` SET State = 1"); ExecuteQuery(tableAdapter, "ALTER TABLE `Projects` ALTER COLUMN State INT NOT NULL"); break; } current++; data.UpdatePrivate(db); if (current < CurrentVersion) { goto applyUpdate; } } } catch (OleDbException) { // We don't have a version table, we can automatically make a new one ExecuteQuery(tableAdapter, "CREATE TABLE `Version` (ID INTEGER NOT NULL CONSTRAINT PK_Version PRIMARY KEY)"); ExecuteQuery(tableAdapter, "INSERT INTO `Version` VALUES (1)"); // We might have multiple versions to migrate after installing the table goto attempt; } } }