Qovery / Replibyte

Seed your development database with real data ⚡️
https://www.replibyte.com
GNU General Public License v3.0
4.17k stars 129 forks source link

feat: add a new sub command 'source' to print the database schema #226

Closed realtimetodie closed 2 years ago

realtimetodie commented 2 years ago

Resolves #109

Example

$ replibyte -c conf.yaml source schema

This is a first basic implementation. I would like to hear your suggestions how to convert this into a humanly readable table format.

SQL schema statements are very complex. For example, you can define an enum for a field in your SQL schema. At the moment, only the field names are printed out. I think it would be best, to simply print the entire SQL statement for a single row in one line, since there is no interpreter available.

For MongoDB, we can obtain a lot information as JSON. Since MongoDB is schemaless, a schema needs to be inferred probabilistically for a single collection using the crate mongodb_schema_parser.

The same probabilistic mechanism is used by Compass, the GUI for MongoDB. At the moment, the entire JSON output is printed out.

collection-schema

Data

name: "john"
level: 12

Output PostgreSQL

 Table users
 Field
-------
 name  
 level

Output MySQL

Table users

 Field
-------
 name  
 level

Output MongoDB

 Collection app.users 
----------------------------------------
 app.users 
 { 
   "count": 1, 
   "fields": { 
     "level": { 
       "name": "level", 
       "path": "level", 
       "count": 1, 
       "bson_types": [ 
         "Int32" 
       ], 
       "probability": 1.0, 
       "types": { 
         "Int32": { 
           "path": "level", 
           "count": 1, 
           "bson_type": "Int32", 
           "name": "Int32", 
           "probability": 1.0, 
           "values": [ 
             12 
           ], 
           "has_duplicates": false, 
           "unique": 1 
         } 
       } 
     }, 
     ...
realtimetodie commented 2 years ago

Example SQL schema

CREATE TABLE "countrylanguage" (
  "CountryCode" char(3) NOT NULL DEFAULT '',
  "Language" char(30) NOT NULL DEFAULT '',
  "IsOfficial" enum('T','F') NOT NULL DEFAULT 'F',
  "Percentage" decimal(4,1) NOT NULL DEFAULT '0.0',
  PRIMARY KEY ("CountryCode","Language"),
  KEY "CountryCode" ("CountryCode"),
  CONSTRAINT "countryLanguage_ibfk_1" FOREIGN KEY ("CountryCode") REFERENCES "country" ("Code")
);
evoxmusic commented 2 years ago

Hi thanks a lot for your contribution. I'll take a look this weekend. Sorry for the delay :)

evoxmusic commented 2 years ago

It looks for me @diceride 👍🏽