Migrate process schema.

        :param process: Process instance
        :param schema: Process schema to migrate
        :param from_state: Database model state
        :return: True if the process was migrated, False otherwise
483994
def migrate_process_schema(self, process, schema, from_state):
        
        container = dict_dot(schema, '.'.join(self.field[:-1]), default=list)

        # Ignore processes, which already contain the target field.
        migrate = False
        for field in container:
            if field['name'] == self.field[-1]:
                field['name'] = self.new_field
                migrate = True
                break
            elif field['name'] == self.new_field:
                # Already has target field.
                migrate = False
                break
        else:
            if not self.skip_no_field:
                raise ValueError(
                    "Unable to rename: there is no field with name '{field}' or '{new_field}'.".format(
                        field=self.field[-1],
                        new_field=self.new_field,
                    )
                )

        return migrate
751398
def run_migrations_online():
    

    # this callback is used to prevent an auto-migration from being generated
    # when there are no changes to the schema
    # reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html
    def process_revision_directives(context, revision, directives):
        if getattr(config.cmd_opts, 'autogenerate', False):
            script = directives[0]
            if len(script.upgrade_ops_list) >= len(bind_names) + 1:
                empty = True
                for upgrade_ops in script.upgrade_ops_list:
                    if not upgrade_ops.is_empty():
                        empty = False
                if empty:
                    directives[:] = []
                    logger.info('No changes in schema detected.')

    # for the direct-to-DB use case, start a transaction on all
    # engines, then run all migrations, then commit all transactions.
    engines = {
        '': {
            'engine': engine_from_config(
                config.get_section(config.config_ini_section),
                prefix='sqlalchemy.',
                poolclass=pool.NullPool,
            )
        }
    }
    for name in bind_names:
        engines[name] = rec = {}
        rec['engine'] = engine_from_config(
            context.config.get_section(name),
            prefix='sqlalchemy.',
            poolclass=pool.NullPool)

    for name, rec in engines.items():
        engine = rec['engine']
        rec['connection'] = conn = engine.connect()

        if USE_TWOPHASE:
            rec['transaction'] = conn.begin_twophase()
        else:
            rec['transaction'] = conn.begin()

    try:
        for name, rec in engines.items():
            logger.info("Migrating database %s" % (name or '<default>'))
            context.configure(
                connection=rec['connection'],
                upgrade_token="%s_upgrades" % name,
                downgrade_token="%s_downgrades" % name,
                target_metadata=get_metadata(name),
                process_revision_directives=process_revision_directives,
                **current_app.extensions['migrate'].configure_args
            )
            context.run_migrations(engine_name=name)

        if USE_TWOPHASE:
            for rec in engines.values():
                rec['transaction'].prepare()

        for rec in engines.values():
            rec['transaction'].commit()
    except:
        for rec in engines.values():
            rec['transaction'].rollback()
        raise
    finally:
        for rec in engines.values():
            rec['connection'].close()
244699
def migrate_constituencies(apps, schema_editor):
    
    Constituency = apps.get_model("representatives", "Constituency")
    for c in Constituency.objects.all():
        c.save()
777893
def _toState(self, state, *args, **kwargs):
        
        try:
            method = getattr(self, '_state_%s' % state)
        except AttributeError:
            raise ValueError("No such state %r" % state)

        log.msg("%s: to state %r" % (self.__class__.__name__, state))
        self._state = state
        method(*args, **kwargs)
