Class: KairosMcp::Storage::Importer
- Inherits:
-
Object
- Object
- KairosMcp::Storage::Importer
- Defined in:
- lib/kairos_mcp/storage/importer.rb
Overview
Importer: Import data from files to SQLite
This allows:
-
Migrating from file-based storage to SQLite
-
Rebuilding SQLite from exported files
-
Recovering from SQLite corruption using file backups
Import sources:
- Exported files (from Exporter)
- Original file-based storage (blockchain.json, action_log.jsonl)
- Knowledge directory (for metadata extraction)
Class Method Summary collapse
-
.import(input_dir:, db_path:, options: {}) ⇒ Hash
Import data from files to SQLite.
-
.import_action_log(db_path:, action_log_file:) ⇒ Integer
Import only action logs.
-
.import_blockchain(db_path:, blockchain_file:) ⇒ Integer
Import only blockchain.
-
.rebuild_from_files(db_path:, storage_dir: nil, knowledge_dir: nil, skills_dir: nil) ⇒ Hash
Rebuild SQLite from original file-based storage.
Class Method Details
.import(input_dir:, db_path:, options: {}) ⇒ Hash
Import data from files to SQLite
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 64 65 66 67 68 69 70 71 |
# File 'lib/kairos_mcp/storage/importer.rb', line 32 def import(input_dir:, db_path:, options: {}) require 'sqlite3' require_relative 'sqlite_backend' # Create or open database backend = SqliteBackend.new(path: db_path) results = { imported_at: Time.now.iso8601, input_dir: input_dir, db_path: db_path, blocks: 0, action_logs: 0, knowledge_meta: 0 } # Import blocks blockchain_file = File.join(input_dir, 'blockchain.json') if File.exist?(blockchain_file) results[:blocks] = import_blocks(backend, blockchain_file) end # Import action logs action_log_file = File.join(input_dir, 'action_log.jsonl') if File.exist?(action_log_file) results[:action_logs] = import_action_logs(backend, action_log_file) end # Import knowledge metadata = File.join(input_dir, 'knowledge_meta.json') if File.exist?() results[:knowledge_meta] = (backend, ) end results rescue LoadError => e { success: false, error: "SQLite not available: #{e.}" } rescue SQLite3::Exception => e { success: false, error: "Database error: #{e.}" } end |
.import_action_log(db_path:, action_log_file:) ⇒ Integer
Import only action logs
148 149 150 151 152 153 154 |
# File 'lib/kairos_mcp/storage/importer.rb', line 148 def import_action_log(db_path:, action_log_file:) require 'sqlite3' require_relative 'sqlite_backend' backend = SqliteBackend.new(path: db_path) import_action_logs(backend, action_log_file) end |
.import_blockchain(db_path:, blockchain_file:) ⇒ Integer
Import only blockchain
135 136 137 138 139 140 141 |
# File 'lib/kairos_mcp/storage/importer.rb', line 135 def import_blockchain(db_path:, blockchain_file:) require 'sqlite3' require_relative 'sqlite_backend' backend = SqliteBackend.new(path: db_path) import_blocks(backend, blockchain_file) end |
.rebuild_from_files(db_path:, storage_dir: nil, knowledge_dir: nil, skills_dir: nil) ⇒ Hash
Rebuild SQLite from original file-based storage
This imports from the original storage locations:
-
storage/blockchain.json
-
skills/action_log.jsonl
-
knowledge/ directory (extracts metadata from *.md files)
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/kairos_mcp/storage/importer.rb', line 85 def rebuild_from_files(db_path:, storage_dir: nil, knowledge_dir: nil, skills_dir: nil) storage_dir ||= KairosMcp.storage_dir knowledge_dir ||= KairosMcp.knowledge_dir skills_dir ||= KairosMcp.skills_dir require 'sqlite3' require_relative 'sqlite_backend' # Remove existing database if present FileUtils.rm_f(db_path) if File.exist?(db_path) # Create new database backend = SqliteBackend.new(path: db_path) results = { rebuilt_at: Time.now.iso8601, db_path: db_path, blocks: 0, action_logs: 0, knowledge_meta: 0 } # Import blockchain blockchain_file = File.join(storage_dir, 'blockchain.json') if File.exist?(blockchain_file) results[:blocks] = import_blocks(backend, blockchain_file) end # Import action logs action_log_file = File.join(skills_dir, 'action_log.jsonl') if File.exist?(action_log_file) results[:action_logs] = import_action_logs(backend, action_log_file) end # Extract and import knowledge metadata from *.md files results[:knowledge_meta] = (backend, knowledge_dir) results rescue LoadError => e { success: false, error: "SQLite not available: #{e.}" } rescue SQLite3::Exception => e { success: false, error: "Database error: #{e.}" } end |