From 1d4a478aa2b52a042592bca506b629ca249f9f63 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 28 Sep 2022 10:21:44 -0500 Subject: refactor sqlite backup --- src/slskd/Common/Sqlite.cs | 46 ++++++++++++++++++++++++++ src/slskd/Common/SqliteConnectionExtensions.cs | 32 ------------------ src/slskd/Shares/ShareRepository.cs | 6 ++-- src/slskd/Shares/SharedFileCache.cs | 13 ++------ 4 files changed, 52 insertions(+), 45 deletions(-) create mode 100644 src/slskd/Common/Sqlite.cs delete mode 100644 src/slskd/Common/SqliteConnectionExtensions.cs (limited to 'src') diff --git a/src/slskd/Common/Sqlite.cs b/src/slskd/Common/Sqlite.cs new file mode 100644 index 00000000..8f5784b6 --- /dev/null +++ b/src/slskd/Common/Sqlite.cs @@ -0,0 +1,46 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// + +namespace slskd +{ + using System; + using Microsoft.Data.Sqlite; + + public static class Sqlite + { + public static void Backup(string sourceConnectionString, string destinationConnectionString) + { + using var source = new SqliteConnection(sourceConnectionString); + using var destination = new SqliteConnection(destinationConnectionString); + + source.Open(); + destination.Open(); + + source.BackupDatabase(destination); + } + + public static void Restore(string sourceConnectionString, string destinationConnectionString) + => Backup(sourceConnectionString, destinationConnectionString); + + public static int ExecuteNonQuery(this SqliteConnection conn, string query, Action action = null) + { + using var cmd = new SqliteCommand(query, conn); + action?.Invoke(cmd); + return cmd.ExecuteNonQuery(); + } + } +} diff --git a/src/slskd/Common/SqliteConnectionExtensions.cs b/src/slskd/Common/SqliteConnectionExtensions.cs deleted file mode 100644 index 3944c10b..00000000 --- a/src/slskd/Common/SqliteConnectionExtensions.cs +++ /dev/null @@ -1,32 +0,0 @@ -// -// Copyright (c) slskd Team. All rights reserved. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published -// by the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see https://www.gnu.org/licenses/. -// - -namespace slskd -{ - using System; - using Microsoft.Data.Sqlite; - - public static class SqliteConnectionExtensions - { - public static int ExecuteNonQuery(this SqliteConnection conn, string query, Action action = null) - { - using var cmd = new SqliteCommand(query, conn); - action?.Invoke(cmd); - return cmd.ExecuteNonQuery(); - } - } -} diff --git a/src/slskd/Shares/ShareRepository.cs b/src/slskd/Shares/ShareRepository.cs index 5df77e6c..a79e0f05 100644 --- a/src/slskd/Shares/ShareRepository.cs +++ b/src/slskd/Shares/ShareRepository.cs @@ -37,7 +37,7 @@ namespace slskd.Shares long DeleteFiles(long olderThanTimestamp); IEnumerable ListDirectories(string prefix = null); IEnumerable ListFiles(string directory = null, bool includeFullPath = false); - void Backup(string backupConnectionString); + void Backup(string destinationConnectionString); string FindOriginalFilename(string maskedFilename); IEnumerable Search(SearchQuery query); } @@ -205,10 +205,10 @@ namespace slskd.Shares } } - public void Backup(string backupConnectionString) + public void Backup(string destinationConnectionString) { using var sourceConn = GetConnection(ConnectionString); - using var backupConn = GetConnection(backupConnectionString); + using var backupConn = GetConnection(destinationConnectionString); sourceConn.BackupDatabase(backupConn); } diff --git a/src/slskd/Shares/SharedFileCache.cs b/src/slskd/Shares/SharedFileCache.cs index 4a443468..93a7da59 100644 --- a/src/slskd/Shares/SharedFileCache.cs +++ b/src/slskd/Shares/SharedFileCache.cs @@ -393,9 +393,9 @@ namespace slskd.Shares Log.Debug("Share cache backup located. Attempting to restore..."); - using var backupConn = GetConnection(Program.ConnectionStrings.SharesBackup); - using var conn = GetConnection(); - backupConn.BackupDatabase(conn); + Sqlite.Restore( + sourceConnectionString: Program.ConnectionStrings.SharesBackup, + destinationConnectionString: Program.ConnectionStrings.Shares); Log.Debug("Share cache successfully restored from backup"); } @@ -423,12 +423,5 @@ namespace slskd.Shares return false; } } - - private SqliteConnection GetConnection(string connectionString = null) - { - var conn = new SqliteConnection(connectionString ?? Program.ConnectionStrings.Shares); - conn.Open(); - return conn; - } } } \ No newline at end of file -- cgit v1.2.3