Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ jobs:

strategy:
matrix:
php: [8.0, 8.1, 8.2]
laravel: [9.*, 10.*]
php: [8.1, 8.2, 8.3]
laravel: [10.*, 11.*]
dependency-version: [prefer-lowest, prefer-stable]
exclude:
- php: 8.0
laravel: 10.*
- php: 8.1
laravel: 11.*

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }}

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ vendor
.phpunit.result.cache
.php_cs.cache
.nova/*
.phpunit.cache
99 changes: 28 additions & 71 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,76 +1,22 @@
# Statamic Eloquent Driver

This package provides support for storing your Statamic data in a database rather than the filesystem.
> Provides support for storing your Statamic data in a database, rather than flat files.

## Installation
## Installation & Usage

Install using Composer:
```
composer require statamic/eloquent-driver
```

Publish the config file:
You can install and configure the Eloquent Driver using a single command:

```
php artisan vendor:publish --tag="statamic-eloquent-config"
php please install:eloquent-driver
```

Since Statamic uses UUIDs within content files by default, we provide two solutions depending on whether you need to use existing content.


### Fresh install of [statamic/statamic](https://github.com/statamic/statamic) (using incrementing ids)

If you're starting from scratch, we can use traditional incrementing integers for IDs.

- Delete `content/collections/pages/home.md`
- Change the structure `tree` in `content/trees/collections/pages.yaml` to `{}`.
- Run `php artisan vendor:publish --provider="Statamic\Eloquent\ServiceProvider" --tag=migrations`.
- Run `php artisan vendor:publish --tag="statamic-eloquent-entries-table"`.
- Run `php artisan migrate`.

### Starting from an existing site (using UUIDs)

If you're planning to use existing content, we can use the existing UUIDs. This will prevent you from needing to update any data or relationships.

- In the `config/statamic/eloquent-driver.php` file, change `entries.model` to `\Statamic\Eloquent\Entries\UuidEntryModel::class`.
- Run `php artisan vendor:publish --provider="Statamic\Eloquent\ServiceProvider" --tag=migrations`.
- Run `php artisan vendor:publish --tag="statamic-eloquent-entries-table-with-string-ids"`.
- Run `php artisan migrate`.

### Publishing migrations seperately

Alternatively, you can publish each repository's migrations individually:

`php artisan vendor:publish --tag="statamic-eloquent-asset-migrations"`

`php artisan vendor:publish --tag="statamic-eloquent-blueprint-migrations"`

`php artisan vendor:publish --tag="statamic-eloquent-collection-migrations"`

`php artisan vendor:publish --tag="statamic-eloquent-form-migrations"`

`php artisan vendor:publish --tag="statamic-eloquent-global-migrations"`

`php artisan vendor:publish --tag="statamic-eloquent-navigation-migrations"`
The command will install the `statamic/eloquent-driver` package, publish the config file, then prompt you to select which repositories you wish to move to the database. The command will then publish the relevant migrations and run `php artisan migrate` behind the scenes.

`php artisan vendor:publish --tag="statamic-eloquent-revision-migrations"`
The command will also give you the opportunity to indicate whether you'd like existing data to be imported.

`php artisan vendor:publish --tag="statamic-eloquent-taxonomy-migrations"`
### Importing flat-file content


## Configuration

The configuration file (`statamic.eloquent-driver`) allows you to choose which repositories you want to be driven by eloquent. By default, all are selected, but if you want to opt out simply change `driver` from `eloquent` to `file` for that repository.

You may also specify your own models for each repository, should you wish to use something different from the one provided.

## Upgrading

After upgrading please ensure to run `php artisan migrate` to update your database to the latest schema.

## Importing existing file based content

We have provided imports from file based content for each repository, which can be run as follows:
If you originally opt-out of importing existing content, then later change your mind, you can import existing content by running the relevant commands:

- Assets: `php please eloquent:import-assets`
- Blueprints and Fieldsets: `php please eloquent:import-blueprints`
Expand All @@ -82,14 +28,13 @@ We have provided imports from file based content for each repository, which can
- Revisions: `php please eloquent:import-revisions`
- Taxonomies: `php please eloquent:import-taxonomies`

If your assets are eloquent driver and you are managing your assets outside of Statamic, we have provided a sync assets command which will check your container for updates and add database entries for any missing files, while removing any that no longer exist.
### Syncing Assets

`php please eloquent:sync-assets`
If your assets are being driven by the Eloquent Driver and you're managing your assets outside of Statamic (eg. directly in the filesystem), you should run the `php please eloquent:sync-assets` command to add any missing files to the database, and remove files that no longer exist on the filesystem.

### Exporting to flat files

## Exporting back to file based content

We have provided exports from eloquent to file based content for each repository, which can be run as follows:
If you wish to move back to flat-files, you may use the following commands to export your content out of the database:

- Assets: `php please eloquent:export-assets`
- Blueprints and Fieldsets: `php please eloquent:export-blueprints`
Expand All @@ -101,10 +46,22 @@ We have provided exports from eloquent to file based content for each repository
- Revisions: `php please eloquent:export-revisions`
- Taxonomies: `php please eloquent:export-taxonomies`

## Storing Users in a Database
## Configuration

The configuration file, found in `config/statamic/eloquent-driver.php` is automatically published when you install the Eloquent Driver.

For each of the repositories, it allows you to determine if they should be driven by flat-files (`file`) or Eloquent (`eloquent`). Some repositories also have additional options, like the ability to override the model used.

## Upgrading

After updating to a new version of the Eloquent Driver, please ensure you run `php artisan migrate` to update your database to the latest schema.

## Questions

### Can I store users in the database too?

Statamic has a [built-in users eloquent driver](https://statamic.dev/tips/storing-users-in-a-database) if you'd like to cross that bridge too.
By default, Statamic users live in the `users` directory of your project. If you wish to move them to the database, please [follow this guide](https://statamic.dev/tips/storing-users-in-a-database).

## Mixed driver entries and collections
### Can I store some collections in the database, while keeping others in flat-files?

This driver **does not** make it possible to have some collections/entries file driven and some eloquent driven. If that is your requirement you may want to look into using [Runway](https://statamic.com/addons/duncanmcclean/runway).
This driver **does not** make it possible to have some collections flat-file driven and others Eloquent driven. If you're looking for that, you may want to checkout the [Runway](https://statamic.com/addons/rad-pack/runway) addon, which is part of The Rad Pack.
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
}
},
"require": {
"php": "^8.0",
"statamic/cms": "^4.51"
"php": "^8.1",
"statamic/cms": "^5.0.0-beta.2"
},
"require-dev": {
"doctrine/dbal": "^3.3",
"laravel/pint": "^1.0",
"orchestra/testbench": "^7.0 || ^8.0",
"phpunit/phpunit": "^9.4"
"orchestra/testbench": "^8.0 || ^9.0.2",
"phpunit/phpunit": "^9.4 || ^10.0 || ^11.0"
},
"scripts": {
"test": "phpunit"
Expand Down
41 changes: 25 additions & 16 deletions config/eloquent-driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,80 +6,89 @@
'table_prefix' => env('STATAMIC_ELOQUENT_PREFIX', ''),

'asset_containers' => [
'driver' => 'eloquent',
'driver' => 'file',
'model' => \Statamic\Eloquent\Assets\AssetContainerModel::class,
],

'assets' => [
'driver' => 'eloquent',
'driver' => 'file',
'model' => \Statamic\Eloquent\Assets\AssetModel::class,
'asset' => \Statamic\Eloquent\Assets\Asset::class,
],

'blueprints' => [
'driver' => 'eloquent',
'driver' => 'file',
'blueprint_model' => \Statamic\Eloquent\Fields\BlueprintModel::class,
'fieldset_model' => \Statamic\Eloquent\Fields\FieldsetModel::class,
],

'collections' => [
'driver' => 'eloquent',
'driver' => 'file',
'model' => \Statamic\Eloquent\Collections\CollectionModel::class,
'update_entry_order_queue' => 'default',
'update_entry_order_connection' => 'default',
],

'collection_trees' => [
'driver' => 'eloquent',
'driver' => 'file',
'model' => \Statamic\Eloquent\Structures\TreeModel::class,
'tree' => \Statamic\Eloquent\Structures\CollectionTree::class,
],

'entries' => [
'driver' => 'eloquent',
'driver' => 'file',
'model' => \Statamic\Eloquent\Entries\EntryModel::class,
'entry' => \Statamic\Eloquent\Entries\Entry::class,
],

'forms' => [
'driver' => 'eloquent',
'model' => \Statamic\Eloquent\Forms\FormModel::class,
'submission_model' => \Statamic\Eloquent\Forms\SubmissionModel::class,
'driver' => 'file',
'model' => \Statamic\Eloquent\Forms\FormModel::class,
],

'form_submissions' => [
'driver' => 'file',
'model' => \Statamic\Eloquent\Forms\SubmissionModel::class,
],

'global_sets' => [
'driver' => 'eloquent',
'driver' => 'file',
'model' => \Statamic\Eloquent\Globals\GlobalSetModel::class,
],

'global_set_variables' => [
'driver' => 'eloquent',
'driver' => 'file',
'model' => \Statamic\Eloquent\Globals\VariablesModel::class,
],

'navigations' => [
'driver' => 'eloquent',
'driver' => 'file',
'model' => \Statamic\Eloquent\Structures\NavModel::class,
],

'navigation_trees' => [
'driver' => 'eloquent',
'driver' => 'file',
'model' => \Statamic\Eloquent\Structures\TreeModel::class,
'tree' => \Statamic\Eloquent\Structures\NavTree::class,
],

'revisions' => [
'driver' => 'eloquent',
'driver' => 'file',
'model' => \Statamic\Eloquent\Revisions\RevisionModel::class,
],

'taxonomies' => [
'driver' => 'eloquent',
'driver' => 'file',
'model' => \Statamic\Eloquent\Taxonomies\TaxonomyModel::class,
],

'terms' => [
'driver' => 'eloquent',
'driver' => 'file',
'model' => \Statamic\Eloquent\Taxonomies\TermModel::class,
],

'tokens' => [
'driver' => 'file',
'model' => \Statamic\Eloquent\Tokens\TokenModel::class,
],
];
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ public function up()
{
Schema::create($this->prefix('form_submissions'), function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('form_id')->index();
$table->string('form', 30)->nullable()->index();
$table->jsonb('data')->nullable();
$table->timestamps(6);

$table->unique(['form_id', 'created_at']);
$table->unique(['form', 'created_at']);
});
}

Expand Down
25 changes: 25 additions & 0 deletions database/migrations/2024_03_07_100000_create_tokens_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Statamic\Eloquent\Database\BaseMigration as Migration;

return new class extends Migration
{
public function up()
{
Schema::create($this->prefix('tokens'), function (Blueprint $table) {
$table->id();
$table->string('token')->unique();
$table->string('handler');
$table->jsonb('data');
$table->timestamp('expire_at');
$table->timestamps();
});
}

public function down()
{
Schema::dropIfExists($this->prefix('tokens'));
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public function up()
$table->string('site')->index();
$table->unsignedBigInteger('origin_id')->nullable()->index();
$table->boolean('published')->default(true);
$table->string('status');
$table->string('slug')->nullable();
$table->string('uri')->nullable()->index();
$table->string('date')->nullable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public function up()
$table->string('site')->index();
$table->uuid('origin_id')->nullable()->index();
$table->boolean('published')->default(true);
$table->string('status');
$table->string('slug')->nullable();
$table->string('uri')->nullable()->index();
$table->string('date')->nullable();
Expand Down
21 changes: 21 additions & 0 deletions database/migrations/updates/drop_status_on_entries.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Statamic\Eloquent\Database\BaseMigration as Migration;

return new class extends Migration {
public function up()
{
Schema::table($this->prefix('entries'), function (Blueprint $table) {
$table->dropColumn('status');
});
}

public function down()
{
Schema::table($this->prefix('entries'), function (Blueprint $table) {
$table->string('status')->nullable();
});
}
};
Loading