Module: Timet::DatabaseSyncHelper

Extended by:
DatabaseSyncer
Defined in:
lib/timet/database_sync_helper.rb

Overview

Helper module for database synchronization operations Provides methods for comparing and syncing local and remote databases

Constant Summary

Constants included from DatabaseSyncer

Timet::DatabaseSyncer::ITEM_FIELDS

Class Method Summary collapse

Methods included from DatabaseSyncer

format_status_message, get_item_values, handle_database_differences, handle_sync_error, insert_item_from_hash, items_to_hash, open_remote_database, process_database_items, process_existing_item, remote_wins?, sync_databases, sync_items_by_id, sync_with_remote_database, update_item_from_hash

Class Method Details

.databases_are_in_sync?(remote_path, local_path) ⇒ Boolean

Note:

Uses MD5 hashing to compare file contents

Compares two database files to check if they are identical

Parameters:

  • remote_path (String)

    Path to the remote database file

  • local_path (String)

    Path to the local database file

Returns:

  • (Boolean)

    true if databases are identical, false otherwise



74
75
76
77
78
# File 'lib/timet/database_sync_helper.rb', line 74

def self.databases_are_in_sync?(remote_path, local_path)
  remote_md5 = Digest::MD5.file(remote_path).hexdigest
  local_md5 = Digest::MD5.file(local_path).hexdigest
  remote_md5 == local_md5
end

.process_remote_database(local_db, remote_storage, bucket, local_db_path) ⇒ void

Note:

This method orchestrates the entire sync process by downloading the remote database, comparing it with the local database, and handling any differences found

This method returns an undefined value.

Processes the remote database by comparing it with the local database and syncing changes

Parameters:

  • local_db (SQLite3::Database)

    The local database connection

  • remote_storage (S3Supabase)

    The remote storage client for cloud operations

  • bucket (String)

    The S3 bucket name

  • local_db_path (String)

    Path to the local database file



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/timet/database_sync_helper.rb', line 41

def self.process_remote_database(local_db, remote_storage, bucket, local_db_path)
  with_temp_file do |temp_file|
    remote_storage.download_file(bucket, 'timet.db', temp_file.path)

    if databases_are_in_sync?(temp_file.path, local_db_path)
      puts 'Local database is up to date'
    else
      handle_database_differences(local_db, remote_storage, bucket, local_db_path, temp_file.path)
    end
  end
end

.sync(local_db, bucket) ⇒ void

Note:

This method initiates the database synchronization process by checking for the presence of a remote database

This method returns an undefined value.

Main entry point for database synchronization

Parameters:

  • local_db (SQLite3::Database)

    The local database connection

  • bucket (String)

    The S3 bucket name



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/timet/database_sync_helper.rb', line 19

def self.sync(local_db, bucket)
  remote_storage = S3Supabase.new
  remote_storage.create_bucket(bucket)

  objects = remote_storage.list_objects(bucket)
  if objects&.any? { |obj| obj[:key] == 'timet.db' }
    process_remote_database(local_db, remote_storage, bucket, Timet::Database::DEFAULT_DATABASE_PATH)
  else
    puts 'No remote database found, uploading local database'
    remote_storage.upload_file(bucket, Timet::Database::DEFAULT_DATABASE_PATH, 'timet.db')
  end
end

.with_temp_file {|Tempfile| ... } ⇒ void

Note:

This method ensures proper resource cleanup by using ensure block

This method returns an undefined value.

Creates a temporary file and ensures it is properly cleaned up after use

Yields:

  • (Tempfile)

    The temporary file object to use



58
59
60
61
62
63
64
65
66
# File 'lib/timet/database_sync_helper.rb', line 58

def self.with_temp_file
  temp_file = Tempfile.new('remote_db')
  raise 'Temporary file path is nil' unless temp_file.path

  yield temp_file
ensure
  temp_file.close
  temp_file.unlink
end