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

DOCSP-41306: schema version trait #3051

Merged
merged 14 commits into from
Jul 19, 2024

Conversation

rustagir
Copy link
Contributor

@rustagir rustagir commented Jul 17, 2024

https://jira.mongodb.org/browse/DOCSP-41306
STAGING

Adds documentation for schema versioning trait/migration feature

Checklist

  • Add tests and ensure they pass
  • Add an entry to the CHANGELOG.md file
  • Update documentation for new features

@rustagir rustagir requested a review from a team as a code owner July 17, 2024 20:13
@github-actions github-actions bot added the docs label Jul 17, 2024
Copy link
Contributor

@mongoKart mongoKart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm w/ 1 suggestion!

version to the current version (``2``).

The following code creates and saves instances of the ``Planet`` model that are
not at the current schema version, then retrieves the planets from the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
not at the current schema version, then retrieves the planets from the
not at the current schema version, then retrieves the Planet documents[? objects?] from the

@rustagir rustagir requested a review from GromNaN July 17, 2024 21:06
To use this feature with models that use MongoDB as a database, add the
``MongoDB\Laravel\Eloquent\HasSchemaVersion`` import to your model.
Then, set the ``SCHEMA_VERSION`` constant to set the current schema
version for your collection.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be 1 for the first version of the data doesn't contain any versioned schema.

@rustagir rustagir force-pushed the DOCSP-41306-versioned-schema branch from 2a8fb47 to 5543d5c Compare July 18, 2024 17:50
Comment on lines 393 to 401
$saturn = Planet::create([
'name' => 'Saturn',
'type' => 'gas',
]);

$wasp = Planet::create([
'name' => 'WASP-39 b',
'type' => 'gas',
'schema_version' => 1,
]);

$planets = Planet::where('type', 'gas')
->get();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code snippets can be extracted from the test file like this:

// begin query where
$result = DB::connection('mongodb')
->collection('movies')
->where('imdb.rating', 9.3)
->get();
// end query where

$planets = Planet::where('type', 'gas')
->get();

echo 'After migration:\n' . $planets;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

echo should be avoided in tests. Otherwise you need to assert the output:

echo 'Number of documents: ' . $count;
// end-count
$this->assertEquals(2, $count);
$this->expectOutputString('Number of documents: 2');
}

@GromNaN GromNaN force-pushed the DOCSP-41306-versioned-schema branch from 16aff95 to 0f645c3 Compare July 18, 2024 18:16
@rustagir rustagir force-pushed the DOCSP-41306-versioned-schema branch from 0f645c3 to e114452 Compare July 18, 2024 18:38
@rustagir rustagir requested a review from a team as a code owner July 18, 2024 18:46
@rustagir rustagir requested review from alcaeus and removed request for alcaeus July 18, 2024 18:46

class Planet extends Model
{
protected $fillable = ['name', 'type', 'galaxy'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rustagir this is required to use Planet::create().

@rustagir rustagir requested a review from GromNaN July 19, 2024 17:16
Comment on lines 390 to 391
When you save or retrieve a model that does not have a schema version
specified, Laravel assumes that it follows the latest schema version.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We automatically save the current "SCHEMA_VERSION". When a model is retrieved, if it doesn't contain the schema_version property, we assume it was inserted before the model was versioned, so we assume its version 0 and run the full migration.

Suggested change
When you save or retrieve a model that does not have a schema version
specified, Laravel assumes that it follows the latest schema version.
When you save a model that does not have a schema version specified,
Laravel assumes that it follows the latest schema version.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will update the copy

@rustagir rustagir requested a review from GromNaN July 19, 2024 17:59
Copy link
Member

@GromNaN GromNaN left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. With a last note: Laravel (the framework) is not involved in the schema version behavior. It is either this library or the trait to be precise.

Comment on lines 365 to 369
When you save a model that does not have a schema version
specified, Laravel assumes that it follows the latest schema version.
When you retrieve a model that does not contain the ``schema_version``
field, Laravel assumes that its schema version is ``0`` and performs the
migration.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
When you save a model that does not have a schema version
specified, Laravel assumes that it follows the latest schema version.
When you retrieve a model that does not contain the ``schema_version``
field, Laravel assumes that its schema version is ``0`` and performs the
migration.
When you save a model that does not have a schema version
specified, the trait assumes that it follows the latest schema version.
When you retrieve a model that does not contain the ``schema_version``
field, the trait assumes that its schema version is ``0`` and performs the
migration.

@rustagir rustagir merged commit 4943bcc into mongodb:4.6 Jul 19, 2024
26 checks passed
This was referenced Jul 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants