On 6/30/15 6:56 PM, Lele Gaifax wrote:
Hi all,

I have a custom migration script that connects to two different databases and
transfers data from one to the other, adapting it to the new structure and
format.

On the source side it uses the autoloading feature of a Table, while on the
target each table is fully described, and some of the fields use custom
TypeDecorators (for example something very similar to the platform indipendent
GUID type described in the recipes on the SA website).

It basically executes something like the following (omitting details like
transactions and errors handling):

   old_table = Table("name", old_metadata, autoload=True")
   query = select([old_table])
   insert = new_table.insert()
   result = old_connection.execute(query)
   for old_row in result:
       new_row = adapt(old_row)
       new_connection.execute(insert, new_row)

where "adapt()" is a function specific to the particular table that can
manipulate the content of each field.

With the approach above custom column types used by the "new_table" are not
playing their "magic", and I currently must repeat it within the "adapt()"
function.

I assume you mean the data you're getting back from old_table isn't being handled because your TypeDecorators aren't part of old_table.

What we have to handle the case of table reflection where you want the reflection to do something special is the column_reflect event. You'd use this event to intercept the column names and datatypes as "old_table" is produced, and you'd swap out the type objects with your custom TypeDecorator. You just need to come up with some way to identify the columns that need it, either by name or by the existing reflected datatype (or both). http://docs.sqlalchemy.org/en/rel_1_0/core/events.html?highlight=column_reflect#sqlalchemy.events.DDLEvents.column_reflect





Is there any alternative approach, or maybe a way to run the equivalent code
path that SA executes when doing an insert using its ORM layer, so that I can
simplify the "adapt()" function applying the DRY principle?

Thank you in advance for any hints,
bye, lele.

--
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to