Skip to content

Commit

Permalink
Use sqlite to reverse table
Browse files Browse the repository at this point in the history
  • Loading branch information
eatradish committed Feb 13, 2025
1 parent eaff30a commit fee567f
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions oma-history/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub fn create_and_maybe_migration_from_oma_db_v2(conn: &Connection) -> HistoryRe
.map_err(HistoryError::ExecuteError)?;

if old_db_count != 0 && new_db_count == 0 {
info!("Mirgration from old history table");
info!("Migrating oma history database, this may take a few minutes ...");
migration_from_oma_db_v2(conn)?;
}

Expand Down Expand Up @@ -56,8 +56,8 @@ fn handle_packages_items(items: &[String]) -> String {
fn migration_from_oma_db_v2(conn: &Connection) -> HistoryResult<()> {
let table = get_old_table(conn)?;

for i in table.iter().rev() {
let command = match &i.summary_type {
for entry in table {
let command = match &entry.summary_type {
OldSummaryType::Install(items) => {
format!("oma install {}", handle_packages_items(items))
}
Expand Down Expand Up @@ -87,39 +87,43 @@ fn migration_from_oma_db_v2(conn: &Connection) -> HistoryResult<()> {
INSERT_NEW_MAIN_TABLE,
(
command,
i.time,
i.inner.is_success,
i.inner.disk_size,
i.inner.total_download_size,
i.inner
entry.time,
entry.inner.is_success,
entry.inner.disk_size,
entry.inner.total_download_size,
entry
.inner
.install
.iter()
.filter(|x| x.operation == InstallOperation::Install)
.count(),
i.inner.remove.len(),
i.inner
entry.inner.remove.len(),
entry
.inner
.install
.iter()
.filter(|x| x.operation == InstallOperation::Upgrade)
.count(),
i.inner
entry
.inner
.install
.iter()
.filter(|x| x.operation == InstallOperation::Downgrade)
.count(),
i.inner
entry
.inner
.install
.iter()
.filter(|x| x.operation == InstallOperation::ReInstall)
.count(),
i.summary_type == OldSummaryType::FixBroken,
i.summary_type == OldSummaryType::Undo,
entry.summary_type == OldSummaryType::FixBroken,
entry.summary_type == OldSummaryType::Undo,
),
|row| row.get(0),
)
.map_err(HistoryError::ExecuteError)?;

for i in &i.inner.install {
for i in &entry.inner.install {
let op: u8 = i.operation.into();
let op = op as i64;

Expand All @@ -140,7 +144,7 @@ fn migration_from_oma_db_v2(conn: &Connection) -> HistoryResult<()> {
.map_err(HistoryError::ExecuteError)?;
}

for j in &i.inner.remove {
for j in &entry.inner.remove {
conn.execute(
INSERT_REMOVE_TABLE,
(id, &j.pkg_name, &j.version, j.size, &j.arch),
Expand Down Expand Up @@ -192,7 +196,7 @@ pub enum OldSummaryType {

fn get_old_table(conn: &Connection) -> Result<Vec<OldTableEntry>, HistoryError> {
let mut stmt = conn
.prepare("SELECT id, time, install_packages, remove_packages, disk_size, total_download_size, is_success, typ FROM \"history_oma_1.2\" ORDER BY id DESC")
.prepare("SELECT id, time, install_packages, remove_packages, disk_size, total_download_size, is_success, typ FROM \"history_oma_1.2\" ORDER BY id ASC")
.map_err(HistoryError::ExecuteError)?;
let res_iter = stmt
.query_map([], |row| {
Expand Down

0 comments on commit fee567f

Please sign in to comment.