66
77use Doctrine \DBAL \Connection ;
88use Doctrine \DBAL \DriverManager ;
9+ use Doctrine \DBAL \Schema \Name \OptionallyQualifiedName ;
910use Doctrine \DBAL \Tools \DsnParser ;
1011use Flow \CLI \Command \Traits \ConfigOptions ;
1112use Flow \CLI \Command \Traits \DBOptions ;
12- use Flow \CLI \Options \ConfigOption ;
13- use Flow \ETL \Config ;
1413use Flow \ETL \Row \Formatter \ASCIISchemaFormatter ;
1514use Flow \ETL \Schema \Formatter \PHPSchemaFormatter ;
15+ use RuntimeException ;
1616use Symfony \Component \Console \Command \Command ;
1717use Symfony \Component \Console \Input \InputArgument ;
1818use Symfony \Component \Console \Input \InputInterface ;
2727use function Flow \CLI \option_list_of_strings_nullable ;
2828use function Flow \ETL \Adapter \Doctrine \table_schema_to_flow_schema ;
2929use function Flow \ETL \DSL \schema_to_json ;
30+ use function Flow \Types \DSL \type_string ;
3031
3132final class DatabaseTableSchemaCommand extends Command
3233{
@@ -35,8 +36,6 @@ final class DatabaseTableSchemaCommand extends Command
3536
3637 private ?Connection $ connection = null ;
3738
38- private ?Config $ flowConfig = null ;
39-
4039 public function configure (): void
4140 {
4241 $ this
@@ -64,23 +63,28 @@ public function configure(): void
6463
6564 protected function execute (InputInterface $ input , OutputInterface $ output ): int
6665 {
66+ if ($ this ->connection === null ) {
67+ throw new RuntimeException ('Command not properly initialized. ' );
68+ }
69+
6770 $ style = new SymfonyStyle ($ input , $ output );
6871
6972 $ tableName = argument_string_nullable ('input-db-table ' , $ input );
7073
7174 if (!$ tableName ) {
72- $ question = new ChoiceQuestion (
73- 'Please select table name for which we are going to generate schema: ' ,
74- $ this ->connection ->createSchemaManager ()->listTableNames (),
75- );
75+ $ question =
76+ new ChoiceQuestion ('Please select table name for which we are going to generate schema: ' , array_map (
77+ static fn (OptionallyQualifiedName $ name ): string => $ name ->getUnqualifiedName ()->getValue (),
78+ $ this ->connection ->createSchemaManager ()->introspectTableNames (),
79+ ));
7680 $ question ->setErrorMessage ('Invalid table: %s ' );
77- $ tableName = $ style ->askQuestion ($ question );
81+ $ tableName = type_string ()-> assert ( $ style ->askQuestion ($ question) );
7882 }
7983
8084 $ table = null ;
8185
82- foreach ($ this ->connection ->createSchemaManager ()->listTables () as $ dbTable ) {
83- if ($ dbTable ->getName () === $ tableName ) {
86+ foreach ($ this ->connection ->createSchemaManager ()->introspectTables () as $ dbTable ) {
87+ if ($ dbTable ->getObjectName ()-> getUnqualifiedName ()-> getValue () === $ tableName ) {
8488 $ table = $ dbTable ;
8589
8690 break ;
@@ -134,19 +138,19 @@ protected function execute(InputInterface $input, OutputInterface $output): int
134138
135139 protected function initialize (InputInterface $ input , OutputInterface $ output ): void
136140 {
137- $ this ->flowConfig = (new ConfigOption ('config ' ))->get ($ input );
138-
139141 if ($ input ->getOption ('db-connection-file ' )) {
140142 $ this ->connection = option_include_file ('db-connection-file ' , $ input , Connection::class);
141143 } else {
142144 $ style = new SymfonyStyle ($ input , $ output );
143- $ connectionString = $ _ENV ['FLOW_DB_CONNECTION_STRING ' ] ?? $ style ->ask (
144- "FLOW_DB_CONNECTION_STRING env not found. \n Please provide database connection string, format: \n \"scheme://username:password@host:port/dbname?param1=value1¶m2=value2&... \"" ,
145- null ,
146- static fn ($ value ) => $ value ,
145+ $ connectionString = type_string ()->assert (
146+ $ _ENV ['FLOW_DB_CONNECTION_STRING ' ] ?? $ style ->ask (
147+ "FLOW_DB_CONNECTION_STRING env not found. \n Please provide database connection string, format: \n \"scheme://username:password@host:port/dbname?param1=value1¶m2=value2&... \"" ,
148+ null ,
149+ static fn ($ value ) => $ value ,
150+ ),
147151 );
148- $ connectionParameters = ( new DsnParser ())-> parse ( $ connectionString );
149- $ this ->connection = DriverManager::getConnection ($ connectionParameters );
152+
153+ $ this ->connection = DriverManager::getConnection (( new DsnParser ())-> parse ( $ connectionString ) );
150154 }
151155 }
152156}
0 commit comments