Chapter 30: Tina4 CLI#
1. Getting a New Developer Up to Speed#
Monday morning. A new developer joins your team. You hand them the repo URL. By 10am they have a running project, a new database model, CRUD routes, a migration, and a deployment to staging. All from the command line. No documentation scavenger hunt. No boilerplate copy-paste.
The Tina4 CLI is a single Rust binary. It manages all four Tina4 frameworks (PHP, Python, Ruby, Node.js). The commands are identical across languages. Learn the CLI for Python. You know it for PHP.
2. tina4 init -- Project Scaffolding#
You saw this in Chapter 1. Now the details.
tina4 init my-projectCreating Tina4 project in ./my-project ... Detected language: Python (pyproject.toml) Created .env Created .env.example Created .gitignore Created src/routes/ Created src/orm/ Created migrations/ Created src/seeds/ Created src/templates/ Created src/templates/errors/ Created src/public/ Created src/public/js/ Created src/public/css/ Created src/public/scss/ Created src/public/images/ Created src/public/icons/ Created src/locales/ Created data/ Created logs/ Created secrets/ Created tests/โProject created! Next steps: cd my-project uv sync tina4 serveLanguage Detection#
The CLI detects the language from existing files:
| File Present | Language |
|---|---|
composer.json | PHP |
pyproject.toml or requirements.txt | Python |
Gemfile | Ruby |
package.json | Node.js |
If no language-specific file exists, the CLI asks:
tina4 init my-projectNo language detected. Which language? 1. PHP 2. Python 3. Ruby 4. Node.js> 2โCreating Tina4 Python project in ./my-project ...Explicit Language Selection#
Skip the prompt by specifying the language:
tina4 init python my-projectThis creates a Python project with pyproject.toml, app.py, and the full directory structure.
Init into an Existing Directory#
Already have a project? Add Tina4 structure:
cd existing-projecttina4 init .The CLI creates only files and directories that do not exist. It never overwrites.
3. tina4 serve -- Dev Server#
tina4 serve Tina4 Python v3.0.0 HTTP server running at http://0.0.0.0:7146 WebSocket server running at ws://0.0.0.0:7146 Live reload enabled Press Ctrl+C to stoptina4 serve detects the language and starts the appropriate server. For Python, it spawns the Tina4 Python runtime with SCSS compilation, file watching, and live reload all wired in.
Options#
tina4 serve --port 8080 # Custom porttina4 serve --host 127.0.0.1 # Bind to localhost onlytina4 serve --production # Production mode (no live reload, debug off)Bypassing the CLI#
The framework refuses to start without the Rust CLI. For sandboxed environments (Docker images that already wrap the framework, CI runners, APM agents that wrap the Python process directly) set TINA4_OVERRIDE_CLIENT=true in .env and only then run:
TINA4_OVERRIDE_CLIENT=true uv run python app.pyThis bypasses SCSS compilation and live reload, so use it only when the Rust CLI is genuinely unavailable.
4. tina4 generate model -- ORM Scaffolding#
The generate model command creates an ORM model file and a matching migration. One command produces both.
tina4 generate model ProductCreated src/orm/product.pyCreated migrations/20260322120000_create_products_table.sqlThe generated model:
from tina4_python.orm import ORMโclass Product(ORM): table_name = "products" id: int created_at: strThe generated migration:
-- UPCREATE TABLE products ( id INTEGER PRIMARY KEY AUTOINCREMENT, created_at TEXT DEFAULT CURRENT_TIMESTAMP);โ-- DOWNDROP TABLE IF EXISTS products;The model is ready to use. Add your fields and run migrations.
Adding Fields#
Specify fields on the command line:
tina4 generate model Product --fields "name:string,price:float,category:string,in_stock:bool"The generated model includes all the fields:
from tina4_python.orm import ORMโclass Product(ORM): table_name = "products" id: int name: str price: float category: str in_stock: bool created_at: strAnd the migration:
-- UPCREATE TABLE products ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL DEFAULT '', price REAL NOT NULL DEFAULT 0, category TEXT NOT NULL DEFAULT '', in_stock INTEGER NOT NULL DEFAULT 0, created_at TEXT DEFAULT CURRENT_TIMESTAMP);โ-- DOWNDROP TABLE IF EXISTS products;Field Types#
| CLI Type | Python Type | SQLite Column |
|---|---|---|
string | str | TEXT |
int | int | INTEGER |
float | float | REAL |
bool | bool | INTEGER |
text | str | TEXT |
date | str | TEXT |
Options#
| Flag | Description | Example |
|---|---|---|
--fields | Comma-separated field definitions | --fields "name:string,price:float" |
--auto-crud | Enable auto-CRUD on the model | --auto-crud |
--soft-delete | Add soft delete support | --soft-delete |
--no-migration | Skip migration generation | --no-migration |
--with-route | Also generate a CRUD route file | --with-route |
5. tina4 generate route -- CRUD Route Scaffolding#
The generate route command creates a complete CRUD route file with all five REST endpoints. It reads the model's properties and builds routes with proper imports and response handling.
tina4 generate route productsCreated src/routes/products.pyThe generated route file:
from tina4_python.core.router import get, post, put, deleteโ@get("/api/products")async def list_products(request, response): page = int(request.params.get("page", 1)) per_page = int(request.params.get("per_page", 20)) offset = (page - 1) * per_pageโ products, total = Product.where("1=1", [], limit=per_page, offset=offset) results = [p.to_dict() for p in products]โ return response({ "data": results, "page": page, "per_page": per_page, "count": len(results) })โโ@get("/api/products/{product_id}")async def get_product(request, response): product = Product.find(request.params["product_id"])โ if product is None: return response({"error": "Product not found"}, 404)โ return response(product.to_dict())โโ@post("/api/products")async def create_product(request, response): body = request.bodyโ product = Product() product.name = body.get("name", "") product.price = float(body.get("price", 0)) product.category = body.get("category", "") product.in_stock = bool(body.get("in_stock", False)) product.save()โ return response(product.to_dict(), 201)โโ@put("/api/products/{product_id}")async def update_product(request, response): product = Product.find(request.params["product_id"])โ if product is None: return response({"error": "Product not found"}, 404)โ body = request.body if "name" in body: product.name = body["name"] if "price" in body: product.price = float(body["price"]) if "category" in body: product.category = body["category"] if "in_stock" in body: product.in_stock = bool(body["in_stock"]) product.save()โ return response(product.to_dict())โโ@delete("/api/products/{product_id}")async def delete_product(request, response): product = Product.find(request.params["product_id"])โ if product is None: return response({"error": "Product not found"}, 404)โ product.delete() return response(None, 204)The generator reads the model's properties and creates routes with type casting and None checks. Customize the generated code immediately. It is regular Python, not magic.
Options#
| Flag | Description | Example |
|---|---|---|
--prefix | Custom route prefix (default: /api) | --prefix /api/v2 |
--middleware | Add middleware to all routes | --middleware auth_middleware |
6. tina4 generate migration -- Migration Scaffolding#
The generate migration command creates a timestamped migration file with UP and DOWN sections. The timestamp ensures migrations run in order.
tina4 generate migration add_category_to_productsCreated migrations/20260322120500_add_category_to_products.sqlThe generated file:
-- UP-- Add your forward migration SQL hereโโ-- DOWN-- Add your rollback migration SQL hereโFill in the SQL:
-- UPALTER TABLE products ADD COLUMN category TEXT DEFAULT '';โ-- DOWNALTER TABLE products DROP COLUMN category;The timestamp prefix (20260322120500) ensures migrations run in order. Each migration runs once. The framework tracks which ones have been applied.
When to Generate a Migration#
Generate a new migration when you need to change the database schema after the initial model migration. Adding a column. Creating an index. Renaming a table. Each change gets its own migration file with a unique timestamp.
7. tina4 generate middleware -- Middleware Scaffolding#
The generate middleware command creates a middleware function with the correct signature and a placeholder for your logic.
tina4 generate middleware rate_limitCreated src/middleware/rate_limit.pyThe generated file:
async def rate_limit(request, response, next_handler): # Add your middleware logic hereโ # Continue to the next middleware or route handler return await next_handler(request, response)โ # Or return early to block the request: # return response({"error": "Rate limit exceeded"}, 429)The middleware is a named function. Reference it in route definitions:
@get("/api/data", middleware=["rate_limit"])async def protected_data(request, response): return response({"data": "protected"})8. Generate All at Once#
Combine flags to generate multiple files in a single command:
tina4 generate model Product --with-route --with-migrationCreated src/orm/product.pyCreated src/routes/products.pyCreated migrations/20260322120000_create_products_table.sqlModel. CRUD routes. Migration. All wired together. Ready to use.
9. tina4 doctor -- Health Check#
The doctor command checks your project for common issues:
tina4 doctorTina4 Doctor -- Checking your project...โ [OK] Python 3.12.0 detected [OK] uv package manager found [OK] tina4_python package installed (v3.0.0) [OK] .env file exists [OK] Database connection: sqlite:///data/app.db [OK] Database is accessible [OK] src/routes/ directory exists (3 route files) [OK] src/orm/ directory exists (2 model files) [OK] src/templates/ directory exists (5 templates) [OK] src/public/ directory exists (static files served) [OK] tests/ directory exists (4 test files) [WARN] No migrations found in migrations/ [OK] AI context: Claude Code detected, CLAUDE.md present [OK] .gitignore includes .env, data/, logs/โ 12 checks passed, 1 warning, 0 errorsDoctor checks:
- Python version and package manager
- Tina4 package installation and version
.envfile existence and critical variables- Database connectivity
- Directory structure
- Missing files or configurations
- AI tool context files
- Git configuration
The warnings give actionable advice. If your database is not configured, it tells you exactly what to add to .env.
10. tina4 test -- Running Tests#
tina4 testRunning tests...โ ProductTest [PASS] test_create_product [PASS] test_load_productโ 2 tests, 2 passed, 0 failed (0.12s)This runs all tests in the tests/ directory. See Chapter 18 for full testing documentation.
Test Options#
tina4 test --file tests/test_product.py # Specific filetina4 test --file tests/test_product.py --method test_create # Specific methodtina4 test --verbose # Show assertion details11. tina4 routes -- Route Listing#
See all registered routes in your project:
tina4 routesRegistered Routes:โ Method Path Handler Middleware ------ -------------------------- -------------------------- ---------- GET /health health_check - GET /api/products list_products ResponseCache:300 GET /api/products/{product_id} get_product - POST /api/products create_product auth_middleware PUT /api/products/{product_id} update_product auth_middleware DELETE /api/products/{product_id} delete_product auth_middleware GET /api/auth/login - - POST /api/auth/login login - GET /admin admin_dashboard -โ 9 routes registeredThis is useful for verifying that your routes are registered and for finding the handler function for a specific URL.
Filtering#
tina4 routes --method POST # Filter by HTTP methodtina4 routes --filter products # Filter by path patterntina4 routes --middleware auth # Filter by middlewareWhen debugging routing issues, check here first. If a route does not match, tina4 routes shows whether it was registered and what middleware is attached.
12. tina4 migrate -- Database Migrations#
Run pending migrations:
tina4 migrateRunning migrations... [UP] 20260322000100_create_users_table.sql [UP] 20260322000200_create_products_table.sql [UP] 20260322000300_add_category_to_products.sqlโ 3 migrations appliedRollback#
tina4 migrate --rollbackRolling back last migration... [DOWN] 20260322000300_add_category_to_products.sqlโ 1 migration rolled backMigration Table Auto-Upgrade#
If your project was created with an earlier version of Tina4, the tina4_migration tracking table may use the older v2 schema. Running tina4 migrate detects the old layout and adds the missing migration_id, batch, and executed_at columns, backfilling existing data. No manual intervention needed.
Status#
tina4 migrate --statusMigration Status:โ Status Migration -------- ----------------------------------------- Applied 20260322000100_create_users_table.sql Applied 20260322000200_create_products_table.sql Applied 20260322000300_add_category_to_products.sql Pending 20260322000400_create_orders_table.sqlโ 3 applied, 1 pending13. Exercise: Scaffold a Feature in 5 Commands#
Scaffold a complete "Customer" feature from scratch using only CLI commands.
Requirements#
Starting from an existing Tina4 Python project, run 5 commands to create:
- A Customer ORM model with name, email, phone, and company fields
- A CRUD route file with all five REST endpoints
- A migration that creates the customers table
- Run the migration to create the table
- Run the doctor to verify everything
Expected Commands#
# 1. Generate the model with route and migrationtina4 generate model Customer --with-route --with-migrationโ# 2. Edit the model to add fields (manual step)# Add fields to src/orm/customer.pyโ# 3. Edit the migration to add columns (manual step)# Add columns to the migration fileโ# 4. Run the migrationtina4 migrateโ# 5. Run the doctor to verify everything is set uptina4 doctorSolution#
Command 1: Generate everything:
tina4 generate model Customer --with-route --with-migrationCommand 2: Edit src/orm/customer.py:
from tina4_python.orm import ORMโclass Customer(ORM): table_name = "customers"โ id: int name: str email: str phone: str company: str created_at: strCommand 3: Edit the migration file:
-- UPCREATE TABLE customers ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, email TEXT NOT NULL UNIQUE, phone TEXT, company TEXT, created_at TEXT DEFAULT CURRENT_TIMESTAMP);โCREATE INDEX idx_customers_email ON customers(email);โ-- DOWNDROP TABLE IF EXISTS customers;Command 4: Run the migration:
tina4 migrateRunning migrations... [UP] 20260322120000_create_customers_table.sqlโ 1 migration appliedCommand 5: Verify with doctor:
tina4 doctor [OK] src/orm/ directory exists (3 model files) [OK] src/routes/ directory exists (4 route files) [OK] Database connection: sqlite:///data/app.db ...Now test the API:
curl -X POST http://localhost:7146/api/customers \ -H "Content-Type: application/json" \ -d '{"name": "Alice Corp", "email": "alice@corp.com", "phone": "+1-555-0100", "company": "Alice Corp"}'{ "id": 1, "name": "Alice Corp", "email": "alice@corp.com", "phone": "+1-555-0100", "company": "Alice Corp", "created_at": "2026-03-22 12:00:00"}From zero to a working CRUD API. Five commands. Under two minutes.
14. Gotchas#
1. tina4 Command Not Found#
Problem: Running tina4 gives "command not found".
Cause: The Tina4 CLI is not installed or not in your PATH.
Fix: Install the CLI: curl -fsSL https://tina4.com/install.sh | sh. Verify with tina4 --version. If installed but not found, add the installation directory to your PATH:
echo 'export PATH="$HOME/.tina4/bin:$PATH"' >> ~/.zshrcsource ~/.zshrc2. Wrong Language Detected#
Problem: tina4 init creates a PHP project instead of Python.
Cause: A composer.json file exists in the directory from a previous project.
Fix: Use explicit language selection: tina4 init python my-project. Or delete the conflicting language file before running init.
3. Generated Files Overwrite Existing Code#
Problem: Running tina4 generate route products overwrites your custom route file.
Cause: The generate command creates files at fixed paths. If the file exists, it is overwritten.
Fix: The CLI warns you before overwriting. Check if the file exists first. If you need to regenerate, rename the existing file: mv src/routes/products.py src/routes/products_backup.py.
4. Migration Order Issues#
Problem: A migration fails because it references a table that does not exist yet.
Cause: Migration files run in alphabetical (timestamp) order. If migration B depends on a table created by migration A, but A has a later timestamp, B runs first and fails.
Fix: Use consistent timestamps. The tina4 generate migration command uses the current timestamp. Generating migrations in order ensures correct execution order. If you need to fix ordering, rename the migration files to adjust their timestamps.
5. tina4 serve Uses Wrong Port#
Problem: tina4 serve starts on port 7146 but you need port 8080.
Cause: The default port is 7146 unless overridden.
Fix: Set it in .env: TINA4_PORT=8080. Or pass it as a flag: tina4 serve --port 8080. The .env value takes precedence over the default. The command-line flag overrides everything.
6. Doctor Shows False Warnings#
Problem: tina4 doctor warns about missing migrations, but your project does not use migrations.
Cause: Doctor checks for common conventions. It warns about missing migrations regardless of your approach.
Fix: These are warnings, not errors. Ignore warnings that do not apply to your project. Doctor is a guide, not a gatekeeper.
7. Generate Creates Python 3.12+ Syntax#
Problem: Generated code uses type: int syntax that does not work in Python 3.10.
Cause: The generator targets the latest Python version supported by Tina4.
Fix: Use Python 3.12 or later with Tina4 Python. Check your Python version with python --version. If you must use an older version, modify the generated code to use compatible syntax.
8. Model Name Must Be PascalCase#
Problem: tina4 generate model order_item creates a model class named order_item which is not valid Python convention.
Cause: The CLI uses the argument as-is for the class name.
Fix: Use PascalCase for model names: tina4 generate model OrderItem. The CLI converts it to snake_case for the table name (order_items).
15. Documentation#
tina4 docs # Download framework-specific book chapters to .tina4-docs/tina4 books # Download the complete Tina4 book (all languages) to tina4-book/tina4 docs detects your project language and downloads only the relevant chapters. The documentation is available in Markdown format, optimised for AI tools and local reference.
16. Test Port (Dual-Port Development)#
When TINA4_DEBUG=true, Tina4 automatically starts a second HTTP server on port + 1000:
- Main port (e.g. 7146): hot-reload enabled, for AI dev tools
- Test port (e.g. 8145): stable, no hot-reload, for user testing
This prevents the browser from refreshing mid-test when AI tools edit files.
| Setting | Effect |
|---|---|
TINA4_NO_AI_PORT=true | Disables the test port entirely |
TINA4_NO_RELOAD=true | Disables hot-reload on the main port too |
--no-reload | CLI flag equivalent of TINA4NORELOAD |