Skip to content

Commit

Permalink
Inital release
Browse files Browse the repository at this point in the history
  • Loading branch information
x7ryan committed Mar 23, 2023
0 parents commit 977e67a
Show file tree
Hide file tree
Showing 19 changed files with 1,426 additions and 0 deletions.
8 changes: 8 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Changelog

All notable changes to `LaravelPlanetscale` will be documented in this file.

## Version 1.0

### Added
- `php artisan pscale:migrate` command to manage the brnaching and merging to deploy a schema change.
41 changes: 41 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "x7media/laravel-planetscale",
"description": "A package for managing Laravel database migrations with Planetscale databases.",
"license": "MIT",
"authors": [
{
"name": "X7 Media, LLC",
"email": "[email protected]",
"homepage": "http://x7media.com"
}
],
"homepage": "https://github.com/x7media/laravel-planetscale",
"keywords": [
"Laravel",
"LaravelPlanetscale"
],
"require": {
"illuminate/support": "~9"
},
"require-dev": {
"phpunit/phpunit": "~9.0",
"orchestra/testbench": "~7"
},
"autoload": {
"psr-4": {
"X7media\\LaravelPlanetscale\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"X7media\\LaravelPlanetscale\\Tests\\": "tests"
}
},
"extra": {
"laravel": {
"providers": [
"X7media\\LaravelPlanetscale\\LaravelPlanetscaleServiceProvider"
]
}
}
}
18 changes: 18 additions & 0 deletions config/planetscale.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

return [
'organization' => env('PLANETSCALE_ORGANIZATION'),

'production_branch' => env('PLANETSCALE_PRODUCTION_BRANCH', 'main'),

'database' => env('DB_DATABASE'),

/*
* For security, when customizing this config,
* DO NOT use a hard-coded service token here.
*/
'service_token' => [
'id' => env('PLANETSCALE_SERVICE_TOKEN_ID'),
'value' => env('PLANETSCALE_SERVICE_TOKEN'),
],
];
20 changes: 20 additions & 0 deletions contributing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Contributing

Contributions are welcome and will be fully credited.

Contributions are accepted via Pull Requests on [Github](https://github.com/x7media/laravel-planetscale).

## Pull Requests

- **Add tests!** - Your patch won't be accepted if it doesn't have tests.

- **Document any change in behaviour** - Make sure the `readme.md` and any other relevant documentation are kept up-to-date.

- **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option.

- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.

- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting.


**Happy coding**!
9 changes: 9 additions & 0 deletions license.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright (c) X7 Media, LLC [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 changes: 30 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Package">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>src/</directory>
</whitelist>
</filter>
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
</php>
</phpunit>
111 changes: 111 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# LaravelPlanetscale

[![Latest Version on Packagist][ico-version]][link-packagist]
[![Total Downloads][ico-downloads]][link-downloads]

This package adds a `php artisan pscale:migrate` command to your Laravel app which can be used instead of the normal `php artisan migrate` command when using a Planetscale database.

## Installation

Via Composer

``` bash
composer require x7media/laravel-planetscale
```

## Configuration & Usage

1. Login to your Planetscale account and get your Service Token and Service Token ID from the organaization settings. Also take a note of your organization name and production branch name as well for the next steps.

2. Add the following database level permissions to your Service Token for your app's database:

- create_branch - Create a database branch
- delete_branch - Delete a database branch
- connect_branch - Connect to, or create passwords and certificates for a database branch
- create_deploy_request - Create a database deploy request
- read_deploy_request - Read database deploy requests

3. From the database settings screen on Planetscale, click the checkmark to enable the "Automatically copy migration data" settings. Select "Laravel" from the migration framework dropdown and it should fill it "migrations" for the migration table name. Then save the database settings. This will allow migration status to be synced across Planetscale database branches.

4. Setup the following enviroment variables in your app with the appropriate values:

`PLANETSCALE_ORGANIZATION=`

`PLANETSCALE_PRODUCTION_BRANCH=`

`PLANETSCALE_SERVICE_TOKEN_ID=`

`PLANETSCALE_SERVICE_TOKEN=`

Additonally yuou'll need to make sure your database name is set under:

`DB_DATABASE=`

**OR**

Optionally you can publish the config:

``` bash
php artisan vendor:publish --tag=laravel-planetscale-config
```

Then customize the values in the config. **NOTE:** If you take this approach we *STRONGLY RECOMMEND* that you still use enviroment varibles or some other secrets storage at least for your service token and service token ID for security.

5. Replase the `php artisan migrate` command in your deployment script or process with this:

``` bash
php artisan pscale:migrate
```

**NOTE:** The `pscale:migrate` command supports the same options are Laravel's built in migration command, and will pass those options along to it when it gets to that step in the process.

## FAQ's

### Why is this necessary?

Planetscale has alot of advantages when using it as your application's production database. However it handles your database and schema migrations in a somewhat unusual way.

It uses branches for your database. A branch can be production or development. You'll want to use a production branch for your app in production because that afford you extra features like automatic backups, however you cannot perform schema changes directly against a production branch. Instead you should create a new development branch based on your production branch and perform your schema changes on that than merge that back into your production branch just like you would do with your code in Git.

This package uses [Planetscale's Public API](https://api-docs.planetscale.com/) to automate the process of creating a new development branch, connecting your app to the development branch, running your Laravel migrations on the development branch, merging that back into your production branch, and deleting the development branch.

### Are there any notable limitations to Planetscale's branching?

Yes, there is one **BIG** caveat. That is branching and merging is for *schema only*. So you will need to seperate your schema migrations from your data migrations. Use this to run your schema migrations and run your data migrations seperatly against your production branch.

An alternative method is to demote your production branch back to a development branch, then you can mix schema and data migrations. Then when that is finished promote the branch back to a production branch. But that is currently a manaual process. I have however made a request with the Planetscale team to make a slight change to their API that would allow this demote-promote process to be automated, and if that change is made I will update this package. However I ultimately have no control over if or when that will become possible.

## Change log

Please see the [changelog](changelog.md) for more information on what has changed recently.

## Testing

``` bash
$ composer test
```

## Contributing

Please see the [contributing](contributing.md) guidelines.

## Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

## Credits

- [Ryan](https://github.com/x7ryan)
- [All Contributors](../../contributors)

## License

MIT. Please see the [license file](license.md) for more information.

[ico-version]: https://img.shields.io/packagist/v/x7media/laravel-planetscale.svg?style=flat-square
[ico-downloads]: https://img.shields.io/packagist/dt/x7media/laravel-planetscale.svg?style=flat-square

[link-packagist]: https://packagist.org/packages/x7media/laravel-planetscale
[link-downloads]: https://packagist.org/packages/x7media/laravel-planetscale
[link-author]: https://github.com/x7media
[link-contributors]: ../../contributors
13 changes: 13 additions & 0 deletions src/Connection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace X7media\LaravelPlanetscale;

class Connection
{
public readonly string $database;

public function __construct(public readonly string $host, public readonly string $username, public readonly string $password)
{
$this->database = config('planetscale.database');
}
}
Loading

0 comments on commit 977e67a

Please sign in to comment.