247697
function () {
                /*  determine parameters  */
                var params = $cs.params("migrate", arguments, {
                    comps:     { pos: "...", req: true    },
                    sync:      {             def: false   },
                    completed: {             def: $cs.nop }
                });

                /*  iterate over all components to migrate  */
                var n = 0;
                var parent = this;
                _cs.foreach(params.comps, function (comp) {
                    if (params.sync) {
                        /*  migrate synchronously  */
                        var state_old = comp.state();
                        comp.state({ state: "created", sync: true });
                        comp.detach();
                        comp.attach(parent);
                        comp.state(state_old);
                    }
                    else {
                        /*  migrate asynchronously  */
                        (function (comp, state_old) {
                            comp.state({ state: "created", sync: false, callback: function (/* state_new */) {
                                this.detach();
                                this.attach(parent);
                                this.state({ state: state_old, sync: false, callback: function (/* state_old */) {
                                    n--;
                                    if (n === 0)
                                        params.completed.call(null);
                                }});
                            }});
                        })(comp, comp.state());
                        n++;
                    }
                });
            }
290375
def migrate(self) -> None:
        

        assert self.ormSessionCreator, "ormSessionCreator is not defined"

        connection = self._dbEngine.connect()
        isDbInitialised = self._dbEngine.dialect.has_table(
            connection, 'alembic_version',
            schema=self._metadata.schema)
        connection.close()

        if isDbInitialised or not self._enableCreateAll:
            self._doMigration(self._dbEngine)

        else:
            self._doCreateAll(self._dbEngine)

        if self._enableForeignKeys:
            self.checkForeignKeys(self._dbEngine)
1293562
@Override
	public String process(String query, Map<String, Object> params) {
		return applySchema(schema, query);
	}
1361413
def migrate_app(sender, *args, **kwargs):
    
    from .registration import registry
    if 'app_config' not in kwargs:
        return
    app_config = kwargs['app_config']

    app_name = app_config.label

    fields = [fld for fld in list(registry._field_registry.keys()) if fld.startswith(app_name)]

    sid = transaction.savepoint()
    for fld in fields:
        model_name, field_name = fld.split('.')[1:]
        if field_exists(app_name, model_name, field_name):
            continue
        model = app_config.get_model(model_name)
        try:
            with connection.schema_editor() as schema_editor:
                schema_editor.add_field(model, registry._field_registry[fld])
                if sid:
                    transaction.savepoint_commit(sid)
        except ProgrammingError:
            if sid:
                transaction.savepoint_rollback(sid)
            continue
341137
def rollback_migration(engine, connection, path, migration_to_rollback):
    

    # Get migrations applied
    migrations_applied = get_migrations_applied(engine, connection)

    # Ensure that the migration was previously applied
    if not is_applied(migrations_applied, migration_to_rollback):
        raise RuntimeError(
            '`%s` is not in the list of previously applied migrations.' % (migration_to_rollback))

    # Rollback file
    file = path + migration_to_rollback + '/down.sql'

    # Ensure that the file exists
    check_exists(file)

    # Set vars
    basename = os.path.basename(os.path.dirname(file))

    # Get migration source
    source = get_migration_source(file)
    # print (source);

    # Run migration rollback
    run_migration(connection, source, engine)

    # Delete migration
    delete_migration(connection, basename)

    # Log
    print('   -> Migration `%s` has been rolled back' % (basename))

    return True
760897
def source_schema(self, sources=None, tables=None, clean=False):
        

        sources = self._resolve_sources(sources, tables, None,
                                        predicate=lambda s: s.is_processable)

        for source in sources:
            source.update_table()
            self.log("Creating source schema for '{}': Table {},  {} columns"
                     .format(source.name, source.source_table.name, len(source.source_table.columns)))

        self.commit()
