database schema dump task.

usage:
$ ./app db:schema:dump

@option      database,d=all      target database (default is all databases).
180895
public function dumpTask(Option $option)
    {
        $databases = $this->getDatabases($option);

        $start = microtime(true);
        foreach ($databases as $alias => $database) {
            $manager = $this->getManager($alias, $database);

            // schema class
            $schemaFile = $this->migrationHelper->getSchemaFile($alias);
            $this->console->log($schemaFile);

            $dump = $manager->dumpSchema($this->application->getEnv());
            $this->console->log($dump);

            if (file_put_contents($schemaFile, $dump) === false) {
                $this->sendMessage('can not write schema file. -> %s', $schemaFile);
            }
            
            // schema yaml
            $schemaYAMLFile = $this->migrationHelper->getSchemaYAMLFile($alias);
            $this->console->log($schemaYAMLFile);
            
            $dump = $manager->dumpSchemaYAML($this->application->getEnv());
            $this->console->log($dump);
            
            if (file_put_contents($schemaYAMLFile, $dump) === false) {
                $this->sendMessage('can not write schema yaml file. -> %s', $schemaYAMLFile);
            }
        }
        $end = microtime(true);

        $this->sendMessage('');
        $this->sendMessage('All Done. Took %.4fs', $end - $start);
    }
140346
public function dump()
    {
        $return = '';
        
        if (count($this->tables) == 0)
        {
            $this->addAllTables();
        }
        
        $return .= "--\n";
        $return .= "-- Backup ".$this->db['name']." - ".date('Y-m-d H:i:s')."\n";
        $return .= "--\n\n\n";
        
        $return .= "SET FOREIGN_KEY_CHECKS=0;";
        $return .= "\n\n\n";

        if ($this->addCreateDatabaseIfNotExists === true)
        {
            $return .= "CREATE DATABASE IF NOT EXISTS `".$this->db['name']."`;\n";
            $return .= "USE `".$this->db['name']."`;";
            $return .= "\n\n\n";
        }
        
        foreach ($this->tables as $table)
        {
            // We skip excluded tables
            if (in_array($table, $this->excludedTables))
                continue;

            $stmt = $this->dbh->query('SELECT * FROM `'.$table.'`');
            $stmt->execute();
            $num_fields = $stmt->columnCount();
            
            $return .= "--\n";
            $return .= "-- Table ".$table."\n";
            $return .= "--\n\n";
            
            // Dump structure ?
            if ($this->dumpStructure === true)
            {
                // Add DROP TABLE ?
                if ($this->addDropTable === true)
                {
                    $return .= 'DROP TABLE IF EXISTS `'.$table.'`;';
                    $return .= "\n\n";
                }

                $create_table_q = $this->query('SHOW CREATE TABLE `'.$table.'`', false);
                $create_table = $create_table_q[1];
                
                // Add IF NOT EXISTS ?
                if ($this->addIfNotExists === true)
                {
                    $create_table = preg_replace('/^CREATE TABLE/', 'CREATE TABLE IF NOT EXISTS', $create_table);
                }
                
                $return .= $create_table.";\n\n";
            }
            
            // Dump datas ?
            if ($this->dumpDatas === true)
            {
                $datas = $this->query('SELECT * FROM `'.$table.'`');

                foreach ($datas as $row)
                {
                    $return .= 'INSERT INTO `'.$table.'` VALUES(';
                    
                    for ($i = 0; $i < $num_fields; $i++) 
                    {
                        if (isset($row[$i]))
                        {
                            $row[$i] = addslashes($row[$i]);
                            
                            $return .= '"'.$row[$i].'"';
                        }
                        else
                        {
                            $return .= '""';
                        }
                        
                        if ($i < ($num_fields-1))
                        {
                            $return .= ', ';
                        }
                    }
                    
                    $return .= ");\n";
                }
            }
            
            $return .= "\n\n";
            $return .= "-- --------------------------------------------------------";
            $return .= "\n\n\n";
        }
        
        
        // Save content in file
        file_put_contents($this->filename.'.'.$this->extension, $return);
        
        
        // Zip the file ?
        if (!is_null($this->compressFormat))
        {
            $this->compress();
        }
        
        // Download the file ?
        if ($this->downloadFile === true)
        {
            $this->download();
        }
        
        // Delete the file ?
        if ($this->deleteFile === true)
        {
            $this->delete();
        }
    }
