Code Monkey home page Code Monkey logo

yii2-schemadump's Introduction

yii2-schemadump

Build Status Latest Stable Version Total Downloads

Generate the schema from an existing database.

Demo

gif

Requirements

  • PHP 7.4 or later
  • Yii 2.x

Installation

composer require --dev jamband/yii2-schemadump

Usage

Add the following in config/console.php:

return [
    ...
    'components' => [
        ...
    ],
    'controllerMap' => [
        'migrate' => [
            'class' => yii\console\controllers\MigrateController::class,
            'templateFile' => '@jamband/schemadump/template.php',
        ],
        'schemadump' => [
            'class' => jamband\schemadump\SchemaDumpController::class,
            'db' => [
                'class' => yii\db\Connection::class,
                'dsn' => 'mysql:host=localhost;dbname=existing_database_name',
                'username' => 'your_username',
                'password' => 'your_password',
            ],
        ],
    ],
    ...
];

And run schemadump command.

cd /path/to/project
./yii schemadump

Example output:

// user
$this->createTable('{{%user}}', [
    'id' => $this->primaryKey()->comment('主キー'),
    'username' => $this->string(20)->notNull()->unique()->comment('ユーザ名'),
    'email' => $this->string(255)->notNull()->unique()->comemnt('メールアドレス'),
    'password' => $this->string(255)->notNull()->comment('パスワード'),
], $this->tableOptions);

Copy the output code and paste it into a migration file.

Commands

Generates the 'createTable' code. (default)

./yii schemadump
./yii schemadump/create

Generates the 'dropTable' code.

./yii schemadump/drop

Useful commands (for macOS user):

./yii schemadump | pbcopy
./yii schemadump/drop | pbcopy

Check help.

./yii help schemadump

Supports

  • Types
  • Size
  • Unsigned
  • NOT NULL
  • DEFAULT value
  • COMMENT
  • Unique key
  • Foreign key
  • Composite primary keys
  • Primary key without AUTO_INCREMENT
  • ENUM type (for MySQL)

yii2-schemadump's People

Contributors

jamband avatar lubobill1990 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

yii2-schemadump's Issues

Sets are created as string(0)

Hey there,
I found a bug: If I dump a schema containing a set this set is represented as string with length 0.

So in code
set('MONDAY','TUESDAY','WEDNESDAY','THURSDAY','FRIDAY','SATURDAY','SUNDAY') NOT NULL
becomes
$this->string(0)->notNull(),

But overall nice and handy extension, keep up the good work

I'm using MySQL as database.

A feature request: A differential version

Check the DB for migrations already run and compare the physical migration files with the db. If the object is in the DB, but not in an already run migration file, then add it to the next migration. Then generate new migrations for only new columns, keys\indexs ect....

Object drops could be done the same way. If they are in a migration file that is already run, but not in the DB any-more, they need to be added to the next migration as a drop.

This is a massive task I know, but it would save a ton of time if we had that! :)

Create indexes and drop foreign keys

Hi,

First of all, thanks for this very useful tool!

I am wondering if it would be possible to add the following features, similar to how it's done in this gist?

  1. Create indexes along with foreign keys
  2. Drop foreign keys before dropping tables, otherwise downgrade simply doesn't work

Empty primary key

There is a possibility that a table, usually is a join table, does not have a primary key at all. In this case it will respond with an exception of offset because it tries to get pk[0] of null $pk. You should change the corresponding line on actionCreate to check if it has primary key, then to call the function getPrimaryKeyDefinition.

if (!empty($table->primaryKey)) $stdout .= $this->getPrimaryKeyDefinition($table->primaryKey, $stdout, $offset);

Use --db option if given

On line 33 of Migration.php, this code is used:

if (Yii::$app->db->driverName === 'mysql') {

When migrating with the --db option, the 'default' db is still used. Please change this line to:

if ($this->db->driverName === 'mysql') {

issue with "unsigned"

There is an Issue with the UNSIGNED field that is added.
For instance I have an unsigned int primary key which is output as:

id int(10) NOT NULL AUTO_INCREMENT PRIMARY KEY UNSIGNED,

MySQL doesn't accept this on a CREATE TABLE statement.

42000: 1064 Syxtax error occured while deploying migrations

First of all, its excellent library. It save alot of my time hats off to you.

While following the process suggested by you. I am able to create migrations scripts successfully. But when I try to deploy it via yii command line utility its gave me error of 1064 syntax error and process aborted

Create action for data dumping

Hi Great work and I have enjoyed it when migrating my websites from local to remote servers, but found that there is no option for copying data from database. if this option is added then it will be very good

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.