def deleteChunk(dbName, tblName, chunkId): """ Delete chunk from a table, both chunk data and overlap data is dropped. """ _log.debug('request: %s', request) _log.debug('request.form: %s', request.form) _log.debug('DELETE => delete chunk') # validate params _validateDbName(dbName) _validateTableName(tblName) if chunkId < 0: raise ExceptionResponse(400, "InvalidArgument", "Chunk ID argument is negative") # check that table exists with Config.instance().dbEngine().begin() as dbConn: if not utils.tableExists(dbConn, tblName, dbName): raise ExceptionResponse(404, "TableMissing", "Table %s.%s does not exist" % (dbName, tblName)) chunkRepr = None # chunk data table # TODO: we need some central location for things like this table = tblName + '_' + str(chunkId) if utils.tableExists(dbConn, table, dbName): # drop table _log.debug('drop chunk table: %s', table) utils.dropTable(dbConn, table, dbName) chunkRepr = _chunkDict(dbName, tblName, chunkId) chunkRepr['chunkTable'] = True # overlap data table # TODO: we need some central location for things like this table = tblName + 'FullOverlap_' + str(chunkId) if utils.tableExists(dbConn, table, dbName): # drop table _log.debug('drop chunk table: %s', table) utils.dropTable(dbConn, table, dbName) if chunkRepr is not None: chunkRepr['overlapTable'] = True else: # chunk data table is missing _log.error('Chunk does not exist, but overlap does') raise ExceptionResponse(404, "ChunkDeleteFailed", "Cannot delete chunk data table", "Chunk %s is not found for table %s.%s " "(but overlap table was deleted)" % (chunkId, dbName, tblName)) if chunkRepr is None: # nothing found _log.error('Chunk does not exist') raise ExceptionResponse(404, "ChunkDeleteFailed", "Cannot delete chunk data table", "Chunk %s is not found for table %s.%s" % (chunkId, dbName, tblName)) return json.jsonify(result=chunkRepr)
def deleteChunk(dbName, tblName, chunkId): """ Delete chunk from a table, both chunk data and overlap data is dropped. """ _log.debug('request: %s', request) _log.debug('request.form: %s', request.form) _log.debug('DELETE => delete chunk') # validate params _validateDbName(dbName) _validateTableName(tblName) if chunkId < 0: raise ExceptionResponse(400, "InvalidArgument", "Chunk ID argument is negative") # check that table exists dbConn = Config.instance().dbEngine().connect() if not utils.tableExists(dbConn, tblName, dbName): raise ExceptionResponse(404, "TableMissing", "Table %s.%s does not exist" % (dbName, tblName)) chunkRepr = None # chunk data table # TODO: we need some central location for things like this table = tblName + '_' + str(chunkId) if utils.tableExists(dbConn, table, dbName): # drop table _log.debug('drop chunk table: %s', table) utils.dropTable(dbConn, table, dbName) chunkRepr = _chunkDict(dbName, tblName, chunkId) chunkRepr['chunkTable'] = True # overlap data table # TODO: we need some central location for things like this table = tblName + 'FullOverlap_' + str(chunkId) if utils.tableExists(dbConn, table, dbName): # drop table _log.debug('drop chunk table: %s', table) utils.dropTable(dbConn, table, dbName) if chunkRepr is not None: chunkRepr['overlapTable'] = True else: # chunk data table is missing _log.error('Chunk does not exist, but overlap does') raise ExceptionResponse(404, "ChunkDeleteFailed", "Cannot delete chunk data table", "Chunk %s is not found for table %s.%s (but overlap table was deleted)" % (chunkId, dbName, tblName)) if chunkRepr is None: # nothing found _log.error('Chunk does not exist') raise ExceptionResponse(404, "ChunkDeleteFailed", "Cannot delete chunk data table", "Chunk %s is not found for table %s.%s" % (chunkId, dbName, tblName)) return json.jsonify(result=chunkRepr)
def deleteTable(dbName, tblName): """ Drop a table and optionally all chunk/overlap tables. Following parameters are expected to come in a request (in query string): dropChunks: boolean flag, false by default, accepted values: '0', '1', 'yes', 'no', 'false', 'true' """ _log.debug('request: %s', request) _log.debug('DELETE => drop table') # validate names _validateDbName(dbName) _validateTableName(tblName) # get options dropChunks = _getArgFlag(request.args, 'dropChunks', False) _log.debug('dropChunks: %s', dropChunks) dbConn = Config.instance().dbEngine().connect() if not utils.dbExists(dbConn, dbName): raise ExceptionResponse(404, "DatabaseMissing", "Database %s does not exist" % dbName) try: # drop chunks first nChunks = 0 if dropChunks: # regexp matching all chunk table names tblRe = re.compile('^' + tblName + '(FullOverlap)?_[0-9]+$') for table in utils.listTables(dbConn, dbName): if tblRe.match(table): _log.debug('dropping chunk table %s.%s', dbName, table) utils.dropTable(dbConn, table, dbName) nChunks += 1 # drop main table _log.debug('dropping main table %s.%s', dbName, tblName) utils.dropTable(dbConn, tblName, dbName) except NoSuchTableError as exc: _log.error('Exception when dropping table: %s', exc) chunkMsg = "" if nChunks: chunkMsg = ", but {0} chunk tables have been dropped".format( nChunks) raise ExceptionResponse( 404, "TableMissing", "Table %s.%s does not exist%s" % (dbName, tblName, chunkMsg)) return json.jsonify(result=_tblDict(dbName, tblName))
def deleteTable(dbName, tblName): """ Drop a table and optionally all chunk/overlap tables. Following parameters are expected to come in a request (in query string): dropChunks: boolean flag, false by default, accepted values: '0', '1', 'yes', 'no', 'false', 'true' """ _log.debug('request: %s', request) _log.debug('DELETE => drop table') # validate names _validateDbName(dbName) _validateTableName(tblName) # get options dropChunks = _getArgFlag(request.args, 'dropChunks', False) _log.debug('dropChunks: %s', dropChunks) dbConn = Config.instance().dbEngine().connect() if not utils.dbExists(dbConn, dbName): raise ExceptionResponse(404, "DatabaseMissing", "Database %s does not exist" % dbName) try: # drop chunks first nChunks = 0 if dropChunks: # regexp matching all chunk table names tblRe = re.compile('^' + tblName + '(FullOverlap)?_[0-9]+$') for table in utils.listTables(dbConn, dbName): if tblRe.match(table): _log.debug('dropping chunk table %s.%s', dbName, table) utils.dropTable(dbConn, table, dbName) nChunks += 1 # drop main table _log.debug('dropping main table %s.%s', dbName, tblName) utils.dropTable(dbConn, tblName, dbName) except NoSuchTableError as exc: _log.error('Exception when dropping table: %s', exc) chunkMsg = "" if nChunks: chunkMsg = ", but {0} chunk tables have been dropped".format(nChunks) raise ExceptionResponse(404, "TableMissing", "Table %s.%s does not exist%s" % (dbName, tblName, chunkMsg)) return json.jsonify(result=_tblDict(dbName, tblName))
def testDropTable(self): conn = self._engine.connect() # using current db utils.createDb(conn, self._dbA) utils.useDb(conn, self._dbA) utils.createTable(conn, "t2", "(i int)") utils.dropTable(conn, "t2") utils.dropTable(conn, "t2", mustExist=False) self.assertRaises(sqlalchemy.exc.NoSuchTableError, utils.dropTable, conn, "t2") utils.dropDb(conn, self._dbA) # using no current db utils.createDb(conn, self._dbB) utils.createTable(conn, "t2", "(i int)", self._dbB) utils.dropTable(conn, "t2", dbName=self._dbB) utils.dropTable(conn, "t2", dbName=self._dbB, mustExist=False) self.assertRaises(sqlalchemy.exc.NoSuchTableError, utils.dropTable, conn, "t2", self._dbB) utils.dropDb(conn, self._dbB) # mix of current and not current db utils.createDb(conn, self._dbA) utils.createDb(conn, self._dbB) utils.useDb(conn, self._dbA) utils.createTable(conn, "t2", "(i int)", self._dbB) utils.createTable(conn, "t2", "(i int)") utils.dropTable(conn, "t2") utils.dropTable(conn, "t2", dbName=self._dbB) utils.dropTable(conn, "t2", mustExist=False) utils.dropTable(conn, "t2", dbName=self._dbB, mustExist=False) self.assertRaises(sqlalchemy.exc.NoSuchTableError, utils.dropTable, conn, "t2") self.assertRaises(sqlalchemy.exc.NoSuchTableError, utils.dropTable, conn, "t2", self._dbB) utils.dropDb(conn, self._dbA) utils.dropDb(conn, self._dbB) conn.close()
def testDropTable(self): conn = getEngineFromArgs( username=self._user, password=self._pass, host=self._host, port=self._port, query={"unix_socket": self._sock}).connect() # using current db utils.createDb(conn, self._dbA) utils.useDb(conn, self._dbA) utils.createTable(conn, "t2", "(i int)") utils.dropTable(conn, "t2") utils.dropTable(conn, "t2", mustExist=False) self.assertRaises(sqlalchemy.exc.NoSuchTableError, utils.dropTable, conn, "t2") utils.dropDb(conn, self._dbA) # using no current db utils.createDb(conn, self._dbB) utils.createTable(conn, "t2", "(i int)", self._dbB) utils.dropTable(conn, "t2", dbName=self._dbB) utils.dropTable(conn, "t2", dbName=self._dbB, mustExist=False) self.assertRaises(sqlalchemy.exc.NoSuchTableError, utils.dropTable, conn, "t2", self._dbB) utils.dropDb(conn, self._dbB) # mix of current and not current db utils.createDb(conn, self._dbA) utils.createDb(conn, self._dbB) utils.useDb(conn, self._dbA) utils.createTable(conn, "t2", "(i int)", self._dbB) utils.createTable(conn, "t2", "(i int)") utils.dropTable(conn, "t2") utils.dropTable(conn, "t2", dbName=self._dbB) utils.dropTable(conn, "t2", mustExist=False) utils.dropTable(conn, "t2", dbName=self._dbB, mustExist=False) self.assertRaises(sqlalchemy.exc.NoSuchTableError, utils.dropTable, conn, "t2") self.assertRaises(sqlalchemy.exc.NoSuchTableError, utils.dropTable, conn, "t2", self._dbB) utils.dropDb(conn, self._dbA) utils.dropDb(conn, self._dbB) conn.close()