2
2
using Bit . Core . Vault . Enums ; // Change this line
3
3
using Bit . Infrastructure . EntityFramework . Repositories ;
4
4
using Bit . Infrastructure . EntityFramework . Vault . Models ;
5
+ using Bogus ;
5
6
using Microsoft . Extensions . Logging ;
6
7
7
8
@@ -38,7 +39,7 @@ public bool SeedDatabase()
38
39
39
40
// Seed the database
40
41
SeedUsers ( context ) ;
41
- SeedCipher ( context ) ;
42
+ SeedCiphers ( context ) ;
42
43
43
44
Console . WriteLine ( "Database seeded successfully!" ) ;
44
45
}
@@ -51,8 +52,12 @@ public bool SeedDatabase()
51
52
}
52
53
catch ( Exception ex )
53
54
{
54
- Console . WriteLine ( $ "An error occurred: { ex . Message } ") ;
55
- return false ;
55
+ Console . WriteLine ( $ "Error adding users: { ex . Message } ") ;
56
+ if ( ex . InnerException != null )
57
+ {
58
+ Console . WriteLine ( $ "Inner exception: { ex . InnerException . Message } ") ;
59
+ }
60
+ throw ; // Re-throw the exception to stop the seeding process
56
61
}
57
62
58
63
@@ -64,66 +69,97 @@ private void SeedUsers(DatabaseContext context)
64
69
{
65
70
if ( ! context . Users . Any ( ) )
66
71
{
67
- context . Users . AddRange (
68
- new Bit . Infrastructure . EntityFramework . Models . User // Specify the full namespace
72
+ Console . WriteLine ( "Generating 5000 users..." ) ;
73
+
74
+ var faker = new Faker < Bit . Infrastructure . EntityFramework . Models . User > ( )
75
+ . RuleFor ( u => u . Id , f => Guid . NewGuid ( ) )
76
+ . RuleFor ( u => u . Name , f => f . Name . FullName ( ) )
77
+ . RuleFor ( u => u . Email , ( f , u ) => f . Internet . Email ( u . Name ) )
78
+ . RuleFor ( u => u . EmailVerified , f => f . Random . Bool ( 0.9f ) )
79
+ . RuleFor ( u => u . SecurityStamp , f => Guid . NewGuid ( ) . ToString ( ) )
80
+ . RuleFor ( u => u . ApiKey , f => Guid . NewGuid ( ) . ToString ( "N" ) . Substring ( 0 , 30 ) )
81
+ . RuleFor ( u => u . CreationDate , f => f . Date . Past ( 2 ) )
82
+ . RuleFor ( u => u . RevisionDate , ( f , u ) => f . Date . Between ( u . CreationDate , DateTime . UtcNow ) ) ;
83
+
84
+ var users = faker . Generate ( 5000 ) ;
85
+
86
+ const int batchSize = 100 ;
87
+ for ( int i = 0 ; i < users . Count ; i += batchSize )
69
88
{
70
- Id = Guid . NewGuid ( ) ,
71
- Name = "Test User" ,
72
-
73
- EmailVerified = true ,
74
- SecurityStamp = Guid . NewGuid ( ) . ToString ( ) ,
75
- ApiKey = "TestApiKey"
89
+ context . Users . AddRange ( users . Skip ( i ) . Take ( batchSize ) ) ;
90
+ context . SaveChanges ( ) ;
91
+ Console . WriteLine ( $ "Added { Math . Min ( i + batchSize , users . Count ) } users") ;
76
92
}
77
- ) ;
78
- context . SaveChanges ( ) ;
79
- Console . WriteLine ( "Test user added to the database." ) ;
93
+
94
+ Console . WriteLine ( "5000 test users added to the database." ) ;
80
95
}
81
96
else
82
97
{
83
98
Console . WriteLine ( "Users table is not empty. Skipping user seeding." ) ;
84
99
}
85
100
}
86
101
87
- private void SeedCipher ( DatabaseContext context )
102
+ private void SeedCiphers ( DatabaseContext context )
88
103
{
89
104
if ( ! context . Ciphers . Any ( ) )
90
105
{
91
- var testUser = context . Users . FirstOrDefault ( ) ;
92
- if ( testUser == null )
106
+ var users = context . Users . ToList ( ) ;
107
+ if ( ! users . Any ( ) )
93
108
{
94
109
Console . WriteLine ( "No users found. Please seed users first." ) ;
95
110
return ;
96
111
}
97
112
98
- var cipher = new Cipher
99
- {
100
- Id = Guid . NewGuid ( ) ,
101
- UserId = testUser . Id ,
102
- OrganizationId = null , // Set this if needed
103
- Type = CipherType . Login ,
104
- Data = JsonSerializer . Serialize ( new
113
+ Console . WriteLine ( $ "Generating ciphers for { users . Count } users...") ;
114
+
115
+ var faker = new Faker < Cipher > ( )
116
+ . RuleFor ( c => c . Id , f => Guid . NewGuid ( ) )
117
+ . RuleFor ( c => c . Type , f => CipherType . Login )
118
+ . RuleFor ( c => c . Data , f => JsonSerializer . Serialize ( new
105
119
{
106
- Name = "Test Login" ,
107
- Notes = "This is a test login cipher" ,
120
+ Name = f . Internet . DomainName ( ) ,
121
+ Notes = f . Lorem . Sentence ( ) ,
108
122
Login = new
109
123
{
110
- Username = "testuser" ,
111
- Password = "testpassword" ,
112
- Uri = "https://example.com"
124
+ Username = f . Internet . UserName ( ) ,
125
+ Password = f . Internet . Password ( ) ,
126
+ Uri = f . Internet . Url ( )
127
+ }
128
+ } ) )
129
+ . RuleFor ( c => c . CreationDate , f => f . Date . Past ( 1 ) )
130
+ . RuleFor ( c => c . RevisionDate , ( f , c ) => f . Date . Between ( c . CreationDate , DateTime . UtcNow ) )
131
+ . RuleFor ( c => c . DeletedDate , f => null )
132
+ . RuleFor ( c => c . Reprompt , f => CipherRepromptType . None ) ;
133
+
134
+ const int batchSize = 100 ;
135
+ for ( int i = 0 ; i < users . Count ; i += batchSize )
136
+ {
137
+ var userBatch = users . Skip ( i ) . Take ( batchSize ) ;
138
+ var ciphers = userBatch . Select ( user =>
139
+ {
140
+ var cipher = faker . Generate ( ) ;
141
+ cipher . UserId = user . Id ;
142
+ return cipher ;
143
+ } ) . ToList ( ) ;
144
+
145
+ try
146
+ {
147
+ context . Ciphers . AddRange ( ciphers ) ;
148
+ context . SaveChanges ( ) ;
149
+ Console . WriteLine ( $ "Added ciphers for users { i + 1 } to { Math . Min ( i + batchSize , users . Count ) } ") ;
150
+ }
151
+ catch ( Exception ex )
152
+ {
153
+ Console . WriteLine ( $ "Error adding ciphers: { ex . Message } ") ;
154
+ if ( ex . InnerException != null )
155
+ {
156
+ Console . WriteLine ( $ "Inner exception: { ex . InnerException . Message } ") ;
113
157
}
114
- } ) ,
115
- Favorites = null ,
116
- Folders = null ,
117
- Attachments = null ,
118
- CreationDate = DateTime . UtcNow ,
119
- RevisionDate = DateTime . UtcNow ,
120
- DeletedDate = null ,
121
- Reprompt = CipherRepromptType . None
122
- } ;
123
-
124
- context . Ciphers . Add ( cipher ) ;
125
- context . SaveChanges ( ) ;
126
- Console . WriteLine ( "Test cipher added to the database." ) ;
158
+ throw ; // Re-throw the exception to stop the seeding process
159
+ }
160
+ }
161
+
162
+ Console . WriteLine ( $ "Ciphers added for all { users . Count } users.") ;
127
163
}
128
164
else
129
165
{
0 commit comments