Esempio n. 1
0
    def getSqlFromVersion(self, version: int):
        """
        Return sql code from defined version (with version)
        If file dont exist the return empty string
        :param version: integer of version
        :return: string
        """
        if self.__isFileLogExist() != True:
            raise FileException("File \'" + self._filePath +
                                "\' is not exists")

        list = self.__getAllFileContentByLine()
        numLine = -1
        startLine = len(list)
        check = False

        # find start version
        for key, line in enumerate(list):
            index = line.find("version:")
            if index != -1:
                check = True
                try:
                    versionStr = line[index + 8:]
                    versionInt = int(versionStr)
                    if versionInt >= version:
                        numLine = key
                        break
                except:
                    raise LogFileException('File \'' + self._filePath +
                                           '\' have syntax error on line ' +
                                           str(startLine - key))

        if not check:
            raise LogFileException('File \'' + self._filePath +
                                   '\' have syntax error - no version exist')
        check = False

        if numLine != -1:
            for key, line in enumerate(list[numLine:]):
                if "*/" in line:
                    startLine = key + numLine + 1
                    check = True
                    break

        if numLine != -1 and check == False:
            raise LogFileException('File \'' + self._filePath +
                                   '\' have syntax error')

        string = '\n'.join(list[startLine:])
        ret = self.__delMultiLineComments(string)
        return ret
Esempio n. 2
0
    def deleteFromVersion(self, version: int):
        """
        Delete logs from defined version number with this verison
        :param version: integer number of version
        """
        if self.__isFileLogExist() != True:
            raise FileException("File \'" + self._filePath +
                                "\' is not exists")

        list = self.__getAllFileContentByLine()
        numLine = -1
        endLine = len(list)
        check = False

        # find start of  version
        for key, line in enumerate(list):
            index = line.find("version:")
            if index != -1:
                check = True
                try:
                    versionStr = line[index + 8:]
                    versionInt = int(versionStr)
                    if versionInt >= version:
                        numLine = key
                        break
                except:
                    raise LogFileException('File \'' + self._filePath +
                                           '\' have syntax error on line ' +
                                           str(endLine - key))

        if not check:
            raise LogFileException('File \'' + self._filePath +
                                   '\' have syntax error - no version exist')
        check = False

        if numLine != -1:
            for key, line in (enumerate(reversed(list[numLine:]))):
                if "/*" in line:
                    check = True
                    endLine = numLine - key
                    break

        if numLine != -1 and check == False:
            raise LogFileException('File \'' + self._filePath +
                                   '\' have syntax error')

        stringToSave = '\n'.join(list[:endLine])

        self.writeIntoFile(stringToSave)
Esempio n. 3
0
 def getLastVersion(self):
     """
     Return last version number
     If last version is no find then return 0
     :return: integer
     """
     if self.__isFileLogExist() != True:
         raise FileException("File \'" + self._filePath +
                             "\' is not exists")
     list = self.__getAllFileContentByLine()
     lastVersion = 0
     lenOfFile = len(list)
     for key, line in enumerate(reversed(list)):
         index = line.find("version:")
         if index != -1:
             try:
                 versionStr = line[index + 8:]
                 versionInt = int(versionStr)
                 lastVersion = versionInt
                 break
             except:
                 raise LogFileException('File \'' + self._filePath +
                                        '\' have syntax error on line ' +
                                        str(lenOfFile - key))
     return lastVersion
Esempio n. 4
0
    def getLastVersionSql(self):
        """
        Return last version sql code
        On error raise exceptins
        :return: string
        """
        if self.__isFileLogExist() != True:
            raise FileException("File \'" + self._filePath +
                                "\' is not exists")

        list = self.__getAllFileContentByLine()
        numLine = -1
        startLine = -1

        for key, line in enumerate(reversed(list)):
            index = line.find("version:")
            if index != -1:
                numLine = len(list) - key
                break

        if numLine >= 0:
            for key, line in enumerate(list[numLine:]):
                if "*/" in line:
                    startLine = key + numLine + 1
                    break

        if numLine == -1:
            raise LogFileException(
                'File \'' + self._filePath +
                '\' have syntax error: string \'version:\' was not found in any line'
            )
        if startLine == -1:
            raise LogFileException(
                'File \'' + self._filePath +
                '\' have syntax error: string \'*/\' was not found in any line'
            )

        string = '\n'.join(list[startLine:])
        ret = self.__delMultiLineComments(string)
        return ret
