@@ -21,13 +21,27 @@ def transfer_data():
21
21
session = Session (bind = op .get_bind ())
22
22
dialect = session .bind .dialect .name
23
23
24
- session .execute (sa .text ("""
24
+ uuid_generation_func = "replace(uuid(),'-','')"
25
+ if dialect == "sqlite" :
26
+ uuid_generation_func = """
27
+ lower(
28
+ hex(randomblob(4)) || '-' || hex(randomblob(2)) || '-' || '4' ||
29
+ substr(hex( randomblob(2)), 2) || '-' ||
30
+ substr('AB89', 1 + (abs(random()) % 4) , 1) ||
31
+ substr(hex(randomblob(2)), 2) || '-' ||
32
+ hex(randomblob(6))
33
+ )
34
+ """
35
+ elif dialect == "postgresql" :
36
+ uuid_generation_func = "gen_random_uuid()"
37
+
38
+ session .execute (sa .text (f"""
25
39
INSERT INTO topologyservice (
26
- id, tenant_id, source_provider_id, repository, tags, service, environment, display_name, description,
40
+ id, external_id, tenant_id, source_provider_id, repository, tags, service, environment, display_name, description,
27
41
team, email, slack, ip_address, mac_address, category, manufacturer, namespace, is_manual
28
42
)
29
43
SELECT
30
- id , tenant_id, source_provider_id, repository, tags, service, environment, display_name, description,
44
+ { uuid_generation_func } as id, id as external_id , tenant_id, source_provider_id, repository, tags, service, environment, display_name, description,
31
45
team, email, slack, ip_address, mac_address, category, manufacturer, namespace, is_manual
32
46
FROM topologyservice_tmp
33
47
""" ))
@@ -39,37 +53,17 @@ def transfer_data():
39
53
40
54
session .execute (sa .text ("""
41
55
INSERT INTO topologyserviceapplication (service_id, application_id, tenant_id)
42
- SELECT tsa.service_id , tsa.application_id, ts.tenant_id FROM topologyserviceapplication_tmp as tsa
43
- JOIN topologyservice_tmp as ts ON tsa.service_id = ts.id
56
+ SELECT ts.id , tsa.application_id, ts.tenant_id FROM topologyserviceapplication_tmp as tsa
57
+ JOIN topologyservice as ts ON tsa.service_id = ts.external_id
44
58
""" ))
45
59
46
- if dialect == "sqlite" :
47
- session .execute (sa .text ("""
48
- INSERT INTO topologyservicedependency (id, service_id, depends_on_service_id, updated_at, protocol, tenant_id)
49
- SELECT lower(
50
- hex(randomblob(4)) || '-' || hex(randomblob(2)) || '-' || '4' ||
51
- substr(hex( randomblob(2)), 2) || '-' ||
52
- substr('AB89', 1 + (abs(random()) % 4) , 1) ||
53
- substr(hex(randomblob(2)), 2) || '-' ||
54
- hex(randomblob(6))
55
- ) as id, tsd.service_id, tsd.depends_on_service_id, tsd.updated_at, tsd.protocol, ts.tenant_id
56
- FROM topologyservicedependency_tmp as tsd
57
- JOIN topologyservice_tmp as ts ON tsd.service_id = ts.id
58
- """ ))
59
- elif dialect == "postgres" :
60
- session .execute (sa .text ("""
61
- INSERT INTO topologyservicedependency (id, service_id, depends_on_service_id, updated_at, protocol, tenant_id)
62
- SELECT gen_random_uuid() as id, tsd.service_id, tsd.depends_on_service_id, tsd.updated_at, tsd.protocol, ts.tenant_id
63
- FROM topologyservicedependency_tmp as tsd
64
- JOIN topologyservice_tmp as ts ON tsd.service_id = ts.id
65
- """ ))
66
- elif dialect == "mysql" :
67
- session .execute (sa .text ("""
68
- INSERT INTO topologyservicedependency (id, service_id, depends_on_service_id, updated_at, protocol, tenant_id)
69
- SELECT uuid() as id, tsd.service_id, tsd.depends_on_service_id, tsd.updated_at, tsd.protocol, ts.tenant_id
70
- FROM topologyservicedependency_tmp as tsd
71
- JOIN topologyservice_tmp as ts ON tsd.service_id = ts.id
72
- """ ))
60
+ session .execute (sa .text (f"""
61
+ INSERT INTO topologyservicedependency (id, service_id, depends_on_service_id, updated_at, protocol, tenant_id)
62
+ SELECT { uuid_generation_func } as id, ts.id as service_id, ts_dep.id as depends_on_service_id, tsd.updated_at, tsd.protocol, ts.tenant_id
63
+ FROM topologyservicedependency_tmp as tsd
64
+ JOIN topologyservice as ts ON tsd.service_id = ts.external_id
65
+ JOIN topologyservice as ts_dep ON tsd.depends_on_service_id = ts_dep.external_id
66
+ """ ))
73
67
74
68
75
69
def transfer_data_back ():
@@ -80,7 +74,7 @@ def transfer_data_back():
80
74
id, tenant_id, source_provider_id, repository, tags, service, environment, display_name, description,
81
75
team, email, slack, ip_address, mac_address, category, manufacturer, namespace, is_manual
82
76
)
83
- SELECT id, tenant_id, source_provider_id, repository, tags, service, environment, display_name, description,
77
+ SELECT external_id as id, tenant_id, source_provider_id, repository, tags, service, environment, display_name, description,
84
78
team, email, slack, ip_address, mac_address, category, manufacturer, namespace, is_manual
85
79
FROM topologyservice_tmp
86
80
""" ))
@@ -92,12 +86,17 @@ def transfer_data_back():
92
86
93
87
session .execute (sa .text ("""
94
88
INSERT INTO topologyserviceapplication (service_id, application_id)
95
- SELECT service_id, application_id FROM topologyserviceapplication_tmp
89
+ SELECT ts.external_id, tsa.application_id FROM topologyserviceapplication_tmp as tsa
90
+ JOIN topologyservice_tmp as ts ON tsa.service_id = ts.id
91
+
96
92
""" ))
97
93
98
94
session .execute (sa .text ("""
99
95
INSERT INTO topologyservicedependency (service_id, depends_on_service_id, updated_at, protocol)
100
- SELECT service_id, depends_on_service_id, updated_at, protocol FROM topologyservicedependency_tmp
96
+ SELECT ts.external_id, ts_dep.external_id, tsd.updated_at, tsd.protocol
97
+ FROM topologyservicedependency_tmp as tsd
98
+ JOIN topologyservice_tmp as ts ON tsd.service_id = ts.id
99
+ JOIN topologyservice_tmp as ts_dep ON tsd.depends_on_service_id = ts_dep.id
101
100
""" ))
102
101
103
102
def upgrade ():
@@ -121,7 +120,8 @@ def upgrade():
121
120
)
122
121
op .create_table (
123
122
"topologyservice" ,
124
- sa .Column ("id" , sa .Integer (), nullable = False ),
123
+ sa .Column ("id" , sa .Uuid (), nullable = False ),
124
+ sa .Column ("external_id" , sa .Integer (), nullable = True ),
125
125
sa .Column ("tenant_id" , sqlmodel .sql .sqltypes .AutoString (), nullable = False ),
126
126
sa .Column (
127
127
"source_provider_id" , sqlmodel .sql .sqltypes .AutoString (), nullable = False
@@ -156,7 +156,7 @@ def upgrade():
156
156
op .create_table (
157
157
"topologyserviceapplication" ,
158
158
sa .Column ("tenant_id" , sqlmodel .sql .sqltypes .AutoString (), nullable = False ),
159
- sa .Column ("service_id" , sa .Integer (), nullable = False ),
159
+ sa .Column ("service_id" , sa .Uuid (), nullable = False ),
160
160
sa .Column ("application_id" , sa .Uuid (), nullable = False ),
161
161
sa .ForeignKeyConstraint (
162
162
["application_id" , "tenant_id" ],
@@ -176,8 +176,8 @@ def upgrade():
176
176
"topologyservicedependency" ,
177
177
sa .Column ("id" , sa .Uuid (), nullable = False ),
178
178
sa .Column ("tenant_id" , sqlmodel .sql .sqltypes .AutoString (), nullable = False ),
179
- sa .Column ("service_id" , sa .Integer (), nullable = False ),
180
- sa .Column ("depends_on_service_id" , sa .Integer (), nullable = False ),
179
+ sa .Column ("service_id" , sa .Uuid (), nullable = False ),
180
+ sa .Column ("depends_on_service_id" , sa .Uuid (), nullable = False ),
181
181
sa .Column ("protocol" , sqlmodel .sql .sqltypes .AutoString (), nullable = True ),
182
182
sa .Column (
183
183
"updated_at" ,
0 commit comments