Package: dbml-sqlite
dbml_sqliteThis is a simple package built on top ofthe PyDBML package by Vanderhoof. It allows you, within certain constraints, to generate valid SQLite from.dbmlfiles for your Python programs.InstallationYou'll need Python 3.7 or higher.pip install dbml_sqliteOr:poetry add dbml_sqliteNote that if you install the tool on your system globally with pip, you should be able to use the CLI anywhere.UsageBasic use case:importsqlite3fromdbml_sqliteimporttoSQLiteddl=toSQLite('dbdiagram.dbml')con=sqlite3.connect('./example.db')withcon:con.executescript(ddl)con.close()Instead of directly executing the produced SQLite DDL, feel free to write it to a file instead so you can manually inspect or manipulate it. The ddl output is valid SQLite, but it is still just a Python string so you could also programmatically manipulate it or compile it further if needed.Given a DBML file, thetoSQLitefunction converts the contents to valid SQLite.Parameters:dbml (str):a valid string for converting to a Path object. Should point to a.dbmlfile containing valid DBMLora directory containing such files. Default is a period, in which case current working directory will be searched and all such files will be parsed.emulation (str):specifies emulation mode for enum functionality since it is not directly supported by SQLite. Default is "full", and the other option is "half".Returns:str:one valid sequence of SQLite syntax.There are other functions in the package, but they are intended for internal use only within the package. In-depth coverage of the rest of the API is at the end of this README.CLIAfter installation, you can use the CLI from your terminal as follows:dbml_sqlite [OPTIONS] SRCSRC is mandatory and is the file containing dbml you want converted.OptionsMeaning-p, --print / -n, --no-printWhether to print output to console.  [default: print]-w, --write PATH(Optional) File you want output written to.-x, --execute PATH(Optional) SQLite database file for executing output DDL on. Will create file if it doesn't exist.-f, --full / -h, --halfFull emulation mode (separate tables) or half emulation mode (check statements) for any enums defined in your dbml. [default: full]-t, --if-table-exists(Optional) Add IF NOT EXISTS language to CREATE TABLE statements.-i, --if-index-exists(Optional) Add IF NOT EXISTS language to CREATE INDEX statements.--helpShow this message and exit.Writing SQLite Compatible DBMLNot all valid DBML will result in valid SQLite. However, this library attempts to coerce commonly used language in DBML for other SQL flavors to compatible SQLite statements. If this is not possible, an error will be raised.For best results, it is recommended to stick to the following valid SQLite types, which are shown next to their corresponding Python types:SQLite TypePython TypeNULLNoneINTEGERintREALfloatTEXTstrBLOBbytesAny of the Python types above as well as any of the types in the table below, if found in your DBML, will be converted to the corresponding SQLite Type. Note the case insensitivity of the mappings; all types are uppercased for the purposes of comparison.Foreign TypeSQLite TypeboolINTEGERbooleanINTEGERintINTEGERtinyintINTEGERsmallintINTEGERmediumintINTEGERbigintINTEGERyearINTEGERfloatREALdoubleREALdecimalREALnumericREALdateTEXTdatetimeTEXTtimestampTEXTtimeTEXTvarcharTEXTtinytextTEXTmediumtextTEXTlongtextTEXTtinyblobBLOBmediumblobBLOBlongblobBLOBbyteBLOBEnumsEnums are an aspect of SQL that is not explicitly supported in SQLite. However, it is possible to emulate the functionality in several ways.See this stackoverflow discussion for more info.By default, this library will emulate enums that you specify in DBML by creating a separate table. For example, given the following DBML:enum message_status {
    unsent
    pending
    sent
    delivered
    failed
}

Table message {
    id integer [primary key]
    body text [not null]
    status message_status [not null]
    contact_id integer [not null]
}

enum zip_code {
    920
    414
    800
    900
    555
}

Table contact {
    id integer [primary key]
    name varchar(0123) [default: "Joe Smith"]
    phone mediumint [not null]
    zip zip_code [not null]

    indexes {
        (name, phone) [name: 'unique_contact', unique]
    }
}

