From 194f7b8772e943dba98d672e738214808eb1a1a7 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 13 Jun 2021 20:30:24 +0100 Subject: Code for setting up an initial source from scratch Includes beginnings of the end-to-end test suite. --- lib/output/pq/sql/insertSource.sql | 3 +++ lib/output/pq/updateDatabase.cpp | 16 ++++++++++++++++ lib/output/pq/updateDatabase.h | 3 +++ test/Jamfile.jam | 1 + test/test-e2e.cpp | 22 ++++++++++++++++++++++ 5 files changed, 45 insertions(+) create mode 100644 lib/output/pq/sql/insertSource.sql create mode 100644 test/test-e2e.cpp diff --git a/lib/output/pq/sql/insertSource.sql b/lib/output/pq/sql/insertSource.sql new file mode 100644 index 0000000..c262070 --- /dev/null +++ b/lib/output/pq/sql/insertSource.sql @@ -0,0 +1,3 @@ +INSERT INTO mygrate.source(host, username, password, port, database, filename, position, serverid, table_schema) +VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9) +RETURNING source_id diff --git a/lib/output/pq/updateDatabase.cpp b/lib/output/pq/updateDatabase.cpp index 4d60fee..99d2861 100644 --- a/lib/output/pq/updateDatabase.cpp +++ b/lib/output/pq/updateDatabase.cpp @@ -1,11 +1,14 @@ #include "updateDatabase.h" #include "pqConn.h" +#include #include #include #include #include #include +#include #include +#include #include #include #include @@ -37,4 +40,17 @@ namespace MyGrate::Output::Pq { } } } + + UpdateDatabase + UpdateDatabase::createNew(PqConn * pq, const char * host, const char * username, const char * password, + unsigned short port, const char * db, int sid, const char * schema) + { + Input::MySQLConn my {host, username, password, port}; + auto ms = input::sql::showMasterStatus::execute(&my); + auto source_id = output::pq::sql::insertSource::execute( + pq, host, username, password, port, db, ms->at(0, 0), ms->at(0, 1), sid, schema); + pq->query(scprintf<"CREATE SCHEMA IF NOT EXISTS %?">(schema).c_str()); + + return UpdateDatabase(pq->connstr.c_str(), source_id->at(0, 0)); + } } diff --git a/lib/output/pq/updateDatabase.h b/lib/output/pq/updateDatabase.h index 70d4fcf..8fa3b3c 100644 --- a/lib/output/pq/updateDatabase.h +++ b/lib/output/pq/updateDatabase.h @@ -32,6 +32,9 @@ namespace MyGrate::Output::Pq { EventSourceBasePtr getSource(); + static UpdateDatabase createNew(PqConn *, const char * host, const char * un, const char * pw, unsigned short p, + const char * db, int sid, const char * sc); + const uint64_t source; private: diff --git a/test/Jamfile.jam b/test/Jamfile.jam index 685ecf9..55dcadf 100644 --- a/test/Jamfile.jam +++ b/test/Jamfile.jam @@ -18,3 +18,4 @@ run test-streams.cpp ; run test-misc.cpp ; run test-mysql.cpp : : : testdb ; run test-postgresql.cpp : -- : ../db/schema.sql : testdb ; +run test-e2e.cpp : -- : ../db/schema.sql : testdb ; diff --git a/test/test-e2e.cpp b/test/test-e2e.cpp new file mode 100644 index 0000000..fe57e17 --- /dev/null +++ b/test/test-e2e.cpp @@ -0,0 +1,22 @@ +#define BOOST_TEST_MODULE EndToEnd +#include + +#include "testdb-mysql.h" +#include "testdb-postgresql.h" +#include + +BOOST_AUTO_TEST_CASE(e2e) +{ + const char * const target_schema {"testout"}; + using namespace MyGrate::Testing; + MySQLDB my; + PqConnDB pq {ROOT "/db/schema.sql"}; + + auto pqm = pq.mock(); + + auto out = MyGrate::Output::Pq::UpdateDatabase::createNew(&pqm, MySQLDB::SERVER, MySQLDB::USER, MySQLDB::PASSWORD, + MySQLDB::PORT, my.mockname.c_str(), 100, target_schema); + BOOST_CHECK_EQUAL(out.source, 1); + auto src = out.getSource(); + BOOST_REQUIRE(src); +} -- cgit v1.2.3