Package migrations not working (no errors) - php

So, I have created a new package that sites in vendor/vendorname/package
I have added the new package to my app/config/app.php file's provider array:
'Seriousjelly\Portfolio\PortfolioServiceProvider'
My package composer.json file seems legit:
"autoload": {
"classmap": [
"src/migrations",
"src/controllers",
"src/models",
"src/repositories"
],
"psr-0": {
"Seriousjelly\\Portfolio\\": "src/"
}
And in seriousjelly/porfolio/src/migrations I have 2 migrations:
2014_06_25_060429_create_portfolio_categories_table.php:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePortfolioCategoriesTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('portfolio_categories', function(Blueprint $table){
$table->increments('id');
$table->string('title', 255);
$table->string('subtitle', 255);
$table->string('short_description', 500);
$table->string('description');
$table->string('slug', 60);
$table->timestamp();
});
Schema::create('portfolio_item_categories', function(Blueprint $table) {
$table->integer('category_id')->unsigned();
$table->foreign('category_id')->references('id')->on('portfolio_categories');
$table->integer('item_id')->unsigned();
$table->foreign('item_id')->references('id')->on('portfolio_items');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('portfolio_categories');
Schema::drop('portfolio_item_categories');
}
}
and 2014_06_25_060414_create_portfolio_items_table.php:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePortfolioItemsTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('portfolio_items', function(Blueprint $table) {
$table->increments('id');
$table->string('title', 255);
$table->string('subtitle', 255)->nullable;
$table->string('short_description', 500)->nullable;
$table->string('description', 500);
$table->string('slug', 60);
$table->timestamp();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('portfolio_items');
}
}
Now, when I run php artisan migrate --package=seriousjelly/portfolio
It comes up saying that the migration ran successfully, however, if I check the database only the migrations table has been populated.
If I run the command once more, nothing happens, no errors, no message, nothing.
Any ideas?
p.s other package migrations are the same and seem to run ok, I have attempted composer update and composer dump-autoload but this doesn't work.

Ah, ok my bad. It seems that I have incorrectly spelt, turns out it's plural:
$table->timestamps();
Im not sure why terminal never threw an error though...

Related

Laravel 8 - Change existing migration

I am developing on Laravel Framework 8.33.1 and have the following migration on my local environment and also in production.
class CreateCompanyTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('company', function (Blueprint $table) {
$table->id();
$table->integer('company_id');
$table->integer('messageId');
$table->string('url');
$table->timestamps();
/**
New table:
$table->id();
$table->integer('company_id')->nullable($value = true);
$table->integer('messageId');
$table->integer('people_id')->nullable($value = true);
$table->string('url')->nullable($value = true);
$table->timestamps();
*/
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('company');
}
}
I would like to change the migration with the following adapted fields:
$table->id();
$table->integer('company_id')->nullable($value = true);
$table->integer('messageId');
$table->integer('people_id')->nullable($value = true);
$table->string('url')->nullable($value = true);
$table->timestamps();
As I am using the current migration in production I do not want to lose data.
I simply tried to modify the migration file with my new table definition, but I get:
> php artisan migrate
Nothing to migrate.
Any suggestions how to change the migration properly in laravel?
I appreciate your replies!
To modify an existing table, create a new migration.
php artisan make:migration alter_company_table
class AlterCompanyTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('company', function (Blueprint $table) {
$table->integer('company_id')->nullable()->change();
$table->integer('people_id')->nullable();
$table->string('url')->nullable()->change();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('company', function (Blueprint $table) {
$table->integer('company_id')->nullable(false)->change();
$table->dropColumn('people_id');
$table->string('url')->nullable(false)->change();
});
}
}
If you don't mind losing data and you are in development mode, you can do php artisan migrate:rollback and after execute: php artisan migrate. So your migrations works correctly. You can to read more here.
Now, if you just want to add new tables and modify others in you database, you should make another migration. But before this, you should to install doctrine/dbal to be able to modify your tables correctly, or else, it will give you many errors. After, add this lines in your config/database.php
use Illuminate\Database\DBAL\TimestampType;
'dbal' => [
'types' => [
'timestamp' => TimestampType::class,
],
],
Now you can do: php artisan make:migration add_new_tables_to_company and your file in database/migrations.
class AlterCompanyTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('company', function (Blueprint $table) {
$table->integer('company_id')->nullable()->change();
$table->integer('people_id')->nullable();
$table->string('url')->nullable()->change();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('company', function (Blueprint $table) {
$table->integer('company_id')->nullable(false)->change();
$table->dropColumn('people_id');
$table->string('url')->nullable(false)->change();
});
}
}

Change column type using laravel migration using Doctrine DBAL gives error

I use Laravel 5.2, Php7, Apache, Windows
my migration file is "2016_03_30_095234_alter_date_update_news_table.php"
class AddSlugUpdateNewsTable extends Migration
{
/**
* Run the migrations.
* php artisan make:migration add_slug_update_news_table
* #return void
*/
public function up()
{
Schema::table('news', function (Blueprint $table) {
$table->date('slug')->change();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('news', function (Blueprint $table) {
$table->dateTime('slug')->change();
});
}
}
But after run migrate,
$\> php artisan migrate
gives me this error!
[RuntimeException] Changing columns for table "news" requires
Doctrine DBAL; install "doctrine/dbal".
what do i do?
according to laravel docs. You must install doctrine/dbal first if you are using ->change() function.
Type composer require doctrine/dbal in your terminal
/**
* Run the migrations.
* php artisan make:migration alter_date_update_news_table
* #return void
*/
public function up()
{
Schema::table('news', function (Blueprint $table) {
$table->dropColumn('date');
});
Schema::table('news', function (Blueprint $table) {
$table->date('date')->after('image');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('news', function (Blueprint $table) {
$table->dropColumn('date');
});
Schema::table('news', function (Blueprint $table) {
$table->dateTime('date')->after('image');
});
}
Seems like you're missing a dependency.
Install it with composer:
composer require doctrine/dbal

Configuring oauth2-server-laravel with laravel-mongodb

I am trying to use oauth2-server-laravel with laravel-mongodb. After I generate migration using this command php artisan oauth2-server:migrations I tried to use php artisan migrate. But I got this error.
[ErrorException]
Missing argument 1 for Illuminate\Database\Schema\Blueprint::primary(),
called in
/home/opu/www/cwc_penguins/app/database/migrations/2015_01_19_203037
_create_oauth_scopes_table.php on line 17 and defined
2015_01_19_203037_create_oauth_scopes_table.php Migration code here
<?php
use Illuminate\Database\Schema\Blueprint;
use LucaDegasperi\OAuth2Server\Support\Migration;
class CreateOauthScopesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
$this->schema()->create('oauth_scopes', function (Blueprint $table) {
$table->string('id', 40)->primary();
$table->string('description');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
$this->schema()->drop('oauth_scopes');
}
}
Remove this:
->primary()
And it should work.
Or you can change it to ->primary('id') (or whatever the field name is). That's what I did.

Laravel migrations error

When I run php artisan migrate i get error B
Base table or view already exists: 1050 Table 'categories' already
exists'
What is it? Why? How to find error?
My categories migrations file:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CategoriesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('title')->index();
$table->text('description');
$table->integer('attachment_id')->unsigned()->index();
$table->foreign('attachment_id')->references('id')->on('attachment')->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
//
}
}
In that migration file, the function drop() should be
public function down()
{
Schema::drop('categories');
}
It seems that you try to create existing table..
so i think you are editing the migration file that you already installed
you can drop your database and re-migrate it
php artisan migrate:reset
then
php artisan migrate

Laravel 4 - one migrations in workbench can be rolled back, while others cannot

I have two types of workbench migrations: creating regular tables and creating pivot tables for Many-to-Many relationships.
Sample of regular migration:
<?php
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('users', function(\Illuminate\Database\Schema\Blueprint $table)
{
$table->increments('id')->unsigned();
$table->string('login')->unique();
$table->string('username')->unique();
$table->string('password');
$table->string('email');
$table->boolean('active')->default(true);
$table->timestamps();
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
Above migration can be rolled back
<?php
use Illuminate\Database\Migrations\Migration;
class CreatePivotRoleUser extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('role_user', function(\Illuminate\Database\Schema\Blueprint $table)
{
$table->integer('role_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->primary(['role_id', 'user_id']);
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('role_user');
}
}
While this cannot, because it gives
"Class 'CreatePivotPermissionRole' not found"
error.
How to fix it?
Your code looks correct.
If CreatePivotPermissionRole is not found, it means it had been deleted before. Check the content of all your down() methods, there must be something wrong there.

Categories