statamic / eloquent-driver

Provides support for storing your Statamic data in a database, rather than flat files.
https://statamic.dev/tips/storing-content-in-a-database
MIT License
104 stars 74 forks source link

Error with migrations when installing package #337

Closed tao closed 1 month ago

tao commented 1 month ago

Bug description

When I try install the eloquent-driver I run into an issue:

 php please install:eloquent-driver

   INFO  Installed statamic/eloquent-driver package.

   INFO  Config file [config/statamic/eloquent-driver.php] published successfully.

 ┌ Which repositories would you like to migrate? 
  │ Entries                                                     
  ─────┘

 ┌ Would you like to import existing entries? 
  │ Yes                                                          
  ─────┘

 ⠤ Migrating entries...

 Failed to run command: vendor:publish --tag=statamic-eloquent-entries-table-with-string-ids

   Illuminate\Database\QueryException

  SQLSTATE[HY000]: General error: 1 no such table: entries (Connection: sqlite, SQL: select * from "entries")

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:829
    825▕                     $this->getName(), $query, $this->prepareBindings($bindings), $e
    826▕                 );
    827▕             }
    828▕
  ➜ 829▕             throw new QueryException(
    830▕                 $this->getName(), $query, $this->prepareBindings($bindings), $e
    831▕             );
    832▕         }
    833▕     }

  1   [internal]:0
      App\Console\Commands\RepairRevisions::__construct()

      +13 vendor frames
  15  app/Console/Commands/RepairRevisions.php:47
      Statamic\Eloquent\Entries\EntryQueryBuilder::get()

The migration files don't seem to be imported into my project, when I tried this with collections selected a single migration file was added but when I examine the package contents I see there are many more migration files.

How to reproduce

php please install:eloquent-driver

Logs

[2024-08-13 08:31:16] local.ERROR: Process Class: Composer
Command: '/Users/tao/Library/Application Support/Herd/bin/php82' '-d memory_limit=-1' '/Users/tao/Library/Application Support/Herd/bin//composer' '--no-ansi' 'require' 'statamic/eloquent-driver' '--update-with-dependencies'
Error: ./composer.json has been updated
Running composer update statamic/eloquent-driver --with-dependencies
> Statamic\Console\Composer\Scripts::preUpdateCmd
Loading composer repositories with package information
Dependency statamic/cms is also a root requirement. Package has not been listed as an update argument, so keeping locked at old version. Use --with-all-dependencies (-W) to include root dependencies.
Updating dependencies
Lock file operations: 1 install, 0 updates, 0 removals
- Locking statamic/eloquent-driver (v4.12.3)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
- Installing statamic/eloquent-driver (v4.12.3): Extracting archive
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi
> @php artisan statamic:install --ansi
106 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
> @php artisan vendor:publish --tag=laravel-assets --ansi --force
No security vulnerability advisories found.
Using version ^4.12 for statamic/eloquent-driver
[2024-08-13 08:31:51] local.ERROR: SQLSTATE[HY000]: General error: 1 no such table: entries (Connection: sqlite, SQL: select * from "entries") {"exception":"[object] (Illuminate\\Database\\QueryException(code: HY000): SQLSTATE[HY000]: General error: 1 no such table: entries (Connection: sqlite, SQL: select * from \"entries\") at /Users/tao/Code/statamic/cms/vendor/laravel/framework/src/Illuminate/Database/Connection.php:829)
[stacktrace]

#0 /Users/tao/Code/statamic/cms/vendor/laravel/framework/src/Illuminate/Database/Connection.php(783): Illuminate\\Database\\Connection->runQueryCallback('select * from \"...', Array, Object(Closure))

...

Environment

Environment
Laravel Version: 10.48.20
PHP Version: 8.2.22
Composer Version: 2.7.7
Environment: local
Debug Mode: ENABLED
URL: www.statamic.test
Maintenance Mode: OFF

Cache
Config: NOT CACHED
Events: NOT CACHED
Routes: NOT CACHED
Views: NOT CACHED

Drivers
Broadcasting: null
Cache: file
Database: sqlite
Logs: stack / single
Mail: smtp
Queue: sync
Session: file

Statamic
Addons: 5
Sites: 27 (English, Arabic, Bulgarian, and 24 more)
Stache Watcher: Disabled
Static Caching: Disabled
Version: 5.20.0 PRO

Statamic Addons
statamic/collaboration: 1.0.0
statamic/eloquent-driver: 4.12.3
statamic/ssg: 3.0.1
stillat/relationships: 2.2.1

Additional details

No response

ryanmitchell commented 1 month ago

We only publish the migrations that are required for the repositories you choose.

If you run php artisan vendor:publish --tag=statamic-eloquent-entries-table-with-string-ids manually, do you get the same error?

tao commented 1 month ago

I couldn't run php artisan or php please commands without running into the issue again, so I wasn't able to manually publish the migration files...

I thought about that a bit more and uncommented this line in the Kernal.php file to stop any console commands from being imported:

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        // $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }

That seemed to help and when I rolled back my commit and installed the eloquent-driver again then it successfully published the entries migration file.

One of my custom console commands accessed the Entries facade.

    public function __construct()
    {
        parent::__construct();

        $this->entries = Entry::query()
            ->get()
            ->filter(function (EntriesEntry $entry) {
                return $entry->revisions()->count() === 0;
            });
    }

I believe that was causing an issue because it would try get Entries before the eloquent-driver command was able to run the migration command. I guess after installing eloquent-driver it would detect that Entries should be in the database and then block the rest of the eloquent-driver commands from running because the database table didn't exist yet.

After the migrations were installed I was able to uncomment that line and it seems to be fine now.

Not sure if there is anything you can do to get around that bug...

ryanmitchell commented 1 month ago

You should run that code in your handle() method rather than the construct method, so it doesnt affect other console commands.

tao commented 1 month ago

Awesome, seems to work smoothly now. I guess this isn't a eloquent-driver bug then but maybe it'll help someone in the future if they discover this thread 👍🏻