Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MySQL] Queuing database migration gives me "You cannot serialize or unserialize PDO instances" #273

Closed
tamkeen-tms opened this issue Jan 19, 2020 · 29 comments · Fixed by #289
Assignees
Labels
bug Something isn't working

Comments

@tamkeen-tms
Copy link

tamkeen-tms commented Jan 19, 2020

Reading your comments on #270 I thought I should queue the database migration, but after setting queue_database_creation to true in config/tenancy.php I get this error:

You cannot serialize or unserialize PDO instances. 

Comming from src\Illuminate\Queue\Queue.php:139.

This is my configuration:

		'queue_database_creation' => true,
		'migrate_after_creation' => true, // run migrations after creating a tenant
		'migration_parameters' => [
			 '--force' => true, // force database migrations
		],
		'seed_after_migration' => true, // should the seeder run after automatic migration
		'seeder_parameters' => [
			'--class' => 'DatabaseSeeder', // root seeder class to run after automatic migrations, e.g.: 'DatabaseSeeder'
			 '--force' => true, // force database seeder
		],
@stancl
Copy link
Member

stancl commented Jan 19, 2020

Did you add any custom jobs?

@tamkeen-tms
Copy link
Author

No, the creation code is the same, I just set queue_database_creation to true.

@stancl
Copy link
Member

stancl commented Jan 19, 2020

What queue driver & database are you using?

@tamkeen-tms
Copy link
Author

Am using the "database" driver, and the central/tenants database connection

@stancl
Copy link
Member

stancl commented Jan 19, 2020

What database engine?

@tamkeen-tms
Copy link
Author

InnoDB

@stancl
Copy link
Member

stancl commented Jan 19, 2020

I know that this error message appears when you try to add another job to the queue chain and try to store Tenant as an object property.

That's weird, is that a fresh Laravel app?

@tamkeen-tms
Copy link
Author

Not really, but I just updated to Laravel 6 couple days ago.

@stancl
Copy link
Member

stancl commented Jan 19, 2020

I never had this issue w/ database queue driver, fresh L6 app and sqlite.

@kevin-ian
Copy link

kevin-ian commented Jan 30, 2020

I'm having the same problem here. How can we debug this? I have a job table. When I create a tenant, I get the error message "You cannot serialize or unserialize PDO instances".

Is there something specific to do?

@kevin-ian
Copy link

If i comment QueuedTenantDatabaseCreator::withChain($chain)->dispatch($manager, $database); in DatabaseManager line 185. The error goes away. But obviously, this is not the solution.

@stancl
Copy link
Member

stancl commented Jan 30, 2020

Of course, because that cancels the DB creation.

Can you try to dd($chain) right before that line? If it's not an empty array, try to comment these lines:

https://github.com/stancl/tenancy/blob/01f080450d652d0e970064de9951daac3d678dcd/src/TenantManager.php#L81-L99

This will result in DB migration & seeding not being done automatically after tenant DB creation. If that makes the create DB job pass, the issue is with the migrate & seed jobs.

You can also try to enable queued DB creation, but disable auto migration & auto seeding.

@kevin-ian
Copy link

kevin-ian commented Jan 30, 2020

dd($chain) just before line 81 ? I'm not at the office at the moment. Will try it in a while.

But just to make sure, the variable $chain is not defined in this method. Are we debugging in the correct file ?

@stancl
Copy link
Member

stancl commented Jan 30, 2020

Right before the line you sent, in DatabaseManager.

@kevin-ian
Copy link

Oh i see, alright sure, i will try it and post the result back.

By the way, I already tried to enable queued DB creation, but disable auto migration & auto seeding, the error was still there.

I will try your suggestion and revert back

@kevin-ian
Copy link

Hi, sorry for late reply, i tried your suggestion.

dd($chain) is not empty, so I commented the lines 81 - 99 in TenantManager.php, i still get the message

