2525$ genresPool = ['fashion ' , 'food ' , 'travel ' , 'music ' , 'lifestyle ' , 'fitness ' , 'diy ' , 'sports ' , 'finance ' ];
2626$ tagsPool = ['short ' , 'quick ' , 'easy ' , 'medium ' , 'hard ' ];
2727
28-
2928/**
3029 * @Example
3130 * docker compose exec tests bin/load --adapter=mariadb --limit=1000
3534 ->desc ('Load database with mock data for testing ' )
3635 ->param ('adapter ' , '' , new Text (0 ), 'Database adapter ' )
3736 ->param ('limit ' , 0 , new Integer (true ), 'Total number of records to add to database ' )
38- ->param ('name ' , 'myapp_ ' . uniqid (), new Text (0 ), 'Name of created database. ' , true )
37+ ->param ('name ' , 'myapp_ ' . uniqid (), new Text (0 ), 'Name of created database. ' , true )
3938 ->param ('sharedTables ' , false , new Boolean (true ), 'Whether to use shared tables ' , true )
4039 ->action (function (string $ adapter , int $ limit , string $ name , bool $ sharedTables ) {
4140
42-
4341 $ createSchema = function (Database $ database ): void {
4442 if ($ database ->exists ($ database ->getDatabase ())) {
4543 $ database ->delete ($ database ->getDatabase ());
6159 $ database ->createIndex ('articles ' , 'text ' , Database::INDEX_FULLTEXT , ['text ' ]);
6260 };
6361
64-
6562 $ start = null ;
6663 $ namespace = '_ns ' ;
67- $ cache = new Cache (new NoCache () );
64+ $ cache = new Cache (new NoCache );
6865
6966 Console::info ("Filling {$ adapter } with {$ limit } records: {$ name }" );
7067
71- //Runtime::enableCoroutine();
68+ // Runtime::enableCoroutine();
7269
7370 $ dbAdapters = [
7471 'mariadb ' => [
103100 ],
104101 ];
105102
106- if (!isset ($ dbAdapters [$ adapter ])) {
103+ if (! isset ($ dbAdapters [$ adapter ])) {
107104 Console::error ("Adapter ' {$ adapter }' not supported " );
105+
108106 return ;
109107 }
110108
111109 $ cfg = $ dbAdapters [$ adapter ];
112110 $ dsn = ($ cfg ['dsn ' ])($ cfg ['host ' ], $ cfg ['port ' ]);
113111
114- //Co\run(function () use (&$start, $limit, $name, $sharedTables, $namespace, $cache, $cfg) {
112+ // Co\run(function () use (&$start, $limit, $name, $sharedTables, $namespace, $cache, $cfg) {
115113 $ pdo = new PDO (
116114 $ dsn ,
117115 $ cfg ['user ' ],
127125 );
128126
129127 $ pool = new PDOPool (
130- (new PDOConfig () )
128+ (new PDOConfig )
131129 ->withDriver ($ cfg ['driver ' ])
132130 ->withHost ($ cfg ['host ' ])
133131 ->withPort ($ cfg ['port ' ])
134132 ->withDbName ($ name )
135- //->withCharset('utf8mb4')
133+ // ->withCharset('utf8mb4')
136134 ->withUsername ($ cfg ['user ' ])
137135 ->withPassword ($ cfg ['pass ' ]),
138136 128
141139 $ start = \microtime (true );
142140
143141 for ($ i = 0 ; $ i < $ limit / 1000 ; $ i ++) {
144- //\go(function () use ($cfg, $pool, $name, $namespace, $sharedTables, $cache) {
142+ // \go(function () use ($cfg, $pool, $name, $namespace, $sharedTables, $cache) {
145143 try {
146- //$pdo = $pool->get();
144+ // $pdo = $pool->get();
147145
148146 $ database = (new Database (new ($ cfg ['adapter ' ])($ pdo ), $ cache ))
149147 ->setDatabase ($ name )
150148 ->setNamespace ($ namespace )
151149 ->setSharedTables ($ sharedTables );
152150
153151 createDocuments ($ database );
154- //$pool->put($pdo);
152+ // $pool->put($pdo);
155153 } catch (\Throwable $ error ) {
156- Console::error ('Coroutine error: ' . $ error ->getMessage ());
154+ Console::error ('Coroutine error: ' . $ error ->getMessage ());
157155 }
158- //});
156+ // });
159157 }
160158
161159 $ time = microtime (true ) - $ start ;
162160 Console::success ("Completed in {$ time } seconds " );
163161 });
164162
165-
166-
167163function createDocuments (Database $ database ): void
168164{
169165 global $ namesPool , $ genresPool , $ tagsPool ;
@@ -176,7 +172,7 @@ function createDocuments(Database $database): void
176172 $ bytes = \random_bytes (intdiv ($ length + 1 , 2 ));
177173 $ text = \substr (\bin2hex ($ bytes ), 0 , $ length );
178174 $ tagCount = \mt_rand (1 , count ($ tagsPool ));
179- $ tagKeys = (array )\array_rand ($ tagsPool , $ tagCount );
175+ $ tagKeys = (array ) \array_rand ($ tagsPool , $ tagCount );
180176 $ tags = \array_map (fn ($ k ) => $ tagsPool [$ k ], $ tagKeys );
181177
182178 $ documents [] = new Document ([
0 commit comments