Class: KairosMcp::Storage::Exporter

Inherits:
Object
  • Object
show all
Defined in:
lib/kairos_mcp/storage/exporter.rb

Overview

Exporter: Export data from SQLite to files

This allows:

  • Backing up SQLite data to human-readable files

  • Inspecting data without SQL commands

  • Migrating from SQLite back to file-based storage

Export structure:

export_path/
├── blockchain.json       # All blocks
├── action_log.jsonl      # Action log entries
└── knowledge_meta.json   # Knowledge metadata (content is in knowledge/*.md)

Class Method Summary collapse

Class Method Details

.export(db_path:, output_dir: nil) ⇒ Hash

Export all data from SQLite to files

Parameters:

  • db_path (String)

    Path to SQLite database

  • output_dir (String) (defaults to: nil)

    Directory to export to

Returns:

  • (Hash)

    Export results



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/kairos_mcp/storage/exporter.rb', line 30

def export(db_path:, output_dir: nil)
  output_dir ||= KairosMcp.export_dir
  require 'sqlite3'

  db = SQLite3::Database.new(db_path)
  FileUtils.mkdir_p(output_dir)

  results = {
    exported_at: Time.now.iso8601,
    db_path: db_path,
    output_dir: output_dir,
    blocks: 0,
    action_logs: 0,
    knowledge_meta: 0
  }

  # Export blocks
  results[:blocks] = export_blocks(db, output_dir)

  # Export action logs
  results[:action_logs] = export_action_logs(db, output_dir)

  # Export knowledge metadata
  results[:knowledge_meta] = export_knowledge_meta(db, output_dir)

  # Write export manifest
  write_manifest(output_dir, results)

  results
rescue LoadError => e
  { success: false, error: "SQLite not available: #{e.message}" }
rescue SQLite3::Exception => e
  { success: false, error: "Database error: #{e.message}" }
end

.export_action_log(db_path:, output_file:) ⇒ Integer

Export only action logs

Parameters:

  • db_path (String)

    Path to SQLite database

  • output_file (String)

    Output file path

Returns:

  • (Integer)

    Number of entries exported



83
84
85
86
87
88
89
# File 'lib/kairos_mcp/storage/exporter.rb', line 83

def export_action_log(db_path:, output_file:)
  require 'sqlite3'

  db = SQLite3::Database.new(db_path)
  FileUtils.mkdir_p(File.dirname(output_file))
  export_action_logs(db, File.dirname(output_file), File.basename(output_file))
end

.export_blockchain(db_path:, output_file:) ⇒ Integer

Export only blockchain data

Parameters:

  • db_path (String)

    Path to SQLite database

  • output_file (String)

    Output file path

Returns:

  • (Integer)

    Number of blocks exported



70
71
72
73
74
75
76
# File 'lib/kairos_mcp/storage/exporter.rb', line 70

def export_blockchain(db_path:, output_file:)
  require 'sqlite3'

  db = SQLite3::Database.new(db_path)
  FileUtils.mkdir_p(File.dirname(output_file))
  export_blocks(db, File.dirname(output_file), File.basename(output_file))
end