Package: django-view-manager
django-view-managerA management command for django, designed to provide a way in pull requests, to see a diff of the sql (CREATE VIEW ...) for unmanaged models.Capable of handling migrations with the same number (refer to the musicians app and test) and partially capable of handing views that use other views. Views using views requires some manual additions to migrations, so it can drop required views and set them back up (refer to migration 0004 in the store app for how this can be accomplished). Once set up, it is capable of updating the sql view name in any migration that uses it.The management command creates ansqlfolder inside an app, along with files likeview-animals_pets-latest.sql(live) andview-animals_pets-0002.sql(historical), where you write your sql. Migrations are also created in the process, which read these files, so you don't need to create them yourself.Refer to folder and file structure, and usage, for more detailed information.Table of Contentsdjango-view-managerInstallationRequirementsFolder and File StructureUsageCalling the command with no arguments:Calling the command with arguments on a new app:Calling the command again, when changes are needed:Installation$pipinstalldjango-view-manageror$pipenvinstalldjango-view-managerAdddjango-view-manager.utilsto the INSTALLED_APPS in settings. If possible, do this in your development settings only, as you don't need this package on a production server.INSTALLED_APPS=[..."django-view-manager.utils",...]RequirementsAt least a django 3.2 and python 3.7.It may work in an older django, but we only test against supported versions.Folder and File StructureThe following folder and file structure is used by this management command. If you were to run the commands listed under usage, you would end up with the following:project_nameemployeesmigrations0001_initial.py0002_create_view.py0003_add_date_to_employee_likes.pysqlview-employees_employeelikes-0002.sqlview-employees_employeelikes-latest.sql__init__.pyapps.pymodels.pyThe numbers in a filename are associated to the corresponding migration number, and are meant to be historic.UsageIf you need to know how to run a django management command, please refer to the documentation in django for more details.Examples in this documentation use the apps in the test folder, which are also used by tests. The examples will focus on theemployeesapp.https://github.com/arrai-innovations/django-view-manager/blob/ccf70282f4ca5a45946a514fd859b8352706296a/tests/employees/models.py#L4-L27Calling the command with no arguments:$pythonmanage.pymakeviewmigrationThe results will be:$pythonmanage.pymakeviewmigration[-h][--version][-v{0,1,2,3}][--settingsSETTINGS][--pythonpathPYTHONPATH][--traceback][--no-color][--force-color][--skip-checks]{animals_pets,employees_employeelikes,food_sweets}migration_name
manage.pymakeviewmigration:error:thefollowingargumentsarerequired:db_table_name,migration_nameCalling the command with arguments on a new app:$pythonmanage.pymakeviewmigrationemployees_employeelikescreate_viewThe results will be:Created 'migrations' folder in app 'employees'.
‧
Creating initial migration for app 'employees'.
Migrations for 'employees':
  project_name/employees/migrations/0001_initial.py
    - Create model Sweets
‧
Created 'sql' folder in app 'employees'.
‧
Creating empty migration for the new SQL view.
Migrations for 'employees':
  project_name/employees/migrations/0002_create_view.py
    - Raw SQL operation
‧
Created new SQL view file - 'view-employees_employeelikes-latest.sql'.
‧
Modified migration '0002_create_view' to read from 'view-employees_employeelikes-latest.sql'.
‧
Done - You can now edit 'view-employees_employeelikes-latest.sql'.Instructions will be added into theview-employees_employeelikes-latest.sqlfile:/*This file was generated using django-view-manager 1.0.0.Add the SQL for this view and then commit the changes.You can remove this comment before committing.‧When you have changes to make to this sql, you need to run the makeviewmigration commandbefore altering the sql, so the historical sql file is created with the correct contents.‧eg.DROP VIEW IF EXISTS animals_pets;CREATE VIEWanimals_pets ASSELECT1 AS id,42 AS employee_id,'Kittens' AS nameUNION2 AS id,314 AS employee_id,'Puppies' AS name*/Calling the command again, when changes are needed:Important:Run the command before you alter the contents of your sql file, likeview-employees_employeelikes-latest.sql. If you do not, the historical version created by the command will not contain the sql that it should,$pythonmanage.pymakeviewmigrationemployees_employeelikesadd_date_to_employee_likesThe results will be:Creating empty migration for the SQL changes.
Migrations for 'employees':
  tests/employees/migrations/0003_add_date_to_employee_likes.py
    - Raw SQL operation
‧
Created historical SQL view file - 'view-employees_employeelikes-0002.sql'.
‧
Modified migration '0002_create_view' to read from 'view-employees_employeelikes-0002.sql'.
‧
Modified migration '0003_add_date_to_employee_likes' to read from 'view-employees_employeelikes-latest.sql' and 'view-employees_employeelikes-0002.sql'.
‧
Done - You can now edit 'view-employees_employeelikes-latest.sql'.The historic fileview-employees_employeelikes-0002.sqlbecomes a copy ofview-employees_employeelikes-latest.sql, and the corresponding migration 0002 is modified to use this historic file./*This file was generated using django-view-manager 1.0.0.Modify the SQL for this view and then commit the changes.You can remove this comment before committing.‧When you have changes to make to this sql, you need to run the makeviewmigration commandbefore altering the sql, so the historical sql file is created with the correct contents.*/DROPVIEWIFEXISTSemployees_employeelikes;CREATEVIEWemployees_employeelikesASSELECT1ASid,42ASemployee_id,'Kittens'ASnameUNION2ASid,314ASemployee_id,'Puppies'ASname