Download the PHP package ricorocks-digital-agency/morpher without Composer
On this page you can find all versions of the php package ricorocks-digital-agency/morpher. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ricorocks-digital-agency/morpher
More information about ricorocks-digital-agency/morpher
Files in ricorocks-digital-agency/morpher
Package morpher
Short Description A Laravel package for managing data changes during database migrations.
License MIT
Informations about the package morpher
We've all been there. You have an application in production, and now one of the database tables needs a structural change. You can't manually go in and change all the affected database rows. So what do you do? Put the logic in a migration? Seems a little risky, no?
Morpher is a Laravel package that provides a unified pattern of transforming data between database migrations. It allows you to keep your migration logic clean and terse and move responsibility for data manipulation to a more appropriate location. It also provides a robust way to write tests for these transformations, which otherwise proves to be a real challenge.
TOC
- Installation
- Usage Guide
- Lifecycle of a Morph
- Testing Morphs
- Disabling Morphs
- Notes and Considerations
Installation
It's not required, but you might want to publish the config file:
Create your first Morph
Let's set up an example scenario. Your users table has a single name
column, but you now need to separate it out into
first_name
and last_name
columns. Your application has been live for a little while, so there is going to be a need
to perform a data transformation.
You start by creating a migration:
That migration's up
method might look something like this:
So, how do we go about taking all of the existing names, preparing the data, and inserting it into our new table?
Let's start by creating out first Morph:
This will create a new class in database/morphs
called SplitUserNames
. Our next step is to link our migration to our
new Morph. We can do this using the $migration
property in the morph class:
If you need more complex logic, you can instead override the migration
method and return a migration class name that way.
:zap: Working with anonymous migrations? You can instead use the filename of the migration as the value of the
$migration
property. For example:protected static $migration = "2021_05_01_000000_create_some_anonymous_table"
;
Our next task is to describe our Morph. In the app/Morphs/SplitUserNames
class, we need to do the following:
- Retrieve the current names prior to the migration being run.
- Split the names into first and last names.
- Insert the names after the migration has finished.
To accomplish this, our Morph
might look as follows:
Now, when we run php artisan migrate
, this Morph will run automatically.
Lifecycle
It helps to understand the lifecycle that a Morph
goes through in order to make full use it.
When a Morph
is linked to a migration, and that migration's up
method is run (usually from migrating the database),
the following happens (in order):
- The
prepare
method will be called on theMorph
class. You can do anything you need to prepare data here. - The migration will run.
- The
canRun
method will be called on theMorph
class. Returning false in this method will stop the process here. - The
run
method will be called on theMorph
class. This is where you should perform your data transformations.
Testing Morphs
One of the biggest challenges presented by data morphing is writing feature tests. It becomes very tricky to insert data to test on prior to the morph taking place. And yet, automated tests are so important when the code you're running will be modifying real data. Morpher makes the process of testing data a breeze so that you no longer have to compromise.
To get started, we recommend creating a separate test case (or more than one test case) per Morph you'd like to write
tests for. Add the TestsMorphs
trait to that test class, and add the supportMorphs
call to end of the setUp
method.
:warning: The
TestsMorphs
trait conflicts with other database traits, such asRefreshDatabase
orDatabaseTransactions
. As such, ensure that your morph test cases are isolated (in separate test classes) from other tests in your suite.
With that done, you can get to work writing your tests! In order to do this, we provide a robust inspection API to facilitate Morph tests.
As you can see, there are several inspections methods we can make use of to fully test our Morphs. Note that you only need to use the inspections relevant to your particular Morph.
beforeThisMigration
This method is run prior to the migration connected to the Morph
being run on the database.
It is also run prior to the prepare
method on your Morph being called.
Seen as your tests won't have "old" data for your Morph to alter, you can use this method to
create fake data ready for your Morph to use.
Note that in most cases, your Laravel Factories will likely be outdated, so you may have to resort to manual methods such as the
DB
Facade. You could also create a versioned Factory that uses the old data structure.
before
This method is executed prior to the run
method being called on your Morph, but after the
prepare method. You could use this as an opportunity to make sure your prepare method
has collected the expected data and stored it on the Morph object, if your Morph
needs to perform that step.
after
This method is executed after the run
method has been called on your Morph. You should
use this to check that the data migration has run successfully and that your data has
actually been transformed.
Disabling Morphs
It may be helpful, particularly in local development where you destroy and rebuild the database regularly, to disable Morphs from running. To do this, add the following to your environment file:
Notes and Considerations
- Everything in the
run
method is encapsulated in a database transaction. This means that if there is an exception whilst running your morph, no data changes will be persisted. - It's important to remember that this package isn't magic. If you do something stupid to the data in your database, there is no going back. Back up your data before migrating.
- You can override the
canRun
method to stop a faulty data set ruining your database. Perform any checks you want in this method, and just return a boolean to tell us if we should go ahead. - Want to write your progress to the console during a Morph? You can do so using the
$this->console
property on the Morph class!