summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/output/pq/sql/insertSource.sql3
-rw-r--r--lib/output/pq/updateDatabase.cpp16
-rw-r--r--lib/output/pq/updateDatabase.h3
-rw-r--r--test/Jamfile.jam1
-rw-r--r--test/test-e2e.cpp22
5 files changed, 45 insertions, 0 deletions
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 <compileTimeFormatter.h>
#include <cstdint>
#include <dbRecordSet.h>
#include <eventSourceBase.h>
#include <helpers.h>
#include <input/replStream.h>
+#include <input/sql/showMasterStatus.h>
#include <memory>
+#include <output/pq/sql/insertSource.h>
#include <output/pq/sql/selectColumns.h>
#include <output/pq/sql/selectSource.h>
#include <output/pq/sql/selectTables.h>
@@ -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 : : : <library>testdb ;
run test-postgresql.cpp : -- : ../db/schema.sql : <library>testdb ;
+run test-e2e.cpp : -- : ../db/schema.sql : <library>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 <boost/test/unit_test.hpp>
+
+#include "testdb-mysql.h"
+#include "testdb-postgresql.h"
+#include <output/pq/updateDatabase.h>
+
+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);
+}