Atomic symlink swaps

By Michael Williams, Published on 10/07/2018

When changing symlinks, particularly as part as a deployment. The goal is generally to eliminate downtime. We often refer to these an atomic deployments.

Just because you are using a symlink those, doesn't necessarily mean your deployment is atomic. Lets dive a bit deeper into this.

By definition, in concurrent programming an operation would be considered atomic if it appears to the rest of the system to occur at once without being interrupted.

Assuming for simplicity we setup the following symlink:

ln -sf release current

Listing that directory as expected would show

ls -lah current -> release
Using the built in web server included with PHP

By Michael Williams, Published on 06/23/2018

Since version 5.4.0. PHP comes with a built in web server. If you're doing anything significant, chances are that you'll likely use Vagrant or similar to manage your local environment(s).

However, if you're a Mac user. Your computer already comes with PHP installed. The same is true for some other operating systems and even if not included by default, you can always install it. Even on Windows.

Before we begin. As listed in the official PHP docs. This server is meant for application development, not production.

Since I'm a Mac user, this setup will be geared towards Mac. However, other than the directory paths. The same principles apply for all OS.

At the time of this writing, the current version of PHP included with a Mac is 7.1.16.

To start the PHP server you can simply run the following from iTerm, Terminal, etc..
Soft Delete Records With Laravel

By Michael Williams, Published on 06/09/2018

When working with data such as users or user related records. Sometimes you may want to soft delete the data rather than permanently removing it. One advantage to soft deletes is easy restore in the event of an accidental delete by someone, but the use cases are unlimited. Luckily, in Laravel the setup for this is quite simple.

First, your table will need to have a deleted_at column, which can be easily added at creation time or through a future migration.

Schema::table('users', function ($table) {
    $table->softDeletes();
});

Your model will also need to use SoftDeletes...
Constraining relationship data with Laravel query builder

By Michael Williams, Published on 05/26/2018

Sometimes when you work with relations in Laravel, a need me arise to add a constraint to the relatioship data. In Laravel terms, this is referred to as Constraining Eager Loads.

Assumming that you have a User and Post Model. One might build a query as such to retrieve active posts for a specific user.

$this->posts->whereStatus('published')->whereUserId($userId)->get();

When loading the User with the Post relationship, we can also get the published posts only by running:

$this->user->with(['posts' => function ($query) { $query->where('status', 'published'); }])->get();
Using the spread operator in ES6

By Michael Williams, Published on 05/22/2018

collections such as an arrays into individual function params and elements.

In the first example, lets look at getting the max value from an array of numbers. That can simply be written as:

const array = [12, 34, 19, 84, 42, 5]; const maxValue = Math.max(...array); console.log(maxValue); // 84

Alternately you may want to take a string of text and convert it to an array

1 2 3 4