How to create a database table?

Hello everyone :slightly_smiling_face:

I tried to create a database table for one app for Nextcloud.

I read this documentaiton : Migrations — Nextcloud latest Developer Manual latest documentation .

I would like to use the migrations:generate occ command.

Currently, the application version is 0.0.1. I changed to 0.1.1.

Then, I inputed this command : sudo -u nginx php occ migrations:generate workspace 0100 and here is the result :

image

I don’t under why it doesn’t work ?

The appid and my release mapped on 3 digits are corrects…

Thanks for your help ! :blush:

Edit - 14 june at 4.23 pm

Aaah ! I didn’t see this message :

Nextcloud or one of the apps require upgrade - only a limited number of commands are available
You may use your browser or the occ upgrade command to do the upgrade

I run the sudo -u nginx php occ migrations:generate workspace 0000 command again. And I have always the same problem…

Your app version 0.1.1 translates to 0001 for the generate command, see
https://docs.nextcloud.com/server/latest/developer_manual/basics/storage/migrations.html#console-commands

So did you run occ upgrade ?

Hi @nickvergessen :slightly_smiling_face:

So did you run occ upgrade ?

Yes I did, and I don’t have this message ! :wink:

Thanks for your help ! :smiley:

Finally, I found the problem !
It was a problem permission in my app project.

I changed the owner with this command : sudo chown nginx: -R <path to app project>, then I ran the sudo -u nginx php occ migrations:generate workspace 0000 command again and it worked ! :wink:

Hovewer, the occ command doesn’t return an error message for a permission problem.

Does it have to a performance normal ?

Otherwise, can I create an issue to resolve this problem ?

One thing again @nickvergessen !

After I generated my migration file.
I don’t see how do I destroy/refresh/rollback my database table with occ command ?

	/**
	 * @param IOutput $output
	 * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
	 * @param array $options
	 */
	public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
	}

	/**
	 * @param IOutput $output
	 * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
	 * @param array $options
	 * @return null|ISchemaWrapper
	 */
	public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
		return null;
	}

	/**
	 * @param IOutput $output
	 * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
	 * @param array $options
	 */
	public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
	}

For example, I know Laravel Framework, it is my reference only… ^^’

With Laravel, I define my migration like this :

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateFlightsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('flights', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('airline');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('flights');
    }
}

Source: Database: Migrations - Laravel - The PHP Framework For Web Artisans

So, they (Laravel dev teams) have a down function called with php artisan migrate:rollback or php artisan migrate:refresh command when you want to apply changes.

Source: Database: Migrations - Laravel - The PHP Framework For Web Artisans

Do you have an equivalent to this ?

We don’t have the “down” way.

I think I should destroy and recreate the database table in changeSchema method ? :thinking:

yes, just look at the migration files in core/ or other apps