本文整理汇总了Python中django.db.connection.schema_editor方法的典型用法代码示例。如果您正苦于以下问题:Python connection.schema_editor方法的具体用法?Python connection.schema_editor怎么用?Python connection.schema_editor使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.db.connection
的用法示例。
在下文中一共展示了connection.schema_editor方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_fake_model
# 需要导入模块: from django.db import connection [as 别名]
# 或者: from django.db.connection import schema_editor [as 别名]
def get_fake_model(fields=None, model_base=LocalizedModel, meta_options={}):
"""Creates a fake model to use during unit tests."""
model = define_fake_model(fields, model_base, meta_options)
class TestProject:
def clone(self, *_args, **_kwargs):
return self
@property
def apps(self):
return self
class TestMigration(migrations.Migration):
operations = [HStoreExtension()]
with connection.schema_editor() as schema_editor:
migration_executor = MigrationExecutor(schema_editor.connection)
migration_executor.apply_migration(
TestProject(), TestMigration("eh", "postgres_extra")
)
schema_editor.create_model(model)
return model
示例2: setUp
# 需要导入模块: from django.db import connection [as 别名]
# 或者: from django.db.connection import schema_editor [as 别名]
def setUp(self):
# Create a dummy model which extends the mixin. A RuntimeWarning will
# occur if the model is registered twice
if not hasattr(self, 'model'):
self.model = ModelBase(
'__TestModel__' +
self.mixin.__name__, (self.mixin,),
{'__module__': self.mixin.__module__}
)
# Create the schema for our test model. If the table already exists,
# will pass
try:
with connection.schema_editor() as schema_editor:
schema_editor.create_model(self.model)
super(AbstractModelMixinTestCase, self).setUpClass()
except ProgrammingError:
pass
示例3: test_install_plugin
# 需要导入模块: from django.db import connection [as 别名]
# 或者: from django.db.connection import schema_editor [as 别名]
def test_install_plugin(self):
"""
Test we can load the example plugin that every version of MySQL ships
with.
"""
assert not plugin_exists("metadata_lock_info")
state = ProjectState()
operation = InstallPlugin("metadata_lock_info", "metadata_lock_info.so")
assert (
operation.describe()
== "Installs plugin metadata_lock_info from metadata_lock_info.so"
)
new_state = state.clone()
with connection.schema_editor() as editor:
operation.database_forwards("testapp", editor, state, new_state)
assert plugin_exists("metadata_lock_info")
new_state = state.clone()
with connection.schema_editor() as editor:
operation.database_backwards("testapp", editor, new_state, state)
assert not plugin_exists("metadata_lock_info")
示例4: test_install_soname
# 需要导入模块: from django.db import connection [as 别名]
# 或者: from django.db.connection import schema_editor [as 别名]
def test_install_soname(self):
"""
Test we can load the 'metadata_lock_info' library.
"""
assert not plugin_exists("metadata_lock_info")
state = ProjectState()
operation = InstallSOName("metadata_lock_info.so")
assert operation.describe() == "Installs library metadata_lock_info.so"
new_state = state.clone()
with connection.schema_editor() as editor:
operation.database_forwards("testapp", editor, state, new_state)
assert plugin_exists("metadata_lock_info")
new_state = state.clone()
with connection.schema_editor() as editor:
operation.database_backwards("testapp", editor, new_state, state)
assert not plugin_exists("metadata_lock_info")
示例5: test_running_with_changes
# 需要导入模块: from django.db import connection [as 别名]
# 或者: from django.db.connection import schema_editor [as 别名]
def test_running_with_changes(self):
project_state = self.set_up_test_model("test_arstd")
operation = AlterStorageEngine("Pony", from_engine="MyISAM", to_engine="InnoDB")
assert table_storage_engine("test_arstd_pony") == "MyISAM"
# Forwards
new_state = project_state.clone()
operation.state_forwards("test_arstd", new_state)
with connection.schema_editor() as editor:
operation.database_forwards("test_arstd", editor, project_state, new_state)
assert table_storage_engine("test_arstd_pony") == "InnoDB"
# Backwards
with connection.schema_editor() as editor:
operation.database_backwards("test_arstd", editor, new_state, project_state)
assert table_storage_engine("test_arstd_pony") == "MyISAM"
示例6: test_running_without_changes
# 需要导入模块: from django.db import connection [as 别名]
# 或者: from django.db.connection import schema_editor [as 别名]
def test_running_without_changes(self):
project_state = self.set_up_test_model("test_arstd")
operation = AlterStorageEngine("Pony", from_engine="MyISAM", to_engine="InnoDB")
assert table_storage_engine("test_arstd_pony") == "InnoDB"
# Forwards - shouldn't actually do an ALTER since it is already InnoDB
new_state = project_state.clone()
operation.state_forwards("test_arstd", new_state)
capturer = CaptureQueriesContext(connection)
with capturer, connection.schema_editor() as editor:
operation.database_forwards("test_arstd", editor, project_state, new_state)
queries = [q["sql"] for q in capturer.captured_queries]
assert not any(q.startswith("ALTER TABLE ") for q in queries)
assert table_storage_engine("test_arstd_pony") == "InnoDB"
# Backwards - will actually ALTER since it is going 'back' to MyISAM
with connection.schema_editor() as editor:
operation.database_backwards("test_arstd", editor, new_state, project_state)
assert table_storage_engine("test_arstd_pony") == "MyISAM"
# Copied from django core migration tests
示例7: test_index_name
# 需要导入模块: from django.db import connection [as 别名]
# 或者: from django.db.connection import schema_editor [as 别名]
def test_index_name(self):
"""
Index names on the built-in database backends::
* Are truncated as needed.
* Include all the column names.
* Include a deterministic hash.
"""
long_name = 'l%sng' % ('o' * 100)
with connection.schema_editor() as editor:
index_name = editor._create_index_name(
model=Article,
column_names=('c1', 'c2', long_name),
suffix='ix',
)
expected = {
'mysql': 'indexes_article_c1_c2_looooooooooooooooooo_255179b2ix',
'oracle': 'indexes_a_c1_c2_loo_255179b2ix',
'postgresql': 'indexes_article_c1_c2_loooooooooooooooooo_255179b2ix',
'sqlite': 'indexes_article_c1_c2_l%sng_255179b2ix' % ('o' * 100),
}
if connection.vendor not in expected:
self.skipTest('This test is only supported on the built-in database backends.')
self.assertEqual(index_name, expected[connection.vendor])
示例8: _test_create_model
# 需要导入模块: from django.db import connection [as 别名]
# 或者: from django.db.connection import schema_editor [as 别名]
def _test_create_model(self, app_label, should_run):
"""
CreateModel honors multi-db settings.
"""
operation = migrations.CreateModel(
"Pony",
[("id", models.AutoField(primary_key=True))],
)
# Test the state alteration
project_state = ProjectState()
new_state = project_state.clone()
operation.state_forwards(app_label, new_state)
# Test the database alteration
self.assertTableNotExists("%s_pony" % app_label)
with connection.schema_editor() as editor:
operation.database_forwards(app_label, editor, project_state, new_state)
if should_run:
self.assertTableExists("%s_pony" % app_label)
else:
self.assertTableNotExists("%s_pony" % app_label)
# And test reversal
with connection.schema_editor() as editor:
operation.database_backwards(app_label, editor, new_state, project_state)
self.assertTableNotExists("%s_pony" % app_label)
示例9: _test_run_sql
# 需要导入模块: from django.db import connection [as 别名]
# 或者: from django.db.connection import schema_editor [as 别名]
def _test_run_sql(self, app_label, should_run, hints=None):
with override_settings(DATABASE_ROUTERS=[MigrateEverythingRouter()]):
project_state = self.set_up_test_model(app_label)
sql = """
INSERT INTO {0}_pony (pink, weight) VALUES (1, 3.55);
INSERT INTO {0}_pony (pink, weight) VALUES (3, 5.0);
""".format(app_label)
operation = migrations.RunSQL(sql, hints=hints or {})
# Test the state alteration does nothing
new_state = project_state.clone()
operation.state_forwards(app_label, new_state)
self.assertEqual(new_state, project_state)
# Test the database alteration
self.assertEqual(project_state.apps.get_model(app_label, "Pony").objects.count(), 0)
with connection.schema_editor() as editor:
operation.database_forwards(app_label, editor, project_state, new_state)
Pony = project_state.apps.get_model(app_label, "Pony")
if should_run:
self.assertEqual(Pony.objects.count(), 2)
else:
self.assertEqual(Pony.objects.count(), 0)
示例10: _test_run_python
# 需要导入模块: from django.db import connection [as 别名]
# 或者: from django.db.connection import schema_editor [as 别名]
def _test_run_python(self, app_label, should_run, hints=None):
with override_settings(DATABASE_ROUTERS=[MigrateEverythingRouter()]):
project_state = self.set_up_test_model(app_label)
# Create the operation
def inner_method(models, schema_editor):
Pony = models.get_model(app_label, "Pony")
Pony.objects.create(pink=1, weight=3.55)
Pony.objects.create(weight=5)
operation = migrations.RunPython(inner_method, hints=hints or {})
# Test the state alteration does nothing
new_state = project_state.clone()
operation.state_forwards(app_label, new_state)
self.assertEqual(new_state, project_state)
# Test the database alteration
self.assertEqual(project_state.apps.get_model(app_label, "Pony").objects.count(), 0)
with connection.schema_editor() as editor:
operation.database_forwards(app_label, editor, project_state, new_state)
Pony = project_state.apps.get_model(app_label, "Pony")
if should_run:
self.assertEqual(Pony.objects.count(), 2)
else:
self.assertEqual(Pony.objects.count(), 0)
示例11: test_delete_model
# 需要导入模块: from django.db import connection [as 别名]
# 或者: from django.db.connection import schema_editor [as 别名]
def test_delete_model(self):
"""
Tests the DeleteModel operation.
"""
project_state = self.set_up_test_model("test_dlmo")
# Test the state alteration
operation = migrations.DeleteModel("Pony")
self.assertEqual(operation.describe(), "Delete model Pony")
new_state = project_state.clone()
operation.state_forwards("test_dlmo", new_state)
self.assertNotIn(("test_dlmo", "pony"), new_state.models)
# Test the database alteration
self.assertTableExists("test_dlmo_pony")
with connection.schema_editor() as editor:
operation.database_forwards("test_dlmo", editor, project_state, new_state)
self.assertTableNotExists("test_dlmo_pony")
# And test reversal
with connection.schema_editor() as editor:
operation.database_backwards("test_dlmo", editor, new_state, project_state)
self.assertTableExists("test_dlmo_pony")
# And deconstruction
definition = operation.deconstruct()
self.assertEqual(definition[0], "DeleteModel")
self.assertEqual(definition[1], [])
self.assertEqual(list(definition[2]), ["name"])
示例12: test_delete_proxy_model
# 需要导入模块: from django.db import connection [as 别名]
# 或者: from django.db.connection import schema_editor [as 别名]
def test_delete_proxy_model(self):
"""
Tests the DeleteModel operation ignores proxy models.
"""
project_state = self.set_up_test_model("test_dlprmo", proxy_model=True)
# Test the state alteration
operation = migrations.DeleteModel("ProxyPony")
new_state = project_state.clone()
operation.state_forwards("test_dlprmo", new_state)
self.assertIn(("test_dlprmo", "proxypony"), project_state.models)
self.assertNotIn(("test_dlprmo", "proxypony"), new_state.models)
# Test the database alteration
self.assertTableExists("test_dlprmo_pony")
self.assertTableNotExists("test_dlprmo_proxypony")
with connection.schema_editor() as editor:
operation.database_forwards("test_dlprmo", editor, project_state, new_state)
self.assertTableExists("test_dlprmo_pony")
self.assertTableNotExists("test_dlprmo_proxypony")
# And test reversal
with connection.schema_editor() as editor:
operation.database_backwards("test_dlprmo", editor, new_state, project_state)
self.assertTableExists("test_dlprmo_pony")
self.assertTableNotExists("test_dlprmo_proxypony")
示例13: test_remove_field
# 需要导入模块: from django.db import connection [as 别名]
# 或者: from django.db.connection import schema_editor [as 别名]
def test_remove_field(self):
"""
Tests the RemoveField operation.
"""
project_state = self.set_up_test_model("test_rmfl")
# Test the state alteration
operation = migrations.RemoveField("Pony", "pink")
self.assertEqual(operation.describe(), "Remove field pink from Pony")
new_state = project_state.clone()
operation.state_forwards("test_rmfl", new_state)
self.assertEqual(len(new_state.models["test_rmfl", "pony"].fields), 2)
# Test the database alteration
self.assertColumnExists("test_rmfl_pony", "pink")
with connection.schema_editor() as editor:
operation.database_forwards("test_rmfl", editor, project_state, new_state)
self.assertColumnNotExists("test_rmfl_pony", "pink")
# And test reversal
with connection.schema_editor() as editor:
operation.database_backwards("test_rmfl", editor, new_state, project_state)
self.assertColumnExists("test_rmfl_pony", "pink")
# And deconstruction
definition = operation.deconstruct()
self.assertEqual(definition[0], "RemoveField")
self.assertEqual(definition[1], [])
self.assertEqual(definition[2], {'model_name': "Pony", 'name': 'pink'})
示例14: test_remove_fk
# 需要导入模块: from django.db import connection [as 别名]
# 或者: from django.db.connection import schema_editor [as 别名]
def test_remove_fk(self):
"""
Tests the RemoveField operation on a foreign key.
"""
project_state = self.set_up_test_model("test_rfk", related_model=True)
self.assertColumnExists("test_rfk_rider", "pony_id")
operation = migrations.RemoveField("Rider", "pony")
new_state = project_state.clone()
operation.state_forwards("test_rfk", new_state)
with connection.schema_editor() as editor:
operation.database_forwards("test_rfk", editor, project_state, new_state)
self.assertColumnNotExists("test_rfk_rider", "pony_id")
with connection.schema_editor() as editor:
operation.database_backwards("test_rfk", editor, new_state, project_state)
self.assertColumnExists("test_rfk_rider", "pony_id")
示例15: test_alter_field_pk
# 需要导入模块: from django.db import connection [as 别名]
# 或者: from django.db.connection import schema_editor [as 别名]
def test_alter_field_pk(self):
"""
Tests the AlterField operation on primary keys (for things like PostgreSQL's SERIAL weirdness)
"""
project_state = self.set_up_test_model("test_alflpk")
# Test the state alteration
operation = migrations.AlterField("Pony", "id", models.IntegerField(primary_key=True))
new_state = project_state.clone()
operation.state_forwards("test_alflpk", new_state)
self.assertIsInstance(project_state.models["test_alflpk", "pony"].get_field_by_name("id"), models.AutoField)
self.assertIsInstance(new_state.models["test_alflpk", "pony"].get_field_by_name("id"), models.IntegerField)
# Test the database alteration
with connection.schema_editor() as editor:
operation.database_forwards("test_alflpk", editor, project_state, new_state)
# And test reversal
with connection.schema_editor() as editor:
operation.database_backwards("test_alflpk", editor, new_state, project_state)