Package: django-db-adapter
Django DB adapterA flexible toolkit for customize how Django creates the databse objects for the
application schemaOverviewDjango DB adapter is a flexible schema editor backend built to solve the following problems:Generate SQL statements for projects working on database-first approachAll objects created (including created from Django) must have a particular name pattern, like add prefixes and suffixesAll table columns should be commentedInline constraints (primary/foreign keys, unique/check constraints and indexes) are not allowedThe database user of application is not the owner of the objects and has no privilege to create/alter/drop them (python manage.py migratewill not work for this user). All DDL statements generated should include a grant of manipulation privileges (select/insert/update/delete) on created objects for this user/roleThe order of SQL statements mattersRequirementsPython (3.6, 3.7, 3.8, 3.9)Django (1.11, 2.2)We highly recommend and only officially support the latest patch release of each Python and Django series.InstallationInstall usingpip...pipinstalldjango-db-adapterAdd'db_adapter'to yourINSTALLED_APPSsetting.INSTALLED_APPS=[...'db_adapter',]Quick ExampleLet's take a look at a quick example of using DB adapter to customize the DDL
statements generated by Django.This example model defines aPerson, which has afirst_nameandlast_name:fromdjango.dbimportmodelsclassPerson(models.Model):first_name=models.CharField(max_length=30)last_name=models.CharField(max_length=30,help_text="It's your last name")classMeta:db_table='person'Add the following to yoursettings.pymodule:INSTALLED_APPS=[...# Make sure to include the default installed apps here.'db_adapter',]DATABASES={'default':{# Make sure to include `db_adapter.db.backends.oracle` as database# engine for schema customization'ENGINE':'db_adapter.db.backends.oracle','NAME':'xe','USER':'a_user','PASSWORD':'a_password','HOST':'dbprod01ned.mycompany.com','PORT':'1540',}}DB_ADAPTER={'DEFAULT_ROLE_NAME':'rl_example',# Apply this pattern for all tables'DEFAULT_DB_TABLE_PATTERN':'"example"."tb_{table_name}"',# Ignore some patterns from normalization'IGNORE_DB_TABLE_PATTERNS':['"{}"."{}"',# Tables with already declared namespace'django_migrations',# Django migrations table],'DEFAULT_OBJECT_NAME_PATTERNS':{'SEQUENCE':'sq_{table_name}','TRIGGER':'tg_{table_name}_b','INDEX':'ix_{name}','PRIMARY_KEY':'cp_{name}','FOREIGN_KEY':'ce_{name}','UNIQUE':'ct_{name}_uq','CHECK':'ct_{name}{qualifier}',},'SQL_FORMAT_OPTIONS':{'unquote':True,'identifier_case':'lower','keyword_case':'lower',},# Order of SQL statements'SQL_STATEMENTS_ORDER':['PRIMARY_KEY','UNIQUE','FOREIGN_KEY','CHECK','INDEX','COMMENT','CONTROL',# Grant/revoke table privileges for specified role (if exists)'AUTOINCREMENT',# Sequence and triggers for auto-incremented fields]}The abovePersonmodel would create a database table like this:createtableexample.tb_person(idnumber(11),first_namenvarchar2(30),last_namenvarchar2(30));/altertableexample.tb_personaddconstraintcp_person_idprimarykey(id);/altertableexample.tb_personaddconstraintct_person_id_nncheck(idisnotnull);/altertableexample.tb_personaddconstraintct_person_first_name_nncheck(first_nameisnotnull);/altertableexample.tb_personaddconstraintct_person_last_name_nncheck(last_nameisnotnull);/commentoncolumnexample.tb_person.last_nameis'It''s your last name';/grantselect,insert,update,deleteonexample.tb_persontorl_example;/createsequenceexample.sq_personminvalue1maxvalue99999999999startwith1incrementby1cache20;/grantselectonexample.sq_persontorl_example;/createorreplacetriggerexample.tg_person_bbeforeinsertonexample.tb_personforeachrowwhen(new.idisnull)beginselectexample.sq_person.nextvalinto:new.idfromdual;end;/Release notesv1.0.0- Apr 16, 2018 - First releasev1.0.1- Apr 16, 2018 - Rename package and fix setup issuesv1.0.2- Apr 17, 2018 - Fix documentation previewv2.0.0- Mar 1, 2021 - Recreate the entire schema editor backend with more flexible featuresv2.0.1- Mar 22, 2021 - Escape single quotes on column comments