Exemple #1
0
def importData(data, db):
  table_renames = {'fptousecase': 'fprepresentation',
                   'fptoview':'fprepresentation',
                   'connection':'blockconnection'}
  table_ignore = ['colorpalette', 'dbaseversion']
  tables = {tab.__tablename__:tab for tab in model.Base.getTables()}
  # Switch off the foreign keys for now
  model.check_fkeys = False
  engine = model.create_engine(db, echo=True)
  old_url = model.the_url
  model.the_url = db
  model.changeEngine(engine)
  # Clean the database
  model.cleanDatabase()
  try:
    session = model.SessionFactory()
    with model.sessionScope(session) as session:
      # In PostgreSQL, ensure the foreign keys are only checked at the commit, not before.
      if db.startswith('postgresql'):
        session.execute('SET CONSTRAINTS ALL DEFERRED')

      v = int(data['dbaseversion'][0]['Version'])
      if v < 6:
        upgradeToVersion6(data)
      del data['dbaseversion']    # Remove the old database version number
      # Add the current database version
      session.add(model.DbaseVersion())

      # Treat the planeable and anchor items differently: these are polymorphic tables.
      # The base tables are not added directly but through their child tables, using the ORM
      poly_items = {}  # Store the contents held by the polymorphic tables. These are needed later..
      poly_bases = {}  # Find the base for a specific table.
      for poly_table, poly_column in [('planeableitem', 'ItemType'), ('anchor', 'AnchorType')]:
        poly_items[poly_table] = {r['Id']:r for r in data[poly_table]}
        children = set([r[poly_column] for r in data[poly_table]])
        for c in children:
          poly_bases[c] = poly_table
        # Do not add the table directly, so remove it from the list.
        del data[poly_table]

      for n1, n2 in table_renames.items():
        if n1 in data:
          data[n2] = data[n1]

      for table, name in [(t, t.__tablename__) for t in model.order] + \
                  [(model.Base.metadata.tables['planeablexref'], 'planeablexref')]:
        records = data.get(name, [])
        if not records:
          continue
        # Start of a new table.
        if name in table_ignore:
          # Skip this table.
          continue
        if name not in tables:
          table = [name]
        else:
          table = tables[name]
        base_class = poly_bases.get(name, None)

        # Determine which fields are no longer used
        fields = records[0].keys()
        exclude = [f for f in fields if not hasattr(table, f)]
        for d in records:
          print 'Table:', name, 'data:', d
          # Exclude fields that have been removed from the database.
          if exclude:
            for e in exclude:
              del d[e]
          if base_class:
            # Add in the data stored in the polymorphic base table
            d.update(poly_items[base_class][d['Id']])

          # Add the record to the database
          if name not in tables:
            # This class needs raw SQL to create.
            if d:
              ins = table.insert().values(**d)
              session.execute(ins)
          else:
            el = table(**d)
            session.add(el)

    if db.startswith('postgresql'):
      # Only update the sequence for direct children from MyBase.
      # This excludes all polymorphic derivatives (e.g. Requirement) that have no sequence.
      for table in model.MyBase.__subclasses__()[0].__subclasses__():
        name = table.__tablename__
        # In PostgreSQL, the sequences are not updated automatically in this case...
        if 'Id' in table.getFields():
          # Fix the sequence number
          seqname = '%s_Id_seq'%name
          q = '''SELECT setval('"%s"', (SELECT MAX("Id") FROM %s))'''%(seqname, name)
          conn = engine.connect()
          conn.execute("commit")
          conn.execute(q)

    
  finally:
    model.the_engine = None
    model.SessionFactory = None
    model.check_fkeys = True
    model.the_url = old_url
Exemple #2
0
 def setUp(self):
   # Do the tests on a temporary database
   self.org_engine = model.the_engine
   self.engine = model.create_engine('sqlite:///:memory:', echo=True)
   model.changeEngine(self.engine)
Exemple #3
0
 def tearDown(self):
   # Restore the database
   model.changeEngine(self.org_engine, create=False)
Exemple #4
0
    requirements = [r for r in requirements if len(r.StateChanges) > 0 and r.StateChanges[0].Status == "Question"]

    if len(requirements) == 0:
        return

    print >> out, ".. list-table:: "
    print >> out, "   :widths: 10, 90"
    print >> out, "   :header-rows: 1", "\n"

    print >> out, "   * - %s" % "\n     - ".join(["Req", "Question"])

    for req in requirements:
        question = req.StateChanges[0].Description
        if question:
            question = question.replace("\n", "\n       ")
        else:
            continue

        elements = ["   * - %s" % req.Name, "     - %s" % question]

        print >> out, "\n".join(elements).encode("cp1252")


if __name__ == "__main__":
    db = "sqlite:///archmodel.db"
    model.changeEngine(model.create_engine(db))
    session = model.SessionFactory()
    out = file("requirements.rst", "w")

    exportRequirementsOverview(session, out)