-
Notifications
You must be signed in to change notification settings - Fork 5
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
Add a migration feature #366
base: trunk
Are you sure you want to change the base?
Changes from 8 commits
37b2e28
d83d9b5
16297bf
4cb4bbb
21ddd46
7292590
47e8c61
d16e8c1
d6e5201
daf40ca
0c52b34
ddda493
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -44,6 +44,8 @@ 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 ); | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My intention was to allow us to be able to migrate the fields that we control within the config as well. A good example of this would be the first migration:
There could be more down the line but that's why it's been done that way. Are you saying to move what I have in Hope that makes sense, and I correctly articulated what you are suggesting. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Can't we just use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This is a design mistake that I think we should just fix. Doesn't need to be handled by a migration, IMO There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Put another way, if the migration fails and you set active = false, how are you going to read and respect that? The config still needs to pass validation (which it likely won't, so it will get elevated to an error anyway). |
||||||||||||
|
||||||||||||
$service_config = $config['service_config'] ?? []; | ||||||||||||
$validator = new Validator( static::get_service_config_schema() ); | ||||||||||||
$validated = $validator->validate( $service_config ); | ||||||||||||
|
@@ -63,6 +65,31 @@ 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; | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably be delegated to the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a fair point. Having us control would negate the point of letting data source owners take charge of the migration process. |
||||||||||||
$config['active'] = true; | ||||||||||||
} | ||||||||||||
|
||||||||||||
return $config; | ||||||||||||
} | ||||||||||||
|
||||||||||||
public static function from_uuid( string $uuid ): DataSourceInterface|WP_Error { | ||||||||||||
$config = DataSourceCrud::get_config_by_uuid( $uuid ); | ||||||||||||
|
||||||||||||
|
@@ -105,4 +132,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 static function migrate_config( array $config ): array { | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
return $config; | ||||||||||||
} | ||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@chriszarate - the new
preprocess_config
method seems like the right place to call the migration method from withinHttpDataSource
looks like. Though if a custom implementor is used then it wouldn't be called viafrom_array
so that is a downside.What do you think about the way the migration methods are called here?
What I haven't done yet:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from_array
is now marked asfinal
so that shouldn't be an issueThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general, I think we should delegate as much as possible and follow the pattern we've used elsewhere of "return the thing or a
WP_Error
, which we then elevate to the user to remedy"