django中对原始sql的迁移支持
django-migrate-sql-deux的Python项目详细描述
注意
这个包是最初发布的django-migrate-sql包的叉 作者Bogdan Klichuk。这个包似乎没有维护,所以我们决定启动一个fork 因为我们依赖它。大部分密码都是他写的。
django迁移支持原始sql。
关于
此工具实现了管理对自定义sql的更改的机制 使用内置迁移的实体(函数、类型、索引、触发器) 机制。技术上在 RunSQLdjango操作。
它的功能
- 使维护SQL函数、自定义组合类型、索引 更容易触发。
- 将sql构造成sql项的配置 通过名字来识别并在应用程序之间划分,就像模型一样。
- 自动将自定义sql的更改收集并持久化到 使用makemigrations进行迁移。
- 正确地向后/向前执行,保持数据库的完整性。
- 创建->;删除->;对未更改的项重新创建方法 支持修改,需要删除和重新创建。
- SQL项的依赖关系系统,解决了 更新依赖于其他项的项(例如自定义项 使用其他自定义类型的类型/函数),并且需要删除 所有依赖树以前与进一步的娱乐。
它不能做什么
- 在makemigrations或 migrate因为是数据库不可知的。出于同样的原因 建立适当的依赖关系是用户的责任。
- 不为支持此功能的项创建ALTER查询 postgre sql中的示例ALTER TYPE,因为它与数据库无关。 如果您的工具允许通过^{tt6}滚动所有更改$ 查询时,可以考虑不使用此应用或重新构造 创建后通过嵌套生成的操作手动迁移 进入`state_operations的 RunSQL<;https://docs.djangoproject.com/en/1.8/ref/migration-operations/#runsql>;。`_ 这是ALTER。
- 在migrate期间,(todo)不会还原 因此,分析不会通知模式的现有更改。 不迁移或不识别循环依赖项 在迁移执行期间。
安装
从pypi安装:
$ pip install django-migrate-sql-deux
将migrate_sql添加到INSTALLED_APPS:
INSTALLED_APPS=[# ...'migrate_sql',]
应用程序定义了一个自定义的makemigrations命令,该命令继承自 django的核心之一,所以为了migrate_sqlapp的投入 在任何其他重新定义makemigrations命令的应用之后。
用法
- 创建sql_config.py模块到要创建的目标应用的根目录 管理的自定义SQL。
- 在其中定义sql项(sql_items),例如:
# PostgreSQL example.# Let's define a simple function and let `migrate_sql` manage it's changes.frommigrate_sql.configimportSQLItemsql_items=[SQLItem('make_sum',# name of the item'create or replace function make_sum(a int, b int) returns int as $$ ''begin return a + b; end; ''$$ language plpgsql;',# forward sqlreverse_sql='drop function make_sum(int, int);',# sql for removal),]
创建迁移./manage.py makemigrations:
Migrations for 'app_name': 0002_auto_xxxx.py: - Create SQL "make_sum"
您可以查看此生成的内容:
# -*- coding: utf-8 -*-from__future__importunicode_literalsfromdjango.dbimportmigrations,modelsimportmigrate_sql.operationsclassMigration(migrations.Migration):dependencies=[('app_name','0001_initial'),]operations=[migrate_sql.operations.CreateSQL(name='make_sum',sql='create or replace function make_sum(a int, b int) returns int as $$ begin return a + b; end; $$ language plpgsql;',reverse_sql='drop function make_sum(int, int);',),]
执行迁移./manage.py migrate:
Operations to perform: Apply all migrations: app_name Running migrations: Rendering model states... DONE Applying app_name.0002_xxxx... OK
在./manage.py dbshell:
中检查结果db_name=# select make_sum(12, 15); make_sum ---------- 27 (1 row)
现在,比方说,您想要更改函数实现,以便 接受自定义类型作为参数:
- 编辑您的sql_config.py:
# PostgreSQL example #2.# Function and custom type.frommigrate_sql.configimportSQLItemsql_items=[SQLItem('make_sum',# name of the item'create or replace function make_sum(a mynum, b mynum) returns mynum as $$ ''begin return (a.num + b.num, 'result')::mynum; end; ''$$ language plpgsql;',# forward sqlreverse_sql='drop function make_sum(mynum, mynum);',# sql for removal# depends on `mynum` since takes it as argument. we won't be able to drop function# without dropping `mynum` first.dependencies=[('app_name','mynum')],),SQLItem('mynum'# name of the item'create type mynum as (num int, name varchar(20));',# forward sqlreverse_sql='drop type mynum;',# sql for removal),]
- 生成迁移./manage.py makemigrations:
Migrations for 'app_name': 0003_xxxx: - Reverse alter SQL "make_sum" - Create SQL "mynum" - Alter SQL "make_sum" - Alter SQL state "make_sum"
您可以查看此生成的内容:
# -*- coding: utf-8 -*-from__future__importunicode_literalsfromdjango.dbimportmigrations,modelsimportmigrate_sql.operationsclassMigration(migrations.Migration):dependencies=[('app_name','0002_xxxx'),]operations=[migrate_sql.operations.ReverseAlterSQL(name='make_sum',sql='drop function make_sum(int, int);',reverse_sql='create or replace function make_sum(a int, b int) returns int as $$ begin return a + b; end; $$ language plpgsql;',),migrate_sql.operations.CreateSQL(name='mynum',sql='create type mynum as (num int, name varchar(20));',reverse_sql='drop type mynum;',),migrate_sql.operations.AlterSQL(name='make_sum',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;',reverse_sql='drop function make_sum(mynum, mynum);',),migrate_sql.operations.AlterSQLState(name='make_sum',add_dependencies=(('app_name','mynum'),),),]
*注意:前一个函数在创建前已完全删除 因为它的定义改变了。CREATE OR REPLACE将创建 它的另一个版本,因此DROP使它干净。*
*如果将“replace=true”设为kwarg,则
不会删除+创建它,只需重新运行forward sql,即
``在本例中创建或替换``
检查结果: 有关更多示例,请参见tests。 请随意open new
issues。Operations to perform:
Apply all migrations: app_name
Running migrations:
Rendering model states... DONE
Applying brands.0003_xxxx... OK
db_name=# select make_sum((5, 'a')::mynum, (3, 'b')::mynum);
make_sum
------------
(8,result)
(1 row)
db_name=# select make_sum(12, 15);
ERROR: function make_sum(integer, integer) does not exist
LINE 1: select make_sum(12, 15);
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
注意
这个包是最初发布的django-migrate-sql包的叉 作者Bogdan Klichuk。这个包似乎没有维护,所以我们决定启动一个fork 因为我们依赖它。大部分密码都是他写的。
django迁移支持原始sql。
关于
此工具实现了管理对自定义sql的更改的机制 使用内置迁移的实体(函数、类型、索引、触发器) 机制。技术上在 RunSQLdjango操作。
它的功能
- 使维护SQL函数、自定义组合类型、索引 更容易触发。
- 将sql构造成sql项的配置 通过名字来识别并在应用程序之间划分,就像模型一样。
- 自动将自定义sql的更改收集并持久化到 使用makemigrations进行迁移。
- 正确地向后/向前执行,保持数据库的完整性。
- 创建->;删除->;对未更改的项重新创建方法 支持修改,需要删除和重新创建。
- SQL项的依赖关系系统,解决了 更新依赖于其他项的项(例如自定义项 使用其他自定义类型的类型/函数),并且需要删除 所有依赖树以前与进一步的娱乐。
它不能做什么
- 在makemigrations或 migrate因为是数据库不可知的。出于同样的原因 建立适当的依赖关系是用户的责任。
- 不为支持此功能的项创建ALTER查询 postgre sql中的示例ALTER TYPE,因为它与数据库无关。 如果您的工具允许通过^{tt6}滚动所有更改$ 查询时,可以考虑不使用此应用或重新构造 创建后通过嵌套生成的操作手动迁移 进入`state_operations的 RunSQL<;https://docs.djangoproject.com/en/1.8/ref/migration-operations/#runsql>;。`_ 这是ALTER。
- 在migrate期间,(todo)不会还原 因此,分析不会通知模式的现有更改。 不迁移或不识别循环依赖项 在迁移执行期间。
安装
从pypi安装:
$ pip install django-migrate-sql-deux
将migrate_sql添加到INSTALLED_APPS:
INSTALLED_APPS=[# ...'migrate_sql',]
应用程序定义了一个自定义的makemigrations命令,该命令继承自 django的核心之一,所以为了migrate_sqlapp的投入 在任何其他重新定义makemigrations命令的应用之后。
用法
- 创建sql_config.py模块到要创建的目标应用的根目录 管理的自定义SQL。
- 在其中定义sql项(sql_items),例如:
# PostgreSQL example.# Let's define a simple function and let `migrate_sql` manage it's changes.frommigrate_sql.configimportSQLItemsql_items=[SQLItem('make_sum',# name of the item'create or replace function make_sum(a int, b int) returns int as $$ ''begin return a + b; end; ''$$ language plpgsql;',# forward sqlreverse_sql='drop function make_sum(int, int);',# sql for removal),]
创建迁移./manage.py makemigrations:
Migrations for 'app_name': 0002_auto_xxxx.py: - Create SQL "make_sum"
您可以查看此生成的内容:
# -*- coding: utf-8 -*-from__future__importunicode_literalsfromdjango.dbimportmigrations,modelsimportmigrate_sql.operationsclassMigration(migrations.Migration):dependencies=[('app_name','0001_initial'),]operations=[migrate_sql.operations.CreateSQL(name='make_sum',sql='create or replace function make_sum(a int, b int) returns int as $$ begin return a + b; end; $$ language plpgsql;',reverse_sql='drop function make_sum(int, int);',),]
执行迁移./manage.py migrate:
Operations to perform: Apply all migrations: app_name Running migrations: Rendering model states... DONE Applying app_name.0002_xxxx... OK
在./manage.py dbshell:
中检查结果db_name=# select make_sum(12, 15); make_sum ---------- 27 (1 row)
现在,比方说,您想要更改函数实现,以便 接受自定义类型作为参数:
- 编辑您的sql_config.py:
# PostgreSQL example #2.# Function and custom type.frommigrate_sql.configimportSQLItemsql_items=[SQLItem('make_sum',# name of the item'create or replace function make_sum(a mynum, b mynum) returns mynum as $$ ''begin return (a.num + b.num, 'result')::mynum; end; ''$$ language plpgsql;',# forward sqlreverse_sql='drop function make_sum(mynum, mynum);',# sql for removal# depends on `mynum` since takes it as argument. we won't be able to drop function# without dropping `mynum` first.dependencies=[('app_name','mynum')],),SQLItem('mynum'# name of the item'create type mynum as (num int, name varchar(20));',# forward sqlreverse_sql='drop type mynum;',# sql for removal),]
- 生成迁移./manage.py makemigrations:
Migrations for 'app_name': 0003_xxxx: - Reverse alter SQL "make_sum" - Create SQL "mynum" - Alter SQL "make_sum" - Alter SQL state "make_sum"
您可以查看此生成的内容:
# -*- coding: utf-8 -*-from__future__importunicode_literalsfromdjango.dbimportmigrations,modelsimportmigrate_sql.operationsclassMigration(migrations.Migration):dependencies=[('app_name','0002_xxxx'),]operations=[migrate_sql.operations.ReverseAlterSQL(name='make_sum',sql='drop function make_sum(int, int);',reverse_sql='create or replace function make_sum(a int, b int) returns int as $$ begin return a + b; end; $$ language plpgsql;',),migrate_sql.operations.CreateSQL(name='mynum',sql='create type mynum as (num int, name varchar(20));',reverse_sql='drop type mynum;',),migrate_sql.operations.AlterSQL(name='make_sum',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;',reverse_sql='drop function make_sum(mynum, mynum);',),migrate_sql.operations.AlterSQLState(name='make_sum',add_dependencies=(('app_name','mynum'),),),]
*注意:前一个函数在创建前已完全删除 因为它的定义改变了。CREATE OR REPLACE将创建 它的另一个版本,因此DROP使它干净。*
*如果将“replace=true”设为kwarg,则
不会删除+创建它,只需重新运行forward sql,即
``在本例中创建或替换``
检查结果: 有关更多示例,请参见tests。 请随意open new
issues。Operations to perform:
Apply all migrations: app_name
Running migrations:
Rendering model states... DONE
Applying brands.0003_xxxx... OK
db_name=# select make_sum((5, 'a')::mynum, (3, 'b')::mynum);
make_sum
------------
(8,result)
(1 row)
db_name=# select make_sum(12, 15);
ERROR: function make_sum(integer, integer) does not exist
LINE 1: select make_sum(12, 15);
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.