Example #1
0
def create_sync_client(pid: int = 0, reset_db=True) -> SyncClient:
    from dbsync import core
    core.mode = "client"
    dbname = client_db(pid)
    if reset_db:
        try:
            print(f"deleting db file {dbname}")
            os.remove(dbname)
        except FileNotFoundError:
            print(f"ignore deleting non existing file {dbname}")

    engine_client = create_engine(f"sqlite:///{dbname}")
    Base.metadata.create_all(engine_client)
    dbsync.set_engine(engine_client)
    dbsync.create_all()

    register_client_tracking()

    try:
        asyncio.get_event_loop()
    except RuntimeError as e:
        asyncio.set_event_loop(asyncio.new_event_loop())
    clientws = SyncClient(port=PORT, path="sync", engine=engine_client, id=pid)
    # client.connect()
    return clientws
Example #2
0
def start_ws_server(**kw):
    """
    postgres must be installed with permissions to create a db
    """
    from dbsync import core
    core.mode = "server"
    # engine_server = create_engine(f"sqlite:///{server_db}")
    try:
        os.system("dropdb synctest")
        os.system("createdb synctest")
        engine_server: Engine = create_engine(server_uri_postgres,
                                              isolation_level='SERIALIZABLE')
    except Exception as ex:
        print("**** PLEASE INSTALL POSTGRES ***")
        raise
    Base.metadata.create_all(engine_server)
    dbsync.set_engine(engine_server)
    dbsync.create_all()
    register_server_tracking()
    try:
        asyncio.get_event_loop()
    except RuntimeError as e:
        asyncio.set_event_loop(asyncio.new_event_loop())

    server = SyncServer(port=PORT, engine=engine_server, **kw)
    print("starting server...")
    # server.start(run_forever=False, start_new_loop=True)
    server.start_in_thread()
    server.started_thead_event.wait()
    print("server ready in thread")

    return PORT
Example #3
0
@server.track
class Person(Base):

    __tablename__ = "person"

    __table_args__ = (UniqueConstraint('first_name',
                                       'last_name'), Base.__table_args__)

    id = Column(Integer, primary_key=True)
    first_name = Column(String(500))
    last_name = Column(String(500))
    house_id = Column(Integer, ForeignKey("house.id"))
    birth_city_id = Column(Integer, ForeignKey("city.id"))
    birth_date = Column(Date)
    email = Column(String(500))

    house = relationship(House, backref="persons")
    birth_city = relationship(City)

    def __repr__(self):
        return "<Person '{0} {1}' house_id: {2}; birth_city_id: {3}>".\
            format(self.first_name,
                   self.last_name,
                   self.house_id,
                   self.birth_city_id)


Base.metadata.create_all(engine)
dbsync.create_all()
dbsync.generate_content_types()
Example #4
0
    name = Column(String)

    def __repr__(self):
        return u"<A id:{0} name:{1}>".format(self.id, self.name)


@client.track
class B(Base):
    __tablename__ = "test_b"

    id = Column(Integer, primary_key=True)
    name = Column(String)
    a_id = Column(Integer, ForeignKey("test_a.id"))

    a = relationship(A, backref="bs")

    def __repr__(self):
        return u"<B id:{0} name:{1} a_id:{2}>".format(
            self.id, self.name, self.a_id)


Base.metadata.create_all(engine)
dbsync.set_engine(engine)
dbsync.create_all()
dbsync.generate_content_types()
_session = Session()
_session.add(
    models.Node(registered=datetime.datetime.now(), secret=generate_secret(128)))
_session.commit()
_session.close()