Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
D
django
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
django
Commits
fe9f342d
Kaydet (Commit)
fe9f342d
authored
Eyl 25, 2013
tarafından
Andrew Godwin
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Allow callables as the argument to RunPython
üst
8a3e543f
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
11 deletions
+29
-11
special.py
django/db/migrations/operations/special.py
+20
-11
test_operations.py
tests/migrations/test_operations.py
+9
-0
No files found.
django/db/migrations/operations/special.py
Dosyayı görüntüle @
fe9f342d
import
re
import
re
import
textwrap
import
textwrap
from
.base
import
Operation
from
.base
import
Operation
from
django.utils
import
six
class
SeparateDatabaseAndState
(
Operation
):
class
SeparateDatabaseAndState
(
Operation
):
...
@@ -107,12 +108,17 @@ class RunPython(Operation):
...
@@ -107,12 +108,17 @@ class RunPython(Operation):
reversible
=
False
reversible
=
False
def
__init__
(
self
,
code
):
def
__init__
(
self
,
code
):
# Trim any leading whitespace that is at the start of all code lines
if
isinstance
(
code
,
six
.
string_types
):
# so users can nicely indent code in migration files
# Trim any leading whitespace that is at the start of all code lines
code
=
textwrap
.
dedent
(
code
)
# so users can nicely indent code in migration files
# Run the code through a parser first to make sure it's at least
code
=
textwrap
.
dedent
(
code
)
# syntactically correct
# Run the code through a parser first to make sure it's at least
self
.
code
=
compile
(
code
,
"<string>"
,
"exec"
)
# syntactically correct
self
.
code
=
compile
(
code
,
"<string>"
,
"exec"
)
self
.
is_callable
=
False
else
:
self
.
code
=
code
self
.
is_callable
=
True
def
state_forwards
(
self
,
app_label
,
state
):
def
state_forwards
(
self
,
app_label
,
state
):
# RunPython objects have no state effect. To add some, combine this
# RunPython objects have no state effect. To add some, combine this
...
@@ -124,11 +130,14 @@ class RunPython(Operation):
...
@@ -124,11 +130,14 @@ class RunPython(Operation):
# object, representing the versioned models as an AppCache.
# object, representing the versioned models as an AppCache.
# We could try to override the global cache, but then people will still
# We could try to override the global cache, but then people will still
# use direct imports, so we go with a documentation approach instead.
# use direct imports, so we go with a documentation approach instead.
context
=
{
if
self
.
is_callable
:
"models"
:
from_state
.
render
(),
self
.
code
(
models
=
from_state
.
render
(),
schema_editor
=
schema_editor
)
"schema_editor"
:
schema_editor
,
else
:
}
context
=
{
eval
(
self
.
code
,
context
)
"models"
:
from_state
.
render
(),
"schema_editor"
:
schema_editor
,
}
eval
(
self
.
code
,
context
)
def
database_backwards
(
self
,
app_label
,
schema_editor
,
from_state
,
to_state
):
def
database_backwards
(
self
,
app_label
,
schema_editor
,
from_state
,
to_state
):
raise
NotImplementedError
(
"You cannot reverse this operation"
)
raise
NotImplementedError
(
"You cannot reverse this operation"
)
...
...
tests/migrations/test_operations.py
Dosyayı görüntüle @
fe9f342d
...
@@ -332,6 +332,15 @@ class OperationTests(MigrationTestBase):
...
@@ -332,6 +332,15 @@ class OperationTests(MigrationTestBase):
# And test reversal fails
# And test reversal fails
with
self
.
assertRaises
(
NotImplementedError
):
with
self
.
assertRaises
(
NotImplementedError
):
operation
.
database_backwards
(
"test_runpython"
,
None
,
new_state
,
project_state
)
operation
.
database_backwards
(
"test_runpython"
,
None
,
new_state
,
project_state
)
# Now test we can do it with a callable
def
inner_method
(
models
,
schema_editor
):
Pony
=
models
.
get_model
(
"test_runpython"
,
"Pony"
)
Pony
.
objects
.
create
(
pink
=
1
,
weight
=
3.55
)
Pony
.
objects
.
create
(
weight
=
5
)
operation
=
migrations
.
RunPython
(
inner_method
)
with
connection
.
schema_editor
()
as
editor
:
operation
.
database_forwards
(
"test_runpython"
,
editor
,
project_state
,
new_state
)
self
.
assertEqual
(
project_state
.
render
()
.
get_model
(
"test_runpython"
,
"Pony"
)
.
objects
.
count
(),
4
)
class
MigrateNothingRouter
(
object
):
class
MigrateNothingRouter
(
object
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment