def main(mysql_dsn, tables, blocking=False, debug=False): level = "DEBUG" if debug else "INFO" setup_logger(level) print_sub(tables) pub_tables = tables if tables else None mysql_pub(mysql_dsn, tables=pub_tables, blocking=blocking)
def binlog(mysql_dsn): # init mysql connection parsed = urlparse(mysql_dsn) db_settings = { "host": parsed.hostname, "port": parsed.port or 3306, "user": parsed.username, "passwd": parsed.password, "database": "meepo_test" } conn = pymysql.connect(**db_settings) cursor = conn.cursor() # test sqls sql = """ INSERT INTO test (data) VALUES ('a'); INSERT INTO test (data) VALUES ('b'), ('c'), ('d'); UPDATE test SET data = 'aa' WHERE id = 1; UPDATE test SET data = 'bb' WHERE id = 2; UPDATE test SET data = 'cc' WHERE id != 1; DELETE FROM test WHERE id != 1; DELETE FROM test WHERE id = 1; """ cursor.execute(sql) cursor.close() conn.commit() conn.close() # generates signals mysql_pub(mysql_dsn, tables=["test"])
def main(mysql_dsn): # make sure the user has permission to read binlog mysql_dsn = mysql_dsn or "mysql+pymysql://root@localhost/meepo_test" from meepo.sub import print_sub print_sub(["test"]) from meepo.pub import mysql_pub mysql_pub(mysql_dsn, ["test"])
def main(master_dsn, slave_dsn, tables, blocking=False): # currently only supports mysql master assert master_dsn.startswith("mysql") if tables: logger.info("replicating tables: %s" % ", ".join(tables)) else: tables = None replicate_sub(master_dsn, slave_dsn, tables) mysql_pub(master_dsn, blocking=blocking)
def main(bind, master_dsn, tables, blocking=False): setup_logger("INFO") logger = logging.getLogger(__name__) # currently only supports mysql master assert master_dsn and master_dsn.startswith("mysql") assert bind.startswith("ipc") or bind.startswith("tcp") logger.info("event sourcing tables: %s" % ", ".join(tables)) zmq_sub(bind, tables) mysql_pub(master_dsn, blocking=blocking)
def main(master_dsn, redis_dsn, tables, namespace=None, blocking=False): setup_logger() logger = logging.getLogger(__name__) # currently only supports mysql master assert master_dsn and master_dsn.startswith("mysql") assert redis_dsn and redis_dsn.startswith("redis") logger.info("event sourcing tables: %s" % ", ".join(tables)) es_sub(redis_dsn, tables, namespace) mysql_pub(master_dsn, blocking=blocking)
def test_mysql_pub(mysql_dsn): test_write_sig, test_update_sig, test_delete_sig = [], [], [] test_binlog_sig = [] signal("test_write").connect(lambda pk: test_write_sig.append(pk), weak=False) signal("test_update").connect(lambda pk: test_update_sig.append(pk), weak=False) signal("test_delete").connect(lambda pk: test_delete_sig.append(pk), weak=False) signal("mysql_binlog_pos").connect(lambda pk: test_binlog_sig.append(pk), weak=False) parsed = urlparse(mysql_dsn) db_settings = { "host": parsed.hostname, "port": parsed.port or 3306, "user": parsed.username, "passwd": parsed.password, "database": "meepo_test" } conn = pymysql.connect(**db_settings) cursor = conn.cursor() # test sqls sql = """ INSERT INTO test (data) VALUES ('a'); INSERT INTO test (data) VALUES ('b'), ('c'), ('d'); UPDATE test SET data = 'aa' WHERE id = 1; UPDATE test SET data = 'bb' WHERE id = 2; UPDATE test SET data = 'cc' WHERE id != 1; DELETE FROM test WHERE id != 1; DELETE FROM test WHERE id = 1; """ cursor.execute(sql) cursor.close() conn.commit() conn.close() from meepo.pub import mysql_pub mysql_pub(mysql_dsn, tables=["test"]) assert test_write_sig == [1, 2, 3, 4] assert test_update_sig == [1, 2, 2, 3, 4] assert test_delete_sig == [2, 3, 4, 1] assert len(test_binlog_sig) == 7