ubiquity-database 0.1.1

Database abstraction layer for Ubiquity supporting SQLite and Astra DB
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# Ubiquity Database Migration Guide: SQLite to Astra DB

This guide provides detailed instructions for migrating from SQLite (local development) to DataStax Astra DB (cloud/enterprise deployment).

## Overview

The Ubiquity database layer supports seamless migration between backends while preserving:
- Consciousness states and history
- Task queues and results
- Memory pool data
- Vector embeddings

## Prerequisites

1. **Astra DB Account**
   - Sign up at [astra.datastax.com]https://astra.datastax.com
   - Create a new Serverless (Vector) database
   - Enable vector search capabilities

2. **API Credentials**
   - Generate an application token with Database Administrator role
   - Note your database endpoint URL
   - Configure embedding provider (OpenAI recommended)

3. **Astra Streaming (Optional)**
   - Create a streaming tenant for pub/sub
   - Generate streaming credentials

## Migration Steps

### Step 1: Prepare Astra DB

```bash
# Set environment variables
export ASTRA_DB_ENDPOINT="https://YOUR-DB-ID-REGION.apps.astra.datastax.com"
export ASTRA_DB_TOKEN="AstraCS:YOUR_TOKEN"
export ASTRA_DB_KEYSPACE="ubiquity"
export OPENAI_API_KEY="your-openai-key"
```

### Step 2: Initialize Astra Schema

```rust
use ubiquity_database::{DatabaseConfig, DatabaseBackend, create_database};

// Create Astra configuration
let mut config = DatabaseConfig::default();
config.backend = DatabaseBackend::Astra;
config.astra.endpoint = std::env::var("ASTRA_DB_ENDPOINT")?;
config.astra.token = std::env::var("ASTRA_DB_TOKEN")?;
config.astra.keyspace = std::env::var("ASTRA_DB_KEYSPACE")?;

// Initialize Astra DB
let astra_db = create_database(config.clone()).await?;
astra_db.initialize().await?;
```

### Step 3: Export SQLite Data

Create a migration tool:

```rust
use ubiquity_database::{DatabaseConfig, DatabaseBackend, create_database};
use chrono::{DateTime, Utc, Duration};

async fn export_sqlite_data() -> Result<MigrationData, Box<dyn Error>> {
    // Connect to SQLite
    let mut config = DatabaseConfig::default();
    config.backend = DatabaseBackend::Sqlite;
    config.sqlite.path = PathBuf::from("ubiquity.db");
    
    let sqlite_db = create_database(config).await?;
    
    // Export all data
    let mut data = MigrationData::default();
    
    // Get all agents
    let agents = get_all_agents(&sqlite_db).await?;
    
    // Export consciousness states (last 30 days per agent)
    for agent_id in &agents {
        let states = sqlite_db.get_consciousness_history(agent_id, 10000).await?;
        data.consciousness_states.extend(states);
    }
    
    // Export ripples (last 7 days)
    let end = Utc::now();
    let start = end - Duration::days(7);
    data.ripples = sqlite_db.get_ripples(start, end).await?;
    
    // Export pending tasks
    data.tasks = sqlite_db.get_pending_tasks().await?;
    
    // Export memory pool data
    data.memory_pool_data = export_memory_pools().await?;
    
    Ok(data)
}

#[derive(Default)]
struct MigrationData {
    consciousness_states: Vec<ConsciousnessState>,
    ripples: Vec<ConsciousnessRipple>,
    tasks: Vec<Task>,
    memory_pool_data: Vec<(String, Vec<u8>)>,
}
```

### Step 4: Import to Astra DB

```rust
async fn import_to_astra(data: MigrationData) -> Result<(), Box<dyn Error>> {
    // Connect to Astra
    let mut config = DatabaseConfig::default();
    config.backend = DatabaseBackend::Astra;
    config.astra.endpoint = std::env::var("ASTRA_DB_ENDPOINT")?;
    config.astra.token = std::env::var("ASTRA_DB_TOKEN")?;
    
    let astra_db = create_database(config.clone()).await?;
    let memory_pools = create_memory_pools(config).await?;
    
    // Import consciousness states with progress
    println!("Importing {} consciousness states...", data.consciousness_states.len());
    let pb = ProgressBar::new(data.consciousness_states.len() as u64);
    
    for state in &data.consciousness_states {
        astra_db.store_consciousness_state(state).await?;
        pb.inc(1);
    }
    pb.finish();
    
    // Import ripples
    println!("Importing {} ripples...", data.ripples.len());
    for ripple in &data.ripples {
        astra_db.store_ripple(ripple).await?;
    }
    
    // Import tasks
    println!("Importing {} tasks...", data.tasks.len());
    for task in &data.tasks {
        astra_db.store_task(task).await?;
    }
    
    // Import memory pool data
    println!("Importing {} memory pool items...", data.memory_pool_data.len());
    for (key, value) in &data.memory_pool_data {
        let pool = memory_pools.get_pool_for_key(key).await?;
        pool.store(key, value).await?;
    }
    
    println!("Migration completed successfully!");
    Ok(())
}
```

### Step 5: Verify Migration

```rust
async fn verify_migration() -> Result<(), Box<dyn Error>> {
    let astra_db = create_astra_database().await?;
    
    // Verify record counts
    let agents = get_all_agents(&astra_db).await?;
    println!("Migrated agents: {}", agents.len());
    
    for agent_id in &agents {
        let history = astra_db.get_consciousness_history(agent_id, 10).await?;
        println!("  Agent {}: {} states", agent_id, history.len());
    }
    
    // Test vector search
    let sample_embedding = vec![0.1; 1536];
    let results = astra_db.vector_search(&sample_embedding, 5).await?;
    println!("Vector search returned {} results", results.len());
    
    // Test hybrid search
    let query = HybridSearchQuery {
        text: "consciousness breakthrough".to_string(),
        vector: None,
        filters: None,
        limit: 10,
        rerank: true,
    };
    let results = astra_db.hybrid_search(query).await?;
    println!("Hybrid search returned {} results", results.len());
    
    Ok(())
}
```

