-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_dates.py
168 lines (155 loc) · 8.61 KB
/
update_dates.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import psycopg2
from psycopg2 import OperationalError
import json
import sys
from config import Config
config = Config()
def create_connection(db_name, db_user, db_password, db_host, db_port):
connection = None
try:
connection = psycopg2.connect(
database=db_name,
user=db_user,
password=db_password,
host=db_host,
port=db_port,
)
print("Connection to PostgreSQL DB successful")
except OperationalError as e:
print(f"The ERROR '{e}' occurred")
return connection
def execute_query(connection, query):
#connection.autocommit = True
cursor = connection.cursor()
try:
cursor.execute(query)
print("Query executed successfully")
connection.commit()
except OperationalError as e:
print(f"The ERROR '{e}' occurred")
connection.rollback()
def execute_read_query(connection, query):
cursor = connection.cursor()
result = None
try:
cursor.execute(query)
result = cursor.fetchall()
return result
except OperationalError as e:
print(f"The ERROR '{e}' occurred")
def take_version(elem):
if 'versionNumber' not in elem:
return sys.maxsize
v = elem['versionNumber']
if v == ':draft':
return sys.maxsize
else:
mv = elem['versionMinorNumber']
if mv == 0:
return v
else:
return v + mv / (len(str(mv)) * 10)
def main():
with open('correspondence_old_new.json') as f:
datasets = json.load(f)
try:
connection = create_connection(config.db_name, config.db_user, config.db_password, config.db_host, config.db_port)
for old_id in datasets:
new_id = datasets[old_id]
try:
resp_old = config.api_origin.get_dataset_versions(old_id,True, False)
except Exception as e:
print("ERROR {}".format(e))
continue
if resp_old.status_code != 200:
print("ERROR - ", resp_old.json())
continue
versions = resp_old.json()['data']
versions.sort(key=take_version)
try:
resp_new = config.api_target.get_dataset_versions(new_id,True, False)
if resp_new.status_code != 200:
print("ERROR - ",resp_new.json())
continue
except Exception as e:
print("ERROR {}".format(e))
continue
versions_new = resp_new.json()['data']
versions_new.sort(key=take_version)
print(versions_new)
number_of_versions = len(versions)
print(number_of_versions)
n = 0
if len(versions_new) == len(versions) or len(versions_new) != len(versions):
for version in versions:
n = n + 1
last_update_time = ""
release_time = ""
versionState = ""
archiveNote = ""
archiveTime = ""
createTime = ""
query = ""
print(version)
if "lastUpdateTime" in version:
last_update_time = version['lastUpdateTime']
print(last_update_time)
if "releaseTime" in version:
release_time = version["releaseTime"]
if "versionState" in version:
versionState = version['versionState']
if "createTime" in version:
createTime = version['createTime']
if "archiveNote" in version:
archiveNote = version['archiveNote']
if "archiveTime" in version:
archiveTime = version['archiveTime']
try:
if versionState == "DRAFT":
if 'archiveTime' in version and 'archiveNote' in version:
query = "update datasetversion set lastupdatetime='{}'::timestamp AT time zone 'UTC', createtime ='{}'::timestamp AT time zone 'UTC', archivetime='{}'::timestamp AT time zone 'UTC', archivenote='{}' where dataset_id={} and versionState='DRAFT'".format(
last_update_time, createTime, archiveTime, archiveNote, new_id)
else:
if 'archiveTime' in version:
query = "update datasetversion set lastupdatetime='{}', createtime ='{}'::timestamp AT time zone 'UTC', archivetime='{}'::timestamp AT time zone 'UTC' where dataset_id={} and versionState='DRAFT'".format(
last_update_time, createTime, archiveTime, new_id)
else:
if 'archiveNote' in version:
query = "update datasetversion set lastupdatetime='{}'::timestamp AT time zone 'UTC', createtime ='{}'::timestamp AT time zone 'UTC', archivenote='{}' where dataset_id={} and versionState='DRAFT'".format(
last_update_time, createTime, archiveNote, new_id)
else:
query = "update datasetversion set lastupdatetime='{}'::timestamp AT time zone 'UTC', createtime ='{}'::timestamp AT time zone 'UTC' where dataset_id={} and versionState='DRAFT'".format(
last_update_time, createTime, new_id)
if versionState == 'RELEASED':
version_number = version['versionNumber']
version_minor_number = version['versionMinorNumber']
if 'archiveTime' in version and 'archiveNote' in version:
query = "update datasetversion set lastupdatetime='{}'::timestamp AT time zone 'UTC', releasetime='{}'::timestamp AT time zone 'UTC', createtime='{}'::timestamp AT time zone 'UTC', archivetime='{}'::timestamp AT time zone 'UTC', archivenote='{}' where dataset_id={} and versionnumber={} and minorversionnumber={}".format(
last_update_time, release_time, createTime, archiveTime, archiveNote, new_id,
version_number, version_minor_number)
else:
if 'archiveTime' in version:
query = "update datasetversion set lastupdatetime='{}'::timestamp AT time zone 'UTC', releasetime='{}'::timestamp AT time zone 'UTC', createtime='{}'::timestamp AT time zone 'UTC', archivetime='{}'::timestamp AT time zone 'UTC' where dataset_id={} and versionnumber={} and minorversionnumber={}".format(
last_update_time, release_time, createTime, archiveTime, new_id,
version_number, version_minor_number)
else:
if 'archiveNote' in version:
query = "update datasetversion set lastupdatetime='{}'::timestamp AT time zone 'UTC', releasetime='{}'::timestamp AT time zone 'UTC', createtime='{}'::timestamp AT time zone 'UTC', archivenote='{}' where dataset_id={} and versionnumber={} and minorversionnumber={}".format(
last_update_time, release_time, createTime, archiveNote, new_id,
version_number, version_minor_number)
else:
query = "update datasetversion set lastupdatetime='{}'::timestamp AT time zone 'UTC', releasetime='{}'::timestamp AT time zone 'UTC', createtime='{}'::timestamp AT time zone 'UTC' where dataset_id={} and versionnumber={} and minorversionnumber={}".format(
last_update_time, release_time, createTime, new_id,
version_number, version_minor_number)
print(query)
execute_query(connection, query)
except Exception as e:
print(e)
connection.close()
connection = create_connection(config.db_name, config.db_user, config.db_password,
config.db_host, config.db_port)
connection.close()
except Exception as e:
print(e)
if __name__ == "__main__":
main()