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 (books gives Book), one field per column. Nullable columns become Option<T>. The primary key keeps its name and its type, an i64, i32, u64 or UUID, passed to octopux with --primary-key and --id-type. A key the database does not generate (no DEFAULT, identity or AUTO_INCREMENT, as the version of a migration table) is given by the creation payload (--assigned-id), but a UUID, generated by Uuid::new_v4 on insert. The tables without primary key, or with a primary key of several columns or of another type, are skipped with a warning, _sqlx_migrations silently.
  • One relation per foreign key to the primary key of another table (GET /author/{id}/books, with --parent-primary-key and --child-primary-key), and many-to-many relations for join tables.
  • --timestamps when the table has created_at, updated_at and deleted_at columns.
  • At the end, the mod and .configure lines to paste into src/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