Skip to content

Commit

Permalink
fix diff between two postgresql databases (fix #185)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed Sep 21, 2022
1 parent c33e4b0 commit 8c3c61e
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 8 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/coverage_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ jobs:
sudo runuser -l postgres -c 'psql -d "geodiffdb" -c "CREATE EXTENSION postgis;"'
sudo runuser -l postgres -c 'psql -d "geodiffdb" -c "CREATE EXTENSION \"uuid-ossp\";"'
sudo runuser -l postgres -c 'psql -c "GRANT ALL PRIVILEGES ON DATABASE geodiffdb TO geodiffuser"'
# second database, to test diffs between different databases and same schemas,
# see https://github.com/MerginMaps/geodiff/issues/185
sudo runuser -l postgres -c 'psql -c "CREATE DATABASE geodiffdb2"'
sudo runuser -l postgres -c 'psql -d "geodiffdb2" -c "CREATE EXTENSION postgis;"'
sudo runuser -l postgres -c 'psql -d "geodiffdb2" -c "CREATE EXTENSION \"uuid-ossp\";"'
sudo runuser -l postgres -c 'psql -c "GRANT ALL PRIVILEGES ON DATABASE geodiffdb2 TO geodiffuser"'
- name: build geodiff with coverage
run: |
Expand All @@ -46,6 +52,7 @@ jobs:
env:
CTEST_TARGET_SYSTEM: Linux-gcc
GEODIFF_PG_CONNINFO: "host=localhost port=5432 user=geodiffuser password=geodiffpass dbname=geodiffdb"
GEODIFF_PG_CONNINFO2: "host=localhost port=5432 user=geodiffuser password=geodiffpass dbname=geodiffdb2"
run: |
cd build_coverage_lnx
ctest -VV
Expand Down
8 changes: 7 additions & 1 deletion .github/workflows/memcheck_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ jobs:
sudo runuser -l postgres -c 'psql -d "geodiffdb" -c "CREATE EXTENSION postgis;"'
sudo runuser -l postgres -c 'psql -d "geodiffdb" -c "CREATE EXTENSION \"uuid-ossp\";"'
sudo runuser -l postgres -c 'psql -c "GRANT ALL PRIVILEGES ON DATABASE geodiffdb TO geodiffuser"'
# second database, to test diffs between different databases and same schemas,
# see https://github.com/MerginMaps/geodiff/issues/185
sudo runuser -l postgres -c 'psql -c "CREATE DATABASE geodiffdb2"'
sudo runuser -l postgres -c 'psql -d "geodiffdb2" -c "CREATE EXTENSION postgis;"'
sudo runuser -l postgres -c 'psql -d "geodiffdb2" -c "CREATE EXTENSION \"uuid-ossp\";"'
sudo runuser -l postgres -c 'psql -c "GRANT ALL PRIVILEGES ON DATABASE geodiffdb2 TO geodiffuser"'
- name: build geodiff with valgrind
run: |
Expand All @@ -50,6 +55,7 @@ jobs:
GLIBCPP_FORCE_NEW: 1
GLIBCXX_FORCE_NEW: 1
GEODIFF_PG_CONNINFO: "host=localhost port=5432 user=geodiffuser password=geodiffpass dbname=geodiffdb"
GEODIFF_PG_CONNINFO2: "host=localhost port=5432 user=geodiffuser password=geodiffpass dbname=geodiffdb2"
run: |
cd build_memcheck_lnx
ctest -T memcheck 2>&1 | tee memcheck.log
Expand Down
4 changes: 3 additions & 1 deletion geodiff/src/geodiff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,9 @@ int GEODIFF_createChangesetDr( GEODIFF_ContextH contextHandle, const char *drive
return GEODIFF_ERROR;
}

if ( strcmp( driverSrcName, driverDstName ) == 0 )
// check both driver names and connection details, for more context
// see https://github.com/MerginMaps/geodiff/issues/185
if ( strcmp( driverSrcName, driverDstName ) == 0 && strcmp( driverSrcExtraInfo, driverDstExtraInfo ) == 0 )
{
return GEODIFF_createChangesetEx( contextHandle, driverSrcName, driverSrcExtraInfo, src, dst, changeset );
}
Expand Down
7 changes: 6 additions & 1 deletion geodiff/tests/geodiff_testutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,13 @@ bool compareDiffsByContent( std::string diffA, std::string diffB )
}

#ifdef HAVE_POSTGRES
std::string pgTestConnInfo()
std::string pgTestConnInfo( bool secondInstance )
{
if ( secondInstance )
{
return getEnvVar( "GEODIFF_PG_CONNINFO2", "" );
}

return getEnvVar( "GEODIFF_PG_CONNINFO", "" );
}
#endif
Expand Down
9 changes: 6 additions & 3 deletions geodiff/tests/geodiff_testutils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,14 @@ bool compareDiffsByContent( std::string diffA, std::string diffB );

