Skip to content

#88 Support the addition of new fields to an existing schema. #275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
#88 Support the addition of new fields to an existing schema.
 - Especially ListFields and DictFields
 - The _to_json need to be idempotent to support string values coming from wraps, and python type values eg DateTime coming from user code.
  • Loading branch information
JonathanWylie committed Mar 16, 2016
commit 9cdf6085a5940ad3308f8792d696a8ba4f5e4de6
17 changes: 16 additions & 1 deletion couchdb/mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@ def wrap(cls, data):
:return: an instance of class which provides a nice interface to the
information in the data object
"""
instance = cls(**data)
data.update(instance._data)
instance._data = data
return instance

instance = cls()

def add_defaults(default_values, data_dict):
Expand All @@ -205,6 +210,10 @@ def add_defaults(default_values, data_dict):
data_dict[key] = defaultValue
if isinstance(defaultValue, dict):
add_defaults(defaultValue, data_dict[key])
# # If it is a list of DictField then the DictField schema may have updated
# if isinstance(defaultValue, list) and :



# Recursively set the defaults if they are not in the doc already
add_defaults(instance._data, data)
Expand Down Expand Up @@ -496,6 +505,8 @@ def _to_python(self, value):
return value

def _to_json(self, value):
# Make sure it is the right format (it might have come from wrap)
value = self._to_python(value)
if isinstance(value, datetime):
value = value.date()
return value.isoformat()
Expand Down Expand Up @@ -524,6 +535,8 @@ def _to_python(self, value):
return value

def _to_json(self, value):
# Make sure it is the right format (it might have come from wrap)
value = self._to_python(value)
if isinstance(value, struct_time):
value = datetime.utcfromtimestamp(timegm(value))
elif not isinstance(value, datetime):
Expand Down Expand Up @@ -553,6 +566,8 @@ def _to_python(self, value):
return value

def _to_json(self, value):
# Make sure it is the right format (it might have come from wrap)
value = self._to_python(value)
if isinstance(value, datetime):
value = value.time()
return value.replace(microsecond=0).isoformat()
Expand Down Expand Up @@ -642,7 +657,7 @@ class ListField(Field):
>>> comment['content']
u'Bla bla'
>>> comment['time'] #doctest: +ELLIPSIS
u'...T...Z'
'...T...Z'

>>> del server['python-tests']
"""
Expand Down
26 changes: 25 additions & 1 deletion couchdb/tests/mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,6 @@ def test_defaults_for_new_schema_should_be_set_more_complex(self):
assert my_new_instance.address.number == 1
assert my_new_instance.address.street == 'street'


data = copy.copy(my_new_instance._data)

my_newer_mapping = mapping.Mapping.build(
Expand All @@ -345,8 +344,33 @@ def test_defaults_for_new_schema_should_be_set_more_complex(self):
my_newer_instance = my_newer_mapping.wrap(data)

assert data['address']['cars'] == []
my_newer_instance.address.cars.append({"registration": "AJ54 VSE"})
assert data['address']['cars'] == [{"registration": "AJ54 VSE"}]
assert my_newer_instance.address.cars[0].registration == "AJ54 VSE"

data = copy.copy(my_newer_instance._data)

my_newest_mapping = mapping.Mapping.build(
name=mapping.TextField(),
address=mapping.DictField(mapping.Mapping.build(
number=mapping.IntegerField(default=1),
street=mapping.TextField(default='street'),
cars=mapping.ListField(
mapping.DictField(
mapping.Mapping.build(
registration=mapping.TextField(),
wheels=mapping.IntegerField(default=4)
)
)
)
)),
pets=mapping.ListField(mapping.TextField(), default=[])
)

my_newest_instance = my_newest_mapping.wrap(data)

assert data['address']['cars'] == [{"registration": "AJ54 VSE", "wheels": 4}]
assert my_newest_instance.address.cars[0].wheels == 4

def test_can_set_new_schema_data(self):
my_mapping = mapping.Mapping.build(
Expand Down