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
1d3d01b4
Kaydet (Commit)
1d3d01b4
authored
May 18, 2014
tarafından
Loic Bistuer
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Fixed #22424 -- Fixed handling of default values for TextField/BinaryField on MySQL.
Thanks syphar for the review and suggestions.
üst
6aacb4c9
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
146 additions
and
2 deletions
+146
-2
schema.py
django/db/backends/mysql/schema.py
+26
-1
schema.py
django/db/backends/schema.py
+9
-1
test_operations.py
tests/migrations/test_operations.py
+111
-0
No files found.
django/db/backends/mysql/schema.py
Dosyayı görüntüle @
1d3d01b4
from
django.db.backends.schema
import
BaseDatabaseSchemaEditor
from
django.db.backends.schema
import
BaseDatabaseSchemaEditor
from
django.db.models
import
NOT_PROVIDED
from
django.utils
import
six
class
DatabaseSchemaEditor
(
BaseDatabaseSchemaEditor
):
class
DatabaseSchemaEditor
(
BaseDatabaseSchemaEditor
):
...
@@ -28,4 +30,27 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
...
@@ -28,4 +30,27 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
def
quote_value
(
self
,
value
):
def
quote_value
(
self
,
value
):
# Inner import to allow module to fail to load gracefully
# Inner import to allow module to fail to load gracefully
import
MySQLdb.converters
import
MySQLdb.converters
return
MySQLdb
.
escape
(
value
,
MySQLdb
.
converters
.
conversions
)
if
isinstance
(
value
,
six
.
string_types
):
return
'"
%
s"'
%
six
.
text_type
(
value
)
else
:
return
MySQLdb
.
escape
(
value
,
MySQLdb
.
converters
.
conversions
)
def
skip_default
(
self
,
field
):
"""
MySQL doesn't accept default values for longtext and longblob
and implicitly treats these columns as nullable.
"""
return
field
.
db_type
(
self
.
connection
)
in
{
'longtext'
,
'longblob'
}
def
add_field
(
self
,
model
,
field
):
super
(
DatabaseSchemaEditor
,
self
)
.
add_field
(
model
,
field
)
# Simulate the effect of a one-off default.
if
self
.
skip_default
(
field
)
and
field
.
default
not
in
{
None
,
NOT_PROVIDED
}:
effective_default
=
self
.
effective_default
(
field
)
self
.
execute
(
'UPDATE
%(table)
s SET
%(column)
s=
%(default)
s'
%
{
'table'
:
self
.
quote_name
(
model
.
_meta
.
db_table
),
'column'
:
self
.
quote_name
(
field
.
column
),
'default'
:
self
.
quote_value
(
effective_default
),
})
django/db/backends/schema.py
Dosyayı görüntüle @
1d3d01b4
...
@@ -118,6 +118,7 @@ class BaseDatabaseSchemaEditor(object):
...
@@ -118,6 +118,7 @@ class BaseDatabaseSchemaEditor(object):
null
=
field
.
null
null
=
field
.
null
# If we were told to include a default value, do so
# If we were told to include a default value, do so
default_value
=
self
.
effective_default
(
field
)
default_value
=
self
.
effective_default
(
field
)
include_default
=
include_default
and
not
self
.
skip_default
(
field
)
if
include_default
and
default_value
is
not
None
:
if
include_default
and
default_value
is
not
None
:
if
self
.
connection
.
features
.
requires_literal_defaults
:
if
self
.
connection
.
features
.
requires_literal_defaults
:
# Some databases can't take defaults as a parameter (oracle)
# Some databases can't take defaults as a parameter (oracle)
...
@@ -148,6 +149,13 @@ class BaseDatabaseSchemaEditor(object):
...
@@ -148,6 +149,13 @@ class BaseDatabaseSchemaEditor(object):
# Return the sql
# Return the sql
return
sql
,
params
return
sql
,
params
def
skip_default
(
self
,
field
):
"""
Some backends don't accept default values for certain columns types
(i.e. MySQL longtext and longblob).
"""
return
False
def
prepare_default
(
self
,
value
):
def
prepare_default
(
self
,
value
):
"""
"""
Only used for backends which have requires_literal_defaults feature
Only used for backends which have requires_literal_defaults feature
...
@@ -398,7 +406,7 @@ class BaseDatabaseSchemaEditor(object):
...
@@ -398,7 +406,7 @@ class BaseDatabaseSchemaEditor(object):
self
.
execute
(
sql
,
params
)
self
.
execute
(
sql
,
params
)
# Drop the default if we need to
# Drop the default if we need to
# (Django usually does not use in-database defaults)
# (Django usually does not use in-database defaults)
if
field
.
default
is
not
None
:
if
not
self
.
skip_default
(
field
)
and
field
.
default
is
not
None
:
sql
=
self
.
sql_alter_column
%
{
sql
=
self
.
sql_alter_column
%
{
"table"
:
self
.
quote_name
(
model
.
_meta
.
db_table
),
"table"
:
self
.
quote_name
(
model
.
_meta
.
db_table
),
"changes"
:
self
.
sql_alter_column_no_default
%
{
"changes"
:
self
.
sql_alter_column_no_default
%
{
...
...
tests/migrations/test_operations.py
Dosyayı görüntüle @
1d3d01b4
from
__future__
import
unicode_literals
import
unittest
import
unittest
try
:
try
:
...
@@ -312,6 +314,115 @@ class OperationTests(MigrationTestBase):
...
@@ -312,6 +314,115 @@ class OperationTests(MigrationTestBase):
operation
.
database_backwards
(
"test_adfl"
,
editor
,
new_state
,
project_state
)
operation
.
database_backwards
(
"test_adfl"
,
editor
,
new_state
,
project_state
)
self
.
assertColumnNotExists
(
"test_adfl_pony"
,
"height"
)
self
.
assertColumnNotExists
(
"test_adfl_pony"
,
"height"
)
def
test_add_charfield
(
self
):
"""
Tests the AddField operation on TextField.
"""
project_state
=
self
.
set_up_test_model
(
"test_adchfl"
)
new_apps
=
project_state
.
render
()
Pony
=
new_apps
.
get_model
(
"test_adchfl"
,
"Pony"
)
pony
=
Pony
.
objects
.
create
(
weight
=
42
)
new_state
=
self
.
apply_operations
(
"test_adchfl"
,
project_state
,
[
migrations
.
AddField
(
"Pony"
,
"text"
,
models
.
CharField
(
max_length
=
10
,
default
=
"some text"
),
),
migrations
.
AddField
(
"Pony"
,
"empty"
,
models
.
CharField
(
max_length
=
10
,
default
=
""
),
),
# If not properly quoted digits would be interpreted as an int.
migrations
.
AddField
(
"Pony"
,
"digits"
,
models
.
CharField
(
max_length
=
10
,
default
=
"42"
),
),
])
new_apps
=
new_state
.
render
()
Pony
=
new_apps
.
get_model
(
"test_adchfl"
,
"Pony"
)
pony
=
Pony
.
objects
.
get
(
pk
=
pony
.
pk
)
self
.
assertEqual
(
pony
.
text
,
"some text"
)
self
.
assertEqual
(
pony
.
empty
,
""
)
self
.
assertEqual
(
pony
.
digits
,
"42"
)
def
test_add_textfield
(
self
):
"""
Tests the AddField operation on TextField.
"""
project_state
=
self
.
set_up_test_model
(
"test_adtxtfl"
)
new_apps
=
project_state
.
render
()
Pony
=
new_apps
.
get_model
(
"test_adtxtfl"
,
"Pony"
)
pony
=
Pony
.
objects
.
create
(
weight
=
42
)
new_state
=
self
.
apply_operations
(
"test_adtxtfl"
,
project_state
,
[
migrations
.
AddField
(
"Pony"
,
"text"
,
models
.
TextField
(
default
=
"some text"
),
),
migrations
.
AddField
(
"Pony"
,
"empty"
,
models
.
TextField
(
default
=
""
),
),
# If not properly quoted digits would be interpreted as an int.
migrations
.
AddField
(
"Pony"
,
"digits"
,
models
.
TextField
(
default
=
"42"
),
),
])
new_apps
=
new_state
.
render
()
Pony
=
new_apps
.
get_model
(
"test_adtxtfl"
,
"Pony"
)
pony
=
Pony
.
objects
.
get
(
pk
=
pony
.
pk
)
self
.
assertEqual
(
pony
.
text
,
"some text"
)
self
.
assertEqual
(
pony
.
empty
,
""
)
self
.
assertEqual
(
pony
.
digits
,
"42"
)
def
test_add_binaryfield
(
self
):
"""
Tests the AddField operation on TextField/BinaryField.
"""
project_state
=
self
.
set_up_test_model
(
"test_adbinfl"
)
new_apps
=
project_state
.
render
()
Pony
=
new_apps
.
get_model
(
"test_adbinfl"
,
"Pony"
)
pony
=
Pony
.
objects
.
create
(
weight
=
42
)
new_state
=
self
.
apply_operations
(
"test_adbinfl"
,
project_state
,
[
migrations
.
AddField
(
"Pony"
,
"blob"
,
models
.
BinaryField
(
default
=
b
"some text"
),
),
migrations
.
AddField
(
"Pony"
,
"empty"
,
models
.
BinaryField
(
default
=
b
""
),
),
# If not properly quoted digits would be interpreted as an int.
migrations
.
AddField
(
"Pony"
,
"digits"
,
models
.
BinaryField
(
default
=
b
"42"
),
),
])
new_apps
=
new_state
.
render
()
Pony
=
new_apps
.
get_model
(
"test_adbinfl"
,
"Pony"
)
pony
=
Pony
.
objects
.
get
(
pk
=
pony
.
pk
)
# SQLite returns buffer/memoryview, cast to bytes for checking.
self
.
assertEqual
(
bytes
(
pony
.
blob
),
b
"some text"
)
self
.
assertEqual
(
bytes
(
pony
.
empty
),
b
""
)
self
.
assertEqual
(
bytes
(
pony
.
digits
),
b
"42"
)
def
test_column_name_quoting
(
self
):
def
test_column_name_quoting
(
self
):
"""
"""
Column names that are SQL keywords shouldn't cause problems when used
Column names that are SQL keywords shouldn't cause problems when used
...
...
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