Skip to content

Commit a4b1afd

Browse files
committed
add README.md
fix release CI
1 parent c2117a4 commit a4b1afd

File tree

7 files changed

+165
-5
lines changed

7 files changed

+165
-5
lines changed

.github/workflows/release.yml

+6
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,9 @@ jobs:
3030
python setup.py --version
3131
python setup.py sdist --format=gztar bdist_wheel
3232
twine check dist/*
33+
34+
- name: Publish package
35+
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
36+
uses: pypa/gh-action-pypi-publish@release/v1
37+
with:
38+
password: ${{ secrets.PYPI_API_TOKEN }}

.github/workflows/default.yml renamed to .github/workflows/test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Python CI
1+
name: Test
22

33
on:
44
push:

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*.egg-info
2+
.eggs
23
/__pycache__
34
.pytest_cache
45
*.pyc

README.md

+150
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
[![GitHub Actions](https://github.com/muehlemann-popp/django-materialized-view/workflows/Test/badge.svg)](https://github.com/muehlemann-popp/django-materialized-view/actions)
2+
[![codecov](https://codecov.io/gh/muehlemann-popp/django-materialized-view/branch/main/graph/badge.svg?token=02FP3IS41T)](https://codecov.io/gh/muehlemann-popp/django-materialized-view)
3+
[![GitHub Actions](https://github.com/muehlemann-popp/django-materialized-view/workflows/Release/badge.svg)](https://github.com/muehlemann-popp/django-materialized-view/actions)
4+
![GitHub](https://img.shields.io/github/license/muehlemann-popp/django-materialized-view)
5+
![GitHub last commit](https://img.shields.io/github/last-commit/muehlemann-popp/django-materialized-view)
6+
7+
[![Supported Django versions](https://img.shields.io/pypi/djversions/django-materialized-view.svg)](https://pypi.python.org/pypi/django-materialized-view)
8+
![PyPI - Downloads](https://img.shields.io/pypi/dd/django-materialized-view)
9+
![PyPI](https://img.shields.io/pypi/v/django-materialized-view)
10+
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/django-materialized-view)
11+
12+
13+
Materialized View support for the Django framework. Django (in general) does not support materialized views by the default
14+
therefor migrations are not created automatically with `./manage.py makemigrations`.
15+
This library provides new `manage.py` command: `./manage.py migrate_with_views`.
16+
This command is to be used instead of the default one `migrate`.
17+
18+
This command automatically finds your materialized view models and keeps them up to date.
19+
In case when materialized view is a parent view for another materialized view, use `migrate_with_views` command
20+
in order to change query of parent materialized view.
21+
`migrate_with_views` command finds all related materialized views and recreates them sequentially.
22+
23+
## Contents
24+
25+
* [Requirements](#requirements)
26+
* [Installation](#installation)
27+
* [Usage](#Usage)
28+
* [Create class and inherit from MaterializedViewModel](#create-class-and-inherit-from-materializedviewmodel)
29+
* [Add materialized view query](#add-materialized-view-query)
30+
* [Create materialized view query with .sql file](#create-materialized-view-query-with-sql-file)
31+
* [Create materialized view query from Queryset](#create-materialized-view-query-from-queryset)
32+
* [Use refresh method to update materialized view data](#use-refresh-method-to-update-materialized-view-data)
33+
34+
35+
## Requirements
36+
37+
django-materialized-view has been tested with:
38+
39+
* Django: 4.0, 4.1
40+
* Python: 3.9, 3.10, 3.11
41+
42+
## Installation
43+
44+
Via pip into a `virtualenv`:
45+
46+
```bash
47+
pip install django-materialized-view
48+
```
49+
50+
In `settings.py` add the following:
51+
52+
```python
53+
INSTALLED_APPS = (
54+
...
55+
'django_materialized_view'
56+
)
57+
```
58+
Before running migrate:
59+
60+
```bash
61+
python manage.py migrate
62+
```
63+
64+
Then you can use new migrate command instead of the default one:
65+
```bash
66+
python manage.py migrate_with_views
67+
```
68+
69+
This command will automatically begin interception of materialized view models,
70+
and proceed to create/delete/update your view on your DB if required.
71+
72+
## Usage
73+
74+
1. ### Create class and inherit from `MaterializedViewModel`
75+
76+
EXAMPLE:
77+
```python
78+
from django.db import models
79+
from django_materialized_view.base_model import MaterializedViewModel
80+
81+
class MyViewModel(MaterializedViewModel):
82+
create_pkey_index = True # if you need add unique field as a primary key and create indexes
83+
84+
class Meta:
85+
managed = False
86+
87+
# if create_pkey_index=True you must add argument primary_key=True
88+
item = models.OneToOneField("app.ItemModel", on_delete=models.DO_NOTHING, primary_key=True, db_column="id")
89+
from_seconds = models.IntegerField()
90+
to_seconds = models.IntegerField()
91+
type = models.CharField(max_length=255)
92+
93+
def get_query_from_queryset(self):
94+
# define this method only in case use queryset as a query for materialized view.
95+
# Method must return Queryset
96+
pass
97+
```
98+
2. ### Add materialized view query
99+
- #### Create materialized view query with .sql file
100+
1. run django default `makemigrations` command for creating model migrations if necessary:
101+
```
102+
./manage.py makemigrations
103+
```
104+
2. run `migrate_with_views` command for getting your new sql file name and path:
105+
```
106+
./manage.py migrate_with_views
107+
```
108+
3. you will get file path in your console
109+
```
110+
[Errno 2] No such file or directory: '.../app/models/materialized_views/sql_files/myviewmodel.sql' - please create SQL file and put it to this directory
111+
```
112+
4. create file on suggested path with suggested name
113+
5. run again django command `migrate_with_views`:
114+
```
115+
./manage.py migrate_with_views
116+
```
117+
this command will run the default `migrate` command and apply materialized views
118+
119+
- #### Create materialized view query from Queryset
120+
1. run django default `makemigrations` command for creating model migrations if necessary:
121+
```
122+
./manage.py makemigrations
123+
```
124+
2. add to your materialized view model the method `get_query_from_queryset`:
125+
```python
126+
def get_query_from_queryset(self):
127+
return SomeModel.objects.all()
128+
```
129+
3. run django command `migrate_with_views`:
130+
```
131+
./manage.py migrate_with_views
132+
```
133+
This command will run default `migrate` command and apply materialized views
134+
3. ### Use `refresh` method to update materialized view data.
135+
1. For updating concurrently:
136+
```
137+
MyViewModel.refresh()
138+
```
139+
2. For updating non-concurrently:
140+
```
141+
MyViewModel.refresh(concurrently=Fasle)
142+
```
143+
Note: All refreshes will be logged in to the model MaterializedViewRefreshLog:
144+
```python
145+
class MaterializedViewRefreshLog(models.Model):
146+
updated_at = models.DateTimeField(auto_now_add=True, db_index=True)
147+
duration = models.DurationField(null=True)
148+
failed = models.BooleanField(default=False)
149+
view_name = models.CharField(max_length=255)
150+
```

django_materialized_view/base_model.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class MaterializedViewModel(DBMaterializedView):
6363
from core.models.materialized_view import MaterializedViewModel
6464
6565
class JiraTimeestimateChangelog(MaterializedViewModel):
66-
create_pkey_index = True # if you need add uniq field as a primary kay and create indexes
66+
create_pkey_index = True # if you need add unique field as a primary key and create indexes
6767
6868
class Meta:
6969
managed = False

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "django-materialized-view"
33
version = "0.1.0"
44
description = ""
5-
authors = ["Farruh Sheripov <sheripov.farruh@gmail.com>"]
5+
authors = ["Farruh Sheripov <farruh[email protected]>"]
66
readme = "README.md"
77
packages = [{include = "django_materialized_view"}]
88

setup.cfg

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ version = 0.1.0
44
url = https://github.com/muehlemann-popp/django_materialized_view
55
description = Plugin for django to support Materialized Views
66
long_description = file: README.md
7+
long_description_content_type = text/markdown
78
description_file = README.md
8-
author = 'Silvan Mühlemann'
9-
author_email = '[email protected]'
9+
author = Farruh Sheripiov
10+
author_email = [email protected]
11+
maintainer = Silvan Mühlemann
12+
maintainer_email = [email protected]
1013
license='MIT'
1114
classifiers =
1215
Framework :: Django

0 commit comments

Comments
 (0)