Skip to content

Commit

Permalink
Adding dropdb goal. README update. Cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Green committed Mar 14, 2014
1 parent 5dc6a7a commit 3e483c0
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 28 deletions.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

A [Maven](http://maven.apache.org/) plugin for controlling [PostgreSQL](http://www.postgresql.org/). There are goals for starting, stopping, initializing a DB, etc. The main purpose is running integration tests.

The project is in a very early stage. It requires JDK 7 to run. Currently there are only 5 goals with very few options.
The project is in a very early stage. It requires JDK 7 to run. Currently there are 6 goals with very few options.

Usage example:

Expand Down Expand Up @@ -59,6 +59,17 @@ Usage example:
<databaseName>products</databaseName>
</configuration>
</execution>
<execution>
<id>drop-db</id>
<phase>pre-integration-test</phase>
<goals>
<goal>dropdb</goal>
</goals>
<configuration>
<username>postgres</username>
<databaseName>products</databaseName>
</configuration>
</execution>
<execution>
<id>stop-postgresql</id>
<phase>post-integration-test</phase>
Expand All @@ -69,3 +80,12 @@ Usage example:
</executions>
</plugin>


## Goals

1. `version` -- display (and enforce) version information
2. `initdb` -- initialize a database root (cluster)
3. `start` -- start the server
4. `createdb` -- connect to the server and create a database
5. `dropdb` -- connect to the server and drop a database
6. `stop` -- stop the server
1 change: 1 addition & 0 deletions src/it/dropdb/password.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
secrets
82 changes: 82 additions & 0 deletions src/it/dropdb/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.github.adrianboimvaser.it</groupId>
<artifactId>initdb</artifactId>
<version>1.0-SNAPSHOT</version>

<description>Integration test for initdb goal.</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<configuration>
<pgsqlHome>@project.build.directory@/pgsql</pgsqlHome>
<dataDir>${project.build.directory}/data</dataDir>
<username>postgres</username>
<passwordFile>password.txt</passwordFile>
<encoding>UTF8</encoding>
<locale>sv_SE</locale>
<databaseName>createanddrop</databaseName>
</configuration>
<executions>
<execution>
<id>initdb</id>
<phase>pre-integration-test</phase>
<goals>
<goal>initdb</goal>
</goals>
</execution>
<execution>
<id>start-postgresql</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>createdb</id>
<phase>pre-integration-test</phase>
<goals>
<goal>createdb</goal>
</goals>
</execution>
<execution>
<id>dropdb</id>
<phase>post-integration-test</phase>
<goals>
<goal>dropdb</goal>
</goals>
</execution>
<execution>
<id>dropdb2</id>
<phase>post-integration-test</phase>
<goals>
<goal>dropdb</goal>
</goals>
<configuration>
<if-exists>true</if-exists>
<noPassword>true</noPassword>
</configuration>
</execution>
<execution>
<id>stop-postgresql</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
2 changes: 2 additions & 0 deletions src/it/dropdb/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
File touchFile = new File(basedir, "target/data/postgresql.conf");
assert touchFile.isFile()
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public class CreatedbMojo extends PgsqlMojo {
protected String template;

/** If specified, password prompts are disabled (may result in an error) */
@Parameter(alias = "no-password", defaultValue = "false", property = "no-password")
protected String noPassword;
@Parameter(alias = "no-password", property = "no-password", defaultValue = "false")
protected boolean noPassword;

public void execute() throws MojoExecutionException, MojoFailureException {
if (skip) {
Expand Down Expand Up @@ -89,7 +89,7 @@ private List<String> createCommand() throws MojoExecutionException {
cmd.add(template);
}

if (trueBooleanString(noPassword)) {
if (noPassword) {
cmd.add("--no-password");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package com.github.adrianboimvaser.postgresql.plugin;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

@Mojo(name = "dropdb")
public class DropdbMojo extends PgsqlMojo {

@Parameter
protected String username;

@Parameter(required = true)
protected String databaseName;

@Parameter
protected String host;

@Parameter
protected Integer port;

/** If specified, no error will be reported when dropping a non-existent database */
@Parameter(alias = "if-exists", property = "if-exists", defaultValue = "false")
protected boolean ifExists;

/** If specified, password prompts are disabled (may result in an error) */
@Parameter(alias = "no-password", property = "no-password", defaultValue = "false")
protected boolean noPassword;

public void execute() throws MojoExecutionException, MojoFailureException {
if (skip) {
getLog().debug("Skipped.");
return;
}

final List<String> cmd = createCommand();
if (getLog().isDebugEnabled()) {
getLog().debug(cmd.toString());
}

final ProcessBuilder processBuilder = new ProcessBuilder(cmd);
String message = "";
Exception cause = null;
int returnValue = Integer.MIN_VALUE;
try {
Process process = processBuilder.start();
logOutput(process);
returnValue = process.waitFor();
message = "dropdb returned " + returnValue;
getLog().debug(message);
} catch (IOException|InterruptedException e) {
message = e.getLocalizedMessage();
cause = e;
getLog().error(e);
}
if (returnValue != 0 && failOnError) {
throw new MojoExecutionException(message, cause);
}
}

private List<String> createCommand() throws MojoExecutionException {
List<String> cmd = new ArrayList<String>();
cmd.add(getCommandPath("dropdb"));

if (host != null) {
cmd.add("-h");
cmd.add(host);
}

if (port != null) {
cmd.add("-p");
cmd.add(port.toString());
}

if (username != null) {
cmd.add("-U");
cmd.add(username);
}

if (ifExists) {
cmd.add("--if-exists");
}

if (noPassword) {
cmd.add("--no-password");
}

cmd.add(databaseName);
return cmd;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,28 +46,4 @@ protected void logOutput(Process process) {
new Thread(errorLogger).run();
}

/**
* Returns {@code true} if {@code arg} is not {@code null} and consists of
* zero-or-more whitespace, or is case-insensitively "{@code true}".
*
* <p>
* In the POM, it can be specified as {@code &lt;argname/&gt;} or
* {@code &lt;argname&gt; &lt;/argname&gt;} or
* {@code &lt;argname&gt; TRUE &lt;/argname&gt;}, etc., all of which will cause
* this method to return true. Non-whitespace values that are not equivalent to
* the case-insensitive value {@code true} will cause this method to return false.
* </p>
*
* <p>
* Some properties may be specified on the command line (e.g. {@code mvn -Dargname},
* and this method exists to explicitly support such usage.
* </p>
*/
protected static boolean trueBooleanString(String arg) {
if (arg != null) {
arg = arg.trim().toLowerCase();
return "".equals(arg) || "true".equals(arg);
}
return false;
}
}

0 comments on commit 3e483c0

Please sign in to comment.