Summarize Rails MySQL Database Tables
Copy and run summary.sql in MySQL console. Run call showalltbl("tbl1,tbl2,tbl3");
to get a summary of tbl1
, tbl2
, tbl3
... in your MySQL database (haven't tried PostgresSQL etc).
Because of how ROR manages/abstracts database, looking at the database tables, can give quite some information about the application. For an app that I had came across, I started with select count(*) from <tbl1>;
followed by a select * from <tbl1> limit 10;
for the tables in the database, one by one. But then I would look at a reference like account_id
and would try to recollect what accounts
table looked like. I would rather have liked to get the above summary for all the tables, copy them to a text editor and then rearrange and analyze them.
The script below defines procedure showalltbl
to get above mentioned
summary for all the tables, that are passed as comma separated list to
it.
How does this work?
There are three functions defined in the script. The most basic being execsql
. Let me start there. Its job is to take a string, and execute it as an SQL statement.
Next up is showtbl
. It takes a table name as string. Uses execsql
to select that string (so, the output shows the table name, to understand which table rest of the summary belongs to). Selects count on the table. And the show 10 entries from the table.
The last one is showalltbl
, which takes the comma separate string of tables. Splits them and calls showtbl
on them.