#ifdef HAVE_POSTGRES
/**
* Returns the connection info for the postgres database
* Use GEODIFF_PG_CONNINFO env variable for setup
* Returns the connection info for the postgres databases
* Use GEODIFF_PG_CONNINFO and GEODIFF_PG_CONNINFO2 env variables for setup.
* By default returns connection info defined by GEODIFF_PG_CONNINFO, to
* access connection info from GEODIFF_PG_CONNINFO2 env variable set
* secondInstance to true.
* Returns empty string by default
*/
std::string pgTestConnInfo();
std::string pgTestConnInfo( bool secondInstance = false );
#endif

#endif // GEODIFF_TESTUTILS_HPP
21 changes: 19 additions & 2 deletions geodiff/tests/test_driver_postgres.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,6 @@ TEST( PostgresDriverTest, test_create_tables )
ASSERT_TRUE( isFileEmpty( output ) );
}


TEST( PostgresDriverTest, test_create_sqlite_from_postgres )
{
std::string conninfo = pgTestConnInfo();
Expand Down Expand Up @@ -891,7 +890,7 @@ TEST( PostgresDriverTest, test_changesetdr_pg_to_pg )
makedir( pathjoin( tmpdir(), testname ) );
std::string changeset = pathjoin( tmpdir(), testname, "changeset.diff" );

// compare the same drivers
// compare the same drivers with the same connection info (same database, different schemas)
int ret = GEODIFF_createChangesetDr( testContext(), "postgres", conninfo.c_str(), "gd_base", "postgres", conninfo.c_str(), "gd_inserted_1_a", changeset.c_str() );
EXPECT_EQ( ret, GEODIFF_SUCCESS );

Expand All @@ -900,7 +899,25 @@ TEST( PostgresDriverTest, test_changesetdr_pg_to_pg )

EXPECT_TRUE( !reader.isEmpty() );

// compare the same drivers with different connection info (different databases, same schemas)
std::string conninfo2 = pgTestConnInfo( true );

execSqlCommands( conninfo, pathjoin( testdir(), "postgres", "diff.sql" ) );
execSqlCommands( conninfo2, pathjoin( testdir(), "postgres", "diff2.sql" ) );

PGconn *c2 = PQconnectdb( conninfo.c_str() );
ASSERT_EQ( PQstatus( c2 ), CONNECTION_OK );

changeset = pathjoin( tmpdir(), testname, "changeset2.diff" );

ret = GEODIFF_createChangesetDr( testContext(), "postgres", conninfo.c_str(), "gd_pg_diff", "postgres", conninfo2.c_str(), "gd_pg_diff", changeset.c_str() );
EXPECT_EQ( ret, GEODIFF_SUCCESS );

EXPECT_TRUE( reader.open( changeset ) );
EXPECT_TRUE( !reader.isEmpty() );

PQfinish( c );
PQfinish( c2 );
}

TEST( PostgresDriverTest, test_multipart_geometries )
Expand Down
19 changes: 19 additions & 0 deletions geodiff/tests/testdata/postgres/diff.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
DROP SCHEMA IF EXISTS gd_pg_diff CASCADE;
CREATE SCHEMA gd_pg_diff;

CREATE TABLE gd_pg_diff.simple (
"fid" SERIAL PRIMARY KEY,
"geometry" GEOMETRY(POINT, 4326),
"name" text
);

INSERT INTO gd_pg_diff.simple (
"fid",
"geometry",
"name"
)
VALUES (
1,
ST_GeomFromText('Point (0 1)', 4326),
'feature 1'
);
30 changes: 30 additions & 0 deletions geodiff/tests/testdata/postgres/diff2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
DROP SCHEMA IF EXISTS gd_pg_diff CASCADE;
CREATE SCHEMA gd_pg_diff;

CREATE TABLE gd_pg_diff.simple (
"fid" SERIAL PRIMARY KEY,
"geometry" GEOMETRY(POINT, 4326),
"name" text
);

INSERT INTO gd_pg_diff.simple (
"fid",
"geometry",
"name"
)
VALUES (
1,
ST_GeomFromText('Point (0 1)', 4326),
'feature 1'
);

INSERT INTO gd_pg_diff.simple (
"fid",
"geometry",
"name"
)
VALUES (
2,
ST_GeomFromText('Point (1 2)', 4326),
'feature 2'
);

0 comments on commit 8c3c61e

Please sign in to comment.