I've googled a ton and tried to get my database for my package testing to work. I am building a package that I am going to use myself, just to try to get a grasp of how things work.
But now when I am trying to test, the migrations do not migrate. Can someone please put me in the right direction?
When I run my tests it just states Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1 no such table: carts (SQL: insert into "carts" ("id", "updated_at", "created_at") values (1051d9f5-4014-4450-ba83-34ded48d7dd9, 2021-11-09 07:26:36, 2021-11-09 07:26:36))
Here are my testing files. And as I understand it the migration should be run every time my tests setUp method is running, right?
I noticed you allow your migration to be published, but it does not seem your service provider itself registers the migrations, thus it seems you force the end user to publish them first?
Usually, you can both offer to publish them, but also register in your service provider using the same thing I placed above in the boot of your provider. If you did that, then you would not have to manually load those in your test setUp.
@Tippin thank you, that was it! :) Really appreciate it!
Tho, I do not understand the latter half of your response. Would you like to clarify? I tried to add the loadMigrationsForm to the boot method in the ServiceProvider. But that gave me this error: Cannot declare class CreateCartsTable, because the name is already in use
@jimmitjoo I do see your migration file you publish in the service provider is not timestamped, so you use a method do to so upon publishing. I suppose that is fine, if you do not want to allow the user to just run migrate without publishing them.
I am not 100% on loading a migration file that is not timestamped by default, so adding to the service provider could break. However, if you added the loadMigrationsFrom into your service provider, then be sure to remove it from the test case's setUp method, as I believe that is giving you the class redeclaration error.
My main side question would be, is there a reason your migration file is not timestamped by default? Any reason for all the naming magic in your getMigrationFileName method?
@Tippin so I timestamped it now and registered it in the service provider boot method. Removed it from the TestCase setUp method. Published it to github.
But still cannot get the database migrated. PDOException: SQLSTATE[HY000]: General error: 1 no such table: carts
Regarding the timestamp, I basically watched how spatie do things in their packages. The getMigrationFileName is copied out of their permissions package. That method is used so that the migration would get the current timestamp when the user publishes the package so that they are ran in the correct order further on. Probably unnecessary here... :)
However still getting that error similar to the one I had earlier Fatal error: Cannot declare class CreateCartsTable, because the name is already in use in /Users/jimmiejohansson/Code/cart/database/migrations/2021_11_09_111109_create_cart_tables.php on line 7
But that is the only place I do declare that class. Same thing if I clone the repo to my other computer.