Upgrade Guide
=================

# Ginger v4.*

Starting with version 4 Ginger provides its own user provider and auth.
FOS User is no longer used.

### Prerequisite

### CAUTION!

**If you want to upgrade the bundle to `v4.*` from within a symfony application that already has real data at it´s disposal, be aware that you will have to migrate some of your database tables!**

### ATTENTION!

When making changes to the database of a production system, **ALWAYS BE SURE TO HAVE A BACKUP OF YOUR DATABASE** before proceeding
with the following steps!!!

### Step 1: Update config

*  security.yaml
*  doctrine.yaml
*  routing.yaml

### Step 2: Migrate user data

As of version 4.0 Ginger comes with its own user class, you need to migrate your existing user data:


#### 2.1 Disable foreign key checks

To verify the current status you may run:

    SELECT IF((SELECT `VARIABLE_VALUE` FROM `performance_schema`.`global_variables`WHERE `VARIABLE_NAME` = 'foreign_key_checks') = 'ON', 1, 0) `global.foreign_key_checks`,
    IF((SELECT `VARIABLE_VALUE`
    FROM `performance_schema`.`session_variables`
    WHERE `VARIABLE_NAME` = 'foreign_key_checks') = 'ON', 1, 0) `session.foreign_key_checks`;


To temporarily turn off foreign key checks for mysql, execute:

    SET FOREIGN_KEY_CHECKS = 0;

**Note:** Make sure to clear the application cache afterwards:

    symfony console c:c


#### 2.2 Create new user table

Run a schema update:

    symfony console c:c
 
  
**Note:** Although the command will fail at this point, this will create the required user table. Don´t mind the error and proceed...

Alternatively, you may create the required user table manually by running:

    CREATE TABLE `ginger_user` (
      `id` int(11) NOT NULL,
      `username` varchar(180) COLLATE utf8mb4_unicode_ci NOT NULL,
      `roles` json NOT NULL,
      `password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
      `enabled` tinyint(1) NOT NULL,
      `first_name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
      `last_name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
      `email` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
      `last_login` datetime DEFAULT NULL,
      `created_at` datetime NOT NULL,
      `updated_at` datetime NOT NULL
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
    

#### 2.2 Copy existing user data

To copy your existing user data from `fos_user` to `ginger_user`, run:
  
    INSERT INTO ginger_user ( id, username, email, password, first_name, last_name, created_at, updated_at, last_login, enabled, roles)
    SELECT id, username, email, password, first_name, last_name, created_at, updated_at, last_login, enabled, '[]'
    FROM fos_user;


#### 2.2 Migrate user roles

Use the following symfony command to adjust the users roles (**todo: move to bundle...**):

    symfony console app:migrate:user
    

#### 2.3 Re-run schema update:
  
    symfony console d:s:u -f
 

#### 2.4 Enable foreign key checks

To revert disabling foreign key checks, run:
  
    SET FOREIGN_KEY_CHECKS = 1;


#### Todo: Add further required steps...

*  TokenGenerator?
*  ROLES in twigs
