Skip to content

Add --database subcommand to rapina new command #473

Merged
arferreira merged 5 commits intorapina-rs:mainfrom
Ismaellima4:feat/add-database-to-new-command
Apr 13, 2026
Merged

Add --database subcommand to rapina new command #473
arferreira merged 5 commits intorapina-rs:mainfrom
Ismaellima4:feat/add-database-to-new-command

Conversation

@Ismaellima4
Copy link
Copy Markdown
Contributor

@Ismaellima4 Ismaellima4 commented Apr 8, 2026

Summary

What does this PR do?

This PR introduces comprehensive database configuration support for the rapina new command, enabling users to scaffold projects with or without database integration based on the new --db flag. It refactors the template generation system to conditionally inject database-related code, centralizes environment variable generation, and improves CLI validation.

Key Changes:

  • CLI & Validation:

    • Added --db <sqlite|postgres|mysql> flag with strict value parsing.
    • Enforced --db requirement for --template crud using Clap's requires_if validation.
    • Moved template & requirement validation to run before any filesystem operations, preventing partial directory creation on failure.
  • Conditional Template Generation:

    • Database code (.with_database(), load_dotenv(), DatabaseConfig imports) is now only generated when --db is explicitly provided.
  • Environment Configuration:

    • Replaced .env.example with direct .env generation containing clear placeholder values and English replacement warnings (⚠️ Replace the values below...).
    • Delegated .env creation responsibility to each individual template (rest_api, auth, crud) for better separation of concerns.
  • Architecture & Safety:

    • Refactored CRUD template to require &DatabaseType explicitly, as CRUD scaffolds inherently need a database.
    • Ensured type-safe optional handling across all template generators.

Related Issues

Closes #371

Checklist

  • cargo fmt passes
  • cargo clippy has no warnings
  • Tests pass
  • Documentation updated (if needed)

Implement database configuration support for project scaffolding with
conditional code generation based on the `--db` flag.

Key changes:
- Add `--db <sqlite|postgres|mysql>` argument with clap value validation
- Enforce `--db` requirement for `--template crud` via clap's
  requires_if
- Conditionally generate `.with_database().await?`, `load_dotenv()`, and
  DB imports only when `--db` is provided
- Replace `.env.example` with direct `.env` generation containing clear
  placeholder values and replacement warnings in English
- Unify env generation: `generate_env_content(db_type: Option,
  extra_vars: Option)`
- Refactor CRUD template to require explicit `&DatabaseType` (DB is
  mandatory)
- Extract shared `generate_with_database_line()` helper to eliminate
  duplication
- Place `.await?` on a separate line for proper async builder chain
  formatting
- Validate template name and DB requirement before creating any
  directories
- Move `.env` generation responsibility to individual templates
- Update 122 tests to cover new conditional logic and refactored
  signatures

Fixes template generation issues where database code was always
injected,
even when no database was configured.
@Ismaellima4 Ismaellima4 requested a review from arferreira as a code owner April 8, 2026 02:39
@github-actions github-actions bot added the area: cli CLI tool label Apr 8, 2026
Copy link
Copy Markdown
Collaborator

@arferreira arferreira left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, good feature, nice work so far, just left two things to fix before merging.

- Use `expect()` instead of `?` for database config initialization
- Refactor `generate_gitignore_extras` to always include `.env`
- Add explicit router setup in REST API template
- Parse `DatabaseType` directly in CLI args using clap
- Simplify database type handling in `main.rs`
- Add test for MySQL gitignore extras
@Ismaellima4 Ismaellima4 requested a review from arferreira April 11, 2026 21:01
Copy link
Copy Markdown
Collaborator

@arferreira arferreira left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@arferreira arferreira changed the title [Feature]: add --database subcommand to rapina new command Add --database subcommand to rapina new command Apr 13, 2026
@arferreira arferreira merged commit a1c6682 into rapina-rs:main Apr 13, 2026
13 checks passed
@Ismaellima4 Ismaellima4 deleted the feat/add-database-to-new-command branch April 13, 2026 17:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: cli CLI tool

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: add --database subcommand to rapina new command

2 participants