1090975
private boolean prepareManagedSchema(String tableName,
      String entitySchemaString) {
    String entityName = getEntityNameFromSchemaString(entitySchemaString);
    AvroEntitySchema entitySchema = parser
        .parseEntitySchema(entitySchemaString);
    AvroKeySchema keySchema = parser.parseKeySchema(entitySchemaString);
    // Verify there are no ambiguities with the managed schemas
    if (schemaManager.hasManagedSchema(tableName, entityName)) {
      KeySchema currentKeySchema = schemaManager
          .getKeySchema(tableName, entityName);
      if (!keySchema.equals(currentKeySchema)) {
        String msg =
            "Migrating schema with different keys. Current: " + currentKeySchema
                .getRawSchema() + " New: " + keySchema.getRawSchema();
        LOG.error(msg);
        throw new ValidationException(msg);
      }
      if (!schemaManager
          .hasSchemaVersion(tableName, entityName, entitySchema)) {
        LOG.info("Migrating Schema: (" + tableName + ", " + entityName + ")");
        schemaManager.migrateSchema(tableName, entityName, entitySchemaString);
      } else {
        LOG.info("Schema hasn't changed, not migrating: (" + tableName + ", "
            + entityName + ")");
        return false;
      }
    } else {
      LOG.info("Creating Schema: (" + tableName + ", " + entityName + ")");
      parser.parseEntitySchema(entitySchemaString).getColumnMappingDescriptor()
          .getRequiredColumnFamilies();
      schemaManager.createSchema(tableName, entityName, entitySchemaString,
          "org.kitesdk.data.hbase.avro.AvroKeyEntitySchemaParser",
          "org.kitesdk.data.hbase.avro.AvroKeySerDe",
          "org.kitesdk.data.hbase.avro.AvroEntitySerDe");
    }
    return true;
  }
954463
def apply_migrations(engine, connection, path):
    

    # Get migrations applied
    migrations_applied = get_migrations_applied(engine, connection)
    # print(migrationsApplied)

    # Get migrations folder
    for file in get_migrations_files(path):
        # Set vars
        basename = os.path.basename(os.path.dirname(file))

        # Skip migrations if they are already applied
        if is_applied(migrations_applied, basename):
            continue

        # Get migration source
        source = get_migration_source(file)
        # print (source);

        # Run migration
        run_migration(connection, source, engine)

        # Save migration
        save_migration(connection, basename)

        # Log
        print('   -> Migration `%s` applied' % (basename))

    # Log
    print(' * Migrations applied')

    return True
267671
def create_schema(self, schema):
        
        if schema not in self.schemas:
            sql = "CREATE SCHEMA " + schema
            self.execute(sql)
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));
          })));
        });
      });
    }
154371
function runMigrations(db, currentVersion) {
  return new Promise((resolve, reject) => {
    const nextVersion = currentVersion + 1;

    if (!migrations[nextVersion]) {
      return resolve();
    }

    if (debug) {
      console.log('Migrating schema to v%d', nextVersion);
    }
    migrations[nextVersion](db)
      .then(() => {
        // write new schemaVersion file
        const json = JSON.stringify({ schemaVersion: nextVersion });
        fs.writeFile(schemaVersionFilePath, json, err => {
          if (err) {
            return reject(err);
          }
          resolve(runMigrations(db, nextVersion));
        });
      })
      .catch(reject);
  });
}
945600
def to_schema(self):
        """"""
        if not self.name or not self.process:
            raise ValueError("field is not registered with process")

        schema = {
            'name': self.name,
            'type': self.get_field_type(),
        }
        if self.required is not None:
            schema['required'] = self.required
        if self.label is not None:
            schema['label'] = self.label
        if self.description is not None:
            schema['description'] = self.description
        if self.default is not None:
            schema['default'] = self.default
        if self.hidden is not None:
            schema['hidden'] = self.hidden
        if self.choices is not None:
            for choice, label in self.choices:
                schema.setdefault('choices', []).append({
                    'label': label,
                    'value': choice,
                })

        return schema
839270
def database_forwards(self, app_label, schema_editor, from_state, to_state):
        """"""
        from resolwe.flow.models.data import DataQuerySet
        # pylint: disable=protected-access

        Data = from_state.apps.get_model('flow', 'Data')  # pylint: disable=invalid-name
        DataQuerySet._delete_chunked(Data.objects.filter(process__slug=self.process))

        Process = from_state.apps.get_model('flow', 'Process')  # pylint: disable=invalid-name
        Process.objects.filter(slug=self.process).delete()