404544
def dump(self):
        """"""
        assert self.database is not None

        cmd = "SELECT count from {} WHERE rowid={}"
        self._execute(cmd.format(self.STATE_INFO_TABLE, self.STATE_INFO_ROW))
        ret = self._fetchall()
        assert len(ret) == 1
        assert len(ret[0]) == 1
        count = self._from_sqlite(ret[0][0]) + self.inserts

        if count > self.row_limit:
            msg = "cleaning up state, this might take a while."
            logger.warning(msg)

            delete = count - self.row_limit
            delete += int(self.row_limit * (self.row_cleanup_quota / 100.0))
            cmd = (
                "DELETE FROM {} WHERE timestamp IN ("
                "SELECT timestamp FROM {} ORDER BY timestamp ASC LIMIT {});"
            )
            self._execute(
                cmd.format(self.STATE_TABLE, self.STATE_TABLE, delete)
            )

            self._vacuum()

            cmd = "SELECT COUNT(*) FROM {}"

            self._execute(cmd.format(self.STATE_TABLE))
            ret = self._fetchall()
            assert len(ret) == 1
            assert len(ret[0]) == 1
            count = ret[0][0]

        cmd = "UPDATE {} SET count = {} WHERE rowid = {}"
        self._execute(
            cmd.format(
                self.STATE_INFO_TABLE,
                self._to_sqlite(count),
                self.STATE_INFO_ROW,
            )
        )

        self._update_cache_directory_state()

        self.database.commit()
        self.cursor.close()
        self.database.close()
        self.database = None
        self.cursor = None
        self.inserts = 0
479579
def db_import(ctx, schema, uuid, object_filter, import_format, filename, all_schemata, dry):
    """"""

    import_format = import_format.upper()

    with open(filename, 'r') as f:
        json_data = f.read()
    data = json.loads(json_data)  # , parse_float=True, parse_int=True)

    if schema is None:
        if all_schemata is False:
            log('No schema given. Read the help', lvl=warn)
            return
        else:
            schemata = data.keys()
    else:
        schemata = [schema]

    from hfos import database
    database.initialize(ctx.obj['dbhost'], ctx.obj['dbname'])

    all_items = {}
    total = 0

    for schema_item in schemata:
        model = database.objectmodels[schema_item]

        objects = data[schema_item]
        if uuid:
            for item in objects:
                if item['uuid'] == uuid:
                    items = [model(item)]
        else:
            items = []
            for item in objects:
                thing = model(item)
                items.append(thing)

        schema_total = len(items)
        total += schema_total

        if dry:
            log('Would import', schema_total, 'items of', schema_item)
        all_items[schema_item] = items

    if dry:
        log('Would import', total, 'objects.')
    else:
        log('Importing', total, 'objects.')
        for schema_name, item_list in all_items.items():
            log('Importing', len(item_list), 'objects of type', schema_name)
            for item in item_list:
                item._fields['_id'] = bson.objectid.ObjectId(item._fields['_id'])
                item.save()
1123451
def drop_schema(self, schema, cascade=False):
        
        if schema in self.schemas:
            sql = "DROP SCHEMA " + schema
            if cascade:
                sql = sql + " CASCADE"
            self.execute(sql)
267671
def create_schema(self, schema):
        
        if schema not in self.schemas:
            sql = "CREATE SCHEMA " + schema
            self.execute(sql)
