UpdateDatabaseFiles.xaml.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. using Comal.Stores;
  2. using InABox.Clients;
  3. using InABox.Core;
  4. using InABox.Database;
  5. using InABox.Database.SQLite;
  6. using InABox.DynamicGrid;
  7. using InABox.Wpf;
  8. using InABox.WPF;
  9. using PRSServices;
  10. using Stripe.Treasury;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.ComponentModel;
  14. using System.Linq;
  15. using System.Text;
  16. using System.Threading;
  17. using System.Threading.Tasks;
  18. using System.Windows;
  19. using System.Windows.Controls;
  20. using System.Windows.Data;
  21. using System.Windows.Documents;
  22. using System.Windows.Input;
  23. using System.Windows.Media;
  24. using System.Windows.Media.Imaging;
  25. using System.Windows.Shapes;
  26. namespace PRSServer.Forms.Version9Update;
  27. public class UpdateDatabaseFilesGridItem : BaseObject
  28. {
  29. public string Name { get; set; }
  30. public string FileName { get; set; }
  31. public IProviderFactory ProviderFactory { get; set; }
  32. public VersionNumber Version { get; set; }
  33. public string VersionText => Version?.ToString() ?? "";
  34. public bool NeedsUpdate => Version is not null && Version.MajorVersion < 9;
  35. }
  36. public class UpdateDatabaseFilesGrid : DynamicItemsListGrid<UpdateDatabaseFilesGridItem>
  37. {
  38. private static readonly BitmapImage? _warning = Properties.Resources.warning.AsBitmapImage();
  39. private static readonly BitmapImage? _tick = Properties.Resources.tick.AsBitmapImage();
  40. protected override void Init()
  41. {
  42. base.Init();
  43. ActionColumns.Add(new DynamicImageColumn(GetImage, UpdateClick)
  44. {
  45. ToolTip = UpdateToolTip
  46. });
  47. }
  48. private FrameworkElement? UpdateToolTip(DynamicActionColumn column, CoreRow? row)
  49. {
  50. if(row is null)
  51. {
  52. return null;
  53. }
  54. var item = LoadItem(row);
  55. if (item.NeedsUpdate)
  56. {
  57. return column.TextToolTip($"This database is currently version {item.VersionText} and needs updating to version 9.");
  58. }
  59. else
  60. {
  61. return null;
  62. }
  63. }
  64. protected override DynamicGridColumns LoadColumns()
  65. {
  66. var columns = new DynamicGridColumns();
  67. columns.Add<UpdateDatabaseFilesGridItem>(x => x.Name);
  68. columns.Add<UpdateDatabaseFilesGridItem>(x => x.VersionText, caption: "Current Version");
  69. return columns;
  70. }
  71. protected override void DoReconfigure(DynamicGridOptions options)
  72. {
  73. options.Clear();
  74. }
  75. private bool UpdateClick(CoreRow? row)
  76. {
  77. if (row is null) return false;
  78. var item = LoadItem(row);
  79. if (!item.NeedsUpdate) return false;
  80. var currentVersion = DbFactory.GetDatabaseVersion(item.ProviderFactory);
  81. if(currentVersion.MajorVersion >= 9)
  82. {
  83. item.Version = currentVersion;
  84. return true;
  85. }
  86. var task = Task.Run(() =>
  87. {
  88. DbFactory.Stores = CoreUtils.TypeList(
  89. AppDomain.CurrentDomain.GetAssemblies(),
  90. myType =>
  91. myType.IsClass
  92. && !myType.IsAbstract
  93. && !myType.IsGenericType
  94. && myType.GetInterfaces().Contains(typeof(IStore))
  95. ).ToArray();
  96. DbFactory.DefaultStore = typeof(BaseStore<>);
  97. var factory = new SQLiteProviderFactory(item.FileName);
  98. DbFactory.ProviderFactory = factory;
  99. DbFactory.Start(); // Doing this will run all the update scripts.
  100. factory.InitializeNulls();
  101. });
  102. var window = Window.GetWindow(this);
  103. var console = new UpdateConsole("Console");
  104. console.Top = window.Top;
  105. console.Left = window.Left + window.Width + 2;
  106. console.Height = window.Height;
  107. console.Loaded += (_, args) =>
  108. {
  109. var worker = new BackgroundWorker();
  110. window.IsEnabled = false;
  111. worker.DoWork += (o, e) =>
  112. {
  113. task.Wait();
  114. };
  115. worker.RunWorkerCompleted +=
  116. (o, e) => console.Close();
  117. worker.RunWorkerAsync();
  118. };
  119. console.ShowDialog();
  120. try
  121. {
  122. task.Wait();
  123. }
  124. catch(Exception e)
  125. {
  126. MessageWindow.ShowError("An error occurred while trying to update this database.", e);
  127. }
  128. window.IsEnabled = true;
  129. currentVersion = DbFactory.GetDatabaseVersion(item.ProviderFactory);
  130. item.Version = currentVersion;
  131. return true;
  132. }
  133. private BitmapImage? GetImage(CoreRow? row)
  134. {
  135. if (row is null) return _tick;
  136. return LoadItem(row).NeedsUpdate ? _warning : _tick;
  137. }
  138. }
  139. /// <summary>
  140. /// Interaction logic for UpdateDatabaseFiles.xaml
  141. /// </summary>
  142. public partial class UpdateDatabaseFiles : Window
  143. {
  144. public UpdateDatabaseFiles()
  145. {
  146. InitializeComponent();
  147. }
  148. private void Window_Loaded(object sender, RoutedEventArgs e)
  149. {
  150. LoadData();
  151. }
  152. private void LoadData()
  153. {
  154. Progress.ShowModal("Loading Data", progress =>
  155. {
  156. var table = new CoreTable();
  157. table.LoadColumns(typeof(Server));
  158. var sections = PRSService.GetConfiguration().LoadAll();
  159. foreach (var section in sections.Where(x => x.Value.Type == ServerType.Database))
  160. {
  161. var properties = section.Value.DeserializeServerProperties();
  162. if (properties is not DatabaseServerProperties databaseProperties) continue;
  163. Grid.Items.Add(new()
  164. {
  165. Name = databaseProperties.Name,
  166. FileName = databaseProperties.FileName,
  167. });
  168. }
  169. var tasks = Grid.Items.ToArray(LoadProvider);
  170. Task.WaitAll(tasks);
  171. foreach(var (item, task) in Grid.Items.Zip(tasks))
  172. {
  173. item.ProviderFactory = task.Result;
  174. item.Version = DbFactory.GetDatabaseVersion(item.ProviderFactory);
  175. }
  176. });
  177. Grid.Refresh(true, true);
  178. }
  179. private Task<IProviderFactory> LoadProvider(UpdateDatabaseFilesGridItem item)
  180. {
  181. return Task.Run(() =>
  182. {
  183. IProviderFactory factory = new SQLiteProviderFactory(item.FileName);
  184. factory.Start();
  185. return factory;
  186. });
  187. }
  188. private void Proceed_Click(object sender, RoutedEventArgs e)
  189. {
  190. if(Grid.Items.Any(x => x.NeedsUpdate))
  191. {
  192. if(!MessageWindow.ShowYesNo("Some database files have not been updated to version 9! If you proceed, these " +
  193. "databases will not work anymore. Are you sure you wish to proceed?", "Unupdated files"))
  194. {
  195. return;
  196. }
  197. }
  198. DialogResult = true;
  199. }
  200. private void Cancel_Click(object sender, RoutedEventArgs e)
  201. {
  202. DialogResult = false;
  203. }
  204. }