| 1 |
""" |
| 2 |
Stuff dealing with the upgrade of the database schema. |
| 3 |
|
| 4 |
From software version 0.8.2 on, there is a dc.metastore table with a key |
| 5 |
schemaversion. Each change in the central schema increases the value |
| 6 |
(interpreted as an integer) by one, and this module will contain a |
| 7 |
corresponding upgrader. |
| 8 |
|
| 9 |
An upgrader inherits form the Upgrader class. See there for more details. |
| 10 |
|
| 11 |
This module contains the current schemaversion expected by the software; gavo |
| 12 |
upgrade does everything required to bring the what's in the database in sync |
| 13 |
with the code (or so I hope). |
| 14 |
""" |
| 15 |
|
| 16 |
#c Copyright 2008-2025, the GAVO project <[email protected]> |
| 17 |
#c |
| 18 |
#c This program is free software, covered by the GNU GPL. See the |
| 19 |
#c COPYING file in the source distribution. |
| 20 |
|
| 21 |
|
| 22 |
import re |
| 23 |
import sys |
| 24 |
|
| 25 |
from psycopg2 import extensions |
| 26 |
|
| 27 |
from gavo import base |
| 28 |
from gavo import registry |
| 29 |
from gavo import rsc |
| 30 |
from gavo import rscdesc #noflake: for cache registration |
| 31 |
from gavo import utils |
| 32 |
|
| 33 |
|
| 34 |
CURRENT_SCHEMAVERSION = 36 |
| 35 |
|
| 36 |
|
| 37 |
class _COMMIT(object): |
| 38 |
"""A sentinel used by iterStatements. |
| 39 |
""" |
| 40 |
|
| 41 |
|
| 42 |
def getColumnNamesFor(tableId, connection): |
| 43 |
"""returns (normalised) column names for tableId. |
| 44 |
|
| 45 |
(tableId is something like //tap#tables) |
| 46 |
""" |
| 47 |
t = rsc.TableForDef(base.resolveCrossId(tableId), connection=connection) |
| 48 |
return set(n for n, t in t.getColumnsFromDB(t.tableDef.getQName())) |
| 49 |
|
| 50 |
|
| 51 |
def relationExists(tableName, connection): |
| 52 |
"""returns True if tableName (as schema.name) exists, False otherwise. |
| 53 |
""" |
| 54 |
q = base.UnmanagedQuerier(connection) |
| 55 |
return q.getTableType(tableName) is not None |
| 56 |
|
| 57 |
|
| 58 |
def _updateTAP_SCHEMA(connection): |
| 59 |
"""re-ingests TAP_SCHEMA metadata for all RDs already mentioned |
| 60 |
in TAP_SCHEMA. |
| 61 |
""" |
| 62 |
rsc.makeData( |
| 63 |
base.resolveCrossId("//tap#refresh"), |
| 64 |
connection=connection) |
| 65 |
|
| 66 |
|
| 67 |
def iterEPNTAPTables(connection): |
| 68 |
"""yields table descriptors for all tables that claim to conform to EPN-TAP. |
| 69 |
""" |
| 70 |
mth = base.caches.getMTH(None) |
| 71 |
|
| 72 |
for tableName, in connection.query("select table_name" |
| 73 |
" from tap_schema.tables" |
| 74 |
" where utype in (" |
| 75 |
" 'ivo://vopdc.obspm/std/epncore#schema-2.0'," |
| 76 |
" 'ivo://ivoa.net/std/epntap#table-2.0')"): |
| 77 |
try: |
| 78 |
yield mth.getTableDefForTable(tableName) |
| 79 |
except: |
| 80 |
base.ui.notifyWarning(f"Published EPN-TAP table {tableName}" |
| 81 |
" has no definition any more") |
| 82 |
|
| 83 |
|
| 84 |
class AnnotatedString(str): |
| 85 |
"""a string with an annotation. |
| 86 |
|
| 87 |
This is (optionally) used to hold SQL statements here; the annotation |
| 88 |
is shown to the user instead of the naked statement when present. |
| 89 |
""" |
| 90 |
def __new__(cls, content, annotation): |
| 91 |
res = str.__new__(cls, content) |
| 92 |
res.annotation = annotation |
| 93 |
return res |
| 94 |
|
| 95 |
|
| 96 |
def showProgress(msg): |
| 97 |
"""outputs msg to stdout without lf "immediately". |
| 98 |
""" |
| 99 |
sys.stdout.write(msg) |
| 100 |
sys.stdout.flush() |
| 101 |
|
| 102 |
|
| 103 |
def getDBSchemaVersion(): |
| 104 |
"""returns the schemaversion given in the database. |
| 105 |
|
| 106 |
This will return -1 if no schemaversion is declared. |
| 107 |
""" |
| 108 |
try: |
| 109 |
return int(base.getDBMeta("schemaversion")) |
| 110 |
except (KeyError, base.DBError): |
| 111 |
return -1 |
| 112 |
|
| 113 |
|
| 114 |
def dumpExtensionUpdater(conn): |
| 115 |
"""prints SQL to bring [db]managedExtensions up to date. |
| 116 |
""" |
| 117 |
statements = [] |
| 118 |
|
| 119 |
for extName in base.getConfig("db", "managedextensions"): |
| 120 |
res = list(conn.query( |
| 121 |
"SELECT default_version, installed_version" |
| 122 |
" FROM pg_available_extensions" |
| 123 |
" WHERE name=%(extName)s", locals())) |
| 124 |
|
| 125 |
if res: |
| 126 |
def_version, ins_version = res[0] |
| 127 |
if ins_version is None: |
| 128 |
# extra treatment for some magic extensions that may have been |
| 129 |
# present from before postgres' proper extension mechanism. |
| 130 |
if extName in ['q3c', 'pg_sphere']: |
| 131 |
statements.append("CREATE EXTENSION %s FROM unpackaged;"%extName) |
| 132 |
else: |
| 133 |
statements.append("CREATE EXTENSION %s;"%extName) |
| 134 |
|
| 135 |
elif ins_version!=def_version: |
| 136 |
statements.append("ALTER EXTENSION %s UPDATE TO %s;"%( |
| 137 |
extName, |
| 138 |
extensions.adapt(def_version).getquoted().decode('utf-8'))) |
| 139 |
# else fall through (either the extension is not installed |
| 140 |
# or it's up to date) |
| 141 |
|
| 142 |
if statements: |
| 143 |
print(("\n".join(statements)+"\n")) |
| 144 |
return 0 |
| 145 |
|
| 146 |
else: |
| 147 |
return 1 |
| 148 |
|
| 149 |
|
| 150 |
class Upgrader(object): |
| 151 |
"""A specification to upgrade from some schema version to another schema |
| 152 |
version. |
| 153 |
|
| 154 |
Upgraders live as uninstanciated classes. Their version attribute gives the |
| 155 |
version their instructions update *from*; their destination version |
| 156 |
therefore is version+1. |
| 157 |
|
| 158 |
An upgrader can have attributes s_<secno>_<something>, which are |
| 159 |
classmethods(connection) -> None performing schema changes. They |
| 160 |
always need to see if the manipulation already has been performed. |
| 161 |
Use relationExists and/or getColumnNamesFor as appropriate. |
| 162 |
|
| 163 |
An upgrader can have attributes named u_<seqno>_<something>. |
| 164 |
|
| 165 |
These can be |
| 166 |
|
| 167 |
* strings, which are then directly executed in the database (but you |
| 168 |
should only do that for idempotent operations) |
| 169 |
* classmethods, which will be executed in sequence (version, seqno) |
| 170 |
after *all* schemachange methods are executed. |
| 171 |
|
| 172 |
The methods will be called with a connection argument. You |
| 173 |
must not commit this connection. You must not swallow exceptions |
| 174 |
that have left the connection unready (i.e., require a rollback). |
| 175 |
|
| 176 |
Note that if you run rsc.makeData, you MUST pass both |
| 177 |
connection=connection and runCommit=False in order to avoid messing |
| 178 |
people's lives up. |
| 179 |
|
| 180 |
The updaters should have 1-line docstrings explaining what they do. |
| 181 |
|
| 182 |
The update of the schemaversion is done automatically, you don't |
| 183 |
need to worry about it. |
| 184 |
|
| 185 |
Note that whenever you change the schema in a system RD, the |
| 186 |
corresponding upgrader needs to be a schemachange; otherwise, |
| 187 |
it's very likely that any use of the system RD will fail, which |
| 188 |
usually makes an installation not upgradeable without manual |
| 189 |
intervention. |
| 190 |
""" |
| 191 |
version = None |
| 192 |
|
| 193 |
@classmethod |
| 194 |
def updateSchemaversion(cls, connection): |
| 195 |
# no docstring, we output our info ourselves |
| 196 |
showProgress("> update schemaversion to %s..."%(cls.version+1)) |
| 197 |
base.setDBMeta(connection, "schemaversion", cls.version+1) |
| 198 |
|
| 199 |
@classmethod |
| 200 |
def iterNormalActions(cls): |
| 201 |
"""returns strings and classmethods that run non-schemachanging |
| 202 |
actions. |
| 203 |
""" |
| 204 |
for cmdAttr in (s for s in sorted(dir(cls)) if s.startswith("u_")): |
| 205 |
yield getattr(cls, cmdAttr) |
| 206 |
yield cls.updateSchemaversion |
| 207 |
|
| 208 |
@classmethod |
| 209 |
def iterSchemaChanges(cls): |
| 210 |
"""returns classmethods changing the schema as necessary. |
| 211 |
|
| 212 |
These always run before the normal actions. |
| 213 |
""" |
| 214 |
for cmdAttr in (s for s in sorted(dir(cls)) if s.startswith("s_")): |
| 215 |
yield getattr(cls, cmdAttr) |
| 216 |
|
| 217 |
|
| 218 |
class To0Upgrader(Upgrader): |
| 219 |
"""This is executed when there's no schema version defined in the database. |
| 220 |
|
| 221 |
The assumption is that the database reflects the status of 0.8, so |
| 222 |
it adds the author column in dc.services if necessary (which it's |
| 223 |
not if the software has been updated to 0.8.1). |
| 224 |
""" |
| 225 |
version = -1 |
| 226 |
|
| 227 |
@classmethod |
| 228 |
def s_000_addauthor(cls, connection): |
| 229 |
"""add an author column to dc.services if necessary""" |
| 230 |
if "authors" in list(connection.queryToDicts( |
| 231 |
"SELECT * FROM dc.resources LIMIT 1"))[0]: |
| 232 |
return |
| 233 |
connection.query("alter table dc.resources add column authors") |
| 234 |
for sourceRD, resId in connection.query("select sourcrd, resid" |
| 235 |
" from dc.resources"): |
| 236 |
try: |
| 237 |
res = base.getRD(sourceRD).getById(resId) |
| 238 |
authors = "; ".join(m.getContent("text") #noflake: used through locals |
| 239 |
for m in res.iterMeta("creator.name", propagate=True)) |
| 240 |
except: |
| 241 |
# don't worry if fetching authors fails; people will notice... |
| 242 |
pass |
| 243 |
else: |
| 244 |
connection.query("update dc.resources set authors=%(authors)s" |
| 245 |
" where resid=%(resId)s and sourcerd=%(sourceRD)s", |
| 246 |
locals()) |
| 247 |
|
| 248 |
@classmethod |
| 249 |
def u_010_makeMetastore(cls, connection): |
| 250 |
"""create the meta store""" |
| 251 |
td = base.caches.getRD("//dc_tables").getById("metastore") |
| 252 |
rsc.TableForDef(td, create=True, connection=connection) |
| 253 |
|
| 254 |
|
| 255 |
class To1Upgrader(Upgrader): |
| 256 |
version = 0 |
| 257 |
|
| 258 |
@classmethod |
| 259 |
def u_000_update_funcs(cls, connection): |
| 260 |
"""update GAVO server-side functions""" |
| 261 |
rsc.makeData(base.caches.getRD("//adql").getById("make_udfs"), |
| 262 |
connection=connection, runCommit=False) |
| 263 |
|
| 264 |
|
| 265 |
class To2Upgrader(Upgrader): |
| 266 |
version = 1 |
| 267 |
|
| 268 |
@classmethod |
| 269 |
def _upgradeTable(cls, td, colName, connection): |
| 270 |
col = td.getColumnByName(colName) |
| 271 |
if not col.type=='double precision' or not col.xtype=='mjd': |
| 272 |
# this is not done via the mixin, it appears; give up |
| 273 |
return |
| 274 |
|
| 275 |
showProgress(td.getQName()+", ") |
| 276 |
connection.execute("ALTER TABLE %s ALTER COLUMN %s" |
| 277 |
" SET DATA TYPE DOUBLE PRECISION USING ts_to_mjd(%s)"% |
| 278 |
(td.getQName(), colName, colName)) |
| 279 |
rsc.TableForDef(td, connection=connection, create=False |
| 280 |
).updateMeta() |
| 281 |
|
| 282 |
@classmethod |
| 283 |
def u_000_siapDateObsToMJD(cls, connection): |
| 284 |
"""change SIAP and SSAP dateObs columns to MJD""" |
| 285 |
dd = base.caches.getRD("//obscore", doQueries=False |
| 286 |
).getById("refreshAfterSchemaUpdate") |
| 287 |
rsc.makeData(dd, connection=connection) |
| 288 |
|
| 289 |
|
| 290 |
class To3Upgrader(Upgrader): |
| 291 |
version = 2 |
| 292 |
|
| 293 |
@classmethod |
| 294 |
def u_000_tapSchema(cls, connection): |
| 295 |
"""add supportedmodels table to tap_schema""" |
| 296 |
rsc.makeData(base.caches.getRD("//tap").getById("createSchema"), |
| 297 |
connection=connection, runCommit=False) |
| 298 |
|
| 299 |
@classmethod |
| 300 |
def u_010_declareObscoreModel(cls, connection): |
| 301 |
"""declare obscore data model if the obscore table is present""" |
| 302 |
if list(connection.query( |
| 303 |
"select * from dc.tablemeta where tablename='ivoa.ObsCore'")): |
| 304 |
from gavo.protocols import tap |
| 305 |
rd = base.caches.getRD("//obscore") |
| 306 |
tap.publishToTAP(rd, connection) |
| 307 |
else: |
| 308 |
showProgress(" (not present)") |
| 309 |
|
| 310 |
|
| 311 |
class To4Upgrader(Upgrader): |
| 312 |
version = 3 |
| 313 |
|
| 314 |
@classmethod |
| 315 |
def u_000_adqlfunctions(cls, connection): |
| 316 |
"""update ADQL GAVO-defined functions for the postgres planner's benefit""" |
| 317 |
rsc.makeData(base.caches.getRD("//adql").getById("make_udfs"), |
| 318 |
connection=connection, runCommit=False) |
| 319 |
|
| 320 |
|
| 321 |
class To5Upgrader(Upgrader): |
| 322 |
version = 4 |
| 323 |
|
| 324 |
@classmethod |
| 325 |
def u_000_updateObscore(cls, connection): |
| 326 |
"""update obscore to work even when the table is empty""" |
| 327 |
rsc.TableForDef(base.caches.getRD("//obscore").getById("emptyobscore"), |
| 328 |
connection=connection, create=True) |
| 329 |
|
| 330 |
dd = base.caches.getRD("//obscore", doQueries=False |
| 331 |
).getById("refreshAfterSchemaUpdate") |
| 332 |
rsc.makeData(dd, connection=connection) |
| 333 |
|
| 334 |
|
| 335 |
class To6Upgrader(Upgrader): |
| 336 |
version = 5 |
| 337 |
|
| 338 |
@classmethod |
| 339 |
def s_010_addProductColumns(cls, connection): |
| 340 |
"""add preview and datalink columns to dc.products""" |
| 341 |
cols = getColumnNamesFor("//products#products", connection) |
| 342 |
if "preview" not in cols: |
| 343 |
connection.execute("ALTER TABLE dc.products ADD COLUMN" |
| 344 |
" preview TEXT DEFAULT 'AUTO'") |
| 345 |
connection.execute("ALTER TABLE dc.products ALTER COLUMN" |
| 346 |
" preview DROP DEFAULT") |
| 347 |
if "datalink" not in cols: |
| 348 |
connection.execute("ALTER TABLE dc.products ADD COLUMN" |
| 349 |
" datalink TEXT") |
| 350 |
|
| 351 |
@classmethod |
| 352 |
def u_020_remetaObscore(cls, connection): |
| 353 |
"""update obscore metadata to fix the erroneous id""" |
| 354 |
rsc.makeData(base.caches.getRD("//obscore").getById("create"), |
| 355 |
connection=connection, runCommit=False, |
| 356 |
parseOptions=rsc.getParseOptions(metaOnly=True)) |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
class To7Upgrader(Upgrader): |
| 361 |
version = 6 |
| 362 |
|
| 363 |
@classmethod |
| 364 |
def s_010_addPreviewMIMEColumn(cls, connection): |
| 365 |
"""add preview_mime column to dc.products""" |
| 366 |
if not "preview_mime" in getColumnNamesFor( |
| 367 |
"//products#products", connection): |
| 368 |
connection.execute("ALTER TABLE dc.products ADD COLUMN" |
| 369 |
" preview_mime TEXT") |
| 370 |
|
| 371 |
|
| 372 |
class To8Upgrader(Upgrader): |
| 373 |
version = 7 |
| 374 |
|
| 375 |
@classmethod |
| 376 |
def s_010_removeColumnsMeta(cls, connection): |
| 377 |
if relationExists("dc.columnmeta", connection): |
| 378 |
connection.execute("DROP TABLE dc.columnmeta") |
| 379 |
|
| 380 |
|
| 381 |
class To9Upgrader(Upgrader): |
| 382 |
version = 8 |
| 383 |
u_010_chuckADQLPrefix = AnnotatedString("UPDATE TAP_SCHEMA.columns" |
| 384 |
" SET datatype=substring(datatype from 6)" |
| 385 |
" WHERE datatype LIKE 'adql:%%'", |
| 386 |
"Remove adql: prefix in TAP_SCHEMA.columns.datatype") |
| 387 |
u_020_setSize1OnAtoms = AnnotatedString("UPDATE tap_schema.columns" |
| 388 |
" SET \"size\"=1 WHERE NOT datatype LIKE '%%(*)'", |
| 389 |
"Set size=1 in TAP_SCHEMA.columns for atomic types") |
| 390 |
u_030_removeArrayMarkInText = AnnotatedString("UPDATE tap_schema.columns" |
| 391 |
" SET datatype=replace(datatype, '(*)', '') WHERE datatype LIKE '%%(*)'", |
| 392 |
"Turn VARCHAR(*) into simple VARCHAR (size=NULL already set for those)") |
| 393 |
|
| 394 |
|
| 395 |
class To10Upgrader(Upgrader): |
| 396 |
version = 9 |
| 397 |
|
| 398 |
@classmethod |
| 399 |
def u_000_dropADQLExamples(cls, connection): |
| 400 |
"""drop old TAP examples tables (gone to _examples meta)""" |
| 401 |
if relationExists("tap_schema.examples", connection): |
| 402 |
from gavo.user import dropping |
| 403 |
dropping._do_dropTable("tap_schema.examples", connection) |
| 404 |
|
| 405 |
@classmethod |
| 406 |
def u_010_createDLAsyncTable(cls, connection): |
| 407 |
"""import job table for async datalink""" |
| 408 |
if not relationExists("dc.datalinkjobs", connection): |
| 409 |
from gavo import rsc |
| 410 |
rsc.makeData(base.caches.getRD("//datalink").getById("import"), |
| 411 |
connection=connection, runCommit=False) |
| 412 |
|
| 413 |
|
| 414 |
class To11Upgrader(Upgrader): |
| 415 |
version = 10 |
| 416 |
|
| 417 |
@classmethod |
| 418 |
def u_000_findMixedinTables(cls, connection): |
| 419 |
"""inform about tables with non-trivial mixins""" |
| 420 |
# in reality, the mixins that really give us a headache here |
| 421 |
# are the ones mixin in products. Hence, we simply look |
| 422 |
# for tables that have both accref and embargo; that's |
| 423 |
# probably a certain indication. |
| 424 |
|
| 425 |
print("\n!! Important: column sequences" |
| 426 |
" of tables with some mixins have changed.") |
| 427 |
print("!! If this affects you, below commands are shown that will re-import") |
| 428 |
print("!! the affected tables. Some services on top of these tables may") |
| 429 |
print("!! be *broken* until these commands have run.") |
| 430 |
print("!! Sorry for this inconvenience; we hope it won't happen again.\n") |
| 431 |
|
| 432 |
for rdId in registry.findAllRDs(): |
| 433 |
if rdId.startswith("__system"): |
| 434 |
continue |
| 435 |
|
| 436 |
try: |
| 437 |
rd = base.caches.getRD(rdId) |
| 438 |
except: |
| 439 |
# ignore broken RDs -- services there are broken anyway |
| 440 |
continue |
| 441 |
|
| 442 |
ids = set() |
| 443 |
|
| 444 |
for td in rd.tables: |
| 445 |
try: |
| 446 |
td.getColumnByName("accref") and td.getColumnByName("embargo") |
| 447 |
except base.NotFoundError: |
| 448 |
continue # table not affected |
| 449 |
else: |
| 450 |
|
| 451 |
if not rsc.TableForDef(td, connection=connection, create=False |
| 452 |
).exists(): |
| 453 |
continue |
| 454 |
|
| 455 |
# table needs re-importing, see if you can find a corresponding |
| 456 |
# data element |
| 457 |
for dd in rd.dds: |
| 458 |
for make in dd.makes: |
| 459 |
if make.table==td: |
| 460 |
ids.add(dd.id) |
| 461 |
if ids: |
| 462 |
print("dachs imp '%s' %s"%(rd.sourceId, |
| 463 |
" ".join("'%s'"%id for id in ids))) |
| 464 |
|
| 465 |
sys.stderr.write("\nEnd of scan of mixin-affected tables...") |
| 466 |
|
| 467 |
|
| 468 |
class To12Upgrader(Upgrader): |
| 469 |
version = 11 |
| 470 |
|
| 471 |
@classmethod |
| 472 |
def u_010_updateTAPRecord(cls, connection): |
| 473 |
"""prettify the TAP record's IVOID""" |
| 474 |
from gavo.registry import publication |
| 475 |
publication.updateServiceList([base.caches.getRD("//services")], |
| 476 |
connection=connection) |
| 477 |
publication.makeDeletedRecord( |
| 478 |
"ivo://"+base.getConfig("ivoa", "authority")+"/__system__/tap/run", |
| 479 |
connection) |
| 480 |
|
| 481 |
|
| 482 |
class To13Upgrader(Upgrader): |
| 483 |
version = 12 |
| 484 |
@classmethod |
| 485 |
def u_010_updateObscore(cls, connection): |
| 486 |
"""upgrade ivoa.obscore to obscore 1.1. |
| 487 |
""" |
| 488 |
if relationExists("ivoa._obscoresources", connection): |
| 489 |
dd = base.caches.getRD("//obscore", doQueries=False |
| 490 |
).getById("refreshAfterSchemaUpdate") |
| 491 |
rsc.makeData(dd, connection=connection) |
| 492 |
|
| 493 |
|
| 494 |
class To14Upgrader(Upgrader): |
| 495 |
version = 13 |
| 496 |
|
| 497 |
@classmethod |
| 498 |
def s_010_addColIndex(cls, connection): |
| 499 |
"""Adding column_index column to TAP_SCHEMA.columns" |
| 500 |
""" |
| 501 |
dbCols = getColumnNamesFor("//tap#columns", connection) |
| 502 |
if not "column_index" in dbCols: |
| 503 |
connection.execute("ALTER TABLE TAP_SCHEMA.columns" |
| 504 |
" ADD COLUMN column_index SMALLINT") |
| 505 |
if not "arraysize" in dbCols: |
| 506 |
connection.execute("ALTER TABLE TAP_SCHEMA.columns" |
| 507 |
" ADD COLUMN arraysize TEXT") |
| 508 |
if not "xtype" in dbCols: |
| 509 |
connection.execute("ALTER TABLE TAP_SCHEMA.columns" |
| 510 |
" ADD COLUMN xtype TEXT") |
| 511 |
|
| 512 |
|
| 513 |
class To15Upgrader(Upgrader): |
| 514 |
version = 14 |
| 515 |
|
| 516 |
@classmethod |
| 517 |
def s_10_add_uws_creationTime(cls, connection): |
| 518 |
"""adding creationTime to UWS tables for UWS 1.1 support""" |
| 519 |
q = base.UnmanagedQuerier(connection) |
| 520 |
for tableId, tableName in [ |
| 521 |
("//datalink#datalinkjobs", "dc.datalinkjobs"), |
| 522 |
("//uws#userjobs", "uws.userjobs"), |
| 523 |
("//tap#tapjobs", "tap_schema.tapjobs")]: |
| 524 |
if q.getTableType(tableName) is not None: |
| 525 |
if not "creationtime" in getColumnNamesFor(tableId, connection): |
| 526 |
connection.execute("ALTER TABLE %s" |
| 527 |
" ADD COLUMN creationTime TIMESTAMP"%tableName) |
| 528 |
|
| 529 |
|
| 530 |
class To16Upgrader(Upgrader): |
| 531 |
version = 15 |
| 532 |
|
| 533 |
u_010_delete_obscore_1_0_model = AnnotatedString( |
| 534 |
# this purges the old ivoid unconditionally; that's never wrong, |
| 535 |
# because obscore 1.1 has one of its own. But it's also |
| 536 |
# fixing spurious publications where old versions of DaCHS |
| 537 |
# have blindly declared obscore support |
| 538 |
"DELETE FROM tap_schema.supportedmodels" |
| 539 |
" WHERE dmivorn='ivo://ivoa.net/std/ObsCore/v1.0'", |
| 540 |
"Deleting obscore 1.0 model support declaration") |
| 541 |
|
| 542 |
@classmethod |
| 543 |
def s_10_upgrade_tap_schema_tables(cls, connection): |
| 544 |
"""Adding 1.1 columns to TAP_SCHEMA.tables. |
| 545 |
""" |
| 546 |
if not "table_index" in getColumnNamesFor("//tap#tables", connection): |
| 547 |
connection.execute("ALTER TABLE TAP_SCHEMA.tables" |
| 548 |
" ADD COLUMN table_index SMALLINT") |
| 549 |
|
| 550 |
@classmethod |
| 551 |
def s_20_upgrade_tap_schema_columns(cls, connection): |
| 552 |
"""Adding 1.1 columns to TAP_SCHEMA.columns. |
| 553 |
""" |
| 554 |
dbCols = getColumnNamesFor("//tap#columns", connection) |
| 555 |
if not "arraysize" in dbCols: |
| 556 |
connection.execute("ALTER TABLE TAP_SCHEMA.columns" |
| 557 |
" ADD COLUMN arraysize TEXT") |
| 558 |
if not "xtype" in dbCols: |
| 559 |
connection.execute("ALTER TABLE TAP_SCHEMA.columns" |
| 560 |
" ADD COLUMN xtype TEXT") |
| 561 |
|
| 562 |
@classmethod |
| 563 |
def u_50_update_tap_schema(cls, connection): |
| 564 |
"""Filling new TAP_SCHEMA columns""" |
| 565 |
_updateTAP_SCHEMA(connection) |
| 566 |
|
| 567 |
@classmethod |
| 568 |
def u_60_update_obscore_spectra(cls, connection): |
| 569 |
"""Updating obscore-published spectral tables""" |
| 570 |
try: |
| 571 |
from gavo.rscdef import scripting |
| 572 |
script = base.resolveCrossId("//obscore#addTableToObscoreSources") |
| 573 |
script.notify = False |
| 574 |
|
| 575 |
with connection.savepoint(): |
| 576 |
for row in connection.query( |
| 577 |
"SELECT DISTINCT tablename FROM ivoa._obscoresources" |
| 578 |
" WHERE sqlfragment LIKE '%%''spectrum'' AS text) AS dataprod%%'"): |
| 579 |
table = rsc.TableForDef( |
| 580 |
base.getTableDefForTable(connection, row[0]), |
| 581 |
connection=connection) |
| 582 |
showProgress(" "+row[0]) |
| 583 |
scripting.PythonScriptRunner(script).run(table) |
| 584 |
|
| 585 |
dd = base.caches.getRD("//obscore", doQueries=False |
| 586 |
).getById("create") |
| 587 |
rsc.makeData(dd, connection=connection, runCommit=False) |
| 588 |
|
| 589 |
except Exception: |
| 590 |
# probably there is no obscore table on this installation. |
| 591 |
showProgress("(skipped)") |
| 592 |
|
| 593 |
|
| 594 |
class To17Upgrader(Upgrader): |
| 595 |
version = 16 |
| 596 |
|
| 597 |
@classmethod |
| 598 |
def u_10_import_adql(cls, connection): |
| 599 |
"""loading new ADQL UDFs""" |
| 600 |
dd = base.caches.getRD("//adql", doQueries=False |
| 601 |
).getById("make_udfs") |
| 602 |
rsc.makeData(dd, connection=connection, runCommit=False) |
| 603 |
|
| 604 |
@classmethod |
| 605 |
def u_20_reload_tap_schema(cls, connection): |
| 606 |
"""updating tap_schema.schemas conflict rule""" |
| 607 |
dd = base.caches.getRD("//tap", doQueries=False |
| 608 |
).getById("createSchema") |
| 609 |
rsc.makeData(dd, connection=connection, runCommit=False, |
| 610 |
parseOptions=rsc.getParseOptions(metaOnly=False, systemImport=False)) |
| 611 |
|
| 612 |
@classmethod |
| 613 |
def u_30_update_obscore_meta(cls, connection): |
| 614 |
"""updating obscore metadata""" |
| 615 |
if relationExists("ivoa._obscoresources", connection): |
| 616 |
dd = base.caches.getRD("//obscore", doQueries=False |
| 617 |
).getById("create") |
| 618 |
rsc.makeData(dd, connection=connection, runCommit=False, |
| 619 |
parseOptions=rsc.getParseOptions(metaOnly=False, systemImport=True)) |
| 620 |
|
| 621 |
|
| 622 |
class To18Upgrader(Upgrader): |
| 623 |
version = 17 |
| 624 |
|
| 625 |
@classmethod |
| 626 |
def u_10_restoreTAP_SCHEMA(cls, connection): |
| 627 |
"""recreating a possibly damaged TAP_SCHEMA""" |
| 628 |
_updateTAP_SCHEMA(connection) |
| 629 |
|
| 630 |
|
| 631 |
class To19Upgrader(Upgrader): |
| 632 |
version = 18 |
| 633 |
|
| 634 |
@classmethod |
| 635 |
def s_10_add_schema_index(cls, connection): |
| 636 |
"""adding schema_index to tap_schema.schemas""" |
| 637 |
if not "schema_index" in getColumnNamesFor("//tap#schemas", connection): |
| 638 |
connection.execute("ALTER TABLE tap_schema.schemas ADD COLUMN" |
| 639 |
" schema_index INTEGER") |
| 640 |
|
| 641 |
@classmethod |
| 642 |
def u_20_add_tap_schema_keys(cls, connection): |
| 643 |
"""re-importing TAP_SCHEMA to update foreign key declarations""" |
| 644 |
_updateTAP_SCHEMA(connection) |
| 645 |
|
| 646 |
|
| 647 |
class To20Upgrader(Upgrader): |
| 648 |
version = 19 |
| 649 |
|
| 650 |
@classmethod |
| 651 |
def u_10_update_obscore_meta(cls, connection): |
| 652 |
"""updating obscore metadata""" |
| 653 |
if (relationExists("ivoa._obscoresources", connection) |
| 654 |
and relationExists("ivoa.emptyobscore", connection)): |
| 655 |
dd = base.caches.getRD("//obscore", doQueries=False |
| 656 |
).getById("refreshAfterSchemaUpdate") |
| 657 |
rsc.makeData(dd, connection=connection) |
| 658 |
|
| 659 |
@classmethod |
| 660 |
def u_20_import_adql(cls, connection): |
| 661 |
"""updating ADQL UDFs""" |
| 662 |
dd = base.caches.getRD("//adql", doQueries=False |
| 663 |
).getById("make_udfs") |
| 664 |
rsc.makeData(dd, connection=connection, runCommit=False) |
| 665 |
|
| 666 |
|
| 667 |
class To21Upgrader(Upgrader): |
| 668 |
version = 20 |
| 669 |
|
| 670 |
@classmethod |
| 671 |
def s_10_update_obscoresources(cls, connection): |
| 672 |
"""adding sourcerd column to obscore sources table""" |
| 673 |
if (relationExists("ivoa._obscoresources", connection) |
| 674 |
and not "sourcerd" in |
| 675 |
getColumnNamesFor("//obscore#_obscoresources", connection)): |
| 676 |
connection.execute("ALTER TABLE ivoa._obscoresources" |
| 677 |
" ADD COLUMN sourcerd TEXT") |
| 678 |
|
| 679 |
@classmethod |
| 680 |
def u_20_fill_obscoresources_sourcerd(cls, connection): |
| 681 |
"""filling sourcerd column in obscore sources""" |
| 682 |
if relationExists("ivoa._obscoresources", connection): |
| 683 |
for row in connection.query("SELECT tableName FROM" |
| 684 |
" ivoa._obscoresources"): |
| 685 |
tableName = row[0] |
| 686 |
matches = list(connection.query("SELECT sourcerd FROM dc.tablemeta" |
| 687 |
" WHERE tablename=%(tableName)s", locals())) |
| 688 |
if not matches: |
| 689 |
# table dropped, drop it from obscoresources |
| 690 |
connection.execute("DELETE FROM ivoa._obscoresources" |
| 691 |
" WHERE tablename=%(tableName)s", locals()) |
| 692 |
else: |
| 693 |
sourceRD = matches[0][0] |
| 694 |
connection.execute("UPDATE ivoa._obscoresources" |
| 695 |
" SET sourcerd=%(sourceRD)s" |
| 696 |
" WHERE tablename=%(tableName)s", locals()) |
| 697 |
|
| 698 |
|
| 699 |
class To22Upgrader(Upgrader): |
| 700 |
version = 21 |
| 701 |
|
| 702 |
@classmethod |
| 703 |
def s_10_add_nrows_to_tablemeta(cls, connection): |
| 704 |
"""adding nrows to dc.tablemeta""" |
| 705 |
if "nrows" not in getColumnNamesFor("//dc_tables#tablemeta", connection): |
| 706 |
connection.execute("ALTER TABLE dc.tablemeta ADD COLUMN" |
| 707 |
" nrows BIGINT") |
| 708 |
|
| 709 |
|
| 710 |
class To23Upgrader(Upgrader): |
| 711 |
version = 22 |
| 712 |
|
| 713 |
@classmethod |
| 714 |
def u_10_add_and_fill_rds(cls, connection): |
| 715 |
"""creating and filling dc.rdmeta""" |
| 716 |
rds = [] |
| 717 |
for rdId in registry.findAllRDs(): |
| 718 |
try: |
| 719 |
rds.append(("bootstrap", base.caches.getRD(rdId))) |
| 720 |
except Exception: |
| 721 |
# let's just ignore bad RDs; if people haven't noticed yet |
| 722 |
# this is not the time to tell them. |
| 723 |
pass |
| 724 |
|
| 725 |
rsc.makeData(base.resolveCrossId("//rds#update-for-rd"), |
| 726 |
forceSource=rsc.MultiForcedSources(rds), connection=connection) |
| 727 |
|
| 728 |
|
| 729 |
class To24Upgrader(Upgrader): |
| 730 |
version = 23 |
| 731 |
|
| 732 |
@classmethod |
| 733 |
def u_000_update_funcs(cls, connection): |
| 734 |
"""update GAVO server-side functions""" |
| 735 |
rsc.makeData(base.caches.getRD("//adql").getById("make_udfs"), |
| 736 |
connection=connection, runCommit=False) |
| 737 |
|
| 738 |
|
| 739 |
class To25Upgrader(Upgrader): |
| 740 |
version = 24 |
| 741 |
|
| 742 |
@classmethod |
| 743 |
def u_000_hash_passwords(cls, connection): |
| 744 |
"""scrypt-hashing existing passwords""" |
| 745 |
from gavo.protocols import creds |
| 746 |
updates = [] |
| 747 |
for username, password in connection.query( |
| 748 |
"SELECT username, password FROM dc.users"): |
| 749 |
if not re.match("[a-z]+:", password): |
| 750 |
updates.append((username, creds.hashPassword(password))) |
| 751 |
|
| 752 |
for username, hash in updates: |
| 753 |
connection.execute("UPDATE dc.users SET password=%(hash)s" |
| 754 |
" WHERE username=%(username)s", locals()) |
| 755 |
|
| 756 |
|
| 757 |
class To26Upgrader(Upgrader): |
| 758 |
version = 25 |
| 759 |
|
| 760 |
# float manipulations are no longer necessary |
| 761 |
|
| 762 |
|
| 763 |
class To27Upgrader(Upgrader): |
| 764 |
version = 26 |
| 765 |
|
| 766 |
@classmethod |
| 767 |
def s_010_create_col_stats_table(cls, connection): |
| 768 |
"""Creating the dc.simple_col_stats table""" |
| 769 |
if not relationExists("dc.simple_col_stats", connection): |
| 770 |
# this will first make tableName alone a primary key on dc.tablemeta |
| 771 |
tablemeta = rsc.TableForDef( |
| 772 |
base.resolveCrossId("//dc_tables#tablemeta"), |
| 773 |
connection=connection) |
| 774 |
tablemeta.updateMeta() |
| 775 |
tablemeta._dropPrimaryKey() |
| 776 |
tablemeta.makeIndices() |
| 777 |
|
| 778 |
rsc.makeData(base.resolveCrossId("//dc_tables#import"), |
| 779 |
connection=connection, runCommit=False) |
| 780 |
|
| 781 |
@classmethod |
| 782 |
def s_020_add_coverage_to_tablemeta(cls, connection): |
| 783 |
"""Adding a coverage column to rdmeta""" |
| 784 |
cols = getColumnNamesFor("//rds#rdmeta", connection) |
| 785 |
if "spatial" not in cols: |
| 786 |
connection.execute( |
| 787 |
"ALTER TABLE dc.rdmeta ADD COLUMN spatial TEXT") |
| 788 |
connection.execute( |
| 789 |
"ALTER TABLE dc.rdmeta ADD COLUMN temporal double precision[]") |
| 790 |
connection.execute( |
| 791 |
"ALTER TABLE dc.rdmeta ADD COLUMN spectral double precision[]") |
| 792 |
|
| 793 |
@classmethod |
| 794 |
def u_30_update_obscore_meta(cls, connection): |
| 795 |
"""Re-import obscore metadata to update for UCD erratum""" |
| 796 |
if list(connection.query( |
| 797 |
"select * from dc.tablemeta where tablename ILIKE 'ivoa.ObsCore'")): |
| 798 |
from gavo.protocols import tap |
| 799 |
rd = base.caches.getRD("//obscore") |
| 800 |
tap.publishToTAP(rd, connection) |
| 801 |
else: |
| 802 |
showProgress(" (not present)") |
| 803 |
|
| 804 |
|
| 805 |
class To28Upgrader(Upgrader): |
| 806 |
version = 27 |
| 807 |
|
| 808 |
@classmethod |
| 809 |
def u_10_update_TAP_SCHEMA_META(cls, connection): |
| 810 |
"""Declaring TAP_SCHEMA descriptions as unicode""" |
| 811 |
from gavo.protocols import tap |
| 812 |
tap._insertRDIntoTAP_SCHEMA(rscdesc.getRD("//tap"), connection) |
| 813 |
|
| 814 |
|
| 815 |
class To29Upgrade(Upgrader): |
| 816 |
version = 28 |
| 817 |
|
| 818 |
@classmethod |
| 819 |
def u_10_update_EPNTAP_meta(cls, connection): |
| 820 |
"""Updating EPNTAP2 metadata where necessary and possible""" |
| 821 |
rds = set() |
| 822 |
for td in iterEPNTAPTables(connection): |
| 823 |
try: |
| 824 |
rds.add(td.rd.sourceId) |
| 825 |
table = rsc.DBTable(td, connection=connection) |
| 826 |
table.updateMeta() |
| 827 |
except Exception as msg: |
| 828 |
base.ui.notifyWarning("Could not update EPN-TAP table" |
| 829 |
f" {td.getQName()}: {msg}. Please fix manually.") |
| 830 |
|
| 831 |
from gavo.protocols import tap |
| 832 |
|
| 833 |
for rdId in rds: |
| 834 |
try: |
| 835 |
tap.publishToTAP( |
| 836 |
base.caches.getRD(rdId), connection) |
| 837 |
except Exception as msg: |
| 838 |
base.ui.notifyWarning("Could not update TAP metadata for" |
| 839 |
f" {rdId}: {msg}. Please fix manually.") |
| 840 |
|
| 841 |
|
| 842 |
class To30Upgrader(Upgrader): |
| 843 |
version = 29 |
| 844 |
|
| 845 |
@classmethod |
| 846 |
def u_10_make_stats_index(cls, connection): |
| 847 |
"""Updating indices on dc.simple_col_stats""" |
| 848 |
table = rsc.TableForDef( |
| 849 |
base.resolveCrossId("//dc_tables#simple_col_stats"), |
| 850 |
connection=connection) |
| 851 |
table.dropIndices() |
| 852 |
table.makeIndices() |
| 853 |
|
| 854 |
@classmethod |
| 855 |
def u_20_create_discrete_text_values(cls, connection): |
| 856 |
"""Creating dc.discrete_string_values""" |
| 857 |
# do this |
| 858 |
rsc.TableForDef( |
| 859 |
base.resolveCrossId("//dc_tables#discrete_string_values"), |
| 860 |
connection=connection, |
| 861 |
create=True) |
| 862 |
|
| 863 |
|
| 864 |
class To31Upgrader(Upgrader): |
| 865 |
version = 30 |
| 866 |
|
| 867 |
@classmethod |
| 868 |
def u_10_update_ssa_fov_ucd(cls, connection): |
| 869 |
"""Updating SSA published metadata for SSA 1.1 erratum 2 (instr.fov)""" |
| 870 |
# Well, SSA metadata will actually fix itself (we might consider |
| 871 |
# re-publishing the services so their tableset gets updated, but |
| 872 |
# I'd say that's overkill. Instead, let's just fix whatever |
| 873 |
# has a problem in the TAP_SCHEMA. |
| 874 |
from gavo.protocols import tap |
| 875 |
for rdId, in connection.query( |
| 876 |
"SELECT DISTINCT sourcerd FROM tap_schema.columns" |
| 877 |
" WHERE ucd='instr.fov'"): |
| 878 |
tap.publishToTAP(base.caches.getRD(rdId), connection) |
| 879 |
|
| 880 |
|
| 881 |
class To32Upgrader(Upgrader): |
| 882 |
version = 31 |
| 883 |
|
| 884 |
@classmethod |
| 885 |
def u_10_update_obscore_defs(cls, connection): |
| 886 |
"""updating obscore definitions to use reals for em_x""" |
| 887 |
if relationExists("ivoa._obscoresources", connection): |
| 888 |
dd = base.caches.getRD("//obscore", doQueries=False |
| 889 |
).getById("refreshAfterSchemaUpdate") |
| 890 |
rsc.makeData(dd, connection=connection) |
| 891 |
|
| 892 |
|
| 893 |
class To33Upgrader(Upgrader): |
| 894 |
version = 32 |
| 895 |
|
| 896 |
@classmethod |
| 897 |
def s_010_add_nrows_in_tap_schema(cls, connection): |
| 898 |
"""adding nrows to tap_schema.tables""" |
| 899 |
if "nrows" not in getColumnNamesFor("//tap#tables", connection): |
| 900 |
connection.execute("ALTER TABLE tap_schema.tables" |
| 901 |
" ADD COLUMN nrows BIGINT") |
| 902 |
|
| 903 |
@classmethod |
| 904 |
def u_020_fill_tap_schema(cls, connection): |
| 905 |
"""updating tap_schema to include nrows""" |
| 906 |
_updateTAP_SCHEMA(connection) |
| 907 |
|
| 908 |
|
| 909 |
class To34Upgrader(Upgrader): |
| 910 |
version = 33 |
| 911 |
@classmethod |
| 912 |
def u_000_update_funcs(cls, connection): |
| 913 |
"""update GAVO server-side functions""" |
| 914 |
rsc.makeData(base.caches.getRD("//adql").getById("make_udfs"), |
| 915 |
connection=connection, runCommit=False) |
| 916 |
|
| 917 |
|
| 918 |
class To35Upgrader(Upgrader): |
| 919 |
version = 34 |
| 920 |
@classmethod |
| 921 |
def s_000_add_rank_column(cls, connection): |
| 922 |
"""add rank column to the resource list""" |
| 923 |
if "rank" not in getColumnNamesFor("//services#resources", connection): |
| 924 |
connection.execute("ALTER TABLE dc.resources" |
| 925 |
" ADD COLUMN rank INTEGER") |
| 926 |
|
| 927 |
@classmethod |
| 928 |
def s_010_refresh_resources_join(cls, connection): |
| 929 |
"""refreshing the dc.resources_join view""" |
| 930 |
rsc.makeData( |
| 931 |
base.resolveCrossId("//services#views"), |
| 932 |
connection=connection) |
| 933 |
|
| 934 |
# we will rely on the upsert trigger, and some versions of DaCHS |
| 935 |
# did this in a way that is no longer reliable. So: update |
| 936 |
# this at least on //services#resources before going on |
| 937 |
rsc.TableForDef( |
| 938 |
base.resolveCrossId("//services#resources"), |
| 939 |
connection=connection).createUniquenessRules() |
| 940 |
|
| 941 |
@classmethod |
| 942 |
def u_000_fill_rank(cls, connection): |
| 943 |
"""filling rank column for published RDs""" |
| 944 |
from gavo.registry import publication |
| 945 |
publication.updateServiceList( |
| 946 |
publication.getRDs( |
| 947 |
publication.findPublishedRDs()), |
| 948 |
keepTimestamp=True) |
| 949 |
|
| 950 |
|
| 951 |
class To36Upgrader(Upgrader): |
| 952 |
version = 35 |
| 953 |
@classmethod |
| 954 |
def u_000_fitsInProducts(cls, connection): |
| 955 |
"""updating FITS media type in dc.products""" |
| 956 |
connection.execute("update dc.products set mime='application/fits'" |
| 957 |
" where mime='image/fits'") |
| 958 |
|
| 959 |
@classmethod |
| 960 |
def u_010_fitsInInstanceTables(cls, connection): |
| 961 |
"""updating FITS media type in products tables""" |
| 962 |
for rdId in registry.findPublishedRDs(): |
| 963 |
try: |
| 964 |
rd = base.caches.getRD(rdId) |
| 965 |
except: |
| 966 |
showProgress(f"<{rdId} broken, skipping>") |
| 967 |
|
| 968 |
for table in rd.tables: |
| 969 |
if not table.onDisk or table.viewStatement: |
| 970 |
continue |
| 971 |
|
| 972 |
tableName = table.getQName() |
| 973 |
if not relationExists(tableName, connection): |
| 974 |
continue |
| 975 |
|
| 976 |
if "mime" in table: |
| 977 |
showProgress(" "+table.getQName()) |
| 978 |
# it's surprisingly hard to figure out whether a table mixes |
| 979 |
# in //products#table because mixesIn does not work |
| 980 |
# transitively. Having a column mime, however, ought to |
| 981 |
# be about as good. |
| 982 |
connection.execute(f"update {table.getQName()}" |
| 983 |
" set mime='application/fits' where mime='image/fits'") |
| 984 |
|
| 985 |
|
| 986 |
def iterStatements(startVersion, endVersion=CURRENT_SCHEMAVERSION, |
| 987 |
upgraders=None): |
| 988 |
"""yields all upgraders from startVersion to endVersion in sequence. |
| 989 |
""" |
| 990 |
toRun = [] |
| 991 |
for upgrader in utils.iterDerivedClasses(Upgrader, |
| 992 |
upgraders or list(globals().values())): |
| 993 |
if startVersion<=upgrader.version<endVersion: |
| 994 |
toRun.append(upgrader) |
| 995 |
toRun.sort(key=lambda upgrader: upgrader.version) |
| 996 |
|
| 997 |
schemaUpgraded = False |
| 998 |
for upgrader in toRun: |
| 999 |
for statement in upgrader.iterSchemaChanges(): |
| 1000 |
yield statement |
| 1001 |
schemaUpgraded = True |
| 1002 |
|
| 1003 |
# schema changes must be written idempotently, so we can |
| 1004 |
# commit here without damaging later runs. |
| 1005 |
if schemaUpgraded: |
| 1006 |
yield _COMMIT |
| 1007 |
|
| 1008 |
for upgrader in toRun: |
| 1009 |
for statement in upgrader.iterNormalActions(): |
| 1010 |
yield statement |
| 1011 |
yield _COMMIT |
| 1012 |
|
| 1013 |
|
| 1014 |
def upgrade(forceDBVersion=None): |
| 1015 |
"""runs all updates necessary to bring a database to the |
| 1016 |
CURRENT_SCHEMAVERSION. |
| 1017 |
|
| 1018 |
Unless catastrophic things go on, each upgrade is a transaction |
| 1019 |
of its own; the first failed transaction stops the upgrade at the |
| 1020 |
version last successfully upgraded to. |
| 1021 |
""" |
| 1022 |
if forceDBVersion is None: |
| 1023 |
startVersion = getDBSchemaVersion() |
| 1024 |
else: |
| 1025 |
startVersion = forceDBVersion |
| 1026 |
|
| 1027 |
with base.getWritableAdminConn() as conn: |
| 1028 |
for statement in iterStatements(startVersion, CURRENT_SCHEMAVERSION): |
| 1029 |
if statement is _COMMIT: |
| 1030 |
showProgress("> committing changes...") |
| 1031 |
conn.commit() |
| 1032 |
|
| 1033 |
elif callable(statement): |
| 1034 |
if statement.__doc__: |
| 1035 |
showProgress("> %s..."%statement.__doc__) |
| 1036 |
# if no docstring is present, we assume the function will output |
| 1037 |
# custom user feedback |
| 1038 |
statement(conn) |
| 1039 |
|
| 1040 |
else: |
| 1041 |
showProgress("> "+getattr(statement, "annotation", |
| 1042 |
"executing %s"%utils.makeEllipsis(statement, 60))+"... ") |
| 1043 |
conn.execute(statement) |
| 1044 |
showProgress(" ok\n") |
| 1045 |
|
| 1046 |
# other chores; add them below for the time being. |
| 1047 |
clearThirdPartyCache() |
| 1048 |
|
| 1049 |
|
| 1050 |
################## misc chores |
| 1051 |
# if we get more of those, I guess we ought to have |
| 1052 |
# a module for those and report on them |
| 1053 |
def clearThirdPartyCache(): |
| 1054 |
from gavo.web import ifpages |
| 1055 |
tpcDir = ifpages.ThirdPartyCachePage.cacheDir |
| 1056 |
if tpcDir.is_dir(): |
| 1057 |
for fName in tpcDir.iterdir(): |
| 1058 |
path = tpcDir / fName |
| 1059 |
if path.is_file(): |
| 1060 |
path.unlink() |
| 1061 |
|
| 1062 |
|
| 1063 |
def parseCommandLine(): |
| 1064 |
import argparse |
| 1065 |
parser = argparse.ArgumentParser() |
| 1066 |
parser.add_argument("--force-dbversion", help="assume this as the" |
| 1067 |
" database's schema version. If you don't develop DaCHS, you" |
| 1068 |
" almost certainly should stay clear of this flag", type=int, |
| 1069 |
dest="forceDBVersion", default=None) |
| 1070 |
parser.add_argument("-e", "--get-extension-script", |
| 1071 |
help="Dump a script to update DaCHS-managed extensions (will" |
| 1072 |
" print nothing if no extensions need updating). This will return" |
| 1073 |
" 0 if material was written, 1 otherwise.", |
| 1074 |
dest="dumpExtScript", action="store_true") |
| 1075 |
parser.add_argument("-t", "--update-tap-schema", |
| 1076 |
help="Force the re-generation of the TAP schema. This is basic" |
| 1077 |
" housekeeping that's perhaps smart to do on the occasion of" |
| 1078 |
" an upgrade.", |
| 1079 |
dest="updateTAPSchema", action="store_true") |
| 1080 |
return parser.parse_args() |
| 1081 |
|
| 1082 |
|
| 1083 |
def main(): |
| 1084 |
args = parseCommandLine() |
| 1085 |
if args.dumpExtScript: |
| 1086 |
with base.getTableConn() as conn: |
| 1087 |
sys.exit(dumpExtensionUpdater(conn)) |
| 1088 |
else: |
| 1089 |
with base.ui.suspended("ScriptRunning"): |
| 1090 |
upgrade(args.forceDBVersion) |
| 1091 |
|
| 1092 |
if args.updateTAPSchema: |
| 1093 |
with base.getWritableAdminConn() as conn: |
| 1094 |
_updateTAP_SCHEMA(conn) |