## Configuration Updates

### Update Application Configuration

```toml
# config.toml
[database]
backend = "astra"  # Changed from "sqlite"

[database.astra]
endpoint = "${ASTRA_DB_ENDPOINT}"
token = "${ASTRA_DB_TOKEN}"
keyspace = "ubiquity"

[database.astra.collections]
consciousness = "consciousness_states"
ripples = "consciousness_ripples"
tasks = "tasks"
pool_prefix = "memory_pool_"

[database.embeddings]
provider = "openai"
model = "text-embedding-3-small"
dimension = 1536
cache_enabled = true
cache_size = 10000
```

### Update Docker Compose

```yaml
version: '3.8'

services:
  ubiquity:
    image: ubiquity:latest
    environment:
      - DATABASE_BACKEND=astra
      - ASTRA_DB_ENDPOINT=${ASTRA_DB_ENDPOINT}
      - ASTRA_DB_TOKEN=${ASTRA_DB_TOKEN}
      - OPENAI_API_KEY=${OPENAI_API_KEY}
    # Remove volume mount for SQLite
    # volumes:
    #   - ./data:/app/data
```

## Performance Optimization

### 1. Batch Operations

```rust
// Instead of individual inserts
for state in states {
    db.store_consciousness_state(&state).await?;
}

// Use batch operations
let documents: Vec<Value> = states.iter()
    .map(|s| create_document(s))
    .collect();

astra_client.insert_many(documents).await?;
```

### 2. Configure Vector Indexing

```json
{
  "createCollection": {
    "name": "consciousness_states",
    "options": {
      "vector": {
        "dimension": 1536,
        "metric": "cosine",
        "service": {
          "provider": "openai",
          "modelName": "text-embedding-3-small"
        }
      },
      "indexing": {
        "allow": ["agent_id", "level", "phase", "timestamp"],
        "deny": ["embedding"]  // Don't index raw embeddings
      }
    }
  }
}
```

### 3. Optimize Queries

```rust
// Use projections to reduce data transfer
let query = json!({
    "find": {
        "filter": { "agent_id": agent_id },
        "projection": {
            "level": 1,
            "coherence": 1,
            "timestamp": 1
        },
        "limit": 100
    }
});
```

## Rollback Plan

If issues occur, you can rollback to SQLite:

1. **Keep SQLite Updated**: Continue writing to both databases during transition
2. **Switch Config**: Change `backend = "sqlite"` in configuration
3. **Restart Services**: Restart all Ubiquity services

## Monitoring

### Astra DB Metrics

Monitor via Astra Portal:
- Request rate and latency
- Storage usage
- Vector search performance
- Rate limit consumption

### Application Metrics

```rust
// Add metrics collection
use prometheus::{Counter, Histogram};

static DB_QUERIES: Counter = Counter::new("db_queries_total", "Total database queries");
static QUERY_DURATION: Histogram = Histogram::new("db_query_duration_seconds", "Query duration");

// Track operations
let timer = QUERY_DURATION.start_timer();
let result = db.vector_search(&embedding, 10).await?;
timer.observe_duration();
DB_QUERIES.inc();
```

## Common Issues

### Issue 1: Rate Limiting

**Symptom**: 429 errors from Astra DB

**Solution**:
```rust
use tokio::time::{sleep, Duration};
use backoff::{ExponentialBackoff, backoff::Backoff};

async fn with_retry<T, F, Fut>(f: F) -> Result<T, Error>
where
    F: Fn() -> Fut,
    Fut: Future<Output = Result<T, Error>>,
{
    let mut backoff = ExponentialBackoff::default();
    
    loop {
        match f().await {
            Ok(result) => return Ok(result),
            Err(e) if e.is_rate_limit() => {
                if let Some(duration) = backoff.next() {
                    sleep(duration).await;
                } else {
                    return Err(e);
                }
            }
            Err(e) => return Err(e),
        }
    }
}
```

### Issue 2: Embedding Dimension Mismatch

**Symptom**: Vector operations fail

**Solution**:
1. Verify embedding model configuration
2. Re-generate embeddings if needed:

```rust
async fn regenerate_embeddings() -> Result<(), Error> {
    let states = db.get_all_consciousness_states().await?;
    
    for state in states {
        let embedding = generator.generate(&state.to_text()).await?;
        db.update_embedding(&state.id, &embedding).await?;
    }
    
    Ok(())
}
```

### Issue 3: Connection Timeouts

**Symptom**: Timeout errors on large operations

**Solution**:
```rust
// Increase timeout for migration
let client = reqwest::Client::builder()
    .timeout(Duration::from_secs(300))
    .build()?;
```

## Best Practices

1. **Test in Staging**: Always test migration in a staging environment first
2. **Backup Data**: Keep SQLite backups until migration is verified
3. **Monitor Performance**: Track query latency and throughput
4. **Gradual Migration**: Consider migrating by agent or time period
5. **Document Changes**: Update runbooks and documentation

## Support

For migration assistance:
- Astra DB Documentation: [docs.datastax.com]https://docs.datastax.com
- Ubiquity Discord: [discord.gg/ubiquity]https://discord.gg/ubiquity
- GitHub Issues: [github.com/ubiquity/ubiquity-rs]https://github.com/ubiquity/ubiquity-rs