bennett-treptow/laravel-migration-generator

Undefined offset: 0 in WritesTablesToFile.php:38

mvalim opened this issue · 10 comments

When I run php artisan generate:migrations with the default configs, I got this error:

Using connection mysql
Using /var/www/network/tests/database/migrations as the output path..

   1/103 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░]   0% < 1 sec/< 1 sec 26.0 MiB
   ErrorException 

  Undefined offset: 0

  at vendor/bennett-treptow/laravel-migration-generator/src/Generators/Concerns/WritesTablesToFile.php:38
     34▕         $baseStubFileName = ConfigResolver::tableNamingScheme($driver);
     35▕         foreach ($this->stubNameVariables() as $variable => $replacement) {
     36▕             if (is_callable($replacement)) {
     37▕                 //replacement is a closure
  ➜  38▕                 [$variable, $replacement] = $replacement($baseStubFileName);
     39▕             }
     40▕             if ($variable === null) {
     41▕                 continue;
     42▕             }

What version are you using? Have you published the config?

Yes, I have published the config but I haven't changed any of the settings, sorry about not addding the version

composer show | grep migrations
bennett-treptow/laravel-migration-generator dev-main 3f6a81e   Generate migrations from existing database structures

Could you copy your config/laravel-migration-generator.php here so I can see?

I just updated to the latest version myself, ran php artisan vendor:publish --force for the LaravelMigrationGenerator/LaravelMigrationGeneratorProvider and ran php artisan config:clear and it is working for me.

<?php

return [
    'run_after_migrations' => env('LMG_RUN_AFTER_MIGRATIONS', false),
    'clear_output_path'    => env('LMG_CLEAR_OUTPUT_PATH', false),
    //default configs
    'table_naming_scheme' => env('LMG_TABLE_NAMING_SCHEME', '[Timestamp]_create_[TableName]_table.php'),
    'view_naming_scheme'  => env('LMG_VIEW_NAMING_SCHEME', '[Timestamp]_create_[ViewName]_view.php'),
    'path'                => env('LMG_OUTPUT_PATH', 'tests/database/migrations'),
    'skippable_tables'    => env('LMG_SKIPPABLE_TABLES', 'migrations'),
    'skip_views'          => env('LMG_SKIP_VIEWS', false),
    'skippable_views'     => env('LMG_SKIPPABLE_VIEWS', ''),
    'definitions'         => [
        'prefer_unsigned_prefix'              => env('LMG_PREFER_UNSIGNED_PREFIX', true),
        'use_defined_index_names'             => env('LMG_USE_DEFINED_INDEX_NAMES', true),
        'use_defined_foreign_key_index_names' => env('LMG_USE_DEFINED_FOREIGN_KEY_INDEX_NAMES', true),
        'use_defined_unique_key_index_names'  => env('LMG_USE_DEFINED_UNIQUE_KEY_INDEX_NAMES', true),
        'use_defined_primary_key_index_names' => env('LMG_USE_DEFINED_PRIMARY_KEY_INDEX_NAMES', true)
    ],

    //now driver specific configs
    //null = use default
    'mysql' => [
        'table_naming_scheme' => env('LMG_MYSQL_TABLE_NAMING_SCHEME', null),
        'view_naming_scheme'  => env('LMG_MYSQL_VIEW_NAMING_SCHEME', null),
        'path'                => env('LMG_MYSQL_OUTPUT_PATH', null),
        'skippable_tables'    => env('LMG_MYSQL_SKIPPABLE_TABLES', null),
        'skippable_views'     => env('LMG_MYSQL_SKIPPABLE_VIEWS', null),
    ],
    'sqlite' => [
        'table_naming_scheme' => env('LMG_SQLITE_TABLE_NAMING_SCHEME', null),
        'view_naming_scheme'  => env('LMG_SQLITE_VIEW_NAMING_SCHEME', null),
        'path'                => env('LMG_SQLITE_OUTPUT_PATH', null),
        'skippable_tables'    => env('LMG_SQLITE_SKIPPABLE_TABLES', null),
        'skippable_views'     => env('LMG_SQLITE_SKIPPABLE_VIEWS', null),
    ],
    'pgsql' => [
        'table_naming_scheme' => env('LMG_PGSQL_TABLE_NAMING_SCHEME', null),
        'view_naming_scheme'  => env('LMG_PGSQL_VIEW_NAMING_SCHEME', null),
        'path'                => env('LMG_PGSQL_OUTPUT_PATH', null),
        'skippable_tables'    => env('LMG_PGSQL_SKIPPABLE_TABLES', null),
        'skippable_views'     => env('LMG_PGSQL_SKIPPABLE_VIEWS', null)
    ],
    'sqlsrv' => [
        'table_naming_scheme' => env('LMG_SQLSRV_TABLE_NAMING_SCHEME', null),
        'view_naming_scheme'  => env('LMG_SQLSRV_VIEW_NAMING_SCHEME', null),
        'path'                => env('LMG_SQLSRV_OUTPUT_PATH', null),
        'skippable_tables'    => env('LMG_SQLSRV_SKIPPABLE_TABLES', null),
        'skippable_views'     => env('LMG_SQLSRV_SKIPPABLE_VIEWS', null),
    ],
];

And to confirm, you do not have any environment variables defined? LMG_TABLE_NAMING_SCHEME in particular

No, not in my .env, but I'm running the command inside a docker container, and also confirmed that there's no environment variable called LMG_TABLE_NAMING_SCHEME defined there too.

The package is able to successfully generate the migrations for 6 of my tables, when I run the command.

If it works using alphabetical order, the next table would be contact_message and it always generates the same six migrations, and fails after 2021_04_26_162231_create_bug_reports_table.php, this one is always the last one it generates before failing.

This is running on >PHP7.1, correct?

It's strange that it is getting that config error, as if it doesn't know what array destructuring is (introduced in php 7.1). If you run SHOW FULL TABLES; on your database and see what the next one is after the last successful one, and then run SHOW CREATE TABLE (table name);, could you copy/paste that here so I could see if it's maybe just parsing something wrong? Of course, redact any column names if necessary.

 php -v
PHP 7.4.16 (cli) (built: Mar  5 2021 07:54:20) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.16, Copyright (c), by Zend Technologies
CREATE TABLE `contact_message` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `country_id` int unsigned DEFAULT NULL,
  `language_id` int unsigned DEFAULT NULL,
  `subject_id` int unsigned DEFAULT NULL,
  `origin_network_id` int unsigned DEFAULT NULL,
  `name` varchar(150) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `email` varchar(200) COLLATE utf8mb4_unicode_ci NOT NULL,
  `message` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
  `status` tinyint(1) DEFAULT '0',
  `read_by_user_id` char(36) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `privacy_policy` tinyint(1) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `contact_message_language_id_foreign` (`language_id`),
  KEY `contact_message_country_id_foreign` (`country_id`),
  KEY `contact_message_subject_id_foreign` (`subject_id`),
  KEY `contact_message_read_by_user_id_foreign` (`read_by_user_id`),
  KEY `contact_message_origin_network_id_foreign` (`origin_network_id`),
  CONSTRAINT `contact_message_country_id_foreign` FOREIGN KEY (`country_id`) REFERENCES `countries` (`id`),
  CONSTRAINT `contact_message_language_id_foreign` FOREIGN KEY (`language_id`) REFERENCES `languages` (`id`),
  CONSTRAINT `contact_message_origin_network_id_foreign` FOREIGN KEY (`origin_network_id`) REFERENCES `mg_network_systems` (`id`),
  CONSTRAINT `contact_message_read_by_user_id_foreign` FOREIGN KEY (`read_by_user_id`) REFERENCES `users` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `contact_message_subject_id_foreign` FOREIGN KEY (`subject_id`) REFERENCES `contact_message_subject` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

I just renamed this table to zcontact_message and ran the command again php artisan generate:migrations -vvv it generated 73 migrations this time, and I got another error this time (not with the same table)

sing connection mysql
Using /var/www/network/tests/database/migrations as the output path..

  73/103 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░]  70%  1 sec/1 sec  26.0 MiB
   ErrorException 

  Undefined offset: 0

  at vendor/laravel/framework/src/Illuminate/Collections/Collection.php:1424
    1420▕      * @return mixed
    1421▕      */
    1422▕     public function offsetGet($key)
    1423▕     {
  ➜ 1424▕         return $this->items[$key];
    1425▕     }
    1426▕ 
    1427▕     /**
    1428▕      * Set the item at a given offset.

  1   vendor/laravel/framework/src/Illuminate/Collections/Collection.php:1424
      Illuminate\Foundation\Bootstrap\HandleExceptions::handleError()

  2   vendor/bennett-treptow/laravel-migration-generator/src/Generators/Concerns/WritesTablesToFile.php:38
      Illuminate\Support\Collection::offsetGet()

  3   vendor/bennett-treptow/laravel-migration-generator/src/Generators/Concerns/WritesToFile.php:16
      LaravelMigrationGenerator\Generators\BaseTableGenerator::getStubFileName()

  4   vendor/bennett-treptow/laravel-migration-generator/src/GeneratorManagers/MySQLGeneratorManager.php:52
      LaravelMigrationGenerator\Generators\BaseTableGenerator::write()

  5   vendor/bennett-treptow/laravel-migration-generator/src/Commands/GenerateMigrationsCommand.php:80
      LaravelMigrationGenerator\GeneratorManagers\MySQLGeneratorManager::handle()

  6   vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:36
      LaravelMigrationGenerator\Commands\GenerateMigrationsCommand::handle()

  7   vendor/laravel/framework/src/Illuminate/Container/Util.php:40
      Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()

  8   vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:93
      Illuminate\Container\Util::unwrapIfClosure()

  9   vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:37
      Illuminate\Container\BoundMethod::callBoundMethod()

  10  vendor/laravel/framework/src/Illuminate/Container/Container.php:611
      Illuminate\Container\BoundMethod::call()

  11  vendor/laravel/framework/src/Illuminate/Console/Command.php:136
      Illuminate\Container\Container::call()

  12  vendor/symfony/console/Command/Command.php:288
      Illuminate\Console\Command::execute()

  13  vendor/laravel/framework/src/Illuminate/Console/Command.php:121
      Symfony\Component\Console\Command\Command::run()

  14  vendor/symfony/console/Application.php:974
      Illuminate\Console\Command::run()

  15  vendor/symfony/console/Application.php:291
      Symfony\Component\Console\Application::doRunCommand()

  16  vendor/symfony/console/Application.php:167
      Symfony\Component\Console\Application::doRun()

  17  vendor/laravel/framework/src/Illuminate/Console/Application.php:92
      Symfony\Component\Console\Application::run()

  18  vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:129
      Illuminate\Console\Application::run()

  19  artisan:37
      Illuminate\Foundation\Console\Kernel::handle()

That would be the next after I renamed the one that was causing the previous error

CREATE TABLE `todo_item_priority` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
  `bg_color` varchar(12) COLLATE utf8mb4_unicode_ci NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

Interesting. I'm not seeing anything that wouldn't get parsed correctly here.

I just tagged 3.1.3 which will remove the line that is breaking for you. Let me know if that fixes it!

Yes, the last commit fixed the problem! Thank you!!