Skip to content

Commit

Permalink
#223: h2 to pgsql cli utility (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarto authored May 14, 2021
1 parent 110018b commit 3b2a24a
Show file tree
Hide file tree
Showing 19 changed files with 1,026 additions and 21 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

Expand Down
78 changes: 78 additions & 0 deletions src/cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
GeoStore Command Line utilities
========
The GeoStore CLI module includes command line utilites to interact with GeoStore and its internal database.

It currently includes the following utilities:
- **H2ToPgSQLExport**: allows migrating the GeoStore database from H2 to PostgreSQL.

Building
--------
The standard GeoStore build does not include the CLI module, but it is possible to enable specific profiles to:

- build the CLI module alone: you can run the maven build with the dedicated *cli* profile.

```bash
mvn install -P cli
```

- run the full build and include also the CLI module: you can run the maven build with the dedicated *all* profile.

```bash
mvn install -P all
```

**Note**: you can also include the optional features, adding more profiles, eg.

```bash
mvn install -P all,postgres,extjs
```

The final artifacts of the build will be:

* the H2toPgSQLExport tool, an executable jar, that includes all the needed dependencies, located in:
```
src/cli/target/H2ToPgSQLExport.jar
```

H2ToPgSQLExport
---------------
This tool can be used to migrate data from a GeoStore H2 database file, to a PostgreSQL database.

We always advice using a full fledged database (like PosgreSQL or Oracle) in production, while an H2 embedded database can be useful during development or testing.

Sometimes migrating a development database into production is therefore needed.

The tool produces an SQL script that can be run on the destination database to import the data to migrate from an H2 geostore database file.

The destination database must be:
* already populated with the geostore schema and the related tables
* all the tables should be empty, no data removal will be done by the produced scripts

### Usage
To get a migration script, run the utility with the following parameters:

```bash
java -jar H2ToPgSQLExport.jar [-o[=<outputPath>]] [-p=<password>] [-u=<username>] H2FILE
```
* **H2FILE**: path to the H2 database file to migrate
* **outputPath**: path to output SQL file, if missing the output is written to the standard output
* **password**: H2 database password, if missing, the default password (geostore) is used
* **username**: H2 database username, if missing, the default username (geostore) is used
To import the data into an empty PostgreSQL database, using the generated script, you can use the PostgreSQL psql tool as follows:
```bash
# creates the geostore user role and schemas
psql -U <admin_user> -d <db_name> -W -f <geostore_src_root>/doc/sql/001_setup_db.sql
# creates the geostore tables in the geostore schema
psql -U geostore -d <db_name> -W -f <geostore_src_root>/doc/sql/002_create_schema_postgres.sql
# imports data exported from H2
psql -U geostore -d <db_name> -W -f <exported_sql_script>
```
### Connect GeoStore to the new database
Read the [DBMS configuration docs](https://github.com/geosolutions-it/geostore/wiki/Configure-the-DBMS) to learn how to connect your new PostgreSQL database to GeoStore.
119 changes: 119 additions & 0 deletions src/cli/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (C) 2007 - 2010 GeoSolutions S.A.S.
http://www.geo-solutions.it
GPLv3 + Classpath exception
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses />.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>it.geosolutions.geostore</groupId>
<artifactId>geostore-root</artifactId>
<version>1.7-SNAPSHOT</version>
</parent>

<artifactId>geostore-cli</artifactId>
<packaging>jar</packaging>
<name>GeoStore - Command Line Interface</name>

<dependencies>

<!-- H2 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.3.168</version>
</dependency>

<!-- Apache Commons -->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
</dependency>

<dependency>
<groupId>jdom</groupId>
<artifactId>jdom</artifactId>
</dependency>

<dependency>
<groupId>info.picocli</groupId>
<artifactId>picocli</artifactId>
<version>4.6.1</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>

<plugins>
<!-- Attach sources ============================================ -->
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<configuration>
<attach>true</attach>
</configuration>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2</version>
<configuration>
<finalName>H2ToPgSQLExport</finalName>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
<mainClass>it.geosolutions.geostore.cli.H2ToPgSQLExportCLI</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* ====================================================================
*
* Copyright (C) 2021 GeoSolutions S.A.S.
* http://www.geo-solutions.it
*
* GPLv3 + Classpath exception
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.
*
* ====================================================================
*
* This software consists of voluntary contributions made by developers
* of GeoSolutions. For more information on GeoSolutions, please see
* <http://www.geo-solutions.it/>.
*
*/
package it.geosolutions.geostore.cli;
import picocli.CommandLine;

/**
* CLI tool to export a GeoStore H2 database as a script for a destination database.
* Usage: H2ToPgSQL [-o, --output=<path to output sql>] <path to H2 database file>
*/
public class H2ToPgSQLExportCLI {

public static void main(String[] args) {
H2ToPgSQLExporter exporter = new H2ToPgSQLExporter();
System.exit(new CommandLine(exporter).execute(args));
}

}
Loading

0 comments on commit 3b2a24a

Please sign in to comment.