66use Illuminate \Database \Console \Migrations \MigrateMakeCommand as BaseCommand ;
77use Illuminate \Support \Composer ;
88use Illuminate \Support \Str ;
9+ use InvalidArgumentException ;
910
1011class MigrateMakeCommand extends BaseCommand
1112{
12- protected $ signature = 'sqlout:make-migration {connection? : Name of the connection}
13+ protected $ signature = 'sqlout:make-migration
14+ {connection? : Name of the connection}
15+ {table? : Name of the table}
16+ {--model= : The model that the index is for}
1317 {--name= : The name of the migration.}
1418 {--path= : The location where the migration file should be created.}
1519 {--realpath : Indicate any provided migration file paths are pre-resolved absolute paths.}
@@ -23,23 +27,38 @@ public function __construct(MigrationCreator $creator, Composer $composer)
2327
2428 public function handle ()
2529 {
26- $ connection = $ this ->input ->getArgument ('connection ' ) ?? config ('database.default ' );
30+ $ name = $ this ->input ->getOption ('name ' );
31+
32+ if ($ modelClass = $ this ->input ->getOption ('model ' )) {
33+ if (!class_exists ($ modelClass )) {
34+ throw new InvalidArgumentException ("$ modelClass class does not exist! " );
35+ }
36+ if (!method_exists ($ modelClass , 'searchableAs ' )) {
37+ throw new InvalidArgumentException ("$ modelClass class is not searchable! " );
38+ }
39+ $ model = new $ modelClass ();
40+ $ connection = $ model ->getConnectionName ();
41+ $ table = $ model ->searchableAs ();
42+ $ name = 'create sqlout index for ' . class_basename ($ modelClass );
43+ } else {
44+ $ connection = $ this ->input ->getArgument ('connection ' ) ?? config ('database.default ' );
45+ $ table = $ this ->input ->getArgument ('table ' ) ?? config ('scout.sqlout.table_name ' );
46+ $ name = "create sqlout index $ connection $ table " ;
47+ }
2748
28- $ this ->writeSqloutMigration ($ connection );
49+ $ this ->writeSqloutMigration ($ name , $ connection, $ table );
2950 $ this ->composer ->dumpAutoloads ();
3051
3152 if ($ this ->input ->hasOption ('migrate ' ) && $ this ->option ('migrate ' )) {
3253 $ this ->call ('migrate ' );
3354 }
3455 }
3556
36- protected function writeSqloutMigration ($ connection )
57+ protected function writeSqloutMigration ($ name , $ connection, $ tableName )
3758 {
3859 // Get the name for the migration file:
39- $ name = $ this ->input ->getOption ('name ' ) ?: 'create_sqlout_index_for_ ' . $ connection ;
4060 $ name = Str::snake (trim ($ name ));
4161 $ className = Str::studly ($ name );
42- $ tableName = config ('scout.sqlout.table_name ' );
4362
4463 // Generate the content of the migration file:
4564 $ contents = $ this ->getMigrationContents ($ className , $ connection , $ tableName );
0 commit comments