Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Replace uniqid with guarantee uniqueness #1102

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class CSVTest extends TestCase
{
public function test_loading_array_entry() : void
{
$path = \sys_get_temp_dir() . '/' . \uniqid('flow_php_etl_csv_loader', true) . '.csv';
$path = \sys_get_temp_dir() . '/flow_php_etl_csv_loader' . bin2hex(random_bytes(16)) . '.csv';

if (\file_exists($path)) {
\unlink($path);
Expand All @@ -31,7 +31,7 @@ public function test_loading_array_entry() : void

public function test_loading_csv_files() : void
{
$path = \sys_get_temp_dir() . '/' . \uniqid('flow_php_etl_csv_loader', true) . '.csv';
$path = \sys_get_temp_dir() . '/flow_php_etl_csv_loader' . bin2hex(random_bytes(16)) . '.csv';

if (\file_exists($path)) {
\unlink($path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,19 @@ public function test_integration_with_entry_factory() : void

$loader->load(new Rows(
Row::create(
new Row\Entry\StringEntry('id', \sha1(\uniqid('id', true))),
new Row\Entry\StringEntry('id', \sha1('id' . bin2hex(random_bytes(16)))),
new Row\Entry\StringEntry('name', 'Łukasz')
),
Row::create(
new Row\Entry\StringEntry('id', \sha1(\uniqid('id', true))),
new Row\Entry\StringEntry('id', \sha1('id' . bin2hex(random_bytes(16)))),
new Row\Entry\StringEntry('name', 'Norbert')
),
Row::create(
new Row\Entry\StringEntry('id', \sha1(\uniqid('id', true))),
new Row\Entry\StringEntry('id', \sha1('id' . bin2hex(random_bytes(16)))),
new Row\Entry\StringEntry('name', 'Dawid')
),
Row::create(
new Row\Entry\StringEntry('id', \sha1(\uniqid('id', true))),
new Row\Entry\StringEntry('id', \sha1('id' . bin2hex(random_bytes(16)))),
new Row\Entry\StringEntry('name', 'Tomek')
),
), new FlowContext(Config::default()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function test_open_file_stream_for_existing_file() : void

public function test_open_file_stream_for_non_existing_file() : void
{
$path = \sys_get_temp_dir() . '/' . \uniqid('flow_php_test_file_', true) . '.txt';
$path = \sys_get_temp_dir() . '/flow_php_test_file_' . bin2hex(random_bytes(16)) . '.txt';

$stream = (new FlysystemFS())->open(new Path($path), Mode::WRITE);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class JsonTest extends TestCase
{
public function test_json_loader() : void
{
$path = \sys_get_temp_dir() . '/' . \uniqid('flow_php_etl_json_loader', true) . '.json';
$path = \sys_get_temp_dir() . '/flow_php_etl_json_loader' . bin2hex(random_bytes(16)) . '.json';

if (\file_exists($path)) {
\unlink($path);
Expand All @@ -40,7 +40,7 @@ public function test_json_loader() : void

public function test_json_loader_loading_empty_string() : void
{
$stream = \sys_get_temp_dir() . '/' . \uniqid('flow_php_etl_json_loader', true) . '.json';
$stream = \sys_get_temp_dir() . '/flow_php_etl_json_loader' . bin2hex(random_bytes(16)) . '.json';

$loader = new JsonLoader(Path::realpath($stream));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,19 @@ public function test_integration_with_entry_factory() : void
$loader = to_meilisearch_bulk_index($this->meilisearchContext->clientConfig(), self::INDEX_NAME);
$loader->load(new Rows(
Row::create(
new Row\Entry\StringEntry('id', \sha1(\uniqid('id', true))),
new Row\Entry\StringEntry('id', \sha1('id' . bin2hex(random_bytes(16)))),
new Row\Entry\StringEntry('name', 'Łukasz')
),
Row::create(
new Row\Entry\StringEntry('id', \sha1(\uniqid('id', true))),
new Row\Entry\StringEntry('id', \sha1('id' . bin2hex(random_bytes(16)))),
new Row\Entry\StringEntry('name', 'Norbert')
),
Row::create(
new Row\Entry\StringEntry('id', \sha1(\uniqid('id', true))),
new Row\Entry\StringEntry('id', \sha1('id' . bin2hex(random_bytes(16)))),
new Row\Entry\StringEntry('name', 'Dawid')
),
Row::create(
new Row\Entry\StringEntry('id', \sha1(\uniqid('id', true))),
new Row\Entry\StringEntry('id', \sha1('id' . bin2hex(random_bytes(16)))),
new Row\Entry\StringEntry('name', 'Tomek')
),
), new FlowContext(Config::default()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class TextTest extends TestCase
{
public function test_loading_text_files() : void
{
$path = \sys_get_temp_dir() . '/' . \uniqid('flow_php_etl_csv_loader', true) . '.csv';
$path = \sys_get_temp_dir() . '/flow_php_etl_csv_loader' . bin2hex(random_bytes(16)) . '.csv';

(new Flow())
->process(
Expand Down
2 changes: 1 addition & 1 deletion src/core/etl/src/Flow/ETL/ConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function __construct()
*/
public function build() : Config
{
$this->id ??= \uniqid('flow_php', true);
$this->id ??= 'flow_php' . bin2hex(random_bytes(16));
$entryFactory = new NativeEntryFactory();
$this->serializer ??= new Base64Serializer(new NativePHPSerializer());
$cachePath = \is_string(\getenv(Config::CACHE_DIR_ENV)) && \realpath(\getenv(Config::CACHE_DIR_ENV))
Expand Down
4 changes: 2 additions & 2 deletions src/core/etl/src/Flow/ETL/Filesystem/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public static function realpath(string $path, array $options = []) : self

public static function tmpFile(string $extension, ?string $name = null) : self
{
$name = \ltrim($name ?? \str_replace('.', '', \uniqid('', true)), '/');
$name = \ltrim($name ?? bin2hex(random_bytes(16)), '/');

return new self(\sys_get_temp_dir() . DIRECTORY_SEPARATOR . $name . '.' . $extension);
}
Expand Down Expand Up @@ -302,7 +302,7 @@ public function randomize() : self
$base = \trim(\mb_substr($this->path(), 0, \mb_strrpos($this->path(), $this->basename())), DIRECTORY_SEPARATOR);

return new self(
$this->scheme . '://' . $base . DIRECTORY_SEPARATOR . $this->filename . '_' . \str_replace('.', '', \uniqid('', true)) . $extension,
$this->scheme . '://' . $base . DIRECTORY_SEPARATOR . $this->filename . '_' . bin2hex(random_bytes(16)) . $extension,
$this->options
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function test_saving_to_psr_simple_cache_implementation() : void
{
$cache = new PSRSimpleCache(
new Psr16Cache(
new FilesystemAdapter(directory: \sys_get_temp_dir() . '/' . \uniqid('flow-etl-cache-', true))
new FilesystemAdapter(directory: \sys_get_temp_dir() . '/flow-etl-cache-' . bin2hex(random_bytes(16)))
),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ function (int $i) : array {

for ($d = 0; $d < $maxItems; $d++) {
$data[] = [
'id' => \uniqid('', true),
'id' => bin2hex(random_bytes(16)),
'created_at' => (new \DateTimeImmutable('2020-01-01'))->add(new \DateInterval('P' . $i . 'D'))->setTime(\random_int(0, 23), \random_int(0, 59), \random_int(0, 59)),
'value' => \random_int(1, 1000),
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function test_open_file_stream_for_existing_file() : void

public function test_open_file_stream_for_non_existing_file() : void
{
$path = \sys_get_temp_dir() . '/' . \uniqid('flow_php_test_file_', true) . '.txt';
$path = \sys_get_temp_dir() . '/flow_php_test_file_' . bin2hex(random_bytes(16)) . '.txt';

$stream = (new LocalFilesystem())->open(new Path($path), Mode::WRITE);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class CompressionTest extends TestCase
{
public function test_writing_and_reading_file_with_gzip_compression() : void
{
$path = \sys_get_temp_dir() . '/test-writer' . \uniqid('parquet-test-', true) . '.parquet';
$path = \sys_get_temp_dir() . '/test-writer-parquet-test-' . bin2hex(random_bytes(16)) . '.parquet';

$writer = new Writer(Compressions::GZIP);

Expand Down Expand Up @@ -61,7 +61,7 @@ public function test_writing_and_reading_file_with_gzip_compression() : void

public function test_writing_and_reading_file_with_snappy_compression() : void
{
$path = \sys_get_temp_dir() . '/test-writer' . \uniqid('parquet-test-', true) . '.parquet';
$path = \sys_get_temp_dir() . '/test-writer-parquet-test-' . bin2hex(random_bytes(16)) . '.parquet';

$writer = new Writer(Compressions::SNAPPY);

Expand Down Expand Up @@ -108,7 +108,7 @@ public function test_writing_and_reading_file_with_snappy_compression() : void

public function test_writing_and_reading_file_with_uncompressed_compression() : void
{
$path = \sys_get_temp_dir() . '/test-writer' . \uniqid('parquet-test-', true) . '.parquet';
$path = \sys_get_temp_dir() . '/test-writer-parquet-test-' . bin2hex(random_bytes(16)) . '.parquet';

$writer = new Writer(Compressions::UNCOMPRESSED);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class ListsWritingTest extends TestCase
{
public function test_writing_list_of_ints() : void
{
$path = \sys_get_temp_dir() . '/test-writer' . \uniqid('parquet-test-', true) . '.parquet';
$path = \sys_get_temp_dir() . '/test-writer-parquet-test-' . bin2hex(random_bytes(16)) . '.parquet';

$writer = new Writer();
$schema = Schema::with(NestedColumn::list('list_of_ints', ListElement::int32()));
Expand All @@ -38,7 +38,7 @@ public function test_writing_list_of_ints() : void

public function test_writing_list_of_strings() : void
{
$path = \sys_get_temp_dir() . '/test-writer' . \uniqid('parquet-test-', true) . '.parquet';
$path = \sys_get_temp_dir() . '/test-writer-parquet-test-' . bin2hex(random_bytes(16)) . '.parquet';

$writer = new Writer();
$schema = Schema::with(NestedColumn::list('list_of_strings', ListElement::string()));
Expand All @@ -62,7 +62,7 @@ public function test_writing_list_of_strings() : void

public function test_writing_list_of_structures() : void
{
$path = \sys_get_temp_dir() . '/test-writer' . \uniqid('parquet-test-', true) . '.parquet';
$path = \sys_get_temp_dir() . '/test-writer-parquet-test-' . bin2hex(random_bytes(16)) . '.parquet';

$writer = new Writer();
$schema = Schema::with(
Expand Down Expand Up @@ -96,7 +96,7 @@ public function test_writing_list_of_structures() : void

public function test_writing_list_with_nullable_elements() : void
{
$path = \sys_get_temp_dir() . '/test-writer' . \uniqid('parquet-test-', true) . '.parquet';
$path = \sys_get_temp_dir() . '/test-writer-parquet-test-' . bin2hex(random_bytes(16)) . '.parquet';

$writer = new Writer();
$schema = Schema::with(NestedColumn::list('list_of_ints', ListElement::int32()));
Expand All @@ -122,7 +122,7 @@ public function test_writing_list_with_nullable_elements() : void

public function test_writing_list_with_nullable_list_values() : void
{
$path = \sys_get_temp_dir() . '/test-writer' . \uniqid('parquet-test-', true) . '.parquet';
$path = \sys_get_temp_dir() . '/test-writer-parquet-test-' . bin2hex(random_bytes(16)) . '.parquet';

$writer = new Writer();
$schema = Schema::with(NestedColumn::list('list_of_ints', ListElement::int32()));
Expand All @@ -148,7 +148,7 @@ public function test_writing_list_with_nullable_list_values() : void

public function test_writing_nullable_list_of_ints() : void
{
$path = \sys_get_temp_dir() . '/test-writer' . \uniqid('parquet-test-', true) . '.parquet';
$path = \sys_get_temp_dir() . '/test-writer-parquet-test-' . bin2hex(random_bytes(16)) . '.parquet';

$writer = new Writer();
$schema = Schema::with(NestedColumn::list('list_of_ints', ListElement::int32()));
Expand All @@ -174,7 +174,7 @@ public function test_writing_nullable_list_of_ints() : void

public function test_writing_nullable_list_of_structures() : void
{
$path = \sys_get_temp_dir() . '/test-writer' . \uniqid('parquet-test-', true) . '.parquet';
$path = \sys_get_temp_dir() . '/test-writer-parquet-test-' . bin2hex(random_bytes(16)) . '.parquet';

$writer = new Writer();
$schema = Schema::with(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class MapsWritingTest extends TestCase
{
public function test_writing_map_of_int_int() : void
{
$path = \sys_get_temp_dir() . '/test-writer' . \uniqid('parquet-test-', true) . '.parquet';
$path = \sys_get_temp_dir() . '/test-writer-parquet-test-' . bin2hex(random_bytes(16)) . '.parquet';

$writer = new Writer();
$schema = Schema::with(NestedColumn::map('map_int_int', MapKey::int32(), MapValue::int32()));
Expand Down Expand Up @@ -43,7 +43,7 @@ public function test_writing_map_of_int_int() : void

public function test_writing_map_of_int_string() : void
{
$path = \sys_get_temp_dir() . '/test-writer' . \uniqid('parquet-test-', true) . '.parquet';
$path = \sys_get_temp_dir() . '/test-writer-parquet-test-' . bin2hex(random_bytes(16)) . '.parquet';

$writer = new Writer();
$schema = Schema::with(NestedColumn::map('map_int_string', MapKey::int32(), MapValue::string()));
Expand Down Expand Up @@ -72,7 +72,7 @@ public function test_writing_map_of_int_string() : void

public function test_writing_nullable_map_of_int_int() : void
{
$path = \sys_get_temp_dir() . '/test-writer' . \uniqid('parquet-test-', true) . '.parquet';
$path = \sys_get_temp_dir() . '/test-writer-parquet-test-' . bin2hex(random_bytes(16)) . '.parquet';

$writer = new Writer();
$schema = Schema::with(NestedColumn::map('map_int_int', MapKey::int32(), MapValue::int32()));
Expand Down
Loading
Loading