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
5ab93bbe
Kaydet (Commit)
5ab93bbe
authored
May 02, 2014
tarafından
Chris Beaven
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
[1.7.x] Fix migration autodector to work correctly with custom deconstructed values
üst
d5a9d74e
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
44 additions
and
5 deletions
+44
-5
autodetector.py
django/db/migrations/autodetector.py
+22
-5
test_autodetector.py
tests/migrations/test_autodetector.py
+22
-0
No files found.
django/db/migrations/autodetector.py
Dosyayı görüntüle @
5ab93bbe
...
...
@@ -64,6 +64,23 @@ class MigrationAutodetector(object):
if
not
model
.
_meta
.
proxy
and
model
.
_meta
.
managed
and
al
not
in
self
.
to_state
.
real_apps
:
new_model_keys
.
append
((
al
,
mn
))
def
_deep_deconstruct
(
obj
,
field
=
True
):
"""
Recursive deconstruction for a field and its arguments.
"""
if
not
hasattr
(
obj
,
'deconstruct'
):
return
obj
deconstructed
=
obj
.
deconstruct
()
if
field
:
deconstructed
=
deconstructed
[
1
:]
name
,
args
,
kwargs
=
deconstructed
return
(
name
,
[
_deep_deconstruct
(
value
,
field
=
False
)
for
value
in
args
],
dict
([(
key
,
_deep_deconstruct
(
value
,
field
=
False
))
for
key
,
value
in
kwargs
.
items
()])
)
def
_rel_agnostic_fields_def
(
fields
):
"""
Return a definition of the fields that ignores field names and
...
...
@@ -71,7 +88,7 @@ class MigrationAutodetector(object):
"""
fields_def
=
[]
for
name
,
field
in
fields
:
deconstruction
=
field
.
deconstruct
()[
1
:]
deconstruction
=
_deep_deconstruct
(
field
)
if
field
.
rel
and
field
.
rel
.
to
:
del
deconstruction
[
2
][
'to'
]
fields_def
.
append
(
deconstruction
)
...
...
@@ -255,11 +272,11 @@ class MigrationAutodetector(object):
new_model_state
=
self
.
to_state
.
models
[
app_label
,
model_name
]
field
=
new_model_state
.
get_field_by_name
(
field_name
)
# Scan to see if this is actually a rename!
field_dec
=
field
.
deconstruct
()[
1
:]
field_dec
=
_deep_deconstruct
(
field
)
found_rename
=
False
for
rem_app_label
,
rem_model_name
,
rem_field_name
in
(
old_fields
-
new_fields
):
if
rem_app_label
==
app_label
and
rem_model_name
==
model_name
:
old_field_dec
=
old_model_state
.
get_field_by_name
(
rem_field_name
)
.
deconstruct
()[
1
:]
old_field_dec
=
_deep_deconstruct
(
old_model_state
.
get_field_by_name
(
rem_field_name
))
if
field
.
rel
and
field
.
rel
.
to
and
'to'
in
old_field_dec
[
2
]:
old_rel_to
=
old_field_dec
[
2
][
'to'
]
if
old_rel_to
in
renamed_models_rel
:
...
...
@@ -326,8 +343,8 @@ class MigrationAutodetector(object):
old_model_state
=
self
.
from_state
.
models
[
app_label
,
old_model_name
]
new_model_state
=
self
.
to_state
.
models
[
app_label
,
model_name
]
old_field_name
=
renamed_fields
.
get
((
app_label
,
model_name
,
field_name
),
field_name
)
old_field_dec
=
old_model_state
.
get_field_by_name
(
old_field_name
)
.
deconstruct
()[
1
:]
new_field_dec
=
new_model_state
.
get_field_by_name
(
field_name
)
.
deconstruct
()[
1
:]
old_field_dec
=
_deep_deconstruct
(
old_model_state
.
get_field_by_name
(
old_field_name
))
new_field_dec
=
_deep_deconstruct
(
new_model_state
.
get_field_by_name
(
field_name
))
if
old_field_dec
!=
new_field_dec
:
self
.
add_to_migration
(
app_label
,
...
...
tests/migrations/test_autodetector.py
Dosyayı görüntüle @
5ab93bbe
...
...
@@ -7,6 +7,15 @@ from django.db.migrations.graph import MigrationGraph
from
django.db
import
models
class
DeconstructableObject
(
object
):
"""
A custom deconstructable object.
"""
def
deconstruct
(
self
):
return
self
.
__module__
+
'.'
+
self
.
__class__
.
__name__
,
[],
{}
class
AutodetectorTests
(
TestCase
):
"""
Tests the migration autodetector.
...
...
@@ -17,6 +26,8 @@ class AutodetectorTests(TestCase):
author_name_longer
=
ModelState
(
"testapp"
,
"Author"
,
[(
"id"
,
models
.
AutoField
(
primary_key
=
True
)),
(
"name"
,
models
.
CharField
(
max_length
=
400
))])
author_name_renamed
=
ModelState
(
"testapp"
,
"Author"
,
[(
"id"
,
models
.
AutoField
(
primary_key
=
True
)),
(
"names"
,
models
.
CharField
(
max_length
=
200
))])
author_name_default
=
ModelState
(
"testapp"
,
"Author"
,
[(
"id"
,
models
.
AutoField
(
primary_key
=
True
)),
(
"name"
,
models
.
CharField
(
max_length
=
200
,
default
=
'Ada Lovelace'
))])
author_name_deconstructable_1
=
ModelState
(
"testapp"
,
"Author"
,
[(
"id"
,
models
.
AutoField
(
primary_key
=
True
)),
(
"name"
,
models
.
CharField
(
max_length
=
200
,
default
=
DeconstructableObject
()))])
author_name_deconstructable_2
=
ModelState
(
"testapp"
,
"Author"
,
[(
"id"
,
models
.
AutoField
(
primary_key
=
True
)),
(
"name"
,
models
.
CharField
(
max_length
=
200
,
default
=
DeconstructableObject
()))])
author_with_book
=
ModelState
(
"testapp"
,
"Author"
,
[(
"id"
,
models
.
AutoField
(
primary_key
=
True
)),
(
"name"
,
models
.
CharField
(
max_length
=
200
)),
(
"book"
,
models
.
ForeignKey
(
"otherapp.Book"
))])
author_renamed_with_book
=
ModelState
(
"testapp"
,
"Writer"
,
[(
"id"
,
models
.
AutoField
(
primary_key
=
True
)),
(
"name"
,
models
.
CharField
(
max_length
=
200
)),
(
"book"
,
models
.
ForeignKey
(
"otherapp.Book"
))])
author_with_publisher_string
=
ModelState
(
"testapp"
,
"Author"
,
[(
"id"
,
models
.
AutoField
(
primary_key
=
True
)),
(
"name"
,
models
.
CharField
(
max_length
=
200
)),
(
"publisher_name"
,
models
.
CharField
(
max_length
=
200
))])
...
...
@@ -550,6 +561,17 @@ class AutodetectorTests(TestCase):
self
.
assertEqual
(
action
.
__class__
.
__name__
,
"AddField"
)
self
.
assertEqual
(
action
.
name
,
"name"
)
def
test_custom_deconstructable
(
self
):
"""
Two instances which deconstruct to the same value aren't considered a
change.
"""
before
=
self
.
make_project_state
([
self
.
author_name_deconstructable_1
])
after
=
self
.
make_project_state
([
self
.
author_name_deconstructable_2
])
autodetector
=
MigrationAutodetector
(
before
,
after
)
changes
=
autodetector
.
_detect_changes
()
self
.
assertEqual
(
changes
,
{})
def
test_replace_string_with_foreignkey
(
self
):
"""
Adding an FK in the same "spot" as a deleted CharField should work. (#22300).
...
...
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