55final class MigrateDumpCommand extends \Illuminate \Console \Command
66{
77 public const SCHEMA_SQL_PATH_SUFFIX = '/migrations/sql/schema.sql ' ;
8- public const SUPPORTED_DB_DRIVERS = ['mysql ' , 'pgsql ' ];
8+ public const SUPPORTED_DB_DRIVERS = ['mysql ' , 'pgsql ' , ' sqlite ' ];
99
1010 protected $ signature = 'migrate:dump
1111 {--database= : The database connection to use} ' ;
@@ -78,6 +78,9 @@ private static function mysqlDump(array $db_config, string $schema_sql_path) : i
7878 . ' --user= ' . escapeshellarg ($ db_config ['username ' ])
7979 . ' --password= ' . escapeshellarg ($ db_config ['password ' ])
8080 . ' ' . escapeshellarg ($ db_config ['database ' ]);
81+ // TODO: Suppress warning about insecure password.
82+ // CONSIDER: Intercepting stdout and stderr and converting to colorized
83+ // console output with `$this->info` and `->error`.
8184 passthru (
8285 $ command_prefix
8386 . ' --result-file= ' . escapeshellarg ($ schema_sql_path )
@@ -127,6 +130,7 @@ private static function pgsqlDump(array $db_config, string $schema_sql_path) : i
127130 . ' --port= ' . escapeshellarg ($ db_config ['port ' ])
128131 . ' --username= ' . escapeshellarg ($ db_config ['username ' ])
129132 . ' ' . escapeshellarg ($ db_config ['database ' ]);
133+ // TODO: Suppress warning about insecure password.
130134 passthru (
131135 $ command_prefix
132136 . ' --file= ' . escapeshellarg ($ schema_sql_path )
@@ -148,4 +152,41 @@ private static function pgsqlDump(array $db_config, string $schema_sql_path) : i
148152
149153 return $ exit_code ;
150154 }
155+
156+ /**
157+ * @param array $db_config like ['host' => , 'port' => ].
158+ * @param string $schema_sql_path like '.../schema.sql'
159+ *
160+ * @return int containing exit code.
161+ */
162+ private static function sqliteDump (array $ db_config , string $ schema_sql_path ) : int
163+ {
164+ // CONSIDER: Accepting command name as option or from config.
165+ $ command_prefix = 'sqlite3 ' . escapeshellarg ($ db_config ['database ' ]);
166+
167+ // Since Sqlite lacks Information Schema, and dumping everything may be
168+ // too slow or memory intense, just query tables and dump them
169+ // individually.
170+ exec ($ command_prefix . ' .tables ' , $ output , $ exit_code );
171+ if (0 !== $ exit_code ) {
172+ return $ exit_code ;
173+ }
174+ $ tables = preg_split ('/\s+/ ' , implode (' ' , $ output ));
175+
176+ foreach ($ tables as $ table ) {
177+ // Only migrations should dump data with schema.
178+ $ sql_command = 'migrations ' === $ table ? '.dump ' : '.schema ' ;
179+
180+ passthru (
181+ $ command_prefix . ' ' . escapeshellarg ("$ sql_command $ table " )
182+ . ' >> ' . escapeshellarg ($ schema_sql_path ),
183+ $ exit_code
184+ );
185+ if (0 !== $ exit_code ) {
186+ return $ exit_code ;
187+ }
188+ }
189+
190+ return $ exit_code ;
191+ }
151192}
0 commit comments