@@ -131,6 +131,7 @@ private static async Task<int> MigrateSqliteToPostgres(string sqliteDbPath, stri
131131 await TruncateTable ( pgContext . Vaults , "Vaults" ) ;
132132 await TruncateTable ( pgContext . UserEncryptionKeys , "UserEncryptionKeys" ) ;
133133 await TruncateTable ( pgContext . AliasVaultUserRefreshTokens , "AliasVaultUserRefreshTokens" ) ;
134+ await TruncateTable ( pgContext . AuthLogs , "Logs" ) ;
134135 await TruncateTable ( pgContext . AuthLogs , "AuthLogs" ) ;
135136 await TruncateTable ( pgContext . DataProtectionKeys , "DataProtectionKeys" ) ;
136137 await TruncateTable ( pgContext . ServerSettings , "ServerSettings" ) ;
@@ -144,8 +145,9 @@ private static async Task<int> MigrateSqliteToPostgres(string sqliteDbPath, stri
144145 await MigrateTable ( sqliteContext . AliasVaultRoles , pgContext . AliasVaultRoles , pgContext , "AliasVaultRoles" ) ;
145146 await MigrateTable ( sqliteContext . AliasVaultUsers , pgContext . AliasVaultUsers , pgContext , "AliasVaultUsers" ) ;
146147 await MigrateTable ( sqliteContext . ServerSettings , pgContext . ServerSettings , pgContext , "ServerSettings" ) ;
147- await MigrateTable ( sqliteContext . DataProtectionKeys , pgContext . DataProtectionKeys , pgContext , "DataProtectionKeys" ) ;
148- await MigrateTable ( sqliteContext . AuthLogs , pgContext . AuthLogs , pgContext , "AuthLogs" ) ;
148+ await MigrateTable ( sqliteContext . DataProtectionKeys , pgContext . DataProtectionKeys , pgContext , "DataProtectionKeys" , true ) ;
149+ await MigrateTable ( sqliteContext . Logs , pgContext . Logs , pgContext , "Logs" , true ) ;
150+ await MigrateTable ( sqliteContext . AuthLogs , pgContext . AuthLogs , pgContext , "AuthLogs" , true ) ;
149151 await MigrateTable ( sqliteContext . AdminUsers , pgContext . AdminUsers , pgContext , "AdminUsers" ) ;
150152
151153 // Then migrate tables with foreign key dependencies
@@ -160,8 +162,8 @@ private static async Task<int> MigrateSqliteToPostgres(string sqliteDbPath, stri
160162 await MigrateTable ( sqliteContext . UserTokens , pgContext . UserTokens , pgContext , "UserTokens" ) ;
161163
162164 // Email related tables (last due to dependencies)
163- await MigrateTable ( sqliteContext . Emails , pgContext . Emails , pgContext , "Emails" ) ;
164- await MigrateTable ( sqliteContext . EmailAttachments , pgContext . EmailAttachments , pgContext , "EmailAttachments" ) ;
165+ await MigrateTable ( sqliteContext . Emails , pgContext . Emails , pgContext , "Emails" , true ) ;
166+ await MigrateTable ( sqliteContext . EmailAttachments , pgContext . EmailAttachments , pgContext , "EmailAttachments" , true ) ;
165167
166168 Console . WriteLine ( "Migration completed successfully!" ) ;
167169 return 0 ;
@@ -201,6 +203,7 @@ private static async Task TruncateTable<T>(DbSet<T> table, string tableName)
201203 /// <param name="destination">The destination database table.</param>
202204 /// <param name="destinationContext">The destination database context.</param>
203205 /// <param name="tableName">The name of the table being migrated (for logging purposes).</param>
206+ /// <param name="resetSequence">Whether to reset the sequence for the table after migration.</param>
204207 /// <returns>A task representing the asynchronous migration operation.</returns>
205208 /// <exception cref="ArgumentException">
206209 /// Thrown when the number of records in source and destination tables don't match after migration.
@@ -212,7 +215,8 @@ private static async Task MigrateTable<T>(
212215 DbSet < T > source ,
213216 DbSet < T > destination ,
214217 DbContext destinationContext ,
215- string tableName )
218+ string tableName ,
219+ bool resetSequence = false )
216220 where T : class
217221 {
218222 Console . WriteLine ( $ "Migrating { tableName } ...") ;
@@ -236,6 +240,22 @@ private static async Task MigrateTable<T>(
236240 await HandleConcurrencyConflict ( ex , destinationContext ) ;
237241 }
238242 }
243+
244+ // Only reset sequence if requested
245+ if ( resetSequence && destinationContext . Database . ProviderName == "Npgsql.EntityFrameworkCore.PostgreSQL" )
246+ {
247+ var tablePgName = destinationContext . Model . FindEntityType ( typeof ( T ) ) ? . GetTableName ( ) ;
248+ if ( ! string . IsNullOrEmpty ( tablePgName ) )
249+ {
250+ var schema = destinationContext . Model . FindEntityType ( typeof ( T ) ) ? . GetSchema ( ) ?? "public" ;
251+ var sql = $ """
252+ SELECT setval(pg_get_serial_sequence('{ schema } ."{ tablePgName } "', 'Id'),
253+ (SELECT COALESCE(MAX("Id"::integer), 0) + 1 FROM { schema } ."{ tablePgName } "), false);
254+ """ ;
255+ await destinationContext . Database . ExecuteSqlRawAsync ( sql ) ;
256+ Console . WriteLine ( $ "Reset sequence for { tableName } ") ;
257+ }
258+ }
239259 }
240260
241261 // Ensure that the amount of records in the source and destination tables match
0 commit comments