From c99766a71efca85ff989df01163662e25fa4093e Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Thu, 8 Mar 2018 22:31:27 +0100 Subject: [PATCH 1/6] deploy.sh: Create relases as published, not as draft. --- deploy.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deploy.sh b/deploy.sh index db62fd4..167cff2 100755 --- a/deploy.sh +++ b/deploy.sh @@ -56,7 +56,7 @@ echo "Building it..." gradle build || error "Build failed. What did you do?!" echo "Generating a release on Github..." -json=$(ruby -e "require 'json'; puts({tag_name: '$VERSION', name: '$VERSION', draft: true, body: \$stdin.read}.to_json)" <<< "$release_notes") || error "Couldn't generate JSON for Github" +json=$(ruby -e "require 'json'; puts({tag_name: '$VERSION', name: '$VERSION', body: \$stdin.read}.to_json)" <<< "$release_notes") || error "Couldn't generate JSON for Github" json=$(curl $CURL_OPTS https://api.github.com/repos/fabianonline/telegram_backup/releases -XPOST -d "$json") || error "Github failure" From d66834c3d5053c15d1e208a12a57b763baaf783a Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Thu, 8 Mar 2018 22:32:18 +0100 Subject: [PATCH 2/6] deploy.sh: Use the html_url of the new release. --- deploy.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deploy.sh b/deploy.sh index 167cff2..ee0d528 100755 --- a/deploy.sh +++ b/deploy.sh @@ -63,7 +63,7 @@ json=$(curl $CURL_OPTS https://api.github.com/repos/fabianonline/telegram_backup echo "Uploading telegram_backup.jar to Github..." upload_url=$(jq -r ".upload_url" <<< "$json") || error "Could not parse JSON from Github" upload_url=$(sed 's/{.*}//' <<< "$upload_url") -release_url=$(jq -r ".url" <<< "$json") || error "Could not parse JSON from Github" +release_url=$(jq -r ".html_url" <<< "$json") || error "Could not parse JSON from Github" curl $CURL_OPTS --header "Content-Type: application/zip" "${upload_url}?name=telegram_backup.jar" --upload-file build/libs/telegram_backup.jar || error "Asset upload to github failed" echo "Building the docker image..." From a8944125b6672d98c2fd111114c3291814b5ad67 Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Thu, 8 Mar 2018 22:32:51 +0100 Subject: [PATCH 3/6] deploy.sh: Check out master after running the tool. --- deploy.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/deploy.sh b/deploy.sh index ee0d528..be3715d 100755 --- a/deploy.sh +++ b/deploy.sh @@ -81,4 +81,7 @@ curl https://api.telegram.org/bot${BOT_TOKEN}/sendMessage -XPOST --form "text=<- echo "Cleaning release_notes.txt..." > release_notes.txt +echo "Checking out master..." +git checkout master + echo "Done." From b0fa297a6158c4338ead5c0d7ebeca3265edc455 Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Thu, 8 Mar 2018 23:11:14 +0100 Subject: [PATCH 4/6] deploy.sh: Moved the gradle build task to earlier in the process to better be able to catch errors. --- deploy.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/deploy.sh b/deploy.sh index be3715d..ea3e0a9 100755 --- a/deploy.sh +++ b/deploy.sh @@ -40,6 +40,9 @@ git commit -m "Bumping the version to $VERSION" Dockerfile echo "Tagging the new version..." git tag -a "$VERSION" -m "Version $VERSION" || error +echo "Building it..." +gradle build || error "Build failed. What did you do?!" + echo "Checking out stable..." git checkout stable || error @@ -52,9 +55,6 @@ git push --all || error echo "Pushing tags to Github..." git push --tags || error -echo "Building it..." -gradle build || error "Build failed. What did you do?!" - echo "Generating a release on Github..." json=$(ruby -e "require 'json'; puts({tag_name: '$VERSION', name: '$VERSION', body: \$stdin.read}.to_json)" <<< "$release_notes") || error "Couldn't generate JSON for Github" From d796cb1bf0b811aaeb8d4607650e2a4fbe456894 Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Thu, 8 Mar 2018 23:11:48 +0100 Subject: [PATCH 5/6] deploy.sh: Telegram message is now HTML formatted. --- deploy.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/deploy.sh b/deploy.sh index ea3e0a9..730c01c 100755 --- a/deploy.sh +++ b/deploy.sh @@ -73,10 +73,10 @@ echo "Pushing the docker image..." docker push fabianonline/telegram_backup echo "Notifying the Telegram group..." -release_notes=$(sed 's/\* /• /' <<< "$release_notes") -message="Version $VERSION released"$'\n'$'\n'"$release_notes"$'\n'$'\n'"$release_url" +release_notes=$(sed 's/\* /• /' | sed 's/&/&/g' | sed 's//\>/g' <<< "$release_notes") +message="Version $VERSION was just released"$'\n'$'\n'"$release_notes"$'\n'$'\n'"$release_url" -curl https://api.telegram.org/bot${BOT_TOKEN}/sendMessage -XPOST --form "text=<-" --form-string "chat_id=${CHAT_ID}" <<< "$message" +curl https://api.telegram.org/bot${BOT_TOKEN}/sendMessage -XPOST --form "text=<-" --form-string "chat_id=${CHAT_ID}" --form-string "parse_mode=HTML" --form-string "disable_web_page_preview=true" <<< "$message" echo "Cleaning release_notes.txt..." > release_notes.txt From e75aa2101eebf3a5ad089c82fd2beeda4b32a624 Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Mon, 12 Mar 2018 22:01:47 +0100 Subject: [PATCH 6/6] Fixed lots of unclosed ResultSets in Database and DatabaseUpdates. --- .../fabianonline/telegram_backup/Database.kt | 46 ++++++++++++------- .../telegram_backup/DatabaseUpdates.kt | 17 +++---- 2 files changed, 35 insertions(+), 28 deletions(-) diff --git a/src/main/kotlin/de/fabianonline/telegram_backup/Database.kt b/src/main/kotlin/de/fabianonline/telegram_backup/Database.kt index 8c35302..20cbda6 100644 --- a/src/main/kotlin/de/fabianonline/telegram_backup/Database.kt +++ b/src/main/kotlin/de/fabianonline/telegram_backup/Database.kt @@ -56,7 +56,9 @@ class Database private constructor(var client: TelegramClient) { try { val rs = stmt!!.executeQuery("SELECT MAX(message_id) FROM messages WHERE source_type IN ('group', 'dialog')") rs.next() - return rs.getInt(1) + val result = rs.getInt(1) + rs.close() + return result } catch (e: SQLException) { return 0 } @@ -87,6 +89,7 @@ class Database private constructor(var client: TelegramClient) { missing.add(i) } } + rs.close() return missing } catch (e: SQLException) { e.printStackTrace() @@ -111,16 +114,7 @@ class Database private constructor(var client: TelegramClient) { } - fun getMessagesFromUserCount(): Int { - try { - val rs = stmt!!.executeQuery("SELECT COUNT(*) FROM messages WHERE sender_id=" + user_manager.user!!.getId()) - rs.next() - return rs.getInt(1) - } catch (e: SQLException) { - throw RuntimeException(e) - } - - } + fun getMessagesFromUserCount() = queryInt("SELECT COUNT(*) FROM messages WHERE sender_id=" + user_manager.user!!.getId()) fun getMessageTypesWithCount(): HashMap = getMessageTypesWithCount(GlobalChat()) @@ -164,7 +158,9 @@ class Database private constructor(var client: TelegramClient) { try { val rs = stmt!!.executeQuery("PRAGMA encoding") rs.next() - return rs.getString(1) + val result = rs.getString(1) + rs.close() + return result } catch (e: SQLException) { logger.debug("SQLException: {}", e) return "unknown" @@ -255,9 +251,7 @@ class Database private constructor(var client: TelegramClient) { } - fun getTopMessageIDForChannel(id: Int): Int { - return queryInt("SELECT MAX(message_id) FROM messages WHERE source_id=$id AND source_type IN('channel', 'supergroup')") - } + fun getTopMessageIDForChannel(id: Int): Int = queryInt("SELECT MAX(message_id) FROM messages WHERE source_id=$id AND source_type IN('channel', 'supergroup')") fun logRun(start_id: Int, end_id: Int, count: Int) { try { @@ -269,6 +263,7 @@ class Database private constructor(var client: TelegramClient) { ps.setInt(2, end_id) ps.setInt(3, count) ps.execute() + ps.close() } catch (e: SQLException) { } @@ -278,7 +273,9 @@ class Database private constructor(var client: TelegramClient) { try { val rs = stmt!!.executeQuery(query) rs.next() - return rs.getInt(1) + val result = rs.getInt(1) + rs.close() + return result } catch (e: SQLException) { throw RuntimeException("Could not get count of messages.") } @@ -431,6 +428,9 @@ class Database private constructor(var client: TelegramClient) { ps_insert_or_ignore.clearBatch() conn!!.commit() conn!!.setAutoCommit(true) + + ps.close() + ps_insert_or_ignore.close() } catch (e: Exception) { e.printStackTrace() throw RuntimeException("Exception shown above happened.") @@ -486,6 +486,9 @@ class Database private constructor(var client: TelegramClient) { ps_insert_or_replace.clearBatch() conn!!.commit() conn!!.setAutoCommit(true) + + ps_insert_or_ignore.close() + ps_insert_or_replace.close() } catch (e: Exception) { e.printStackTrace() throw RuntimeException("Exception shown above happened.") @@ -535,6 +538,9 @@ class Database private constructor(var client: TelegramClient) { ps_insert_or_replace.clearBatch() conn!!.commit() conn!!.setAutoCommit(true) + + ps_insert_or_ignore.close() + ps_insert_or_replace.close() } catch (e: Exception) { e.printStackTrace() throw RuntimeException("Exception shown above happened.") @@ -564,6 +570,7 @@ class Database private constructor(var client: TelegramClient) { while (rs.next()) { map.put("count.messages.type." + rs.getString(1), rs.getInt(2)) } + rs.close() return map } catch (e: Exception) { throw RuntimeException(e) @@ -586,6 +593,7 @@ class Database private constructor(var client: TelegramClient) { map.put("count.messages.media_type.$s", rs.getInt(2)) } map.put("count.messages.media_type.any", count) + rs.close() return map } catch (e: Exception) { throw RuntimeException(e) @@ -625,6 +633,7 @@ class Database private constructor(var client: TelegramClient) { } map.put("authors.count.others", count_others) map.put("authors.all", all_data) + rs.close() return map } catch (e: Exception) { throw RuntimeException(e) @@ -634,7 +643,9 @@ class Database private constructor(var client: TelegramClient) { fun getMessageCountForExport(c: AbstractChat): Int { val rs = stmt!!.executeQuery("SELECT COUNT(*) FROM messages WHERE " + c.query); rs.next() - return rs.getInt(1) + val result = rs.getInt(1) + rs.close() + return result } fun getMessageTimesMatrix(c: AbstractChat): Array { @@ -647,6 +658,7 @@ class Database private constructor(var client: TelegramClient) { while (rs.next()) { result[if (rs.getInt(1) == 0) 6 else rs.getInt(1) - 1][rs.getInt(2)] = rs.getInt(3) } + rs.close() return result } catch (e: Exception) { throw RuntimeException(e) diff --git a/src/main/kotlin/de/fabianonline/telegram_backup/DatabaseUpdates.kt b/src/main/kotlin/de/fabianonline/telegram_backup/DatabaseUpdates.kt index 0f8d12c..d3b3d99 100644 --- a/src/main/kotlin/de/fabianonline/telegram_backup/DatabaseUpdates.kt +++ b/src/main/kotlin/de/fabianonline/telegram_backup/DatabaseUpdates.kt @@ -37,25 +37,19 @@ class DatabaseUpdates(protected var conn: Connection, protected var db: Database fun doUpdates() { try { val stmt = conn.createStatement() - var rs: ResultSet logger.debug("DatabaseUpdate.doUpdates running") logger.debug("Getting current database version") var version: Int logger.debug("Checking if table database_versions exists") - rs = stmt.executeQuery("SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='database_versions'") - rs.next() - if (rs.getInt(1) == 0) { + val table_count = db.queryInt("SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='database_versions'") + if (table_count == 0) { logger.debug("Table does not exist") version = 0 } else { logger.debug("Table exists. Checking max version") - rs.close() - rs = stmt.executeQuery("SELECT MAX(version) FROM database_versions") - rs.next() - version = rs.getInt(1) + version = db.queryInt("SELECT MAX(version) FROM database_versions") } - rs.close() logger.debug("version: {}", version) System.out.println("Database version: " + version) logger.debug("Max available database version is {}", maxPossibleVersion) @@ -106,6 +100,9 @@ class DatabaseUpdates(protected var conn: Connection, protected var db: Database } catch (e: SQLException) { throw RuntimeException(e) } + + println("Cleaning up the database (this might take some time)...") + try { stmt.executeUpdate("VACUUM") } catch (t: Throwable) { logger.debug("Exception during VACUUMing: {}", t) } } else { logger.debug("No update necessary.") @@ -441,7 +438,5 @@ internal class DB_Update_9(conn: Connection, db: Database) : DatabaseUpdate(conn } println() logger.info("Converted ${i} of ${count} messages.") - println(" Cleaning up the database (this might also take some time, sorry)...") - execute("VACUUM") } }