parsql-cli 0.5.0

Command-line interface for parsql database toolkit
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
411
412
# parsql-cli

Command-line interface for the parsql database toolkit and migration system with interactive TUI mode.

## Features

- 🎨 **Interactive TUI Mode**: Beautiful terminal interface with Ratatui (Claude Code style)
- 📋 **Command Mode**: Traditional CLI for scripts and automation
- 🔄 **Migration Management**: Create, run, rollback, and track database migrations
- 🗄️ **Multi-Database Support**: PostgreSQL and SQLite
-**Real-time Status**: Live migration status and progress tracking
- 🎯 **Auto-completion**: Smart command suggestions in interactive mode

## Installation

```bash
cargo install parsql-cli
```

Or build from source:
```bash
git clone https://github.com/yazdostum-nettr/parsql
cd parsql/parsql-cli
cargo install --path .
```

## Quick Start

### 1. Global Installation
```bash
# Install from crates.io (when available)
cargo install parsql-cli

# Or install from source
git clone https://github.com/yazdostum-nettr/parsql
cd parsql
cargo install --path parsql-cli
```

### 2. Initialize Project
```bash
# Initialize in current directory
parsql init .

# This creates:
# - parsql.toml (configuration file)
# - migrations/ (directory for migration files)  
# - .gitignore (ignores database files)
# - README.md (project documentation)
```

### 3. Create Your First Migration
```bash
parsql migrate create create_users_table

# Edit the generated SQL files:
# - migrations/{timestamp}_create_users_table.up.sql
# - migrations/{timestamp}_create_users_table.down.sql
```

### 4. Run Migrations
```bash
# Set database URL
export DATABASE_URL="sqlite:database.db"
# or
export DATABASE_URL="postgresql://user:password@localhost/dbname"

# Run all pending migrations
parsql migrate run

# Check status
parsql migrate status --detailed

# Rollback if needed
parsql migrate rollback --to 0
```

### 5. Interactive TUI Mode (Optional)
```bash
# Launch interactive mode
parsql --interactive
# or just
parsql
```

In TUI mode:
- Use arrow keys to navigate
- Press `Enter` to select items
- Press `/` for command mode
- Press `?` for help
- `Ctrl+Q` to quit

## Configuration

Create a `parsql.toml` file in your project root:

```toml
[database]
url = "postgresql://user:pass@localhost/dbname"
# Or for SQLite:
# url = "sqlite:app.db"

[migrations]
directory = "migrations"
table_name = "schema_migrations"
verify_checksums = true
allow_out_of_order = false
transaction_per_migration = true
```

### Environment Variables

You can override configuration with environment variables:
- `DATABASE_URL` - Database connection string
- `PARSQL_MIGRATIONS_DIR` - Migrations directory
- `PARSQL_CONFIG` - Path to config file

## Interactive TUI Mode

The interactive mode provides a rich terminal interface inspired by Claude Code:

### Navigation
- **Tab**: Switch between views (Migrations, Logs, Config)
- **↑/↓** or **j/k**: Navigate lists
- **Enter**: Select/open item
- **ESC** or **q**: Go back

### Command Input (`/` key)
When you press `/`, a command input appears at the bottom with auto-completion:

Available commands:
- `/help` or `/h` - Show help
- `/quit` or `/q` - Exit application
- `/connect <url>` - Connect to database
- `/create <name>` - Create new migration
- `/run` - Run pending migrations
- `/rollback <version>` - Rollback to version
- `/status` - Show migration status
- `/validate` - Validate migrations
- `/list` - List migrations
- `/logs` - Show application logs
- `/config` - Show configuration
- `/refresh` - Refresh data

### Views

#### Migration List View
- Shows all migrations with status (Applied/Pending)
- Color-coded for easy identification
- Quick actions: `r` to refresh, `a` to apply all

#### Migration Detail View
- Shows SQL content of selected migration
- Actions: `r` to run, `b` to rollback

#### Logs View
- Real-time application logs
- Color-coded by severity

#### Configuration View
- Current database connection
- Migration settings

## Commands (CLI Mode)

### `parsql init`

Initialize a new parsql project in the current directory.

Options:
- `--database-url <URL>` - Set initial database URL
- `--migrations-dir <DIR>` - Set migrations directory (default: "migrations")

### `parsql migrate create`

Create a new migration file.

```bash
parsql migrate create "add users table" --migration-type sql
```

Options:
- `--migration-type <TYPE>` - Migration type: `sql` or `rust` (default: sql)

Creates:
- `{timestamp}_{name}.up.sql` - Forward migration
- `{timestamp}_{name}.down.sql` - Rollback migration

### `parsql migrate run`

Run all pending migrations.

```bash
parsql migrate run
parsql migrate run --database-url sqlite:test.db
parsql migrate run --target 20240101120000  # Run up to specific version
parsql migrate run --dry-run  # Show what would be run
```

