Social Reiot

Social Game Developer wandering in strange dungeon.

Export/Import Generic ReferenceProperty

구글 앱엔진의 참조 속성인 db.ReferenceProperty(reference_class=None) 은 - 권장할만 하지는 않지만 - 아무 키 값이나 담을 수 있는 컬럼으로 사용할 수 있다. 문제는 이 값을 export/import 할 때 key_id_or_name_as_string + create_foreign_key 조합을 사용할 수 없다는 점이다. 이걸 해결하려면 내보낼 때에는 (kind, id or name) 2개의 컬럼으로 내보낸 다음, 가져올 때에는 이걸 합치면 된다.

또다른 문제는 부모키가 없는 단순 객체일 경우에는 create_deep_key 를 쓸 수도 없다는 점이다. 따라서 이런 유틸리티 함수를 만들었다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
def create_foreign_key_with(kind_col):
  """
  for ReferenceProperty(reference_class=None) => generic key with unknowning kind.

  - property: item_type
  external_name: item_type_value
  import_transform: transformutil.create_foreign_key_with('item_type_kind'))
  export:
  - external_name: item_type_kind
  export_transform: transform.key_kind
  - external_name: item_type_value
  export_transform: transform.key_id_or_name_as_string

  """
  def create_foreign_key_with_lambda(value, bulkload_state):
  kind = bulkload_state.current_dictionary[kind_col]
  if not kind or not value:
  return None
  logging.info('kind:%s value:%s' %(kind, value))
  return datastore.Key.from_path(kind, value)
  return create_foreign_key_with_lambda

물론 bulkloader.yaml 맨 위에 위 소스 파일을 import 해주는 것은 잊지 말아야 한다.

Comments