Skip to content

Commit

Permalink
Support all models (#42)
Browse files Browse the repository at this point in the history
Co-authored-by: Julio Carlos Menendez <9697+juliomenendez@users.noreply.github.com>
Co-authored-by: Oliver Kaufmann <oliver.kaufmann@novu.ch>
Co-authored-by: Benaja Hunzinger <benhu00@outlook.com>
Co-authored-by: Jason Varga <jason@pixelfear.com>
Co-authored-by: Maurice Wijnia <maurice@van-ons.nl>
Co-authored-by: Frederik Sauer <fritten.keez@gmail.com>
  • Loading branch information
7 people authored Sep 1, 2022
1 parent 929ffd0 commit bba3c81
Show file tree
Hide file tree
Showing 102 changed files with 7,430 additions and 184 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[*.{yml,yaml}]
indent_size = 2
10 changes: 7 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ jobs:

strategy:
matrix:
php: [7.4, 7.3]
laravel: [8.*]
php: [8.0, 8.1]
laravel: [8.*, 9.*]
dependency-version: [prefer-lowest, prefer-stable]

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }}
Expand All @@ -23,12 +23,16 @@ jobs:
uses: actions/checkout@v1

- name: Setup PHP
uses: shivammathur/setup-php@v1
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extension-csv: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick
coverage: none

- name: Set PHP 8.1 Testbench
run: composer require "orchestra/testbench ^6.22.0" --no-interaction --no-update
if: matrix.laravel == '8.*' && matrix.php >= 8.1

- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" --no-interaction --no-update
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
composer.lock
vendor
.phpunit.result.cache
.php_cs.cache
33 changes: 28 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

This package provides support for storing your Statamic data in a database rather than the filesystem.

This driver currently supports entries but not taxonomies, navigations, globals, or form submissions. We'll be working on those in the future.

## Installation

Install using Composer:
Expand All @@ -26,18 +24,43 @@ If you're starting from scratch, we can use traditional incrementing integers fo

- Delete `content/collections/pages/home.md`
- Change the structure `tree` in `content/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 `model` to `UuidEntryModel`.
- In the `config/statamic/eloquent-driver.php` file, change `model` to `\Statamic\Eloquent\Entries\UuidEntryModel`.
- 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`.
- Import entries into database with `php please eloquent:import-entries`.

## 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.

## Importing existing file based content

We have provided imports from file based content for each repository, which can be run as follows:

- Assets: `php please eloquent:import-assets`
- Blueprints and Fieldsets: `php please eloquent:import-blueprints`
- Collections: `php please eloquent:import-collections`
- Entries: `php please eloquent:import-entries`
- Forms: `php please eloquent:import-forms`
- Globals: `php please eloquent:import-globals`
- Navs: `php please eloquent:import-navs`
- Revisions: `php please eloquent:import-revisions`
- Taxonomies: `php please eloquent:import-taxonomies`

## Storing Users in a Database

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.
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.

## Mixed driver entries and collections

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).
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,17 @@
}
},
"require": {
"statamic/cms": "^3.0"
"php": "^8.0",
"statamic/cms": "^3.3.31"
},
"require-dev": {
"doctrine/dbal": "^3.3",
"orchestra/testbench": "^6.7.0 || ^7.0",
"phpunit/phpunit": "^9.4"
},
"scripts": {
"test": "phpunit"
},
"config": {
"allow-plugins": {
"pixelfear/composer-dist-plugin": true
Expand Down
57 changes: 57 additions & 0 deletions config/eloquent-driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,65 @@

return [

'connection' => env('STATAMIC_ELOQUENT_CONNECTION', ''),
'table_prefix' => env('STATAMIC_ELOQUENT_PREFIX', ''),

'assets' => [
'driver' => 'eloquent',
'container_model' => \Statamic\Eloquent\Assets\AssetContainerModel::class,
'model' => \Statamic\Eloquent\Assets\AssetModel::class,
],

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

'collections' => [
'driver' => 'eloquent',
'model' => \Statamic\Eloquent\Collections\CollectionModel::class,
'tree' => \Statamic\Eloquent\Structures\CollectionTree::class,
'tree_model' => \Statamic\Eloquent\Structures\TreeModel::class,
],

'entries' => [
'driver' => 'eloquent',
'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,
],

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

'navigations' => [
'driver' => 'eloquent',
'model' => \Statamic\Eloquent\Structures\NavModel::class,
'tree' => \Statamic\Eloquent\Structures\NavTree::class,
'tree_model' => \Statamic\Eloquent\Structures\TreeModel::class,
],

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

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

'terms' => [
'driver' => 'eloquent',
'model' => \Statamic\Eloquent\Taxonomies\TermModel::class,
],
];
24 changes: 24 additions & 0 deletions database/migrations/create_asset_containers_table.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?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('asset_containers'), function (Blueprint $table) {
$table->id();
$table->string('handle')->unique();
$table->string('title');
$table->string('disk');
$table->json('settings')->nullable();
$table->timestamps();
});
}

public function down()
{
Schema::dropIfExists($this->prefix('asset_containers'));
}
};
23 changes: 23 additions & 0 deletions database/migrations/create_asset_table.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?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('assets_meta'), function (Blueprint $table) {
$table->id();
$table->string('handle')->index();
$table->json('data')->nullable();
$table->timestamps();
});
}


public function down()
{
Schema::dropIfExists($this->prefix('assets_meta'));
}
};
73 changes: 73 additions & 0 deletions database/migrations/create_blueprints_table.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

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

return new class extends Migration {
public function up()
{
Schema::create($this->prefix('blueprints'), function (Blueprint $table) {
$table->id();
$table->string('namespace')->nullable()->default(null)->index();
$table->string('handle');
$table->json('data');
$table->timestamps();

$table->unique(['handle', 'namespace']);
});

$this->seedDefaultBlueprint();
}

public function down()
{
Schema::dropIfExists($this->prefix('blueprints'));
}

public function seedDefaultBlueprint()
{
try {
$config = json_encode([
'fields' => [
[
'field' => [
'type' => 'markdown',
'display' => 'Content',
'localizable' => true,
],
'handle' => 'content',
],
[
'field' => [
'type' => 'users',
'display' => 'Author',
'default' => 'current',
'localizable' => true,
'max_items' => 1,
],
'handle' => 'author',
],
[
'field' => [
'type' => 'template',
'display' => 'Template',
'localizable' => true,
],
'handle' => 'template',
],
],
]);
} catch (\JsonException $e) {
$config = '[]';
}

DB::table($this->prefix('blueprints'))->insert([
'namespace' => null,
'handle' => 'default',
'data' => $config,
'created_at' => Carbon::now(),
]);
}
};
23 changes: 23 additions & 0 deletions database/migrations/create_collections_table.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?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('collections'), function (Blueprint $table) {
$table->id();
$table->string('handle')->unique();
$table->string('title');
$table->json('settings')->nullable();
$table->timestamps();
});
}

public function down()
{
Schema::dropIfExists($this->prefix('collections'));
}
};
40 changes: 0 additions & 40 deletions database/migrations/create_entries_table.php

This file was deleted.

Loading

0 comments on commit bba3c81

Please sign in to comment.