Introspecting Legacy Databases - Open-Source Tool
Tags: opensource, tools, python, database
Published: 2022-02-11 17:14:01

---
title: Introspecting Legacy Databases - Open-Source Tool
published: true
description: Free tool for legacy databases introspection. Tables definitions and data can be accessed and dumped with a few commands typed in the Python console.
tags: opensource, tools, python, databases
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k8nbxky44xpc0u0h5rcu.jpg
canonical_url: https://blog.appseed.us/database-introspection-developers-tool-open-source/
---
Hello Coders!

This article presents an `open-source` tool for [legacy databases introspection](https://blog.appseed.us/database-introspection-developers-tool-open-source/) (tables definitions and data). The project can be used to extract complete tables information (fields, constraints, foreign keys) and also data. The code is published on Github (MIT License) and works with `SQLite`, `MySql` and `PostgreSql`.

---

> Thanks for reading!

- 👉 [Database Introspection Tool](https://github.com/app-generator/devtool-db) - source code (MIT License)
- 🎁 Free [Support](https://appseed.us/support) via email and [Discord](https://discord.gg/fZC6hup)

---

## ✨ Project Features

- 👉 Minimal set up, `Python-based` tool
- 👉 Supported DB: `SQLite`, `MySql`, `PostgreSQL`
- 👉 `Helpers`: Dump Tables Definition (SQL and Model)
- 👉 Dump Tables Data: `CSV format`

Available helpers can be invoked and used inside Python CLI. Curious minds can have a quick look over the generated files [here](https://github.com/app-generator/devtool-db/tree/main/output) (output directory): [Tables SQL Definitions](https://github.com/app-generator/devtool-db/blob/main/output/09_19_SQLITE.sql), [Table Dump Sample](https://github.com/app-generator/devtool-db/blob/main/output/09_19_SQLITE_api_user_user.sql).

---

![Database Introspection Tool - Execution Commands](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z7lq58pzlc1l2jzkq0i4.png)

--- 

## ✨ How to use the tool

This tool can be used in any environment that has Python3 and GIT installed.

> **Step #1** - Clone the sources 

```bash
$ git clone https://github.com/app-generator/devtool-db.git
$ cd devtool-db
```

---

> **Step #2** - Install the dependencies 

```bash
$ virtualenv env
$ source env/bin/activate
$ pip install -r requirements.txt
```

---

> **Step #3** - Launch the Python console

```bash
$ python
>>> 
>>> from util import *                                # import helpers     
>>>                    
>>> db_sqlite = DbWrapper()                           # invoke the Base Class  
>>> db_sqlite.driver = COMMON.DB_SQLITE               # set driver
>>> db_sqlite.db_name = 'samples/api-django.sqlite3'  # set db name
>>> db_sqlite.connect()                               # connect 
True 
>>> db_sqlite.load_models()                           # load DB SChema 
True
>>> db_sqlite.dump_tables()                           # Dump tables definitions 
True
>>> db_sqlite.dump_tables_data()                      # Dump data
 > Dump data for [api_user_user]
 > Dump data for [api_authentication_activesession]
...
(truncated data)
...
 > Dump data for [django_migrations]
 > Dump data for [django_session]
True
>>> db_sqlite.reset()                                  # reset data  
>>>
```

---

The above code chunk does the following tasks:

- Invoke the database wrapper class `DbWrapper()`
- Set up the database credentials: `Driver`, `Name` .. etc
- Connect to the Database via `connect()` helper
- `load_models()` inspect tables metadata

From this point, we are able to access all information related to tables and data. The output files are saved in the output directory.

- [SQLITE.sql](https://github.com/app-generator/devtool-db/blob/main/output/09_19_SQLITE.sql) - SQL definitions for all tables of [api-django.sqlite3](https://github.com/app-generator/devtool-db/tree/main/samples) sample
- [SQLITE_api_user_user.sql](https://github.com/app-generator/devtool-db/blob/main/output/09_19_SQLITE_api_user_user.sql) - table data

---

> `Thanks for reading!` For more resources, feel free to access:

---

- ✨ [AppSeed](https://appseed.us/) for more tools and [free starters](https://appseed.us/admin-dashboards/open-source)
- ✨ More [sample projects](https://github.com/topics/appseed-sample) published on Github