You cannot serialize or unserialize PDO instance

I wonder if it's related to PHP version or Database? I'm using MariaDB.

@kevin-ian
Copy link

@TamkeenLMS , have you been able to figure this out?

@tamkeen-tms
Copy link
Author

@kevin-ian Actually no.

@kevin-ian
Copy link

kevin-ian commented Feb 3, 2020

Stancl\Tenancy\TenantDatabaseManagers\MySQLDatabaseManager {#752
  #database: Illuminate\Database\MySqlConnection {#556
    #pdo: PDO {#475 <---- HERE
      inTransaction: false
      attributes: {
        CASE: NATURAL
        ERRMODE: EXCEPTION
        AUTOCOMMIT: 1
        PERSISTENT: false
        DRIVER_NAME: "mysql"
        SERVER_INFO: "Uptime: 7536  Threads: 8  Questions: 3028  Slow queries: 0  Opens: 111  Flush tables: 1  Open tables: 30  Queries per second avg: 0.401"
        ORACLE_NULLS: NATURAL
        CLIENT_VERSION: "mysqlnd 7.4.1"
        SERVER_VERSION: "5.5.5-10.4.11-MariaDB-1:10.4.11+maria~bionic"
        STATEMENT_CLASS: array:1 [
          0 => "PDOStatement"
        ]
        EMULATE_PREPARES: 0
        CONNECTION_STATUS: "mariadb via TCP/IP"
        DEFAULT_FETCH_MODE: BOTH
      }
    }

I think it's trying to serialize this PDO instance. But I have no clue how to prevent that.

@stancl
Copy link
Member

stancl commented Feb 3, 2020

Oh that makes sense. I was definitely unable to reproduce this issue before and now I have no issues with SQLite, but the MySQLDatabaseManager now stores the connection (to get config like charset, collation, etc).

Can you try switching to SQLite and seeing if the bug persists?

@kevin-ian
Copy link

@stancl ,

Tested with SQLite, and it works. The job is being created in the jobs table.

The bug is still present for the MySQL Manager. Any clue ?

@stancl stancl added bug Something isn't working and removed potential bug support labels Feb 4, 2020
@stancl stancl changed the title Queuing database migration gives me "You cannot serialize or unserialize PDO instances" [MySQL] Queuing database migration gives me "You cannot serialize or unserialize PDO instances" Feb 4, 2020
@stancl
Copy link
Member

stancl commented Feb 4, 2020

Thanks for reporting, I'll try to reproduce & fix this as soon as possible.

@kevin-ian
Copy link

@stancl thanks for taking this into consideration. I'd be glad to help (debugging, testing etc...) if needed.

@kevin-ian
Copy link

@stancl Any updates regarding this bug?

@stancl
Copy link
Member

stancl commented Feb 6, 2020

@kevin-ian I hope to fix it in the next few days. Maybe today, maybe on Saturday. I'll see when I get to it. I'll try to find some time today.

@stancl
Copy link
Member

stancl commented Feb 7, 2020

I assume the fix is to store a string (connection name) and resolve it using the Illuminate DatabaseManager, instead of storing the connection instance itself - on TenantDatabaseManagers: https://github.com/stancl/tenancy/pull/244/files#diff-3705ef5100e6ec06b1d60e715a84a742R23-R27

But before I push that fix I need to reproduce this and verify that things work. I think I could get this done today.

@stancl
Copy link
Member

stancl commented Feb 7, 2020

Try

composer require stancl/tenancy:dev-fix-pdo-serialization-exception

If that fixes this for you, I'll merge #289.

@kevin-ian
Copy link

@stancl , now it saves the information in the jobs table and the database is created when i run the job. Thanks for this fix

@stancl
Copy link
Member

stancl commented Feb 10, 2020

Alright, I'm merging the PR.

You should now run this to change your version constraint back to the 2.x release, so that you get future updates.

composer require stancl/tenancy:^2.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants