Package: alchemyrohan
AlchemyrohanAlchemyrohan is an extension package forSqlAlchemywhich automatically creates the database models according to the database schema.📖 ContentHow to InstallDatabase SupportHow to useFunctionsModelsExampleDependenciesImportant NoteRelease NotesLicense🔧 How to InstallWith pip package installer fromPyPIdirectly:pip install alchemyrohanor from source:git clone --recursive https://github.com/wamberger/alchemyrohan.git
cd alchemyrohan
python3 setup.py install🗄 Database SupportThis project is currently designed to work with the following databases:SqLiteOracle🔨 How to useImport in your code:import alchemyrohanorimport alchemyrohan as ar🪄 Functionsassemble_model()is the main function. This function is used to create a SqlAlchemy database model and is accepting the following arguments:argumentdescriptionenginethis is SqlAlchemy enginefrom sqlalchemy import create_enginetable_namethis is the name of the database tableabs_os_path_to_modelabsolute path to the model's folderpy_path_to_modelpythonic path to the modelsreload_module()when the code and file are created, the new code needs to be compiled - if the function <assemble_model> is called inside a program which will use the new created models. Thus you need to call thereloadfunction. You will need to add pythonic path/import:importtests.test_model...somecode...reload_module(tests.test_model)is_model()this function is used to check if the model was created. You need to pass the <table_name> and <abs_os_path_to_model> arguments.get_model()you will retrieve the wanted database object of the SqlAlchemy model. It needs the <table_name> and <py_path_to_model> arguments.is_module()this is optional function if you want to check the pythonic path. It needs <py_path_to_model> argument.🗂 ModelsCreated SqlAlchemy models have some additional features:default valuesparent-child relationships<post_init> method is used as validationwhen 'printing' the string will contain the model/object name and attributes names with their values.All models are named with the same naming convention as they are in the database with one difference, they are capitalized (python class naming convention).📝 ExampleSimple example how to use the code:importosfromsqlalchemyimportcreate_enginefromalchemyrohan.assembleimportassemble_modelfromalchemyrohan.utilsimportis_modelfromalchemyrohan.utilsimportget_modelfromalchemyrohan.utilsimportreload_moduleimporttests.test_modeldir=os.path.dirname(__file__)conn=f"sqlite:///{dir}{os.sep}test_sqlite{os.sep}test.db"engine=create_engine(conn)table_name='child'# all names will be capitilizedabs_os_path_to_model=os.path.join(dir,'test_model')# pathpy_path_to_model='tests.test_model'# pythonic pathtry:assemble_model(engine,table_name,abs_os_path_to_model,py_path_to_model)exceptExceptionase:print(e)exit(1)reload_module(tests.test_model)# compile the new codeifis_model(table_name,abs_os_path_to_model):model=get_model(table_name,py_path_to_model)print(f'SqlAlchemy model exist:{model}')exit(0)print(f'Something unexpected went wrong')exit(-1)Example of one created model:fromsqlalchemyimportColumnfromtests.test_modelimportBasefromsqlalchemy.dialects.sqliteimportINTEGERfromsqlalchemy.dialects.sqliteimportTEXTfromsqlalchemy.ormimportrelationshipclassChild(Base):__tablename__='child'id=Column(INTEGER,primary_key=True)parent_id=Column(INTEGER,nullable=True,default=None)name=Column(TEXT,nullable=True,default=None)grade=Column(INTEGER,nullable=True,default=None)parent_Parent=relationship("Parent",back_populates="children_Child",lazy="joined")def__post_init__(self):ifnotisinstance(self.id,int):try:self.id=int(self.id)except:raiseSyntaxError(f'<{self.id}> is not integer')ifnotisinstance(self.parent_id,int):try:self.parent_id=int(self.parent_id)except:raiseSyntaxError(f'<{self.parent_id}> is not integer')ifnotisinstance(self.name,str):try:self.name=str(self.name)except:raiseSyntaxError(f'<{self.name}> is not string')ifnotisinstance(self.grade,int):try:self.grade=int(self.grade)except:raiseSyntaxError(f'<{self.grade}> is not integer')def__str__(self):returnf'User(id={self.id},'\f'parent_id={self.parent_id},'\f'name={self.name},'\f'grade={self.grade})'📚 Dependenciessqlalchemy(version 2.0.x) is an ORM and provides code for its models,oracledb(version 1.4.x) is used to shape a database table model with an oracle table schema.❗Important NoteIn some cases you will need to correct the code manually.
This will be in case when:Your are creating only one model which has relationship to other tables, thus you will need to create also those models or delete the part of the codeYour tables have no primary keys. SqlAlchemy needs one primary keyYour database has some datatypes or features which were not yet been testet📋 Release Notesv0.1.0- creation of the initial code and tested with SqLite databasev0.2.0- tested with Oracle databasev0.3.0- added additional functionsv0.3.1- bug fixingv0.3.2- text fixing and adding third party licenses📄 License and Third-Party LicensesAlchemyrohan is MIT licensed, as found in theLICENSEfile.The following software components are included in this project:SqlAlchemy (MIT License)python-oracledb (Apache License 2.0)THIRD PARTY LICENSES