Options:
- `--database-url <URL>` - Override database URL
- `--target <VERSION>` - Run migrations up to this version
- `--dry-run` - Show migrations without running them

### `parsql migrate rollback`

Rollback migrations to a specific version.

```bash
parsql migrate rollback --to 20240101000000
parsql migrate rollback --to 0  # Rollback all
```

Options:
- `--to <VERSION>` - Target version (required)
- `--database-url <URL>` - Override database URL
- `--dry-run` - Show what would be rolled back

### `parsql migrate status`

Show migration status.

```bash
parsql migrate status
parsql migrate status --detailed  # Show checksums and timestamps
```

Options:
- `--database-url <URL>` - Override database URL
- `--detailed` - Show detailed information

Example output:
```
Migration Status:
  10 Total migrations
  7 Applied migrations
  3 Pending migrations

Detailed Status:
Version         Name                Status    Applied At           Checksum
--------------  ------------------  --------  -------------------  ----------
20240101120000  create_users_table  Applied   20.5.01-01 12:00:00  ✓
20240102130000  add_posts_table     Applied   20.5.01-02 13:00:00  ✓
20240103140000  add_comments        Pending   -                    -
```

### `parsql migrate validate`

Validate migration files.

```bash
parsql migrate validate
parsql migrate validate --check-gaps
parsql migrate validate --verify-checksums
```

Options:
- `--check-gaps` - Check for version gaps
- `--verify-checksums` - Verify migration checksums

### `parsql migrate list`

List available migrations.

```bash
parsql migrate list
parsql migrate list --pending  # Only pending
parsql migrate list --applied  # Only applied
```

Options:
- `--pending` - Show only pending migrations
- `--applied` - Show only applied migrations

## Migration File Format

### SQL Migrations

**Up migration** (`{version}_{timestamp}_{name}.up.sql`):
```sql
-- Migration: create_users_table
-- Version: 20240101120000
-- Created: 20.5.01-01 12:00:00

CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    email VARCHAR(255) NOT NULL UNIQUE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
```

**Down migration** (`{version}_{timestamp}_{name}.down.sql`):
```sql
-- Migration: create_users_table (rollback)
-- Version: 20240101120000
-- Created: 20.5.01-01 12:00:00

DROP TABLE IF EXISTS users;
```

### Rust Migrations

For complex migrations, you can use Rust:

```rust
//! Migration: complex_data_migration
//! Version: 20240101120000
//! Created: 20.5.01-01 12:00:00

use parsql_migrations::prelude::*;

pub struct Migration20240101120000;

impl Migration for Migration20240101120000 {
    fn version(&self) -> i64 {
        20240101120000
    }
    
    fn name(&self) -> &str {
        "complex_data_migration"
    }
    
    fn up(&self, conn: &mut dyn MigrationConnection) -> Result<(), MigrationError> {
        // Complex migration logic
        conn.execute("ALTER TABLE users ADD COLUMN status VARCHAR(50)")?;
        // Update data...
        Ok(())
    }
    
    fn down(&self, conn: &mut dyn MigrationConnection) -> Result<(), MigrationError> {
        conn.execute("ALTER TABLE users DROP COLUMN status")?;
        Ok(())
    }
}
```

## Features

### Transaction Safety
Each migration runs in its own transaction by default. If a migration fails, it's automatically rolled back.

### Checksum Verification
Migrations are checksummed to detect modifications after they've been applied. The status command shows checksum mismatches.

### Gap Detection
The system detects gaps in migration versions to ensure migrations run in order.

### Progress Indicators
Long-running operations show progress with spinner animations and execution times.

### Colored Output
- ✓ Success messages in green
- ✗ Errors in red
- ⚠ Warnings in yellow
- ℹ Info in blue

## Examples

### PostgreSQL Project
```bash
# Initialize
parsql init --database-url postgresql://localhost/myapp

# Create migrations
parsql migrate create "create users table"
parsql migrate create "create posts table"
parsql migrate create "add user profiles"

# Run migrations
parsql migrate run

# Check status
parsql migrate status --detailed

# Rollback if needed
parsql migrate rollback --to 20240101000000
```

### SQLite Project
```bash
# Initialize with SQLite
parsql init --database-url sqlite:app.db

# Create and run migrations
parsql migrate create "initial schema"
parsql migrate run

# Validate migrations
parsql migrate validate --verify-checksums
```

## Troubleshooting

### Connection Errors
- Verify DATABASE_URL is correct
- For PostgreSQL: Check server is running and credentials are valid
- For SQLite: Ensure directory permissions are correct

### Migration Failures
- Check SQL syntax matches your database
- Verify table/column names don't conflict
- Use `--dry-run` to preview changes
- Check logs for detailed error messages

### Checksum Mismatches
- Don't modify migration files after they're applied
- If you must change a migration, rollback first
- Use `validate --verify-checksums` to find issues

## License

This project is part of the parsql workspace and shares its license.