|
35 | 35 | import git |
36 | 36 | import sys |
37 | 37 |
|
38 | | -parser = argparse.ArgumentParser(description='Check Neotoma SQL functions against functions in the online database servers (`neotoma` and `neotomadev`).') |
39 | | - |
40 | | -parser.add_argument('-dev', dest='isDev', default = False, help = 'Use the `dev` database? (`False` without the flag)', action = 'store_true') |
41 | | -parser.add_argument('-push', dest='isPush', default = False, help = 'Assume that SQL functions in the repository are newer, push to the db server.', action = 'store_true') |
42 | | -parser.add_argument('-g', dest='pullGit', nargs = '?', type = str, default = None, help = 'Pull from the remote git server before running?.') |
43 | | -parser.add_argument('-tilia', dest='isTilia', default = False, help = 'Use the `dev` database? (`False` without the flag)', action = 'store_true') |
| 38 | +parser = argparse.ArgumentParser( |
| 39 | + description='Check Neotoma SQL functions against functions in the ' |
| 40 | + + 'online database servers (`neotoma` and `neotomadev`).') |
| 41 | + |
| 42 | +parser.add_argument('-dev', dest='isDev', default=False, |
| 43 | + help='Use the `dev` database? (`False` without the flag)', |
| 44 | + action='store_true') |
| 45 | +parser.add_argument('-push', dest='isPush', default=False, |
| 46 | + help='Assume that SQL functions in the repository are ' |
| 47 | + + 'newer, push to the db server.', action='store_true') |
| 48 | +parser.add_argument('-g', dest='pullGit', nargs='?', type=str, default=None, |
| 49 | + help='Pull from the remote git server before running?.') |
| 50 | +parser.add_argument('-tilia', dest='isTilia', default=False, |
| 51 | + help='Use the `dev` database? (`False` without the flag)', |
| 52 | + action='store_true') |
44 | 53 |
|
45 | 54 | args = parser.parse_args() |
46 | 55 |
|
|
54 | 63 | break |
55 | 64 |
|
56 | 65 | if good is False: |
57 | | - print("The connect_remote.json file is not in your .gitignore file. Please add it!") |
| 66 | + print("The connect_remote.json file is not in your .gitignore file. " |
| 67 | + + "Please add it!") |
58 | 68 |
|
59 | 69 | with open('connect_remote.json') as f: |
60 | 70 | data = json.load(f) |
|
64 | 74 | try: |
65 | 75 | repo.heads[args.pullGit].checkout() |
66 | 76 | except git.exc.GitCommandError: |
67 | | - sys.exit("Stash or commit changes in the current branch before switching to " + args.pullGit + ".") |
| 77 | + sys.exit("Stash or commit changes in the current branch before " |
| 78 | + + "switching to " + args.pullGit + ".") |
68 | 79 |
|
69 | 80 | repo.remotes.origin.pull() |
70 | 81 |
|
|
76 | 87 |
|
77 | 88 | print("Using the " + data['database'] + ' Neotoma server.') |
78 | 89 |
|
79 | | -conn = psycopg2.connect(**data) |
| 90 | +conn = psycopg2.connect(**data, connect_timeout=5) |
80 | 91 |
|
81 | 92 | cur = conn.cursor() |
82 | 93 |
|
|
91 | 102 | FROM pg_catalog.pg_proc AS f |
92 | 103 | INNER JOIN pg_catalog.pg_namespace AS n ON f.pronamespace = n.oid |
93 | 104 | WHERE |
94 | | - n.nspname IN ('ti','ndb','ts', 'mca', 'ecg', 'ap', 'da', 'emb', 'gen', 'doi') |
| 105 | + n.nspname IN ('ti','ndb','ts', 'mca', 'ecg', 'ap', |
| 106 | + 'da', 'emb', 'gen', 'doi') |
95 | 107 | ORDER BY n.nspname, proname""") |
96 | 108 |
|
97 | 109 | # For each sql function in the named namespaces go in and write out the actual |
|
105 | 117 | z = 0 |
106 | 118 |
|
107 | 119 | for record in cur: |
| 120 | + print(record[1]) |
108 | 121 | # This checks each function in the database and then tests whether there |
109 | 122 | # is a file associated with it. |
110 | 123 | newFile = "./function/" + record[0] + "/" + record[1] + ".sql" |
|
124 | 137 | file = open(newFile) |
125 | 138 | textCheck = copy.deepcopy(file.read()) |
126 | 139 | serverFun = copy.deepcopy(record[3]) |
127 | | - textCheck = re.sub('[\s+\t+\n+\r+]','', textCheck) |
128 | | - serverFun = re.sub('[\s+\t+\n+\r+]','', serverFun) |
| 140 | + textCheck = re.sub('[\s+\t+\n+\r+]', '', textCheck) |
| 141 | + serverFun = re.sub('[\s+\t+\n+\r+]', '', serverFun) |
129 | 142 | match = serverFun == textCheck |
130 | | - # Pushing (to the db) and pulling (from the db) are defined by the user. |
131 | | - if match == False: |
132 | | - if args.isPush == False: |
133 | | - print('The function ' + record[0] + '.' + record[1] + ' differs between the database and your local copy.\n *' + newFile + ' will be written locally.') |
| 143 | + # Pushing (to the db) and pulling (from the db) are defined by the user |
| 144 | + if match is False: |
| 145 | + if args.isPush is False: |
| 146 | + print('The function ' + record[0] + '.' + record[1] |
| 147 | + + ' differs between the database and your local copy.\n*' |
| 148 | + + newFile + ' will be written locally.') |
134 | 149 | file = open(newFile, 'w') |
135 | 150 | file.write(record[3]) |
136 | 151 | file.close() |
137 | | - print('The file for ' + record[0] + '.' + record[1] + ' has been updated in the repository.') |
| 152 | + print('The file for ' + record[0] + '.' + record[1] |
| 153 | + + ' has been updated in the repository.') |
138 | 154 | else: |
139 | 155 | cur2 = conn.cursor() |
140 | 156 | try: |
141 | | - cur2.execute("DROP FUNCTION " + record[0] + "." + record[1] + "(" + record[2] + ");") |
| 157 | + cur2.execute("DROP FUNCTION " + record[0] + "." + record[1] |
| 158 | + + "(" + record[2] + ");") |
142 | 159 | conn.commit() |
143 | | - except: |
| 160 | + print("Dropped function.") |
| 161 | + except Exception as e: |
| 162 | + print(e) |
144 | 163 | conn.rollback() |
145 | 164 | print("Could not delete " + record[0] + "." + record[1]) |
146 | 165 | failed.add(record[0] + "." + record[1]) |
147 | 166 |
|
148 | 167 | try: |
149 | | - cur2.execute(open("./function/" + record[0] + "/" + record[1] + ".sql", "r").read()) |
| 168 | + print("trying to execute") |
| 169 | + cur2 = conn.cursor() |
| 170 | + cur2.execute( |
| 171 | + open("./function/" + record[0] + "/" + record[1] |
| 172 | + + ".sql", "r").read()) |
150 | 173 | conn.commit() |
151 | | - print('The function for ' + record[0] + '.' + record[1] + ' has been updated in the `' + data['database'] + '` database.') |
| 174 | + print("executed") |
152 | 175 | cur2.execute("REASSIGN OWNED BY sug335 TO functionwriter;") |
153 | 176 | conn.commit() |
| 177 | + print("reassigned") |
154 | 178 | rewrite.add(record[0] + "." + record[1]) |
| 179 | + print('The function for ' + record[0] + '.' + record[1] |
| 180 | + + ' has been updated in the `' + data['database'] |
| 181 | + + '` database.') |
155 | 182 | z = z + 1 |
156 | | - except: |
| 183 | + except Exception as e: |
157 | 184 | conn.rollback() |
158 | | - print('The function for ' + record[0] + '.' + record[1] + ' has not been updated in the `' + data['database'] + '` database.') |
| 185 | + print(e) |
| 186 | + print('The function for ' + record[0] + '.' + record[1] |
| 187 | + + ' has not been updated in the `' + data['database'] |
| 188 | + + '` database.') |
159 | 189 | failed.add(record[0] + "." + record[1]) |
160 | 190 |
|
161 | 191 | for schema in ['ti', 'ts', 'doi', 'ap', 'ndb']: |
|
175 | 205 | print(data) |
176 | 206 | try: |
177 | 207 | cur.execute(SQL, data) |
178 | | - except: |
| 208 | + except Exception as e: |
179 | 209 | conn.rollback() |
180 | 210 | print("Failed to run. " + schema) |
| 211 | + print(e) |
181 | 212 |
|
182 | 213 | if cur.rowcount == 0: |
183 | 214 | # Execute the new script if there is one. Needs the commit. |
184 | 215 | print("Executing " + schema + "." + functs.split(".")[0]) |
185 | 216 | try: |
186 | | - cur.execute(open("./function/" + schema + "/" + functs, "r").read()) |
| 217 | + cur.execute(open("./function/" + schema |
| 218 | + + "/" + functs, "r").read()) |
187 | 219 | conn.commit() |
188 | 220 | cur2.execute("REASSIGN OWNED BY sug335 TO functionwriter;") |
189 | 221 | conn.commit() |
|
196 | 228 | failed.add(schema + "." + functs.split(".")[0]) |
197 | 229 | z = z + 1 |
198 | 230 | if cur.rowcount > 1: |
199 | | - # TODO: Need to add a script to check that the definitions are the same. |
200 | | - print(schema + "." + functs.split(".")[0] + " has " + |
201 | | - str(cur.rowcount) + " definitions.") |
| 231 | + # TODO: Need to add a script to check that the definitions are |
| 232 | + # the same. |
| 233 | + print(schema + "." + functs.split(".")[0] + " has " |
| 234 | + + str(cur.rowcount) + " definitions.") |
| 235 | + |
| 236 | +conn.close() |
| 237 | + |
202 | 238 |
|
203 | 239 | print("The script has rewritten:") |
204 | 240 |
|
|
0 commit comments