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
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
from sqlalchemy import Column, Integer, String, Date, ForeignKey, create_engine from sqlalchemy.orm import relationship, sessionmaker from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.schema import UniqueConstraint import dbsync from dbsync import server engine = create_engine("sqlite:///server.db", echo=True) Session = sessionmaker(bind=engine) dbsync.set_engine(engine) Base = declarative_base() Base.__table_args__ = { 'sqlite_autoincrement': True, } @server.track class City(Base): __tablename__ = "city" id = Column(Integer, primary_key=True) name = Column(String(500)) def __repr__(self): return "<City id: {0}; name: {1}>".format(self.id, self.name)
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()