|
3 | 3 | from attribute.models import Attribute, Level |
4 | 4 |
|
5 | 5 |
|
| 6 | +def migrate_third_attrs(target: int, dest: int, from_order: int = 1): |
| 7 | + attr_data = File.objects \ |
| 8 | + .filter( |
| 9 | + project_id=dest, |
| 10 | + rebound_project=target, |
| 11 | + attributegroup__attribute__project_id=target, |
| 12 | + attributegroup__attribute__level__order__gte=from_order |
| 13 | + ) \ |
| 14 | + .values_list( |
| 15 | + "attributegroup__uid", |
| 16 | + "attributegroup__attribute__id", |
| 17 | + "attributegroup__attribute__name", |
| 18 | + "attributegroup__attribute__project_id", |
| 19 | + "attributegroup__attribute__level__order" |
| 20 | + ) \ |
| 21 | + .distinct() |
| 22 | + print(f"[1/4] Found {attr_data.count()} attrs to migrate") |
| 23 | + |
| 24 | + rebound = [] |
| 25 | + created = 0 |
| 26 | + failed = [] |
| 27 | + |
| 28 | + cache = {} |
| 29 | + |
| 30 | + for ag_id, a_id, a_name, p_id, l_order in attr_data: |
| 31 | + ag_id = str(ag_id) |
| 32 | + |
| 33 | + if p_id != target: continue |
| 34 | + |
| 35 | + if (cached := cache.get((a_id, a_name))): |
| 36 | + print(f"[2/4] Cached {a_name}") |
| 37 | + rebound.append((cached, a_id, ag_id,)) |
| 38 | + continue |
| 39 | + |
| 40 | + try: |
| 41 | + map_id = Attribute.objects.filter(project_id=dest, name=a_name).id |
| 42 | + rebound.append((map_id, a_id, ag_id)) |
| 43 | + cache[(a_id, a_name)] = map_id |
| 44 | + print(f"[2/4] Found {a_name} in dest") |
| 45 | + except: |
| 46 | + levels = Level.objects.filter(project_id=dest, order=l_order) |
| 47 | + if levels.count() == 1: |
| 48 | + map_id = levels.first().attribute_set.create(name=a_name, project_id=dest).id |
| 49 | + rebound.append((map_id, a_id, ag_id)) |
| 50 | + created += 1 |
| 51 | + cache[(a_id, a_name)] = map_id |
| 52 | + print(f"[2/4] Created {a_name} in dest") |
| 53 | + else: failed.append((a_id, a_name)) |
| 54 | + |
| 55 | + print(f"[3/4] Got {len(rebound)} attributes with {created} created") |
| 56 | + |
| 57 | + with connection.cursor() as cursor: cursor.executemany( |
| 58 | + """ |
| 59 | + update attribute_group_attribute |
| 60 | + set attribute_id = %s |
| 61 | + where attribute_id = %s and attributegroup_id = %s; |
| 62 | + """, |
| 63 | + rebound |
| 64 | + ) |
| 65 | + |
| 66 | + print(f"[4/4] Migrate done with {len(failed)} failed") |
| 67 | + |
| 68 | + |
6 | 69 | def migrate_files( |
7 | 70 | target_project: int, |
8 | 71 | dest_project: int, |
@@ -46,6 +109,7 @@ def get_ids(sep: str, path_from: str, path_to: str) -> tuple[set[str], set[str]] |
46 | 109 | set([row.split(sep)[0] for row in data_to.split("\n")[1:] if row]) |
47 | 110 | ) |
48 | 111 |
|
| 112 | + |
49 | 113 | def main(target: tuple[int, str], dest: tuple[int, str], sep: str): |
50 | 114 | target_id, target_file = target |
51 | 115 | dest_id, dest_file = dest |
@@ -83,3 +147,6 @@ def main(target: tuple[int, str], dest: tuple[int, str], sep: str): |
83 | 147 | .filter(project_id=target_id, attributegroup__attribute__payload__in=target_set) \ |
84 | 148 | .update(rebound_project=target_id, project_id=dest_id) |
85 | 149 | print(f"[4/4] Rebound {res} total files to the new projects") |
| 150 | + |
| 151 | + |
| 152 | + # migrate_third_attrs(target_id, dest_id, 1) |
0 commit comments