Reverse engineering a database
octopux-reverse reads an existing database and generates the matching models and relations, by calling the octopux CLI for you.
cargo install octopux-reverse
# print the commands to review them, then run them
octopux-reverse --database-url postgres://localhost/my_db --openapi > reverse.sh
sh reverse.sh
# or run them directly
octopux-reverse --database-url postgres://localhost/my_db --openapi --run
What you get
- One model per table with an
idprimary key (booksgivesBook), one field per column. Nullable columns becomeOption<T>. - One relation per foreign key to another table’s
id(GET /author/{id}/books), and many-to-many relations for join tables. --timestampswhen the table hascreated_at,updated_atanddeleted_atcolumns.- At the end, the
modand.configurelines to paste intosrc/main.rs.
Columns that cannot be mapped to a Rust type are skipped with a warning.
Options
| Option | Description |
|---|---|
--database-url |
The database to read, DATABASE_URL by default |
--migrations <dir> |
Build the schema from sqlx migrations instead of a live database |
--schema <name> |
Read the tables of this PostgreSQL schema instead of the current one (public). Needs a DATABASE_URL or --database-url with a postgresql: connection string scheme |
--tables, --exclude |
Only read, or skip, these tables (comma separated) |
--no-relations |
Only generate the models |
--run |
Run the commands instead of printing them |
--openapi, --graphql, --output, --force |
Passed to octopux |