Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Migration to GAE

What is the best way to migrate MySQL tables to Google Datastore and create python models for them?

I have a PHP+MySQL project that I want to migrate to Python+GAE project. So far the big obstacle is migrating the tables and creating corresponding models. Each table is about 110 columns wide. Creating a model for the table manually is a bit tedious, let alone creating a loader and importing a generated csv table representation.

Is there a more efficient way for me to do the migration?

like image 364
notnoop Avatar asked Jun 12 '26 19:06

notnoop


2 Answers

In general, generating your models automatically shouldn't be too difficult. Suppose you have a csv file for each table, with lines consisting of (field name, data type), then something like this would do the job:

# Maps MySQL types to Datastore property classes
type_map = {
    'char': 'StringProperty',
    'text': 'TextProperty',
    'int': 'IntegerProperty',
    # ...
}

def generate_model_class(classname, definition_file):
  ret = []
  ret.append("class %s(db.Model):" % (classname,))
  for fieldname, type in csv.reader(open(definition_file)):
    ret.append("  %s = db.%s()" % (fieldname, type_map[type]))
  return "\n".join(ret)

Once you've defined your schema, you can bulk load directly from the DB - no need for intermediate CSV files. See my blog post on the subject.

like image 163
Nick Johnson Avatar answered Jun 17 '26 00:06

Nick Johnson


approcket can mysql⇌gae or gae builtin remote api from google

like image 41
Niklas Rosencrantz Avatar answered Jun 17 '26 00:06

Niklas Rosencrantz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!