casperw/laravel-tidy-tables

Structures your database columns nice and tidy.

1.0.1 2018-10-10 17:13 UTC

This package is auto-updated.

Last update: 2024-10-24 05:59:07 UTC


README

Latest Version on Packagist Total Downloads Software License StyleCI

This package provides a way to reorder the columns of all your existing database tables. The sorting algorithm will sort table columns in the following structure:

  • Primary key
  • Universally unique identifier
  • Foreign keys
  • Native columns
  • Timestamps

Example

For example, I've created a 'products' table. You might recognize the situation on the left, where the table became messy and unstructured.

The table on the right shows how the columns are grouped after running the tidy command.

Alt text

Installation

Via Composer

$ composer require casperw/laravel-tidy-tables --dev

Configuration

The defaults are set in config/laraveltidytables.php. Copy this file to your own config directory to modify the values. You can publish the config using this command:

$ php artisan vendor:publish --tag=laraveltidytables.config

The configuration file contains an array of fields, and an array of data types.

Available fields

    /*
    |--------------------------------------------------------------------------
    | Fields
    |--------------------------------------------------------------------------
    |
    | The following fields are configurable and are used during
    | sorting of the configured database. Change these existing fields to your liking.
    | Note, that it's possible to add timestamps and change order to your liking.
    |
    */
    'fields' => [
        'primary_key' => 'id',
        'universally_unique_identifier' => 'uuid',
        'foreign_key_affix' => '_id',
        'timestamps' => [
            'deleted_at',
            'updated_at',
            'created_at',
        ],
    ],

Available data types

Note: By default, the configuration supports laravel's default datatypes. Check if your database corrosponds with the default values, and change them if needed!

    /*
    |--------------------------------------------------------------------------
    | Data types
    |--------------------------------------------------------------------------
    |
    | Here are all the datatypes that are used by the sorting algoritm.
    | It's important to note that these values are based on laravel's default migration data types.
    | You might not use a CHAR(36) for uuid's in your configuration. Change these values if so.
    |
    */
    'data_types' => [
        'universally_unique_identifier' => 'CHAR(36)',
        'foreign_keys' => 'INTEGER UNSIGNED',
        'timestamps' => 'TIMESTAMP NULL'
    ]

Usage

The following command will sort all of your database's tables.

Note: Always make sure to back-up your database first.

$ php artisan migrate --tidy

Change log

Please see the changelog for more information on what has changed recently.

License

MIT. Please see the license file for more information.