-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlywayDockerContainer.php
More file actions
122 lines (94 loc) · 3.74 KB
/
FlywayDockerContainer.php
File metadata and controls
122 lines (94 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
declare(strict_types=1);
namespace TinyBlocks\DockerContainer;
use TinyBlocks\DockerContainer\Contracts\ContainerStarted;
use TinyBlocks\DockerContainer\Contracts\MySQL\MySQLContainerStarted;
use TinyBlocks\DockerContainer\Waits\Conditions\MySQL\MySQLReady;
use TinyBlocks\DockerContainer\Waits\ContainerWaitForDependency;
use TinyBlocks\DockerContainer\Waits\ContainerWaitForTime;
class FlywayDockerContainer implements FlywayContainer
{
protected function __construct(private GenericDockerContainer $container)
{
}
public static function from(string $image, ?string $name = null): static
{
return new static(container: GenericDockerContainer::from(image: $image, name: $name));
}
public function migrate(): ContainerStarted
{
return $this->container->run(commands: ['migrate']);
}
public function repair(): ContainerStarted
{
return $this->container->run(commands: ['repair']);
}
public function validate(): ContainerStarted
{
return $this->container->run(commands: ['validate']);
}
public function pullImage(): static
{
$this->container->pullImage();
return $this;
}
public function withTable(string $table): static
{
$this->container->withEnvironmentVariable(key: 'FLYWAY_TABLE', value: $table);
return $this;
}
public function withSchema(string $schema): static
{
$this->container->withEnvironmentVariable(key: 'FLYWAY_SCHEMAS', value: $schema);
return $this;
}
public function withNetwork(string $name): static
{
$this->container->withNetwork(name: $name);
return $this;
}
public function withCleanDisabled(bool $disabled): static
{
$this->container->withEnvironmentVariable(key: 'FLYWAY_CLEAN_DISABLED', value: $disabled ? 'true' : 'false');
return $this;
}
public function withConnectRetries(int $retries): static
{
$this->container->withEnvironmentVariable(key: 'FLYWAY_CONNECT_RETRIES', value: (string)$retries);
return $this;
}
public function cleanAndMigrate(): ContainerStarted
{
return $this->container->run(
commands: ['clean', 'migrate'],
waitAfterStarted: ContainerWaitForTime::forSeconds(seconds: 10)
);
}
public function withMigrations(string $pathOnHost): static
{
$this->container->copyToContainer(pathOnHost: $pathOnHost, pathOnContainer: '/flyway/migrations');
$this->container->withEnvironmentVariable(key: 'FLYWAY_LOCATIONS', value: 'filesystem:/flyway/migrations');
return $this;
}
public function withValidateMigrationNaming(bool $enabled): static
{
$this->container->withEnvironmentVariable(
key: 'FLYWAY_VALIDATE_MIGRATION_NAMING',
value: $enabled ? 'true' : 'false'
);
return $this;
}
public function withSource(MySQLContainerStarted $container, string $username, string $password): static
{
$schema = $container->getEnvironmentVariables()->getValueBy(key: 'MYSQL_DATABASE');
$this->container->withEnvironmentVariable(key: 'FLYWAY_URL', value: $container->getJdbcUrl());
$this->container->withEnvironmentVariable(key: 'FLYWAY_USER', value: $username);
$this->container->withEnvironmentVariable(key: 'FLYWAY_TABLE', value: 'schema_history');
$this->container->withEnvironmentVariable(key: 'FLYWAY_SCHEMAS', value: $schema);
$this->container->withEnvironmentVariable(key: 'FLYWAY_PASSWORD', value: $password);
$this->container->withWaitBeforeRun(
wait: ContainerWaitForDependency::untilReady(condition: MySQLReady::from(container: $container))
);
return $this;
}
}