use of cz.metacentrum.perun.core.api.DBVersion in project perun by CESNET.
the class DatabaseManagerImpl method getChangelogVersions.
@Override
public List<DBVersion> getChangelogVersions(String currentDBVersion, String fileName) {
Pattern versionPattern = Pattern.compile("^[1-9][0-9]*[.][0-9]+[.][0-9]+");
Pattern commentPattern = Pattern.compile("^--.*");
List<DBVersion> versions = new ArrayList<>();
boolean versionFound = false;
Resource resource = new ClassPathResource(fileName);
try (BufferedReader br = new BufferedReader(new InputStreamReader(resource.getInputStream()))) {
String line = br.readLine();
while (line != null) {
line = line.trim();
// ignore empty lines and comments at the start of the file and between versions
if (line.isEmpty() || commentPattern.matcher(line).matches()) {
line = br.readLine();
continue;
}
if (versionPattern.matcher(line).matches()) {
// saving version
DBVersion version = new DBVersion(line);
// comparing two last version numbers
if (!versions.isEmpty()) {
DBVersion previousVersion = versions.get(versions.size() - 1);
if (version.compareTo(previousVersion) >= 0) {
throw new InternalErrorException("Version numbers in changelog file are not increasing properly. " + "Version " + previousVersion.getVersion() + " should be higher than " + version.getVersion());
}
}
// the current db version was found, all new versions and their commands were saved to versions list
if (line.equals(currentDBVersion)) {
versionFound = true;
break;
}
List<String> commands = new ArrayList<>();
while ((line = br.readLine()) != null) {
line = line.trim();
// empty line means end of current version
if (line.isEmpty()) {
break;
}
commands.add(line);
}
// saving version commands
version.setCommands(commands);
versions.add(version);
} else {
throw new InternalErrorException("Version does not match the pattern required in " + fileName);
}
}
if (!versionFound) {
throw new InternalErrorException("Version " + currentDBVersion + " not found in " + fileName);
}
} catch (IOException e) {
throw new InternalErrorException("Error reading " + fileName, e);
}
return versions;
}
use of cz.metacentrum.perun.core.api.DBVersion in project perun by CESNET.
the class DatabaseManagerBlImpl method initialize.
protected void initialize() {
log.debug("Initialize manager starts!");
// This part of code probably need to be replaced by readOnly setting for every connection in perun
// not just the one (this one)
boolean readOnly = BeansUtils.isPerunReadOnly();
// Initialize property for performance testing if not exists
if (!this.propertyExists(DatabaseManagerImpl.PERFORMANCE_PROPERTY)) {
if (readOnly) {
log.error("There is missing property for DB performance testing!");
} else {
this.createProperty(DatabaseManagerImpl.PERFORMANCE_PROPERTY);
}
}
String fileName = POSTGRES_CHANGELOG;
String currentDBVersion = getCurrentDatabaseVersion();
List<DBVersion> dbVersions;
// trying to parse db versions from changelog
try {
dbVersions = getChangelogVersions(currentDBVersion, fileName);
} catch (Exception e) {
// Call exception which kills the initialization process in spring
throw new InternalErrorException("Error parsing DB changelog file " + fileName, e);
}
String codeDBVersion = this.databaseManagerImpl.getCodeDatabaseVersion(dbVersions, currentDBVersion);
if (codeDBVersion.equals(currentDBVersion)) {
log.debug("DB version is up to date - CurrentVersion: " + currentDBVersion);
} else {
log.debug("DB version is not up to date! Updating database.");
if (BeansUtils.initializatorEnabled()) {
// If read only, end with error
if (readOnly)
throw new InternalErrorException("Can't update database version in read only Perun. End with error.");
try {
updateDatabaseVersion(dbVersions);
log.debug("DB version updated successfully. Current version: " + codeDBVersion);
} catch (Exception e) {
// Call exception which kills the initialization process in spring
throw new InternalErrorException("DB version is NOT up to date, database update was unsuccessful! Look to the logs for more info.", e);
}
} else {
log.error("Initializator of DB is disabled on this instance of Perun. Please, do manual changes in database.");
throw new InternalErrorException("DB version is NOT up to date, automatic update through initializer is disabled. Please do manual changes!");
}
}
log.debug("Initialize manager ends!");
}
use of cz.metacentrum.perun.core.api.DBVersion in project perun by CESNET.
the class DatabaseManagerImpl method updateDatabaseVersion.
@Override
public void updateDatabaseVersion(List<DBVersion> dbVersions) {
Collections.reverse(dbVersions);
for (DBVersion v : dbVersions) {
log.debug("Executing update commands of version " + v.getVersion());
List<String> successfulCommands = new ArrayList<>();
for (String c : v.getCommands()) {
try {
jdbc.execute(c);
log.debug("Command executed: " + c);
successfulCommands.add(c);
} catch (EmptyResultDataAccessException ex) {
log.error("Update unsuccessful. All versions before " + v.getVersion() + " were successfully executed. " + "Error executing command in version " + v.getVersion() + ": " + c, ex);
log.error("Successful commands from " + v.getVersion() + ": " + successfulCommands);
throw new ConsistencyErrorException("Update unsuccessful. Error executing command in version " + v.getVersion() + ": " + c, ex);
} catch (RuntimeException ex) {
log.error("Update unsuccessful. All versions before " + v.getVersion() + " were successfully executed. " + "Error executing command in version " + v.getVersion() + ": " + c, ex);
log.error("Successful commands from " + v.getVersion() + ": " + successfulCommands);
throw new InternalErrorException("Update unsuccessful. Error executing command in version " + v.getVersion() + ": " + c, ex);
}
}
}
}
Aggregations