Example #1
0
    def command(self):
        from next.models import Base
        from shapely.wkb import loads
        from yaml import dump
        config_uri = self.args[0]
        env = bootstrap(config_uri)
        engine = engine_from_config(env['registry'].settings, 'sqlalchemy.')
        initialize_base(engine)

        tables = Base.metadata.sorted_tables
        assert self.args[1] is not None, 'You should provide a output file'
        yaml_file = open(self.args[1], 'w')
        fixtures = []

        for table in tables:
            for row in table.select().execute():
                c = {'table': table.name, 'fields': {}}
                columns = table.c.keys()
                # sanity check before we export the data
                assert len(columns) == len(row)
                for i in range(len(columns)):
                    column = table.c[columns[i]]
                    cell = row[i]
                    if str(column.type) in geom_types:
                        # we have to call str on the binary column first
                        c['fields'][column.name] = loads(str(cell)).wkt
                    else:
                        c['fields'][column.name] = cell

                fixtures.append(c)

        dump(fixtures, yaml_file)
Example #2
0
    def command(self):
        from yaml import load
        from shapely.wkt import loads

        config_uri = self.args[0]
        fixtures_data = load(open(self.args[1], 'r'))

        env = bootstrap(config_uri)
        engine = engine_from_config(env['registry'].settings, 'sqlalchemy.')

        try:
            engine.execute(
                'ALTER TABLE nodes DROP CONSTRAINT enforce_srid_point')
        except:
            pass

        initialize_base(engine)

        from next.model.models import Base

        tables = Base.metadata.tables
        for record in fixtures_data:
            table = tables.get(record['table'], None)
            if table is not None:
                logger.info('Load fixtures for table -> %s ' % table)
                inst = {}
                for column_name, cell in record['fields'].iteritems():
                    column_spec = table.c.get(column_name, None)
                    if column_spec is not None:
                        if str(column_spec.type) in geom_types:
                            # Hardcoding SRID=4326 for now
                            # PostGIS 2.0 doesn't default it
                            inst[column_spec.name] \
                                = "SRID=4326;%s" % loads(cell).wkb.encode('hex')
                        else:
                            inst[column_spec.name] = cell
                table.insert().execute(inst)