Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to migrate NDB model property

I currently have a model in NDB and I'd like to change the property name without necessarily touching NBD. Let's say I have the following:

from google.appengine.ext import ndb

class User(ndb.Model):
  company = ndb.KeyProperty(repeated=True)

What I would like to have is something more like this:

class User(ndb.Model):
  company_   = ndb.KeyProperty(repeated=True)

@property
def company(self):
   return '42'

@company.setter
def company(self, new_company):
    #set company here

Is there a relatively pain-free way to do so? I'd like the convienance of using property getter/setters, but given the current implementation I would like to avoid touching the underlying datastore.

like image 441
TJ1S Avatar asked Dec 25 '22 21:12

TJ1S


2 Answers

you can change the class-level property name while keeping the underlying NDB property name by specifying the name="xx" param in the Property() constructor

so something like this could be done:

class User(ndb.Model):
  company_   = ndb.KeyProperty(name="company", repeated=True)

  @property
  def company(self):
    return self.company_

  @company.setter
  def company(self, new_company):
    self.company_ = new_company

so now anytime you access .company_ NDB will actually set/get "company" internally... and you don't have to do any data migrations

like image 148
Nick Franceschina Avatar answered Dec 27 '22 10:12

Nick Franceschina


From my understanding of ndb, the property names are stored in the database along with their contents for every entity. You would have to rewrite every entity with the new property name (and without the old one).

Since that is not pain-free, maybe you could choose other names for your getter and setter like get_company and set_company.

like image 25
Brent Washburne Avatar answered Dec 27 '22 12:12

Brent Washburne