1
1
extern alias MySqlConnectorAnnotations ;
2
2
using Cysharp . Threading . Tasks ;
3
+ using Microsoft . EntityFrameworkCore ;
3
4
using MySqlConnectorAnnotations ::MySql . Data . MySqlClient ;
4
5
using OpenMod . API . Commands ;
5
6
using OpenMod . API . Prioritization ;
6
7
using OpenMod . Core . Commands ;
7
8
using OpenMod . Unturned . Commands ;
8
9
using SDG . Unturned ;
10
+ using ShopsUI . API . Migrations ;
9
11
using ShopsUI . Commands . Items ;
10
12
using ShopsUI . Database ;
11
13
using ShopsUI . Database . Models ;
@@ -52,11 +54,14 @@ public class ZaupShopConfiguration
52
54
}
53
55
54
56
private readonly ShopsDbContext _dbContext ;
57
+ private readonly IMigrationVerifier _migrationVerifier ;
55
58
56
59
public CShopMigrate ( ShopsDbContext dbContext ,
60
+ IMigrationVerifier migrationVerifier ,
57
61
IServiceProvider serviceProvider ) : base ( serviceProvider )
58
62
{
59
63
_dbContext = dbContext ;
64
+ _migrationVerifier = migrationVerifier ;
60
65
}
61
66
62
67
private async Task < T > GetPluginConfig < T > ( string path ) where T : class , new ( )
@@ -88,6 +93,19 @@ public CShopMigrate(ShopsDbContext dbContext,
88
93
}
89
94
}
90
95
96
+ private async Task ClearExistingDatabase ( )
97
+ {
98
+ async Task ClearSet < T > ( DbSet < T > set ) where T : class
99
+ {
100
+ set . RemoveRange ( await set . ToListAsync ( ) ) ;
101
+ }
102
+
103
+ await ClearSet ( _dbContext . ItemGroups ) ;
104
+ await ClearSet ( _dbContext . VehicleGroups ) ;
105
+ await ClearSet ( _dbContext . ItemShops ) ;
106
+ await ClearSet ( _dbContext . VehicleShops ) ;
107
+ }
108
+
91
109
private async Task MigrateItemShops ( MySqlConnection connection , ZaupShopConfiguration config )
92
110
{
93
111
var query = $ "SELECT `id`,`cost`,`buyback` FROM `{ config . ItemShopTableName } `;";
@@ -140,9 +158,9 @@ private async Task MigrateGroups(MySqlConnection connection, ZaupShopConfigurati
140
158
141
159
var tables = new List < ( string Name , bool Whitelist ) > ( ) ;
142
160
143
- using ( var command = new MySqlCommand ( mainGroupQuery , connection ) )
161
+ await using ( var command = new MySqlCommand ( mainGroupQuery , connection ) )
144
162
{
145
- using var reader = command . ExecuteReader ( ) ;
163
+ await using var reader = await command . ExecuteReaderAsync ( ) ;
146
164
147
165
while ( await reader . ReadAsync ( ) )
148
166
{
@@ -158,9 +176,9 @@ private async Task MigrateGroups(MySqlConnection connection, ZaupShopConfigurati
158
176
{
159
177
var groupTableQuery = $ "SELECT `assetid`,`vehicle` FROM `{ name } `;";
160
178
161
- using var command = new MySqlCommand ( groupTableQuery , connection ) ;
179
+ await using var command = new MySqlCommand ( groupTableQuery , connection ) ;
162
180
163
- using var reader = command . ExecuteReader ( ) ;
181
+ await using var reader = await command . ExecuteReaderAsync ( ) ;
164
182
165
183
while ( await reader . ReadAsync ( ) )
166
184
{
@@ -191,6 +209,14 @@ await _dbContext.ItemGroups.AddAsync(new ItemGroupModel
191
209
192
210
protected override async UniTask OnExecuteAsync ( )
193
211
{
212
+ if ( ! _migrationVerifier . CheckShouldContinueMigration ( Context . Actor ) )
213
+ {
214
+ await PrintAsync ( "By using this migration command, you will erase all existing ShopsUI shops. " +
215
+ "If you wish to continue, please run the command again." ) ;
216
+
217
+ return ;
218
+ }
219
+
194
220
await UniTask . SwitchToThreadPool ( ) ;
195
221
196
222
await PrintAsync ( "Beginning shop migration. This may take a while." ) ;
@@ -209,7 +235,9 @@ protected override async UniTask OnExecuteAsync()
209
235
$ "User={ uconomyConfig . DatabaseUsername } ;" +
210
236
$ "Password={ uconomyConfig . DatabasePassword } ";
211
237
212
- using var connection = new MySqlConnection ( connectionString ) ;
238
+ await ClearExistingDatabase ( ) ;
239
+
240
+ await using var connection = new MySqlConnection ( connectionString ) ;
213
241
214
242
await connection . OpenAsync ( ) ;
215
243
@@ -224,9 +252,12 @@ protected override async UniTask OnExecuteAsync()
224
252
225
253
await connection . CloseAsync ( ) ;
226
254
227
- var entries = await _dbContext . SaveChangesAsync ( ) ;
255
+ await _dbContext . SaveChangesAsync ( ) ;
256
+
257
+ var itemShops = await _dbContext . ItemShops . CountAsync ( ) ;
258
+ var vehicleShops = await _dbContext . VehicleShops . CountAsync ( ) ;
228
259
229
- await PrintAsync ( $ "Successfully migrated and written { entries } new entries to the database .") ;
260
+ await PrintAsync ( $ "Successfully migrated { itemShops } item shops and { vehicleShops } vehicle shops .") ;
230
261
231
262
if ( migrateGroups )
232
263
{
0 commit comments