Esempio n. 5
0
    def __init__(self, filePath: string):
        """
        Constructor
        :param filePath: string of file path can be relative or absolute
        """
        if not os.path.isabs(filePath):
            self._filePath = os.path.realpath(filePath)
        else:
            self._filePath = filePath

        #if file exist check if is writeables
        if os.path.isfile(self._filePath):
            if not os.access(self._filePath, os.W_OK):
                raise LogFileException(
                    "Permissinon denied to write in file \'" +
                    self.__filePath + "\'")
Esempio n. 6
0
    def processMerge(self, dbName, fromVersion):
        """
        Merge in log files
        Merge fromVersion to lastest version into lastest version in log files
        :param dbName: str name of db
        :param fromVersion: str from version you want to merger
        :return:
        """
        if not self.dbConnection.isDatabaseExist(dbName):
            raise DatabaseException("Database: '" + dbName + "' is not exist")
        try:
            fromVersion = int(fromVersion)
        except ValueError:
            raise Exception('Version is not a integer')
        if fromVersion <= 0:
            raise LogFileException("Version number can not be less than zero")
        self.verDatabaseConnection = Database(
            Config.getConfig('databaseHost'), Config.getConfig('databasePort'),
            Config.getConfig("databaseUser"),
            Config.getConfig("databasePassword"), dbName)

        versionedDatabaseRecord = database.VersionedDatabase(self.dbConnection,
                                                             dbName=dbName)

        if versionedDatabaseRecord.getActualVersion() < fromVersion:
            pass
            #raise LogFileException("Version number can not be bigger than actual version of database")

        files = LogFile.getAllVerFileNameInDirectory(
            versionedDatabaseRecord.getDestinationFolder())

        #proces for every versed files
        for fileName in files.keys():
            elementName = files[fileName]
            filePath = LogFile.makeLogFilePath(
                versionedDatabaseRecord.getDestinationFolder(), elementName)
            logFile = LogFile(filePath)

            logFile.mergeFromVersion(fromVersion)
Esempio n. 7
0
    def mergeFromVersion(self, version):
        """
        merge sql form secifific version to lastet version and save like last version
        This method is transactional
        :param version: integer number of version
        """
        if self.__isFileLogExist() != True:
            raise FileException("File \'" + self._filePath +
                                "\' is not exists")

        if version <= 0:
            raise LogFileException("Version number can not be less than zero")

        all = self.getAllContent()
        try:
            lastestVerions = self.getLastVersion()
            merged = self.getSqlFromVersion(version)
            self.deleteFromVersion(version)
            sqlList = merged.split("\n")
            self.appendVersionIntoFile(sqlList, lastestVerions)
        except Exception as e:
            self.writeIntoFile(all)
            raise
