django-migrate-sql-deux 0.5.2__tar.gz → 0.6.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. django_migrate_sql_deux-0.6.0/PKG-INFO +312 -0
  2. django_migrate_sql_deux-0.6.0/README.md +276 -0
  3. django_migrate_sql_deux-0.6.0/django_migrate_sql_deux.egg-info/PKG-INFO +312 -0
  4. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/django_migrate_sql_deux.egg-info/SOURCES.txt +1 -1
  5. django_migrate_sql_deux-0.6.0/migrate_sql/__init__.py +1 -0
  6. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/setup.py +11 -4
  7. django_migrate_sql_deux-0.6.0/tests/test_app/urls.py +6 -0
  8. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/tests/test_project/settings.py +1 -0
  9. django_migrate_sql_deux-0.6.0/tests/test_project/urls.py +6 -0
  10. django_migrate_sql_deux-0.5.2/PKG-INFO +0 -302
  11. django_migrate_sql_deux-0.5.2/README.rst +0 -274
  12. django_migrate_sql_deux-0.5.2/django_migrate_sql_deux.egg-info/PKG-INFO +0 -302
  13. django_migrate_sql_deux-0.5.2/migrate_sql/__init__.py +0 -1
  14. django_migrate_sql_deux-0.5.2/tests/test_app/urls.py +0 -13
  15. django_migrate_sql_deux-0.5.2/tests/test_project/urls.py +0 -10
  16. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/LICENSE +0 -0
  17. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/django_migrate_sql_deux.egg-info/dependency_links.txt +0 -0
  18. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/django_migrate_sql_deux.egg-info/not-zip-safe +0 -0
  19. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/django_migrate_sql_deux.egg-info/top_level.txt +0 -0
  20. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/migrate_sql/autodetector.py +0 -0
  21. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/migrate_sql/config.py +0 -0
  22. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/migrate_sql/graph.py +0 -0
  23. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/migrate_sql/management/__init__.py +0 -0
  24. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/migrate_sql/management/commands/__init__.py +0 -0
  25. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/migrate_sql/management/commands/makemigrations.py +0 -0
  26. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/migrate_sql/operations.py +0 -0
  27. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/pyproject.toml +0 -0
  28. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/setup.cfg +0 -0
  29. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/tests/__init__.py +0 -0
  30. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/tests/manage.py +0 -0
  31. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/tests/test_app/__init__.py +0 -0
  32. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/tests/test_app/apps.py +0 -0
  33. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/tests/test_app/migrations/0001_initial.py +0 -0
  34. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/tests/test_app/migrations/__init__.py +0 -0
  35. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/tests/test_app/migrations_change/0001_initial.py +0 -0
  36. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/tests/test_app/migrations_change/0002_auto_20151224_1827.py +0 -0
  37. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/tests/test_app/migrations_change/__init__.py +0 -0
  38. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/tests/test_app/migrations_deps_delete/0001_initial.py +0 -0
  39. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/tests/test_app/migrations_deps_delete/0002_auto_20160106_0947.py +0 -0
  40. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/tests/test_app/migrations_deps_delete/0003_auto_20160108_0048.py +0 -0
  41. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/tests/test_app/migrations_deps_delete/0004_auto_20160108_0048.py +0 -0
  42. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/tests/test_app/migrations_deps_delete/__init__.py +0 -0
  43. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/tests/test_app/migrations_deps_update/0001_initial.py +0 -0
  44. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/tests/test_app/migrations_deps_update/0002_auto_20160106_0947.py +0 -0
  45. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/tests/test_app/migrations_deps_update/__init__.py +0 -0
  46. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/tests/test_app/migrations_recreate/0001_initial.py +0 -0
  47. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/tests/test_app/migrations_recreate/0002_auto_20151224_1827.py +0 -0
  48. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/tests/test_app/migrations_recreate/0003_auto_20160106_1038.py +0 -0
  49. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/tests/test_app/migrations_recreate/__init__.py +0 -0
  50. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/tests/test_app/models.py +0 -0
  51. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/tests/test_app/sql_config.py +0 -0
  52. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/tests/test_app/test_migrations.py +0 -0
  53. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/tests/test_app/test_utils.py +0 -0
  54. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/tests/test_app2/__init__.py +0 -0
  55. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/tests/test_app2/apps.py +0 -0
  56. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/tests/test_app2/migrations/__init__.py +0 -0
  57. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/tests/test_app2/migrations_deps_delete/0001_initial.py +0 -0
  58. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/tests/test_app2/migrations_deps_delete/0002_auto_20160108_0041.py +0 -0
  59. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/tests/test_app2/migrations_deps_delete/__init__.py +0 -0
  60. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/tests/test_app2/migrations_deps_update/0001_initial.py +0 -0
  61. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/tests/test_app2/migrations_deps_update/__init__.py +0 -0
  62. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/tests/test_app2/models.py +0 -0
  63. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/tests/test_app2/sql_config.py +0 -0
  64. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/tests/test_app2/urls.py +0 -0
  65. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/tests/test_project/__init__.py +0 -0
  66. {django_migrate_sql_deux-0.5.2 → django_migrate_sql_deux-0.6.0}/tests/test_project/wsgi.py +0 -0
