Dump DB Records for DB Migration

On the db migration I’ve got perfect script from Leric for dumping the DB schema, so the developer doesn’t need to write manually when the db is big already. That script is very useful, but beside schema, I also need some script to dump the db records (e.g: records on lookup table, rbac tables or even user tables)

Is there any script for dumping db records like that? ::)

Hi junxiong,

I am working with this at the moment, please have a look:


    public function run($args) {

        $schema = $args[0];

        $tables = Yii::app()->db->schema->getTables($schema);

        $result = '';

        foreach ($tables as $def) {

            $result .= '$this->createTable("' . $def->name . '", array(' . "\n";

            foreach ($def->columns as $col) {

                $result .= '    "' . $col->name . '"=>"' . $this->getColType($col) . '",' . "\n";

            }

            $result .= '), "");' . "\n\n";

            

            $data = Yii::app()->db->createCommand()

                ->from($def->name)

                ->query();

            foreach ($data AS $row) {

                $result .= '$this->insert("' . $def->name . '", array(' . "\n";

                foreach($row AS $column => $value) {

                    $result .= '    "' . $column . '"=>"' .  $value . '",' . "\n";            

                }

                $result .= ') );' . "\n\n";

            }

        }

        echo $result;

    }

Still very simple, but works :)

Best regards,

schmunk

Hey, thanks Schmunk

I’ll take a look

Hi

I was also looking for solution, couldn’t find any thing that absolutely worked for me, So I wrote mine:

Please feel free to use it (Under MIT License) :rolleyes:

It is in GIT here Deftmigrations

Thanks