Ref: message.contact_id > contact.id [delete: cascade, update: no action]The following SQLite will be generated:CREATETABLEIFNOTEXISTSmessage_status(idINTEGERPRIMARYKEY,typeTEXTNOTNULLUNIQUE,seqINTEGERNOTNULLUNIQUE);INSERTINTOmessage_status(type,seq)VALUES('unsent',1);INSERTINTOmessage_status(type,seq)VALUES('pending',2);INSERTINTOmessage_status(type,seq)VALUES('sent',3);INSERTINTOmessage_status(type,seq)VALUES('delivered',4);INSERTINTOmessage_status(type,seq)VALUES('failed',5);CREATETABLEIFNOTEXISTSzip_code(idINTEGERPRIMARYKEY,typeTEXTNOTNULLUNIQUE,seqINTEGERNOTNULLUNIQUE);INSERTINTOzip_code(type,seq)VALUES('920',1);INSERTINTOzip_code(type,seq)VALUES('414',2);INSERTINTOzip_code(type,seq)VALUES('800',3);INSERTINTOzip_code(type,seq)VALUES('900',4);INSERTINTOzip_code(type,seq)VALUES('555',5);CREATETABLEIFNOTEXISTSmessage(idINTEGERPRIMARYKEY,bodyTEXTNOTNULL,statusTEXTNOTNULLREFERENCESmessage_status(type),contact_idINTEGERNOTNULL,FOREIGNKEY(contact_id)REFERENCEScontact(id)ONUPDATENOACTIONONDELETECASCADE);CREATETABLEIFNOTEXISTScontact(idINTEGERPRIMARYKEY,nameTEXTDEFAULT'Joe Smith',phoneINTEGERNOTNULL,zipTEXTNOTNULLREFERENCESzip_code(type));CREATEUNIQUEINDEXIFNOTEXISTSunique_contactONcontact(name,phone);I refer to this asfullemulation, and it is the default. The alternative ishalfemulation, and you use it as follows in your Python code:fromdbml_sqliteimporttoSQLiteoutput=toSQLite('dbdiagram.dbml',emulation="half")If used on the DBML above, the following SQLite is produced:CREATETABLEIFNOTEXISTSmessage(idINTEGERPRIMARYKEY,bodyTEXTNOTNULL,statusTEXTCHECK(statusIN('unsent','pending','sent','delivered','failed'))NOTNULL,contact_idINTEGERNOTNULL,FOREIGNKEY(contact_id)REFERENCEScontact(id)ONUPDATENOACTIONONDELETECASCADE);CREATETABLEIFNOTEXISTScontact(idINTEGERPRIMARYKEY,nameTEXTDEFAULT'Joe Smith',phoneINTEGERNOTNULL,zipTEXTCHECK(zipIN('920','414','800','900','555'))NOTNULL);CREATEUNIQUEINDEXIFNOTEXISTSunique_contactONcontact(name,phone);Note that in the case offullemulation, you will need to turn on the foreign key constraint as follows:conn=sqlite3.connect("default.db")conn.execute("PRAGMA foreign_keys = 1")cur=conn.cursor()Testing and CoverageAfter all dependencies (including development dependencies) are installed, run the tests:poetryrunpytestAlternatively, run the tests with coverage:poetryruncoveragerun--sourcedbml_sqlite-mpytestView the coverage report:poetryruncoveragereport-mAPItoSQLiteGiven a DBML file, convert contents to valid SQLite.Parameters:dbml (str):a valid string for converting to a Path object. Should point to a.dbmlfile containing valid DBMLora directory containing such files. Default is a period, in which case current working directory will be searched and all such files will be parsed.emulation (str):specifies emulation mode for enum functionality since it is not directly supported by SQLite. Default is "full", and the other option is "half".tableExists (bool):Default is True. If True, all generatedCREATE TABLESQLite statements will haveIF NOT EXISTSlanguage included.indexExists (bool):Default is True. If True, all generatedCREATE INDEXSQLite statements will haveIF NOT EXISTSlanguage included.join (bool):Default is True. If True, function willjointhe result list of string segments with an empty string and return the resulting string to you. Otherwise, the one-dimensional list of string segments will be returned to you directly.Returns:str or list of str:a valid sequence of SQLite syntax.validDBMLFileReturn a boolean indicating whether passed string has valid.dbmlfile extension. Case-sensitive (i.e..DBMLnot accepted).Parameters:s (str):name of file.Returns:bool:True if s ends with '.dbml', else False.processFileGiven a target.dbmlfile, parse and generate a valid SQLite string.Parameters:target (Path):File with contents to convert to SQLite.emulationMode (str):Specifies "half" or "full" emulation for enum functionality in SQLite.tableExists (bool):Default is True. If True, all generatedCREATE TABLESQLite statements will haveIF NOT EXISTSlanguage included.indexExists (bool):Default is True. If True, all generatedCREATE INDEXSQLite statements will haveIF NOT EXISTSlanguage included.join (bool):Default is True. If True, function willjointhe result list of string segments with an empty string and return the resulting string to you. Otherwise, the one-dimensional list of string segments will be returned to you directly.Returns:str or list of str:valid SQLite DDL.processIndexGiven objects produced by the PyDBML library (or appropriately mocked), generate valid SQLite DDL for creating indexes.Parameters:table (Table):a Table object generated by the PyDBML library. This object should represent the SQLite table relevant to the index you want to create.index (Index):an Index object generated by the PyDBML library. This object should represent the SQLite index you want to create.idxNameFunc (function):defaults touuid.uuid4. Can mock that function by passing a different function that returns a more predictable result. The result of calling this argument in either case is used as the name of an index if one is not provided for anyCREATE INDEXstatements.indexExists (bool):Default is True. If True, the generatedCREATE INDEXSQLite statement will haveIF NOT EXISTSlanguage included.join (bool):Default is True. If True, function willjointhe result list of string segments with an empty string and return the resulting string to you. otherwise, the one-dimensional list of string segments will be returned to you directly.Returns:str or list of str:SQLite DDL for creating an index.processEnumTake an Enum object generated by the PyDBML library and use it to generate SQLite DDL for creating an enum table for "full" enum emulation mode only.Parameters:enum (Enum):Enum object generated by PyDBML library representing an SQL enum.tableExists (bool):Default is True. If True, all generatedCREATE TABLESQLite statements will haveIF NOT EXISTSlanguage included.join (bool):Default is True. If True, function willjointhe result list of string segments with an empty string and return the resulting string to you. Otherwise, the one-dimensional list of string segments will be returned to you directly.Returns:str or list of str:SQLite DDL for creating a table to emulate SQL enum functionality.processTableGenerate SQLite DDL for creating a table.Parameters:table (Table):Table object generated by PyDBML, representing SQLite table you want to make.emulationMode (str):if SQL enums are defined by dbml parsed by PyDBML, there are two ways to emulate them. Passing "full" for this parameter emulates enum by making a separate enum table. Passing "half" simply uses SQLite CHECK statements within column definitions utilizing enum types.tableExists (bool):Default is True. If True, all generatedCREATE TABLESQLite statements will haveIF NOT EXISTSlanguage included.join (bool):Default is True. If True, function willjointhe result list of string segments with an empty string and return the resulting string to you. Otherwise, the one-dimensional list of string segments will be returned to you directly.Return:str or list of str:SQLite DDL for generating a table.processRefConvert a Ref object parsed by PyDBML from dbml into SQLite DDL.Parameters:ref (Ref):Ref object generated by PyDBML.join (bool):Default is True. If True, function willjointhe result list of string segments with an empty string and return the resulting string to you. Otherwise, the one-dimensional list of string segments will be returned to you directly.Returns:str or list of str:SQLite DDL for defining a foreign key within aCREATE TABLEstatement.processColumnGenerate SQLite DDL for creating a column.Parameters:column (Column):the Column object generated by PyDBML library.emulationMode (str):"half" or "full" emulation of SQL enums for SQLite. The former usesCHECKstatements within column definitions, and the latter uses separate tables.join (bool):Default is True. If True, function willjointhe result list of string segments with an empty string and return the resulting string to you. Otherwise, the one-dimensional list of string segments will be returned to you directly.Returns:str or list of str:SQLite DDL for creating a column.coerceColType(colType):Given a colType, coerce to closest native SQLite type and return that, otherwise raise a ValueError.Parameters:colType (str):column type from DBML specification.Returns:str:valid SQLite column type.ReferencesPyDBML by VanderhoofDatabase Markup Language — DBMLSQLite OfficialSQLite3 Python LibraryWed May 26 14:55:20 UTC 2021