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 a primary key of one column (
booksgivesBook), one field per column. Nullable columns becomeOption<T>. The primary key keeps its name and its type, ani64,i32,u64or UUID, passed tooctopuxwith--primary-keyand--id-type. A key the database does not generate (noDEFAULT, identity orAUTO_INCREMENT, as theversionof amigrationtable) is given by the creation payload (--assigned-id), but a UUID, generated byUuid::new_v4on insert. The tables without primary key, or with a primary key of several columns or of another type, are skipped with a warning,_sqlx_migrationssilently. - One relation per foreign key to the primary key of another table (
GET /author/{id}/books, with--parent-primary-keyand--child-primary-key), 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 |