db migrate
Run Alembic database migrations to update the database schema.Syntax
Output
Use Cases
- After git pull: Apply new schema changes
- After scrapai upgrade: Update to new database structure
- Initial setup: Run by
./scrapai setupautomatically
db current
Show current migration revision.Syntax
Output
db stats
Show database statistics.Syntax
Output
Use Cases
- Monitor database growth
- Check queue status at a glance
- Verify data after imports/migrations
db tables
List all tables with row counts.Syntax
Output
Table Validation
Only scrapai tables are displayed. System tables and non-scrapai tables are skipped:Table validation prevents SQL injection by using a whitelist of known scrapai table names.
db inspect
Show schema for a specific table.Syntax
Arguments
Table name. Must be one of:
spiders, scraped_items, crawl_queue, spider_rules, spider_settings, alembic_version.Examples
Output (SQLite)
Output (PostgreSQL)
Invalid Table
db query
Execute SQL queries (SELECT, UPDATE, DELETE only).Syntax
Arguments
SQL query to execute.
Options
Output format:
table, json, csv.Skip confirmation prompt for UPDATE/DELETE.
SELECT Queries
Output (Table Format)
Output (JSON Format)
Output (CSV Format)
UPDATE Queries
Output
DELETE Queries
Output
Blocked Operations
INSERT, DROP, ALTER, and TRUNCATE are blocked for safety:Complex Queries
Joins, aggregations, and subqueries are supported:db transfer
Transfer data from another database.Syntax
Arguments
Source database connection string.
Options
Skip scraped_items table (transfer only spiders and queue).
Workflow
- Update
DATABASE_URLin.envto your new database - Run migrations on new database:
./scrapai db migrate - Transfer data from old database:
./scrapai db transfer <old_db_url>
Examples
SQLite to PostgreSQL
PostgreSQL to PostgreSQL
Skip Scraped Items
Output
What Gets Transferred
- Spiders: All spider configurations
- Spider Rules: URL matching rules
- Spider Settings: Custom settings
- Scraped Items: All scraped data (unless
--skip-items) - Queue: All queue items with status
ID Remapping
Foreign key relationships are preserved. New spider IDs are assigned and referenced data is updated accordingly.Error Handling
Safety Features
SQL Injection Prevention
- Table names validated against whitelist
- All queries use SQLAlchemy parameterized bindings
- INSERT, DROP, ALTER, TRUNCATE blocked via
db query
Confirmation Prompts
UPDATE and DELETE queries show affected row count and require confirmation. Use--yes to skip prompts.
PostgreSQL vs SQLite
PostgreSQL Benefits
- Atomic queue operations:
FOR UPDATE SKIP LOCKEDfor race-free parallel processing - Better performance for large datasets (100k+ items)
- Concurrent writes from multiple workers
- Advanced JSONB operators for querying spider configs
SQLite Benefits
- Zero configuration, no server setup required
- Single file, easy to backup and copy
- Sufficient for most use cases (under 100k items)
Switching Databases
Example Queries
Spider Analytics
Queue Analytics
Scraped Items
Troubleshooting
Migration Failed
DATABASE_URL in .env), write permissions, and disk space.
Query Timeout
Transfer Failed
Next Steps
Data Commands
View and export scraped items
Queue Management
Manage batch processing queue