File tree Expand file tree Collapse file tree 5 files changed +44
-4
lines changed Expand file tree Collapse file tree 5 files changed +44
-4
lines changed Original file line number Diff line number Diff line change @@ -81,7 +81,7 @@ public function handle(): void
81
81
{
82
82
$ previousConnection = DB ::getDefaultConnection ();
83
83
try {
84
- $ this ->setup ();
84
+ $ this ->setup ($ previousConnection );
85
85
86
86
$ connection = $ this ->option ('connection ' ) ?: $ previousConnection ;
87
87
@@ -116,10 +116,13 @@ public function handle(): void
116
116
/**
117
117
* Setup by setting configuration + command options into Setting.
118
118
* Setting is a singleton and will be used as generator configuration.
119
+ *
120
+ * @param string $connection The default DB connection name.
119
121
*/
120
- protected function setup (): void
122
+ protected function setup (string $ connection ): void
121
123
{
122
124
$ setting = app (Setting::class);
125
+ $ setting ->setDefaultConnection ($ connection );
123
126
$ setting ->setUseDBCollation ($ this ->option ('use-db-collation ' ));
124
127
$ setting ->setIgnoreIndexNames ($ this ->option ('default-index-names ' ));
125
128
$ setting ->setIgnoreForeignKeyNames ($ this ->option ('default-fk-names ' ));
Original file line number Diff line number Diff line change 2
2
3
3
namespace KitLoong \MigrationsGenerator \Migration \Blueprint ;
4
4
5
- use Illuminate \Support \Facades \Config ;
6
5
use KitLoong \MigrationsGenerator \Enum \Migrations \Method \SchemaBuilder ;
7
6
use KitLoong \MigrationsGenerator \Migration \Enum \Space ;
7
+ use KitLoong \MigrationsGenerator \Setting ;
8
8
use KitLoong \MigrationsGenerator \Support \TableName ;
9
9
10
10
class SchemaBlueprint implements WritableBlueprint
@@ -56,8 +56,10 @@ public function toString(): string
56
56
*/
57
57
private function getLines (): array
58
58
{
59
+ $ setting = app (Setting::class);
60
+
59
61
$ schema = "Schema:: $ this ->schemaBuilder " ;
60
- if ($ this ->connection !== Config:: get ( ' database.default ' )) {
62
+ if ($ this ->connection !== $ setting -> getDefaultConnection ( )) {
61
63
$ schema = "Schema:: " . SchemaBuilder::CONNECTION () . "(' $ this ->connection ')-> $ this ->schemaBuilder " ;
62
64
}
63
65
Original file line number Diff line number Diff line change 6
6
7
7
class Setting
8
8
{
9
+ /**
10
+ * The default DB connection name, also known as "previous" connection name if migration is called
11
+ * with `--connection=other` option.
12
+ *
13
+ * @var string
14
+ */
15
+ private $ defaultConnection ;
16
+
9
17
/** @var boolean */
10
18
private $ useDBCollation ;
11
19
@@ -36,6 +44,22 @@ class Setting
36
44
/** @var string */
37
45
private $ fkFilename ;
38
46
47
+ /**
48
+ * @return string
49
+ */
50
+ public function getDefaultConnection (): string
51
+ {
52
+ return $ this ->defaultConnection ;
53
+ }
54
+
55
+ /**
56
+ * @param string $defaultConnection
57
+ */
58
+ public function setDefaultConnection (string $ defaultConnection ): void
59
+ {
60
+ $ this ->defaultConnection = $ defaultConnection ;
61
+ }
62
+
39
63
/**
40
64
* @return bool
41
65
*/
Original file line number Diff line number Diff line change 3
3
namespace KitLoong \MigrationsGenerator \Tests \Feature \MySQL57 ;
4
4
5
5
use Illuminate \Support \Facades \DB ;
6
+ use Illuminate \Support \Facades \File ;
6
7
use Illuminate \Support \Facades \Schema ;
7
8
use PDO ;
8
9
@@ -80,6 +81,11 @@ public function testDBConnection()
80
81
};
81
82
82
83
$ this ->verify ($ migrateTemplates , $ generateMigrations );
84
+
85
+ $ this ->assertStringContainsString (
86
+ 'Schema::connection ' ,
87
+ File::files ($ this ->storageMigrations ())[0 ]->getContents ()
88
+ );
83
89
}
84
90
85
91
public function testLogMigrationToAnotherSource ()
Original file line number Diff line number Diff line change 2
2
3
3
namespace KitLoong \MigrationsGenerator \Tests ;
4
4
5
+ use Illuminate \Support \Facades \DB ;
5
6
use KitLoong \MigrationsGenerator \Enum \Migrations \Method \SchemaBuilder ;
6
7
use KitLoong \MigrationsGenerator \Migration \Blueprint \SchemaBlueprint ;
7
8
use KitLoong \MigrationsGenerator \Migration \Blueprint \TableBlueprint ;
8
9
use KitLoong \MigrationsGenerator \Migration \Enum \MigrationFileType ;
9
10
use KitLoong \MigrationsGenerator \Migration \Writer \MigrationWriter ;
11
+ use KitLoong \MigrationsGenerator \Setting ;
10
12
use KitLoong \MigrationsGenerator \Support \TableName ;
11
13
use Mockery \MockInterface ;
12
14
@@ -17,6 +19,9 @@ class MigrationWriterTest extends TestCase
17
19
*/
18
20
public function testWrite ()
19
21
{
22
+ $ setting = app (Setting::class);
23
+ $ setting ->setDefaultConnection (DB ::getDefaultConnection ());
24
+
20
25
$ this ->mock (TableName::class, function (MockInterface $ mock ) {
21
26
$ mock ->shouldReceive ('stripPrefix ' )
22
27
->andReturn ('test ' );
You can’t perform that action at this time.
0 commit comments