From 37b2e2815d49f31000178f3bfbf4cf5f06e51d9f Mon Sep 17 00:00:00 2001 From: ingeniumed Date: Tue, 11 Feb 2025 14:57:05 +1100 Subject: [PATCH 01/11] Add the migration service, and a sample migration for the SFCC --- inc/Config/ArraySerializable.php | 13 +++- inc/Config/ArraySerializableInterface.php | 10 +++ inc/Config/DataSource/HttpDataSource.php | 59 +++++++++++++++ .../SalesforceB2C/SalesforceB2CDataSource.php | 11 ++- inc/MigrationService/MigrationService.php | 72 +++++++++++++++++++ remote-data-blocks.php | 3 + 6 files changed, 166 insertions(+), 2 deletions(-) create mode 100644 inc/MigrationService/MigrationService.php diff --git a/inc/Config/ArraySerializable.php b/inc/Config/ArraySerializable.php index e3373c5c2..679c6cfd1 100644 --- a/inc/Config/ArraySerializable.php +++ b/inc/Config/ArraySerializable.php @@ -13,7 +13,11 @@ * ArraySerializable class */ abstract class ArraySerializable implements ArraySerializableInterface { - final private function __construct( protected array $config ) {} + public readonly bool $perform_migrations_only; + + final private function __construct( protected array $config, bool $perform_migrations_only = false ) { + $this->perform_migrations_only = $perform_migrations_only; + } protected function get_or_call_from_config( string $property_name, mixed ...$callable_args ): mixed { $config_value = $this->config[ $property_name ] ?? null; @@ -44,6 +48,13 @@ public static function from_array( array $config, ?ValidatorInterface $validator return new static( $sanitized ); } + /** + * @inheritDoc + */ + public static function from_array_for_migrations( array $config ): self { + return new static( $config, true ); + } + /** * @inheritDoc */ diff --git a/inc/Config/ArraySerializableInterface.php b/inc/Config/ArraySerializableInterface.php index 42fc24916..39096dec5 100644 --- a/inc/Config/ArraySerializableInterface.php +++ b/inc/Config/ArraySerializableInterface.php @@ -28,4 +28,14 @@ public static function from_array( array $config, ?ValidatorInterface $validator * @return array An associative array representing the object's current state. */ public function to_array(): array; + + /** + * Creates an instance of the class from an array representation for migrations only. + * + * Note that, no other operations will be allowed. + * + * @param array $config An associative array containing the configuration or data needed to create an instance of the class. + * @return mixed Returns a new instance of the implementing class. + */ + public static function from_array_for_migrations( array $config ): mixed; } diff --git a/inc/Config/DataSource/HttpDataSource.php b/inc/Config/DataSource/HttpDataSource.php index 9942a1686..87c844425 100644 --- a/inc/Config/DataSource/HttpDataSource.php +++ b/inc/Config/DataSource/HttpDataSource.php @@ -76,6 +76,25 @@ public static function from_uuid( string $uuid ): DataSourceInterface|WP_Error { return static::from_array( $config ); } + /** + * @inheritDoc + */ + public static function from_array_for_migrations( array $config ): self { + $service_config = $config['service_config'] ?? []; + + return parent::from_array_for_migrations( + array_merge( + static::map_service_config( $service_config ), + [ + // Store the exact data used to create the instance to preserve determinism. + 'service' => static::SERVICE_NAME, + 'service_config' => $service_config, + 'uuid' => $config['uuid'] ?? null, + ] + ) + ); + } + /** * @inheritDoc * @@ -89,6 +108,38 @@ final public function to_array(): array { ]; } + /** + * Performs a migration if the config is out of date. + */ + final public function perform_migration( array $service_config ): array { + // By default, we want to have an active data source. + if ( ! isset( $service_config['active'] ) ) { + $service_config['active'] = true; + } + + // By default, we want to have no error. + if ( ! isset( $service_config['error'] ) ) { + $service_config['error'] = null; + } + + if ( static::SERVICE_SCHEMA_VERSION === $service_config['__version'] ) { + return $service_config; + } + + $migrated_service_config = $this->migrate_config( $service_config ); + + if ( is_wp_error( $migrated_service_config ) ) { + $service_config['active'] = false; + $service_config['error'] = $migrated_service_config->get_error_message(); + } else { + $service_config['__version'] = static::SERVICE_SCHEMA_VERSION; + $service_config['active'] = true; + $service_config['error'] = null; + } + + return $service_config; + } + /** * @inheritDoc */ @@ -107,4 +158,12 @@ protected static function map_service_config( array $service_config ): array { 'request_headers' => $service_config['request_headers'] ?? [], ]; } + + /** + * Migrates the config to the current schema version. + * Can be overridden by child classes to perform custom migrations. + */ + protected function migrate_config( array $service_config ): array|WP_Error { + return $service_config; + } } diff --git a/inc/Integrations/SalesforceB2C/SalesforceB2CDataSource.php b/inc/Integrations/SalesforceB2C/SalesforceB2CDataSource.php index 806f7ae90..17a7ef7f2 100644 --- a/inc/Integrations/SalesforceB2C/SalesforceB2CDataSource.php +++ b/inc/Integrations/SalesforceB2C/SalesforceB2CDataSource.php @@ -4,13 +4,14 @@ use RemoteDataBlocks\Config\DataSource\HttpDataSource; use RemoteDataBlocks\Validation\Types; +use WP_Error; use function plugins_url; defined( 'ABSPATH' ) || exit(); class SalesforceB2CDataSource extends HttpDataSource { protected const SERVICE_NAME = REMOTE_DATA_BLOCKS_SALESFORCE_B2C_SERVICE; - protected const SERVICE_SCHEMA_VERSION = 1; + protected const SERVICE_SCHEMA_VERSION = 2; protected static function get_service_config_schema(): array { return Types::object( [ @@ -34,4 +35,12 @@ protected static function map_service_config( array $service_config ): array { ], ]; } + + protected function migrate_config( array $service_config ): array|WP_Error { + if ( ! isset( $service_config['site_id'] ) ) { + $service_config['site_id'] = 'RefArchGlobal'; + } + + return $service_config; + } } diff --git a/inc/MigrationService/MigrationService.php b/inc/MigrationService/MigrationService.php new file mode 100644 index 000000000..355cd5228 --- /dev/null +++ b/inc/MigrationService/MigrationService.php @@ -0,0 +1,72 @@ +perform_migration( $data_source['service_config'] ); + if ( is_wp_error( $migrated_service_config ) ) { + return $migrated_service_config; + } + + // verify if the configs are the same so we can exit early + if ( $data_source['service_config'] === $migrated_service_config ) { + continue; + } + + $updated_config = DataSourceCrud::update_config_by_uuid( $data_source['uuid'], $migrated_service_config ); + if ( is_wp_error( $updated_config ) ) { + return $updated_config; + } + } + + self::set_migration_version(); + + return true; + } + + private static function set_migration_version(): bool|WP_Error { + return update_option( self::MIGRATION_OPTION_NAME, PluginSettings::get_version() ); + } + + private static function get_migration_version(): string { + return get_option( self::MIGRATION_OPTION_NAME, '' ); + } +} diff --git a/remote-data-blocks.php b/remote-data-blocks.php index 996feaf11..2f1f58874 100644 --- a/remote-data-blocks.php +++ b/remote-data-blocks.php @@ -52,6 +52,9 @@ // REST endpoints REST\RemoteDataController::init(); +// Migrations +MigrationService\MigrationService::init(); + // Plugin developers: If you need to register additional code for testing, you // can do so here, e.g.: // require_once __DIR__ . '/example/shopify/register.php'; From d83d9b50000991ac5442ecd8de808346a51761e3 Mon Sep 17 00:00:00 2001 From: ingeniumed Date: Tue, 11 Feb 2025 15:04:35 +1100 Subject: [PATCH 02/11] Tweaking the migration code --- inc/Integrations/SalesforceB2C/SalesforceB2CDataSource.php | 2 +- remote-data-blocks.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/inc/Integrations/SalesforceB2C/SalesforceB2CDataSource.php b/inc/Integrations/SalesforceB2C/SalesforceB2CDataSource.php index 17a7ef7f2..be7bbe20e 100644 --- a/inc/Integrations/SalesforceB2C/SalesforceB2CDataSource.php +++ b/inc/Integrations/SalesforceB2C/SalesforceB2CDataSource.php @@ -11,7 +11,7 @@ class SalesforceB2CDataSource extends HttpDataSource { protected const SERVICE_NAME = REMOTE_DATA_BLOCKS_SALESFORCE_B2C_SERVICE; - protected const SERVICE_SCHEMA_VERSION = 2; + protected const SERVICE_SCHEMA_VERSION = 1; protected static function get_service_config_schema(): array { return Types::object( [ diff --git a/remote-data-blocks.php b/remote-data-blocks.php index 2f1f58874..dff1fc8fc 100644 --- a/remote-data-blocks.php +++ b/remote-data-blocks.php @@ -42,6 +42,9 @@ // Load Settings Page PluginSettings\PluginSettings::init(); +// Migrations +MigrationService\MigrationService::init(); + // Integrations Integrations\Airtable\AirtableIntegration::init(); Integrations\Google\Sheets\GoogleSheetsIntegration::init(); @@ -52,9 +55,6 @@ // REST endpoints REST\RemoteDataController::init(); -// Migrations -MigrationService\MigrationService::init(); - // Plugin developers: If you need to register additional code for testing, you // can do so here, e.g.: // require_once __DIR__ . '/example/shopify/register.php'; From 16297bf7cbd104a814a02939245312a5a72fa893 Mon Sep 17 00:00:00 2001 From: ingeniumed Date: Wed, 12 Feb 2025 15:23:55 +1100 Subject: [PATCH 03/11] Make use of the migrations variable and fix the return value of the migration version --- inc/Config/DataSource/HttpDataSource.php | 4 ++++ inc/MigrationService/MigrationService.php | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/inc/Config/DataSource/HttpDataSource.php b/inc/Config/DataSource/HttpDataSource.php index 87c844425..5bcb21eef 100644 --- a/inc/Config/DataSource/HttpDataSource.php +++ b/inc/Config/DataSource/HttpDataSource.php @@ -112,6 +112,10 @@ final public function to_array(): array { * Performs a migration if the config is out of date. */ final public function perform_migration( array $service_config ): array { + if ( ! $this->perform_migrations_only ) { + return new WP_Error( 'migration_not_allowed', 'Migrations are not allowed for this data source.' ); + } + // By default, we want to have an active data source. if ( ! isset( $service_config['active'] ) ) { $service_config['active'] = true; diff --git a/inc/MigrationService/MigrationService.php b/inc/MigrationService/MigrationService.php index 355cd5228..450d7a19d 100644 --- a/inc/MigrationService/MigrationService.php +++ b/inc/MigrationService/MigrationService.php @@ -62,8 +62,8 @@ public static function migrate_data_source_configs( ): WP_Error|bool { return true; } - private static function set_migration_version(): bool|WP_Error { - return update_option( self::MIGRATION_OPTION_NAME, PluginSettings::get_version() ); + private static function set_migration_version(): void { + update_option( self::MIGRATION_OPTION_NAME, PluginSettings::get_version() ); } private static function get_migration_version(): string { From 4cb4bbb8c0a4776df0df22c10c86a1f800d4d238 Mon Sep 17 00:00:00 2001 From: ingeniumed Date: Wed, 12 Feb 2025 15:33:10 +1100 Subject: [PATCH 04/11] Fix the phpcs issue --- inc/MigrationService/MigrationService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/MigrationService/MigrationService.php b/inc/MigrationService/MigrationService.php index 450d7a19d..1e404c587 100644 --- a/inc/MigrationService/MigrationService.php +++ b/inc/MigrationService/MigrationService.php @@ -17,7 +17,7 @@ public static function init(): void { add_action( 'init', [ __CLASS__, 'migrate_data_source_configs' ] ); } - public static function migrate_data_source_configs( ): WP_Error|bool { + public static function migrate_data_source_configs(): WP_Error|bool { $migration_version = self::get_migration_version(); if ( $migration_version === PluginSettings::get_version() ) { From 21ddd46c70c4160d95ab7bb578b9ce13b10d8c43 Mon Sep 17 00:00:00 2001 From: ingeniumed Date: Wed, 12 Feb 2025 15:34:15 +1100 Subject: [PATCH 05/11] Fix the yoda condition problems --- inc/MigrationService/MigrationService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/MigrationService/MigrationService.php b/inc/MigrationService/MigrationService.php index 1e404c587..06dfc0513 100644 --- a/inc/MigrationService/MigrationService.php +++ b/inc/MigrationService/MigrationService.php @@ -20,7 +20,7 @@ public static function init(): void { public static function migrate_data_source_configs(): WP_Error|bool { $migration_version = self::get_migration_version(); - if ( $migration_version === PluginSettings::get_version() ) { + if ( PluginSettings::get_version() === $migration_version ) { return true; } From 72925902c507fb5c6c27475cb7deb2ae430e9580 Mon Sep 17 00:00:00 2001 From: ingeniumed Date: Thu, 13 Feb 2025 09:29:03 +1100 Subject: [PATCH 06/11] Attempting to refactor the migration strategy based on feedback --- inc/Config/ArraySerializable.php | 12 +-- inc/Config/ArraySerializableInterface.php | 10 --- inc/Config/DataSource/HttpDataSource.php | 86 +++++++------------ .../SalesforceB2C/SalesforceB2CDataSource.php | 12 +-- inc/MigrationService/MigrationService.php | 72 ---------------- remote-data-blocks.php | 3 - 6 files changed, 36 insertions(+), 159 deletions(-) delete mode 100644 inc/MigrationService/MigrationService.php diff --git a/inc/Config/ArraySerializable.php b/inc/Config/ArraySerializable.php index 679c6cfd1..0088ea2a1 100644 --- a/inc/Config/ArraySerializable.php +++ b/inc/Config/ArraySerializable.php @@ -13,10 +13,7 @@ * ArraySerializable class */ abstract class ArraySerializable implements ArraySerializableInterface { - public readonly bool $perform_migrations_only; - - final private function __construct( protected array $config, bool $perform_migrations_only = false ) { - $this->perform_migrations_only = $perform_migrations_only; + final private function __construct( protected array $config ) { } protected function get_or_call_from_config( string $property_name, mixed ...$callable_args ): mixed { @@ -48,13 +45,6 @@ public static function from_array( array $config, ?ValidatorInterface $validator return new static( $sanitized ); } - /** - * @inheritDoc - */ - public static function from_array_for_migrations( array $config ): self { - return new static( $config, true ); - } - /** * @inheritDoc */ diff --git a/inc/Config/ArraySerializableInterface.php b/inc/Config/ArraySerializableInterface.php index 39096dec5..42fc24916 100644 --- a/inc/Config/ArraySerializableInterface.php +++ b/inc/Config/ArraySerializableInterface.php @@ -28,14 +28,4 @@ public static function from_array( array $config, ?ValidatorInterface $validator * @return array An associative array representing the object's current state. */ public function to_array(): array; - - /** - * Creates an instance of the class from an array representation for migrations only. - * - * Note that, no other operations will be allowed. - * - * @param array $config An associative array containing the configuration or data needed to create an instance of the class. - * @return mixed Returns a new instance of the implementing class. - */ - public static function from_array_for_migrations( array $config ): mixed; } diff --git a/inc/Config/DataSource/HttpDataSource.php b/inc/Config/DataSource/HttpDataSource.php index 5bcb21eef..5e3f52522 100644 --- a/inc/Config/DataSource/HttpDataSource.php +++ b/inc/Config/DataSource/HttpDataSource.php @@ -45,6 +45,8 @@ final public function get_service_name(): string { * define their own validation schema. */ public static function from_array( array $config, ?ValidatorInterface $validator = null ): self|WP_Error { + $config = static::perform_migration( $config ); + $service_config = $config['service_config'] ?? []; $validator = $validator ?? new Validator( static::get_service_config_schema() ); $validated = $validator->validate( $service_config ); @@ -66,6 +68,31 @@ public static function from_array( array $config, ?ValidatorInterface $validator ); } + /** + * Performs a migration if the config is out of date. + */ + private static function perform_migration( array $config ): array { + // By default, we want to have an active data source. + if ( ! isset( $config['active'] ) ) { + $config['active'] = true; + } + + if ( static::SERVICE_SCHEMA_VERSION === $config['service_config']['__version'] ) { + return $config; + } + + $migrated_config = static::migrate_config( $config ); + + if ( is_wp_error( $migrated_config ) ) { + $config['active'] = false; + } else { + $config['service_config']['__version'] = static::SERVICE_SCHEMA_VERSION; + $config['active'] = true; + } + + return $config; + } + public static function from_uuid( string $uuid ): DataSourceInterface|WP_Error { $config = DataSourceCrud::get_config_by_uuid( $uuid ); @@ -76,25 +103,6 @@ public static function from_uuid( string $uuid ): DataSourceInterface|WP_Error { return static::from_array( $config ); } - /** - * @inheritDoc - */ - public static function from_array_for_migrations( array $config ): self { - $service_config = $config['service_config'] ?? []; - - return parent::from_array_for_migrations( - array_merge( - static::map_service_config( $service_config ), - [ - // Store the exact data used to create the instance to preserve determinism. - 'service' => static::SERVICE_NAME, - 'service_config' => $service_config, - 'uuid' => $config['uuid'] ?? null, - ] - ) - ); - } - /** * @inheritDoc * @@ -108,42 +116,6 @@ final public function to_array(): array { ]; } - /** - * Performs a migration if the config is out of date. - */ - final public function perform_migration( array $service_config ): array { - if ( ! $this->perform_migrations_only ) { - return new WP_Error( 'migration_not_allowed', 'Migrations are not allowed for this data source.' ); - } - - // By default, we want to have an active data source. - if ( ! isset( $service_config['active'] ) ) { - $service_config['active'] = true; - } - - // By default, we want to have no error. - if ( ! isset( $service_config['error'] ) ) { - $service_config['error'] = null; - } - - if ( static::SERVICE_SCHEMA_VERSION === $service_config['__version'] ) { - return $service_config; - } - - $migrated_service_config = $this->migrate_config( $service_config ); - - if ( is_wp_error( $migrated_service_config ) ) { - $service_config['active'] = false; - $service_config['error'] = $migrated_service_config->get_error_message(); - } else { - $service_config['__version'] = static::SERVICE_SCHEMA_VERSION; - $service_config['active'] = true; - $service_config['error'] = null; - } - - return $service_config; - } - /** * @inheritDoc */ @@ -167,7 +139,7 @@ protected static function map_service_config( array $service_config ): array { * Migrates the config to the current schema version. * Can be overridden by child classes to perform custom migrations. */ - protected function migrate_config( array $service_config ): array|WP_Error { - return $service_config; + protected static function migrate_config( array $config ): array { + return $config; } } diff --git a/inc/Integrations/SalesforceB2C/SalesforceB2CDataSource.php b/inc/Integrations/SalesforceB2C/SalesforceB2CDataSource.php index be7bbe20e..c2e629b41 100644 --- a/inc/Integrations/SalesforceB2C/SalesforceB2CDataSource.php +++ b/inc/Integrations/SalesforceB2C/SalesforceB2CDataSource.php @@ -4,14 +4,13 @@ use RemoteDataBlocks\Config\DataSource\HttpDataSource; use RemoteDataBlocks\Validation\Types; -use WP_Error; use function plugins_url; defined( 'ABSPATH' ) || exit(); class SalesforceB2CDataSource extends HttpDataSource { protected const SERVICE_NAME = REMOTE_DATA_BLOCKS_SALESFORCE_B2C_SERVICE; - protected const SERVICE_SCHEMA_VERSION = 1; + protected const SERVICE_SCHEMA_VERSION = 2; protected static function get_service_config_schema(): array { return Types::object( [ @@ -22,6 +21,7 @@ protected static function get_service_config_schema(): array { 'enable_blocks' => Types::nullable( Types::boolean() ), 'organization_id' => Types::string(), 'shortcode' => Types::string(), + 'site_id' => Types::string(), ] ); } @@ -36,11 +36,11 @@ protected static function map_service_config( array $service_config ): array { ]; } - protected function migrate_config( array $service_config ): array|WP_Error { - if ( ! isset( $service_config['site_id'] ) ) { - $service_config['site_id'] = 'RefArchGlobal'; + protected static function migrate_config( array $config ): array { + if ( ! isset( $config['service_config']['site_id'] ) ) { + $config['service_config']['site_id'] = 'RefArchGlobal'; } - return $service_config; + return $config; } } diff --git a/inc/MigrationService/MigrationService.php b/inc/MigrationService/MigrationService.php deleted file mode 100644 index 06dfc0513..000000000 --- a/inc/MigrationService/MigrationService.php +++ /dev/null @@ -1,72 +0,0 @@ -perform_migration( $data_source['service_config'] ); - if ( is_wp_error( $migrated_service_config ) ) { - return $migrated_service_config; - } - - // verify if the configs are the same so we can exit early - if ( $data_source['service_config'] === $migrated_service_config ) { - continue; - } - - $updated_config = DataSourceCrud::update_config_by_uuid( $data_source['uuid'], $migrated_service_config ); - if ( is_wp_error( $updated_config ) ) { - return $updated_config; - } - } - - self::set_migration_version(); - - return true; - } - - private static function set_migration_version(): void { - update_option( self::MIGRATION_OPTION_NAME, PluginSettings::get_version() ); - } - - private static function get_migration_version(): string { - return get_option( self::MIGRATION_OPTION_NAME, '' ); - } -} diff --git a/remote-data-blocks.php b/remote-data-blocks.php index dff1fc8fc..996feaf11 100644 --- a/remote-data-blocks.php +++ b/remote-data-blocks.php @@ -42,9 +42,6 @@ // Load Settings Page PluginSettings\PluginSettings::init(); -// Migrations -MigrationService\MigrationService::init(); - // Integrations Integrations\Airtable\AirtableIntegration::init(); Integrations\Google\Sheets\GoogleSheetsIntegration::init(); From d16e8c195c05578cff9ac1a2137b1c932ee73e7d Mon Sep 17 00:00:00 2001 From: ingeniumed Date: Fri, 21 Feb 2025 13:41:40 +1100 Subject: [PATCH 07/11] Remove old code --- inc/Config/ArraySerializable.php | 3 +-- .../SalesforceD2C/SalesforceD2CDataSource.php | 8 -------- 2 files changed, 1 insertion(+), 10 deletions(-) diff --git a/inc/Config/ArraySerializable.php b/inc/Config/ArraySerializable.php index 47830c8f5..94f39dd47 100644 --- a/inc/Config/ArraySerializable.php +++ b/inc/Config/ArraySerializable.php @@ -13,8 +13,7 @@ * ArraySerializable class */ abstract class ArraySerializable implements ArraySerializableInterface { - final private function __construct( protected array $config ) { - } + final private function __construct( protected array $config ) {} protected function get_or_call_from_config( string $property_name, mixed ...$callable_args ): mixed { $config_value = $this->config[ $property_name ] ?? null; diff --git a/inc/Integrations/SalesforceD2C/SalesforceD2CDataSource.php b/inc/Integrations/SalesforceD2C/SalesforceD2CDataSource.php index 41ec57889..ea75d2f16 100644 --- a/inc/Integrations/SalesforceD2C/SalesforceD2CDataSource.php +++ b/inc/Integrations/SalesforceD2C/SalesforceD2CDataSource.php @@ -34,12 +34,4 @@ protected static function map_service_config( array $service_config ): array { ], ]; } - - protected static function migrate_config( array $config ): array { - if ( ! isset( $config['service_config']['site_id'] ) ) { - $config['service_config']['site_id'] = 'RefArchGlobal'; - } - - return $config; - } } From d6e520128c31e243c88f4269b6bc67ee5401650c Mon Sep 17 00:00:00 2001 From: ingeniumed Date: Wed, 26 Feb 2025 12:24:37 +1100 Subject: [PATCH 08/11] Experimenting with making a single migration method that can be overriden and values merged all the way through --- inc/Config/ArraySerializable.php | 11 ++++++++ inc/Config/DataSource/HttpDataSource.php | 35 ++++-------------------- 2 files changed, 16 insertions(+), 30 deletions(-) diff --git a/inc/Config/ArraySerializable.php b/inc/Config/ArraySerializable.php index 94f39dd47..1efaaa64a 100644 --- a/inc/Config/ArraySerializable.php +++ b/inc/Config/ArraySerializable.php @@ -91,4 +91,15 @@ protected static function get_implementor( array $config ): ?string { * @inheritDoc */ abstract public static function get_config_schema(): array; + + /** + * Migrates the config to the current schema version. + * Can be overridden by child classes to perform custom migrations. + * + * @param array $config The config to migrate. + * @return array The migrated config. + */ + protected static function migrate_config( array $config ): array|WP_Error { + return $config; + } } diff --git a/inc/Config/DataSource/HttpDataSource.php b/inc/Config/DataSource/HttpDataSource.php index 06f10c849..b3ea7723e 100644 --- a/inc/Config/DataSource/HttpDataSource.php +++ b/inc/Config/DataSource/HttpDataSource.php @@ -44,7 +44,7 @@ final public function get_service_name(): string { * define their own validation schema. */ public static function preprocess_config( array $config ): array|WP_Error { - $config = static::perform_migration( $config ); + $config = static::migrate_config( $config ); $service_config = $config['service_config'] ?? []; $validator = new Validator( static::get_service_config_schema() ); @@ -65,31 +65,6 @@ public static function preprocess_config( array $config ): array|WP_Error { ); } - /** - * Performs a migration if the config is out of date. - */ - private static function perform_migration( array $config ): array { - // By default, we want to have an active data source. - if ( ! isset( $config['active'] ) ) { - $config['active'] = true; - } - - if ( static::SERVICE_SCHEMA_VERSION === $config['service_config']['__version'] ) { - return $config; - } - - $migrated_config = static::migrate_config( $config ); - - if ( is_wp_error( $migrated_config ) ) { - $config['active'] = false; - } else { - $config['service_config']['__version'] = static::SERVICE_SCHEMA_VERSION; - $config['active'] = true; - } - - return $config; - } - public static function from_uuid( string $uuid ): DataSourceInterface|WP_Error { $config = DataSourceCrud::get_config_by_uuid( $uuid ); @@ -134,10 +109,10 @@ protected static function map_service_config( array $service_config ): array { } /** - * Migrates the config to the current schema version. - * Can be overridden by child classes to perform custom migrations. + * @inheritDoc */ - protected static function migrate_config( array $config ): array { - return $config; + protected static function migrate_config( array $config ): array|WP_Error { + return static::migrate_config( $config ); } + } From daf40ca29045c4f869e81c3646cddddbb6a1d34f Mon Sep 17 00:00:00 2001 From: ingeniumed Date: Wed, 26 Feb 2025 12:27:35 +1100 Subject: [PATCH 09/11] Fix the linting issues --- inc/Config/DataSource/HttpDataSource.php | 1 - 1 file changed, 1 deletion(-) diff --git a/inc/Config/DataSource/HttpDataSource.php b/inc/Config/DataSource/HttpDataSource.php index b3ea7723e..637e8a489 100644 --- a/inc/Config/DataSource/HttpDataSource.php +++ b/inc/Config/DataSource/HttpDataSource.php @@ -114,5 +114,4 @@ protected static function map_service_config( array $service_config ): array { protected static function migrate_config( array $config ): array|WP_Error { return static::migrate_config( $config ); } - } From 0c52b34eafeca83346c56c36f5dac2c2827a2420 Mon Sep 17 00:00:00 2001 From: ingeniumed Date: Wed, 26 Feb 2025 12:35:15 +1100 Subject: [PATCH 10/11] Stop the seg fault --- inc/Config/DataSource/HttpDataSource.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/Config/DataSource/HttpDataSource.php b/inc/Config/DataSource/HttpDataSource.php index 637e8a489..3ba1262ac 100644 --- a/inc/Config/DataSource/HttpDataSource.php +++ b/inc/Config/DataSource/HttpDataSource.php @@ -112,6 +112,6 @@ protected static function map_service_config( array $service_config ): array { * @inheritDoc */ protected static function migrate_config( array $config ): array|WP_Error { - return static::migrate_config( $config ); + return $config; } } From ddda49347eaef793c6c917801db054143c5c7b81 Mon Sep 17 00:00:00 2001 From: ingeniumed Date: Wed, 26 Feb 2025 13:31:34 +1100 Subject: [PATCH 11/11] Move the migrate method into from_array and make it public --- inc/Config/ArraySerializable.php | 7 ++++++- inc/Config/ArraySerializableInterface.php | 8 ++++++++ inc/Config/DataSource/HttpDataSource.php | 4 +--- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/inc/Config/ArraySerializable.php b/inc/Config/ArraySerializable.php index 1efaaa64a..ef45ab21a 100644 --- a/inc/Config/ArraySerializable.php +++ b/inc/Config/ArraySerializable.php @@ -34,6 +34,11 @@ final public static function from_array( array $config, ?ValidatorInterface $val return $subclass::from_array( $config, $validator ); } + $config = static::migrate_config( $config ); + if ( is_wp_error( $config ) ) { + return $config; + } + $config = static::preprocess_config( $config ); if ( is_wp_error( $config ) ) { return $config; @@ -99,7 +104,7 @@ abstract public static function get_config_schema(): array; * @param array $config The config to migrate. * @return array The migrated config. */ - protected static function migrate_config( array $config ): array|WP_Error { + public static function migrate_config( array $config ): array|WP_Error { return $config; } } diff --git a/inc/Config/ArraySerializableInterface.php b/inc/Config/ArraySerializableInterface.php index 92f3243c5..2cee6b9a6 100644 --- a/inc/Config/ArraySerializableInterface.php +++ b/inc/Config/ArraySerializableInterface.php @@ -39,6 +39,14 @@ public static function preprocess_config( array $config ): array|WP_Error; */ public static function get_config_schema(): array; + /** + * Migrates the config to the current schema version. + * + * @param array $config The config to migrate. + * @return array The migrated config. + */ + public static function migrate_config( array $config ): array|WP_Error; + /** * Converts the current object to an array representation. * diff --git a/inc/Config/DataSource/HttpDataSource.php b/inc/Config/DataSource/HttpDataSource.php index 3ba1262ac..1be71bde3 100644 --- a/inc/Config/DataSource/HttpDataSource.php +++ b/inc/Config/DataSource/HttpDataSource.php @@ -44,8 +44,6 @@ final public function get_service_name(): string { * define their own validation schema. */ public static function preprocess_config( array $config ): array|WP_Error { - $config = static::migrate_config( $config ); - $service_config = $config['service_config'] ?? []; $validator = new Validator( static::get_service_config_schema() ); $validated = $validator->validate( $service_config ); @@ -111,7 +109,7 @@ protected static function map_service_config( array $service_config ): array { /** * @inheritDoc */ - protected static function migrate_config( array $config ): array|WP_Error { + public static function migrate_config( array $config ): array|WP_Error { return $config; } }