Esempio n. 8
0
    def processUp(self, dbName):
        """
        Make new database revision
        :return:
        """

        if not self.dbConnection.isDatabaseExist(dbName):
            raise DatabaseException("Database: '" + dbName + "' is not exist")
        self.verDatabaseConnection = Database(
            Config.getConfig('databaseHost'), Config.getConfig('databasePort'),
            Config.getConfig("databaseUser"),
            Config.getConfig("databasePassword"), dbName)

        versionedDatabaseRecord = database.VersionedDatabase(self.dbConnection,
                                                             dbName=dbName)
        binnaryLogNames = BinnaryLogNames(self.dbConnection)
        listOfBinneryLogNames = binnaryLogNames.getBinnaryLogsNamesOlderThan(
            versionedDatabaseRecord.getLogFile())
        lastBinnaryLogName = binnaryLogNames.getLastBinnaryLogName()
        lastBinnaryLogPosition = versionedDatabaseRecord.getLogFilePosition()

        #check if first log name have name like versionedDatabaseRecord bin name
        if versionedDatabaseRecord.getLogFile() not in listOfBinneryLogNames:
            lastBinnaryLogPosition = 0
            print(
                'Warning: Binnary Logs is out of date. Prease increase expire_logs_days'
            )
            res = TerminalCommand.runDialogYorN(
                'All data for version is unavailable. Some data in new version can be missing. Do you want continue'
            )
            if not res:
                return

        #check if version in logFiles is same in db
        destination = versionedDatabaseRecord.getDestinationFolder()
        actualVersion = versionedDatabaseRecord.getActualVersion()
        fileNames = LogFile.getAllVerFileNameInDirectory(destination)
        for name in fileNames:
            logFile = LogFile(
                LogFile.makeLogFilePath(destination, fileNames[name]))
            fileVersion = logFile.getLastVersion()
            if fileVersion > actualVersion:
                raise LogFileException(
                    'Versined files in directory \'' + destination +
                    "' have newer version then data in database. Please import data into datababe before make new version"
                )

        #get data from logs
        binnarylogs = BinnaryLogs(listOfBinneryLogNames, dbName)
        binnarylogs.setStartPositionFirstLog(lastBinnaryLogPosition)
        logDataString = binnarylogs.getAllLogData()

        #parse data
        binnlogParser = BinnaryLogParser(logDataString)
        lastBinnaryLogPosition = binnlogParser.getLastPosition()

        #increment actual version - dont save
        actualVersion = versionedDatabaseRecord.getActualVersion()
        actualVersion = actualVersion + 1

        #flush sql into log file
        result = binnlogParser.getLogParserResult()

        #save to make transaction
        logFileData = {}
        toDelete = []
        try:
            #process
            for elementName in result.getAllUpdatedElementNames():
                elementData = versionedDatabaseRecord.getTableByName(
                    elementName)
                logFileName = LogFile.makeLogFilePath(
                    versionedDatabaseRecord.getDestinationFolder(),
                    elementName)

                if elementName in result.getCreated():
                    logFile = LogFile(logFileName)
                elif result.getRenameOldNameByNewName(elementName) is not None:
                    oldName = result.getRenameOldNameByNewName(elementName)
                    oldLogFileName = LogFile.makeLogFilePath(
                        versionedDatabaseRecord.getDestinationFolder(),
                        oldName)
                    oldLogFile = LogFile(oldLogFileName)
                    logFileData[oldLogFileName] = oldLogFile.getAllContent()
                    logFile = LogFile(logFileName)
                    toDelete.append(logFileName)
                    logFile.writeIntoFile(oldLogFile.getAllContent())
                    oldLogFile.deleteFile()
                elif versionedDatabaseRecord.getTableByName(elementName):
                    logFile = LogFile(logFileName)
                    logFileData[logFileName] = logFile.getAllContent()
                else:
                    #someting wrong
                    continue

                logFile.appendVersionIntoFile(
                    result.getAllQueriesByName(elementName), actualVersion)

        #do others
            if result.getOtherQueries():
                logFileName = LogFile.makeLogFilePath(
                    versionedDatabaseRecord.getDestinationFolder(),
                    LogFile.OTHER_LOG_NAME)
                logFile = LogFile(logFileName)
                logFileData[logFileName] = logFile.getAllContent()
                logFile.appendVersionIntoFile(result.getOtherQueries(),
                                              actualVersion)

            #save metadata
            if (result.haveSomeData()):
                versionedDatabaseRecord.setSomeData(lastBinnaryLogName,
                                                    lastBinnaryLogPosition,
                                                    actualVersion)
                versionedDatabaseRecord.updateMetaDataAboutTables()
            else:
                versionedDatabaseRecord.setSomeData(lastBinnaryLogName,
                                                    lastBinnaryLogPosition)
                versionedDatabaseRecord.updateMetaDataAboutTables()
                print(
                    "No data to flush into log files. New version was not make"
                )

        except Exception as e:
            #make transaction
            for fileName in toDelete:
                logFile = LogFile(fileName)
                logFile.deleteFile()
            for fileName in logFileData.keys():
                logFile = LogFile(fileName)
                logFile.writeIntoFile(logFileData[fileName])
            raise