Inspect DB¶
Does it happen often changing ORMs during a project? Well, not that often really but it might happen and usually during the discovery time where the best stack is being figured.
Well, something that usually remains is the SQL database and what it changes is normally the ORM that operates on the top of it.
The inspectdb
is another client management tool that allows you to read from an existing database
all the tables and generates ReflectModel objects for your.
In other words, it maps existing database tables into an Saffier like syntax to make your life easier to manage.
Tip
If you are not familiar with ReflectModel, not is is a good time to catch-up.
Reflect models¶
These are the models automatically generated by Saffier when the inspectdb
is triggered.
The reason for the ReflectModel it is simply because those are not managed by the migration system but you still operate as a normal Saffier model.
In other words, it is a safety measure of Saffier.
How does it work¶
Now it is time for the good stuff right? Well, it is actually very simple.
- Via database url.
Database url¶
This is the easiest and probably the one way you will be using all the time and syntax is as simple as this:
saffier inspectdb --database <CONNECTION-STRING> > <NAME-OF-FILE>.py
Example
saffier inspectdb --database "postgres+asyncpg://user:password@localhost:5432/my_db" > models.py
And that is it! This simple. The inspectdb
will write the models inside the specified file and
from there you can use them anywhere.
Parameters¶
To check the available parameters for the inspectdb
:
saffier inspectdb --help
- schema - The name of the schema to connect. For example, in
MSSQL
thedbo
is usually used. This will be probably used on rare occasions by it is available just in case you need. - database - The fully qualified connection string to the database. Example:
postgres+asyncpg://user:password@localhost:5432/my_db
.