Skip to content

Commit 0894d11

Browse files
authored
improve register of schemas (#7)
* improve register of schemas
1 parent 9a28724 commit 0894d11

File tree

2 files changed

+50
-14
lines changed

2 files changed

+50
-14
lines changed

src/Command/RegisterChangedSchemasCommand.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,20 +124,23 @@ protected function isLocalSchemaCompatible(
124124
/**
125125
* @param string $schemaName
126126
* @param string $localSchema
127-
* @param string $latestVersion
128127
* @return boolean
129128
*/
130-
protected function isLocalSchemaEqualToLatestSchema(
129+
protected function isAlreadyRegistered(
131130
string $schemaName,
132-
string $localSchema,
133-
string $latestVersion
131+
string $localSchema
134132
): bool {
135-
$schema = $this->schemaRegistryApi->getSchemaByVersion(
136-
$schemaName,
137-
$latestVersion
138-
);
133+
$version = null;
134+
135+
try {
136+
$version = $this->schemaRegistryApi->getVersionForSchema(
137+
$schemaName,
138+
$localSchema
139+
);
140+
} catch (\Throwable $e) {
141+
}
139142

140-
return $schema === $localSchema;
143+
return null !== $version;
141144
}
142145

143146
/**
@@ -161,7 +164,7 @@ private function registerFiles(array $avroFiles, OutputInterface $output, array
161164
$latestVersion = $this->schemaRegistryApi->getLatestSchemaVersion($schemaName);
162165

163166
if (null !== $latestVersion) {
164-
if (true === $this->isLocalSchemaEqualToLatestSchema($schemaName, $localSchema, $latestVersion)) {
167+
if (true === $this->isAlreadyRegistered($schemaName, $localSchema)) {
165168
$output->writeln(sprintf('Schema %s has been skipped (no change)', $schemaName));
166169
continue;
167170
}

tests/Command/RegisterChangedSchemasCommandTest.php

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ class RegisterChangedSchemasCommandTest extends AbstractSchemaRegistryTestCase
1616
protected const DUMMY_SCHEMA = <<<EOF
1717
{
1818
"type": "record",
19-
"name": "evolution",
20-
"namespace": "com.landoop",
19+
"name": "test",
20+
"namespace": "ch.jobcloud",
2121
"doc": "This is a sample Avro schema to get you started. Please edit",
2222
"fields": [
2323
{
@@ -72,6 +72,11 @@ protected function generateFiles(int $numberOfFiles, string $contents = self::DU
7272
$contents
7373
);
7474
});
75+
76+
file_put_contents(
77+
sprintf('%s/test.txt', self::SCHEMA_DIRECTORY),
78+
'bla'
79+
);
7580
}
7681

7782
public function testOutputWhenCommandRegisterWithSuccess():void
@@ -113,7 +118,7 @@ public function testOutputWhenCommandSuccessWithSkipping():void
113118
/** @var MockObject|SchemaRegistryApi $schemaRegistryApi */
114119
$schemaRegistryApi = $this->makeMock(SchemaRegistryApi::class, [
115120
'checkSchemaCompatibilityForVersion' => TRUE,
116-
'getSchemaByVersion' => json_encode(json_decode(self::DUMMY_SCHEMA)),
121+
'getVersionForSchema' => 1,
117122
'createNewSchemaVersion',
118123
'getLatestSchemaVersion' => '1'
119124
]);
@@ -138,6 +143,34 @@ public function testOutputWhenCommandSuccessWithSkipping():void
138143
self::assertEquals(0, $commandTester->getStatusCode());
139144
}
140145

146+
public function testOutputWhenCommandFailsRegisteringASchema():void
147+
{
148+
$this->generateFiles(1, 'asdf');
149+
150+
/** @var MockObject|SchemaRegistryApi $schemaRegistryApi */
151+
$schemaRegistryApi = $this->makeMock(SchemaRegistryApi::class, [
152+
'checkSchemaCompatibilityForVersion' => TRUE,
153+
'getVersionForSchema' => null,
154+
'createNewSchemaVersion',
155+
'getLatestSchemaVersion' => '1'
156+
]);
157+
158+
$application = new Application();
159+
$application->add(new RegisterChangedSchemasCommand($schemaRegistryApi));
160+
$command = $application->find('kafka-schema-registry:register:changed');
161+
$commandTester = new CommandTester($command);
162+
163+
$commandTester->execute([
164+
'schemaDirectory' => self::SCHEMA_DIRECTORY
165+
]);
166+
167+
$commandOutput = trim($commandTester->getDisplay());
168+
169+
self::assertStringContainsString('Skipping test.schema.1 for now because is not a schema we know about.', $commandOutput);
170+
171+
self::assertEquals(1, $commandTester->getStatusCode());
172+
}
173+
141174
public function testOutputTotalFailDueToIncompatibility():void
142175
{
143176
$this->generateFiles(5);
@@ -165,4 +198,4 @@ public function testOutputTotalFailDueToIncompatibility():void
165198

166199
self::assertEquals(1, $commandTester->getStatusCode());
167200
}
168-
}
201+
}

0 commit comments

Comments
 (0)