@@ -0,0 +1,312 @@
1
+ Metadata-Version: 2.4
2
+ Name: django-migrate-sql-deux
3
+ Version: 0.6.0
4
+ Summary: Migration support for raw SQL in Django
5
+ Home-page: https://github.com/browniebroke/django-migrate-sql-deux
6
+ Author: Bruno Alla
7
+ Author-email: oss@browniebroke.com
8
+ License: BSD
9
+ Classifier: Development Status :: 3 - Alpha
10
+ Classifier: Framework :: Django
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: License :: OSI Approved :: BSD License
13
+ Classifier: Natural Language :: English
14
+ Classifier: Programming Language :: Python :: 3.6
15
+ Classifier: Programming Language :: Python :: 3.7
16
+ Classifier: Programming Language :: Python :: 3.8
17
+ Classifier: Programming Language :: Python :: 3.9
18
+ Classifier: Programming Language :: Python :: 3.10
19
+ Classifier: Framework :: Django :: 2.1
20
+ Classifier: Framework :: Django :: 2.2
21
+ Classifier: Framework :: Django :: 3.0
22
+ Classifier: Framework :: Django :: 3.1
23
+ Classifier: Framework :: Django :: 3.2
24
+ Classifier: Framework :: Django :: 4.0
25
+ Description-Content-Type: text/markdown
26
+ License-File: LICENSE
27
+ Dynamic: author
28
+ Dynamic: author-email
29
+ Dynamic: classifier
30
+ Dynamic: description
31
+ Dynamic: description-content-type
32
+ Dynamic: home-page
33
+ Dynamic: license
34
+ Dynamic: license-file
35
+ Dynamic: summary
36
+
37
+ # django-migrate-sql-deux
38
+
39
+ <p align="center">
40
+ <a href="https://github.com/browniebroke/django-migrate-sql-deux/actions/workflows/ci.yml?query=branch%3Amain">
41
+ <img src="https://img.shields.io/github/actions/workflow/status/browniebroke/django-migrate-sql-deux/ci.yml?branch=main&label=CI&logo=github&style=flat-square" alt="CI Status" >
42
+ </a>
43
+ <a href="https://codecov.io/gh/browniebroke/django-migrate-sql-deux">
44
+ <img src="https://img.shields.io/codecov/c/github/browniebroke/django-migrate-sql-deux.svg?logo=codecov&logoColor=fff&style=flat-square" alt="Test coverage percentage">
45
+ </a>
46
+ </p>
47
+ <p align="center">
48
+ <a href="https://github.com/astral-sh/uv">
49
+ <img src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/uv/main/assets/badge/v0.json" alt="uv">
50
+ </a>
51
+ <a href="https://github.com/astral-sh/ruff">
52
+ <img src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json" alt="Ruff">
53
+ </a>
54
+ <a href="https://github.com/pre-commit/pre-commit">
55
+ <img src="https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white&style=flat-square" alt="pre-commit">
56
+ </a>
57
+ </p>
58
+ <p align="center">
59
+ <a href="https://pypi.org/project/django-migrate-sql-deux/">
60
+ <img src="https://img.shields.io/pypi/v/django-migrate-sql-deux.svg?logo=python&logoColor=fff&style=flat-square" alt="PyPI Version">
61
+ </a>
62
+ <img src="https://img.shields.io/pypi/pyversions/django-migrate-sql-deux.svg?style=flat-square&logo=python&amp;logoColor=fff" alt="Supported Python versions">
63
+ <img src="https://img.shields.io/pypi/l/django-migrate-sql-deux.svg?style=flat-square" alt="License">
64
+ </p>
65
+
66
+ ---
67
+
68
+ **Source Code**: <a href="https://github.com/browniebroke/django-migrate-sql-deux" target="_blank">https://github.com/browniebroke/django-migrate-sql-deux </a>
69
+
70
+ ---
71
+
72
+ Django Migrations support for raw SQL.
73
+
74
+ > [!NOTE]
75
+ > This package is a fork of the `django-migrate-sql` package, originally published by Bogdan Klichuk. This package appears unmaintained, so we decided to start a fork as we depended on it. Most of the code is from the original author.
76
+
77
+ ## About
78
+
79
+ This tool implements mechanism for managing changes to custom SQL entities (functions, types, indices, triggers) using built-in migration mechanism. Technically creates a sophistication layer on top of the
80
+ `RunSQL` Django operation.
81
+
82
+ ## What it does
83
+
84
+ - Makes maintaining your SQL functions, custom composite types, indices and triggers easier.
85
+ - Structures SQL into configuration of **SQL items**, that are
86
+ identified by names and divided among apps, just like models.
87
+ - Automatically gathers and persists changes of your custom SQL into
88
+ migrations using `makemigrations`.
89
+ - Properly executes backwards/forwards keeping integrity of database.
90
+ - Create -> Drop -> Recreate approach for changes to items that do not
91
+ support altering and require dropping and recreating.
92
+ - Dependencies system for SQL items, which solves the problem of
93
+ updating items, that rely on others (for example custom
94
+ types/functions that use other custom types), and require dropping all
95
+ dependency tree previously with further recreation.
96
+
97
+ ## What it does not
98
+
99
+ - Does not parse SQL nor validate queries during `makemigrations` or
100
+ `migrate` because is database-agnostic. For this same reason setting
101
+ up proper dependencies is user's responsibility.
102
+ - Does not create `ALTER` queries for items that support this, for
103
+ example `ALTER TYPE` in PostgreSQL, because is database-agnostic. In
104
+ case your tools allow rolling all the changes through `ALTER` queries,
105
+ you can consider not using this app **or** restructure migrations
106
+ manually after creation by nesting generated operations into
107
+ `` `state_operations `` of [`RunSQL`](https://docs.djangoproject.com/en/1.8/ref/migration-operations/#runsql)
108
+ that does `ALTER`.
109
+ - (**TODO**)During `migrate` does not restore full state of items for
110
+ analysis, thus does not notify about existing changes to schema that
111
+ are not migrated **nor** does not recognize circular dependencies
112
+ during migration execution.
113
+
114
+ ## Installation
115
+
116
+ Install from PyPi:
117
+
118
+ ```shell
119
+ pip install django-migrate-sql-deux
120
+ ```
121
+
122
+ Add `migrate_sql` to `INSTALLED_APPS`:
123
+
124
+ ```python
125
+ INSTALLED_APPS = [
126
+ # ...
127
+ 'migrate_sql',
128
+ ]
129
+ ```
130
+
131
+ App defines a custom `makemigrations` command, that inherits from
132
+ Django's core one, so in order `migrate_sql` app to kick in put it
133
+ after any other apps that redefine `makemigrations` command too.
134
+
135
+ ## Usage
136
+
137
+ 1) Create `sql_config.py` module to root of a target app you want to
138
+ manage custom SQL for.
139
+ 2) Define SQL items in it (`sql_items`), for example:
140
+
141
+ ```python
142
+ # PostgreSQL example.
143
+ # Let's define a simple function and let `migrate_sql` manage it's changes.
144
+
145
+ from migrate_sql.config import SQLItem
146
+
147
+ sql_items = [
148
+ SQLItem(
149
+ 'make_sum', # name of the item
150
+ 'create or replace function make_sum(a int, b int) returns int as $$ '
151
+ 'begin return a + b; end; '
152
+ '$$ language plpgsql;', # forward sql
153
+ reverse_sql='drop function make_sum(int, int);', # sql for removal
154
+ ),
155
+ ]
156
+ ```
157
+
158
+ 3) Create migration `./manage.py makemigrations`:
159
+
160
+ Migrations for 'app_name':
161
+ 0002_auto_xxxx.py:
162
+ - Create SQL "make_sum"
163
+
164
+ You can take a look at content this generated:
165
+
166
+ ```python
167
+ from django.db import migrations, models
168
+ import migrate_sql.operations
169
+
170
+
171
+ class Migration(migrations.Migration):
172
+ dependencies = [
173
+ ('app_name', '0001_initial'),
174
+ ]
175
+ operations = [
176
+ migrate_sql.operations.CreateSQL(
177
+ name='make_sum',
178
+ sql='create or replace function make_sum(a int, b int) returns int as $$ begin return a + b; end; $$ language plpgsql;',
179
+ reverse_sql='drop function make_sum(int, int);',
180
+ ),
181
+ ]
182
+ ```
183
+
184
+ 4) Execute migration `./manage.py migrate`:
185
+
186
+ Operations to perform:
187
+ Apply all migrations: app_name
188
+ Running migrations:
189
+ Rendering model states... DONE
190
+ Applying app_name.0002_xxxx... OK
191
+
192
+ Check result in `./manage.py dbshell`:
193
+
194
+ db_name=# select make_sum(12, 15);
195
+ make_sum
196
+ ----------
197
+ 27
198
+ (1 row)
199
+
200
+ Now, say, you want to change the function implementation so that it
201
+ takes a custom type as argument:
202
+
203
+ 5) Edit your `sql_config.py`:
204
+
205
+ ``` python
206
+ # PostgreSQL example #2.
207
+ # Function and custom type.
208
+
209
+ from migrate_sql.config import SQLItem
210
+
211
+ sql_items = [
212
+ SQLItem(
213
+ 'make_sum', # name of the item
214
+ 'create or replace function make_sum(a mynum, b mynum) returns mynum as $$ '
215
+ 'begin return (a.num + b.num, 'result')::mynum; end; '
216
+ '$$ language plpgsql;', # forward sql
217
+ reverse_sql='drop function make_sum(mynum, mynum);', # sql for removal
218
+ # depends on `mynum` since takes it as argument. we won't be able to drop function
219
+ # without dropping `mynum` first.
220
+ dependencies=[('app_name', 'mynum')],
221
+ ),
222
+ SQLItem(
223
+ 'mynum' # name of the item
224
+ 'create type mynum as (num int, name varchar(20));', # forward sql
225
+ reverse_sql='drop type mynum;', # sql for removal
226
+ ),
227
+ ]
228
+ ```
229
+
230
+ 6) Generate migration `./manage.py makemigrations`:
231
+
232
+ ```
233
+ Migrations for 'app_name':
234
+ 0003_xxxx:
235
+ - Reverse alter SQL "make_sum"
236
+ - Create SQL "mynum"
237
+ - Alter SQL "make_sum"
238
+ - Alter SQL state "make_sum"
239
+ ```
240
+
241
+ You can take a look at the content this generated:
242
+
243
+ ```python
244
+ from django.db import migrations, models
245
+ import migrate_sql.operations
246
+
247
+
248
+ class Migration(migrations.Migration):
249
+ dependencies = [
250
+ ('app_name', '0002_xxxx'),
251
+ ]
252
+ operations = [
253
+ migrate_sql.operations.ReverseAlterSQL(
254
+ name='make_sum',
255
+ sql='drop function make_sum(int, int);',
256
+ reverse_sql='create or replace function make_sum(a int, b int) returns int as $$ begin return a + b; end; $$ language plpgsql;',
257
+ ),
258
+ migrate_sql.operations.CreateSQL(
259
+ name='mynum',
260
+ sql='create type mynum as (num int, name varchar(20));',
261
+ reverse_sql='drop type mynum;',
262
+ ),
263
+ migrate_sql.operations.AlterSQL(
264
+ name='make_sum',
265
+ sql='create or replace function make_sum(a mynum, b mynum) returns mynum as $$ begin return (a.num + b.num, \'result\')::mynum; end; $$ language plpgsql;',
266
+ reverse_sql='drop function make_sum(mynum, mynum);',
267
+ ),
268
+ migrate_sql.operations.AlterSQLState(
269
+ name='make_sum',
270
+ add_dependencies=(('app_name', 'mynum'),),
271
+ ),
272
+ ]
273
+ ```
274
+
275
+ **NOTE:** Previous function is completely dropped before creation
276
+ because definition of it changed. `CREATE OR REPLACE` would create
277
+ another version of it, so `DROP` makes it clean.
278
+
279
+ **If you put `replace=True` as kwarg to an `SQLItem`
280
+ definition, it will NOT drop + create it, but just rerun forward SQL,
281
+ which is `CREATE OR REPLACE` in this example.**
282
+
283
+ 7) Execute migration `./manage.py migrate`:
284
+
285
+
286
+ ```
287
+ Operations to perform:
288
+ Apply all migrations: app_name
289
+ Running migrations:
290
+ Rendering model states... DONE
291
+ Applying brands.0003_xxxx... OK
292
+ ```
293
+
294
+ Check results:
295
+
296
+ ```
297
+ db_name=# select make_sum((5, 'a')::mynum, (3, 'b')::mynum);
298
+ make_sum
299
+ ------------
300
+ (8,result)
301
+ (1 row)
302
+
303
+ db_name=# select make_sum(12, 15);
304
+ ERROR: function make_sum(integer, integer) does not exist
305
+ LINE 1: select make_sum(12, 15);
306
+ ^
307
+ HINT: No function matches the given name and argument types. You might need to add explicit type casts.
308
+ ```
309
+
310
+ For more examples see `tests`.
311
+
312
+ Feel free to [open new issues](https://github.com/browniebroke/django-migrate-sql-deux/issues).
@@ -0,0 +1,276 @@
1
+ # django-migrate-sql-deux
2
+
3
+ <p align="center">
4
+ <a href="https://github.com/browniebroke/django-migrate-sql-deux/actions/workflows/ci.yml?query=branch%3Amain">
5
+ <img src="https://img.shields.io/github/actions/workflow/status/browniebroke/django-migrate-sql-deux/ci.yml?branch=main&label=CI&logo=github&style=flat-square" alt="CI Status" >
6
+ </a>
7
+ <a href="https://codecov.io/gh/browniebroke/django-migrate-sql-deux">
8
+ <img src="https://img.shields.io/codecov/c/github/browniebroke/django-migrate-sql-deux.svg?logo=codecov&logoColor=fff&style=flat-square" alt="Test coverage percentage">
9
+ </a>
10
+ </p>
11
+ <p align="center">
12
+ <a href="https://github.com/astral-sh/uv">
13
+ <img src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/uv/main/assets/badge/v0.json" alt="uv">
14
+ </a>
15
+ <a href="https://github.com/astral-sh/ruff">
16
+ <img src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json" alt="Ruff">
17
+ </a>
18
+ <a href="https://github.com/pre-commit/pre-commit">
19
+ <img src="https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white&style=flat-square" alt="pre-commit">
20
+ </a>
21
+ </p>
22
+ <p align="center">
23
+ <a href="https://pypi.org/project/django-migrate-sql-deux/">
24
+ <img src="https://img.shields.io/pypi/v/django-migrate-sql-deux.svg?logo=python&logoColor=fff&style=flat-square" alt="PyPI Version">
25
+ </a>
26
+ <img src="https://img.shields.io/pypi/pyversions/django-migrate-sql-deux.svg?style=flat-square&logo=python&amp;logoColor=fff" alt="Supported Python versions">
27
+ <img src="https://img.shields.io/pypi/l/django-migrate-sql-deux.svg?style=flat-square" alt="License">
28
+ </p>
29
+
30
+ ---
31
+
32
+ **Source Code**: <a href="https://github.com/browniebroke/django-migrate-sql-deux" target="_blank">https://github.com/browniebroke/django-migrate-sql-deux </a>
33
+
34
+ ---
35
+
36
+ Django Migrations support for raw SQL.
37
+
38
+ > [!NOTE]
39
+ > This package is a fork of the `django-migrate-sql` package, originally published by Bogdan Klichuk. This package appears unmaintained, so we decided to start a fork as we depended on it. Most of the code is from the original author.
40
+
41
+ ## About
42
+
43
+ This tool implements mechanism for managing changes to custom SQL entities (functions, types, indices, triggers) using built-in migration mechanism. Technically creates a sophistication layer on top of the
44
+ `RunSQL` Django operation.
45
+
46
+ ## What it does
47
+
48
+ - Makes maintaining your SQL functions, custom composite types, indices and triggers easier.
49
+ - Structures SQL into configuration of **SQL items**, that are
50
+ identified by names and divided among apps, just like models.
51
+ - Automatically gathers and persists changes of your custom SQL into
52
+ migrations using `makemigrations`.
53
+ - Properly executes backwards/forwards keeping integrity of database.
54
+ - Create -> Drop -> Recreate approach for changes to items that do not
55
+ support altering and require dropping and recreating.
56
+ - Dependencies system for SQL items, which solves the problem of
57
+ updating items, that rely on others (for example custom
58
+ types/functions that use other custom types), and require dropping all
59
+ dependency tree previously with further recreation.
60
+
61
+ ## What it does not
62
+
63
+ - Does not parse SQL nor validate queries during `makemigrations` or
64
+ `migrate` because is database-agnostic. For this same reason setting
65
+ up proper dependencies is user's responsibility.
66
+ - Does not create `ALTER` queries for items that support this, for
67
+ example `ALTER TYPE` in PostgreSQL, because is database-agnostic. In
68
+ case your tools allow rolling all the changes through `ALTER` queries,
69
+ you can consider not using this app **or** restructure migrations
70
+ manually after creation by nesting generated operations into
71
+ `` `state_operations `` of [`RunSQL`](https://docs.djangoproject.com/en/1.8/ref/migration-operations/#runsql)
72
+ that does `ALTER`.
73
+ - (**TODO**)During `migrate` does not restore full state of items for
74
+ analysis, thus does not notify about existing changes to schema that
75
+ are not migrated **nor** does not recognize circular dependencies
76
+ during migration execution.
77
+
78
+ ## Installation
79
+
80
+ Install from PyPi:
81
+
82
+ ```shell
83
+ pip install django-migrate-sql-deux
84
+ ```
85
+
86
+ Add `migrate_sql` to `INSTALLED_APPS`:
87
+
88
+ ```python
89
+ INSTALLED_APPS = [
90
+ # ...
91
+ 'migrate_sql',
92
+ ]
93
+ ```
94
+
95
+ App defines a custom `makemigrations` command, that inherits from
96
+ Django's core one, so in order `migrate_sql` app to kick in put it
97
+ after any other apps that redefine `makemigrations` command too.
98
+
99
+ ## Usage
100
+
101
+ 1) Create `sql_config.py` module to root of a target app you want to
102
+ manage custom SQL for.
103
+ 2) Define SQL items in it (`sql_items`), for example:
104
+
105
+ ```python
106
+ # PostgreSQL example.
107
+ # Let's define a simple function and let `migrate_sql` manage it's changes.
108
+
109
+ from migrate_sql.config import SQLItem
110
+
111
+ sql_items = [
112
+ SQLItem(
113
+ 'make_sum', # name of the item
114
+ 'create or replace function make_sum(a int, b int) returns int as $$ '
115
+ 'begin return a + b; end; '
116
+ '$$ language plpgsql;', # forward sql
117
+ reverse_sql='drop function make_sum(int, int);', # sql for removal
118
+ ),
119
+ ]
120
+ ```
121
+
122
+ 3) Create migration `./manage.py makemigrations`:
123
+
124
+ Migrations for 'app_name':
125
+ 0002_auto_xxxx.py:
126
+ - Create SQL "make_sum"
127
+
128
+ You can take a look at content this generated:
129
+
130
+ ```python
131
+ from django.db import migrations, models
132
+ import migrate_sql.operations
133
+
134
+
135
+ class Migration(migrations.Migration):
136
+ dependencies = [
137
+ ('app_name', '0001_initial'),
138
+ ]
139
+ operations = [
140
+ migrate_sql.operations.CreateSQL(
141
+ name='make_sum',
142
+ sql='create or replace function make_sum(a int, b int) returns int as $$ begin return a + b; end; $$ language plpgsql;',
143
+ reverse_sql='drop function make_sum(int, int);',
144
+ ),
145
+ ]
146
+ ```
147
+
148
+ 4) Execute migration `./manage.py migrate`:
149
+
150
+ Operations to perform:
151
+ Apply all migrations: app_name
152
+ Running migrations:
153
+ Rendering model states... DONE
154
+ Applying app_name.0002_xxxx... OK
155
+
156
+ Check result in `./manage.py dbshell`:
157
+
158
+ db_name=# select make_sum(12, 15);
159
+ make_sum
160
+ ----------
161
+ 27
162
+ (1 row)
163
+
164
+ Now, say, you want to change the function implementation so that it
165
+ takes a custom type as argument:
166
+
167
+ 5) Edit your `sql_config.py`:
168
+
169
+ ``` python
170
+ # PostgreSQL example #2.
171
+ # Function and custom type.
172
+
173
+ from migrate_sql.config import SQLItem
174
+
175
+ sql_items = [
176
+ SQLItem(
177
+ 'make_sum', # name of the item
178
+ 'create or replace function make_sum(a mynum, b mynum) returns mynum as $$ '
179
+ 'begin return (a.num + b.num, 'result')::mynum; end; '
180
+ '$$ language plpgsql;', # forward sql
181
+ reverse_sql='drop function make_sum(mynum, mynum);', # sql for removal
182
+ # depends on `mynum` since takes it as argument. we won't be able to drop function
183
+ # without dropping `mynum` first.
184
+ dependencies=[('app_name', 'mynum')],
185
+ ),
186
+ SQLItem(
187
+ 'mynum' # name of the item
188
+ 'create type mynum as (num int, name varchar(20));', # forward sql
189
+ reverse_sql='drop type mynum;', # sql for removal
190
+ ),
191
+ ]
192
+ ```
193
+
194
+ 6) Generate migration `./manage.py makemigrations`:
195
+
196
+ ```
197
+ Migrations for 'app_name':
198
+ 0003_xxxx:
199
+ - Reverse alter SQL "make_sum"
200
+ - Create SQL "mynum"
201
+ - Alter SQL "make_sum"
202
+ - Alter SQL state "make_sum"
203
+ ```
204
+
205
+ You can take a look at the content this generated:
206
+
207
+ ```python
208
+ from django.db import migrations, models
209
+ import migrate_sql.operations
210
+
211
+
212
+ class Migration(migrations.Migration):
213
+ dependencies = [
214
+ ('app_name', '0002_xxxx'),
215
+ ]
216
+ operations = [
217
+ migrate_sql.operations.ReverseAlterSQL(
218
+ name='make_sum',
219
+ sql='drop function make_sum(int, int);',
220
+ reverse_sql='create or replace function make_sum(a int, b int) returns int as $$ begin return a + b; end; $$ language plpgsql;',
221
+ ),
222
+ migrate_sql.operations.CreateSQL(
223
+ name='mynum',
224
+ sql='create type mynum as (num int, name varchar(20));',
225
+ reverse_sql='drop type mynum;',
226
+ ),
227
+ migrate_sql.operations.AlterSQL(
228
+ name='make_sum',
229
+ sql='create or replace function make_sum(a mynum, b mynum) returns mynum as $$ begin return (a.num + b.num, \'result\')::mynum; end; $$ language plpgsql;',
230
+ reverse_sql='drop function make_sum(mynum, mynum);',
231
+ ),
232
+ migrate_sql.operations.AlterSQLState(
233
+ name='make_sum',
234
+ add_dependencies=(('app_name', 'mynum'),),
235
+ ),
236
+ ]
237
+ ```
238
+
239
+ **NOTE:** Previous function is completely dropped before creation
240
+ because definition of it changed. `CREATE OR REPLACE` would create
241
+ another version of it, so `DROP` makes it clean.
242
+
243
+ **If you put `replace=True` as kwarg to an `SQLItem`
244
+ definition, it will NOT drop + create it, but just rerun forward SQL,
245
+ which is `CREATE OR REPLACE` in this example.**
246
+
247
+ 7) Execute migration `./manage.py migrate`:
248
+
249
+
250
+ ```
251
+ Operations to perform:
252
+ Apply all migrations: app_name
253
+ Running migrations:
254
+ Rendering model states... DONE
255
+ Applying brands.0003_xxxx... OK
256
+ ```
257
+
258
+ Check results:
259
+
260
+ ```
261
+ db_name=# select make_sum((5, 'a')::mynum, (3, 'b')::mynum);
262
+ make_sum
263
+ ------------
264
+ (8,result)
265
+ (1 row)
266
+
267
+ db_name=# select make_sum(12, 15);
268
+ ERROR: function make_sum(integer, integer) does not exist
269
+ LINE 1: select make_sum(12, 15);
270
+ ^
271
+ HINT: No function matches the given name and argument types. You might need to add explicit type casts.
272
+ ```
273
+
274
+ For more examples see `tests`.
275
+
276
+ Feel free to [open new issues](https://github.com/browniebroke/django-migrate-sql-deux/issues).