Exemple #1
0
  def dump_schema (class_, connection_string, schema_name, tables=None):
    db.connect(connection_string)

    oracle_database = Database()

    diffs = []
    db_schema = Schema(schema_name, database=oracle_database)
    oracle_database.add(db_schema)

    if tables:
      tables = {OracleIdentifier(table_name): ([c.strip()
                                                for c in columns[0].split(',')]
                                               if len(columns) else None)
                for table_name, *columns in (table.split(':', 1)
                                             for table in tables)}
      for table in Table.from_db(db_schema.name.schema, oracle_database,
                                 tables):
        db_schema.add(table)

      for table_name, columns in tables.items():
        table = db_schema.find(table_name, Table, False)
        if columns:
          for column_name in columns:
            db_schema.find(table.name.with_(part=column_name), Column, False)
        Data.from_db(table, columns)
        diffs.append(Diff(["-- Data for {}".format(table_name)] +
                          [datum.sql(fq=False, columns=columns)
                           for datum in table.data], produces=set(table.data)))
    else:
      db_schema.from_db()

      # Set index ownership for all unique constraints
      if Constraint in db_schema.objects:
        for cons in db_schema.objects[Constraint].values():
          if isinstance(cons, UniqueConstraint):
            cons.index_ownership = UniqueConstraint.FULL_INDEX_CREATE

      diffs = db_schema.diff(Schema(schema_name, database=Database()))

    if diffs:
      diffs = order_diffs(diffs)

    return diffs
Exemple #2
0
  def diff_to_db (self, connection_string):
    self.log.info('Loading current database state...')

    # Perform all schema aliasing on self, before passing any attributes on to
    # the oracle_database. The oracle_database will therefore be fully aliased.
    self.default_schema = schema_alias(self.default_schema)
    for schema_name in set(self.schemas):
      aliased_schema_name = schema_alias(schema_name)
      if schema_name != aliased_schema_name:
        if aliased_schema_name in self.schemas:
          self.merge_schemas(aliased_schema_name, schema_name)
        else:
          self.rename_schema(schema_name, aliased_schema_name)

    db.connect(connection_string)

    oracle_database = Database(self.default_schema)
    oracle_database._ignores = self._ignores
    oracle_database._ignore_schemas = self._ignore_schemas

    for schema_name in self.schemas:
      if (schema_name not in self._ignore_schemas and
          schema_name not in oracle_database.schemas):
        oracle_database.add(Schema(schema_name))
    oracle_database.from_db()

    self.log.info('Comparing database definition to current database state...')

    diffs = []
    for schema_name in self.schemas:
      if schema_name not in self._ignore_schemas:
        diffs.extend(self.schemas[schema_name].diff(
          oracle_database.schemas[schema_name]))

    if diffs:
      diffs = order_diffs(diffs)

    return diffs