195573
function getDatabase(index, opts) {
  var db;
  if(!this._databases[index]) {
    db = new Database(this, opts);
    this._databases[index] = db;
  }
  return this._databases[index];
}
165616
public void dumpIidToIvfpqDB(String dumpFilename) throws Exception {
		DatabaseEntry foundKey = new DatabaseEntry();
		DatabaseEntry foundData = new DatabaseEntry();

		ForwardCursor cursor = iidToIvfpqDB.openCursor(null, null);
		BufferedWriter out = new BufferedWriter(new FileWriter(new File(dumpFilename)));
		while (cursor.getNext(foundKey, foundData, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
			int iid = IntegerBinding.entryToInt(foundKey);
			TupleInput input = TupleBinding.entryToInput(foundData);
			int listId = input.readInt();
			out.write(iid + " " + listId + "\n");
		}
		cursor.close();
		out.close();
	}
1174201
function migrate(cb) {

      // get all already executed migrations.
      db.query('SELECT "version" FROM "schema_migrations"', function(err, resp) {
        var migrations_run = 0;
        var executed = [];

        // if there is an error assume we are at the default state, and run root migration
        if (err) {

          // attempt to run file located at app_root/migrations/schema/root.sql -- good for previous data force load (e.g. Rails)
          var schema = fs.readFileSync(migrations_dir + "schema/root.sql").toString();
          db.query(schema); // populate with root schema if not populated.
        } else {
          // if no error, dump all versions into an array called executed for quick searching.
          for (var rl = resp.rows.length; rl > 0; rl--) {
            executed.push(resp.rows[rl - 1].version);
          }
        }


        // function loop to execute through the remainin "torun" migrations
        var run_migrations = (function(torun) {

          // get the next migration
          var current = torun.pop();

          // if there is a migration
          if (current) {

            // test if already executed
            if (executed.indexOf(current.id) < 0) {
              // alert of execution
              console.log("Executing migration: " + current.id);

              // check if migration is SQL
              if (current.sql) {

                // suffix with schema migrations insertion so we dont run again if successful.
                var sql = OPEN_TRANSACTION+current.sql + "; INSERT INTO schema_migrations VALUES (" + current.id + "); "+CLOSE_TRANSACTION;

                // execute the full query
                db.query(sql, function(e, r) {
                  if (e) {
                    db.query("ROLLBACK;", function () {
                      // if error, dump why we couldn't migrate and process no more.
                      console.log("Could not migrate database. Error provided below.")
                      console.log(e);
                      db.end();
                    });
                  } else {

                    // otherwise add to counter and run the rest of the "torun" array
                    migrations_run++;
                    run_migrations(torun);
                  }
                });

                // if migration is JS code
              } else if (current.js) {

                // pass our db object and execute with callback to insert into schema migration AND recurse with rest of "torun" array
                current.js(db, function() {
                  db.query("INSERT INTO schema_migrations VALUES (" + current.id + ")", function(err, results) {
                    migrations_run++;
                    run_migrations(torun);
                  });
                });
              }
            } else {

              // if no idea what to do, just skip!
              run_migrations(torun);

            }

            // if no more migrations
          } else {
            // only output if we done work.
            if (migrations_run > 0) console.log("Migrations run: " + migrations_run);

            // if there is a callback, exec it
            if (cb) {
              cb();
            }
          }
        });




        // populate all existing migrations by reading the migrations directory
        fs.readdir(migrations_dir, function(err, list) {
          var migrations = [];


          for (var li = 0, ll = list.length; li < ll; li++) {

            // if the file has a .sql extension, load it as a file read for sql schema updating
            if (m = list[li].match(/(.*)\.sql/)) {
              migrations.push({
                id: m[1],
                sql: fs.readFileSync(migrations_dir + m[0]).toString()
              });

              // if the file has a .js extension, load via require system and set js attribute as .migrate function
            } else if (j = list[li].match(/(.*)\.js/)) {
              migrations.push({
                id: j[1],
                js: require(migrations_dir + "/" + list[li]).migrate
              });
            }
          }

          //recursively run through all migrations after they have been sorted by ID in ascending order
          run_migrations(migrations.sort((function(a, b) {
            return (parseInt(b.id) - parseInt(a.id));
          })));
        });
      });
    }
1284561
function schema (args) {
    // get args object
    args = requireValidOptionalObject(args)
    // get colunmn and index information from database
    return Promise.all([
        this.mysql().query(sql.describe(this)),
        this.mysql().query(sql.showCreateTable(this)),
        this.mysql().query(sql.showIndexes(this)),
    ])
    // build schema from database response
    .then(res => {
        // get columns and indexes from describe and show index queries
        var columns = res[0][0]
        var createTable = res[1][0]
        var indexes = res[2][0]
        // schema data
        var schema = {
            columns: {},
            indexes: [],
        }
        // get first and only create table record
        createTable = _.get(createTable, "[0]['Create Table']", '')
        // get engine and charset from create table sql
        var engine = createTable.match(/ENGINE=(\w+)/)
        schema.engine = engine && engine[1] || ''
        var charset = createTable.match(/DEFAULT CHARSET=(\w+)/)
        schema.charset = charset && charset[1] || ''
        // build column data
        _.each(columns, dbColumn => {
            var column = schema.columns[dbColumn.Field] = {}
            // check unsigned
            if (dbColumn.Type.match(/unsigned$/)) {
                column.unsigned = true
                dbColumn.Type = dbColumn.Type.replace(/ unsigned/, '')
            }
            // char id column
            if (dbColumn.Type === 'char(32)') {
                // model must not be set for binary ids
                assert.ok(this.binaryIds === false, 'binaryIds must be disabled for char id columns')
                // set type
                column.type = 'id'
            }
            // binary id column
            else if (dbColumn.Type === 'binary(16)') {
                // model must be set for binary ids
                assert.ok(this.binaryIds === true, 'binaryIds must be enabled for binary id columns')
                // set type
                column.type = 'id'
            }
            // all other column types
            else  {
                // require valid type
                assert.ok(sql.columnTypesInverse[dbColumn.Type], 'unrecognized column type '+dbColumn.Type)
                // map database to internal type
                column.type = sql.columnTypesInverse[dbColumn.Type]
            }
            // is column nullable
            if (dbColumn.Null === 'NO') {
                column.null = false
            }
            // set default if not null
            if (dbColumn.Default) {
                column.default = dbColumn.Default
                // convert boolean
                if (column.type === 'boolean') {
                    column.default = column.default === '1' ? true : false
                }
            }
        })
        // map of indexes by key name since keys may cover multiple columns
        var indexesByKey = {}
        // build map of indexes by key name
        _.each(indexes, dbIndex => {
            // create entry for index if it does not already exist
            if (!indexesByKey[dbIndex.Key_name]) {
                indexesByKey[dbIndex.Key_name] = {
                    columns: {}
                }
            }
            // get entry for index
            var index = indexesByKey[dbIndex.Key_name]
            // add column with sequence in index
            index.columns[dbIndex.Column_name] = parseInt(dbIndex.Seq_in_index)
            // add unique flag if true
            if (dbIndex.Non_unique === '0') {
                index.unique = true
            }
        })
        // add indexes to schema
        _.each(indexesByKey, (index, name) => {
            var indexColumns = _.keys(index.columns)
            // multi-column index
            if (indexColumns.length > 1) {
                // sort index columns by the Seq_in_index value - these may
                // always be in order so this may not be needed
                indexColumns = _.sortBy(indexColumns, indexColumn => index.columns[indexColumn])
                // create schema index entry
                var schemaIndex = {
                    columns: indexColumns,
                }
                // set unique flag
                if (index.unique) {
                    schemaIndex.unique = true
                }
                // add index to schema
                schema.indexes.push(schemaIndex)
            }
            // single column index
            else {
                // get first and only column
                var indexColumn = indexColumns[0]
                // PRIMARY KEY
                if (name === 'PRIMARY') {
                    schema.columns[indexColumn].primary = true
                }
                // unique index
                else if (index.unique) {
                    schema.columns[indexColumn].unique = true
                }
                // non-unique index
                else {
                    schema.columns[indexColumn].index = true
                }
            }
        })
        // resolve with schema
        return schema
    })
    .catch(err => {
        // reject on anything besides a table does not exist error
        // if table does not exist resolve with undefined
        if (err.errno !== 1146) {
            throw err
        }
    })
}
120656
def compile(self, db):
        
        sql = self.expression
        if self.alias:
            sql += (' AS ' + db.quote_column(self.alias))
        return sql
885167
def handle(self, *args, **options):
        
        self.db = options.get("database", DEFAULT_DB_ALIAS)
        self.current_name = connections[self.db].settings_dict["NAME"]
        self.compare_name = options.get("db_name")
        self.lines = options.get("lines")
        self.ignore = int(options.get('ignore'))

        if not self.compare_name:
            self.compare_name = "%s_compare" % self.current_name
        
        command = NASHVEGAS.get("dumpdb", "pg_dump -s {dbname}")
        
        print "Getting schema for current database..."
        current_sql = Popen(
            command.format(dbname=self.current_name),
            shell=True,
            stdout=PIPE
        ).stdout.readlines()
        
        print "Getting schema for fresh database..."
        self.setup_database()
        connections[self.db].close()
        connections[self.db].settings_dict["NAME"] = self.compare_name
        try:
            call_command("syncdb", interactive=False, verbosity=0, migrations=False)
            new_sql = Popen(
                command.format(dbname=self.compare_name).split(),
                stdout=PIPE
            ).stdout.readlines()
        finally:
            connections[self.db].close()
            connections[self.db].settings_dict["NAME"] = self.current_name
            self.teardown_database()
        
        print "Outputing diff between the two..."
        print "".join(difflib.unified_diff(normalize_sql(current_sql, self.ignore),
                                           normalize_sql(new_sql, self.ignore),
                                           n=int(self.lines)))
651130
def init_db():
    
    with current_app.app_context():
        for filename in CHILL_CREATE_TABLE_FILES:
            db.execute(text(fetch_query_string(filename)))
419122
def create_dump():
    
    dump = upkey("dump").setResultsName("action")
    return (
        dump
        + upkey("schema")
        + Optional(Group(delimitedList(table)).setResultsName("tables"))
    )
796370
public function dump()
    {
        foreach ($this->children as $child) {
            $this->logger->info(sprintf('[dizda-backup] Dumping %s database', $child->getName()));
            $child->dump();
        }
    }
50366
public function digipolisDatabaseBackup($database = 'default', $opts = [
      'file-system-config|fsconf' => null,
      'database-config|dbconf' => null,
      'compression|c' => 'gzip',
      'destination|d' => null,
      'destination-type|dtype' => 'local',
    ])
    {
        if (is_callable([$this, 'readProperties'])) {
            $this->readProperties();
        }
        $destination = $opts['destination']
            ? $opts['destination']
            : realpath(getcwd()) . '/project.tar.gz';
        if (!$opts['file-system-config']) {
            $opts['file-system-config'] = [
                $opts['destination-type'] => [
                    'type' => ucfirst($opts['destination-type']),
                    'root' => realpath(dirname($destination)),
                ],
            ];
            $destination = basename($destination);
        }
        return $this->createDbTask('taskDatabaseBackup', $database, $opts)
            ->destination($destination, $opts['destination-type'])
            ->run();
    }
654095
def export_datasource_schema(back_references):
    """"""
    data = dict_import_export.export_schema_to_dict(
        back_references=back_references)
    yaml.safe_dump(data, stdout, default_flow_style=False)