Improved support for PostgreSQL in config management

This commit is contained in:
Tugdual Saunier 2014-02-12 12:27:31 +00:00
parent 8fac0b0b7a
commit d089f05127
2 changed files with 9 additions and 2 deletions

View File

@ -28,6 +28,7 @@ import com.metamx.common.concurrent.ScheduledExecutors;
import com.metamx.common.lifecycle.LifecycleStart; import com.metamx.common.lifecycle.LifecycleStart;
import com.metamx.common.lifecycle.LifecycleStop; import com.metamx.common.lifecycle.LifecycleStop;
import com.metamx.common.logger.Logger; import com.metamx.common.logger.Logger;
import io.druid.db.DbConnector;
import io.druid.db.DbTablesConfig; import io.druid.db.DbTablesConfig;
import org.joda.time.Duration; import org.joda.time.Duration;
import org.skife.jdbi.v2.Handle; import org.skife.jdbi.v2.Handle;
@ -79,6 +80,12 @@ public class ConfigManager
this.selectStatement = String.format("SELECT payload FROM %s WHERE name = :name", configTable); this.selectStatement = String.format("SELECT payload FROM %s WHERE name = :name", configTable);
this.insertStatement = String.format( this.insertStatement = String.format(
DbConnector.isPostgreSQL(dbi) ?
"BEGIN;\n" +
"LOCK TABLE %1$s IN SHARE ROW EXCLUSIVE MODE;\n" +
"WITH upsert AS (UPDATE %1$s SET payload=:payload WHERE name=:name RETURNING *)\n" +
" INSERT INTO %1$s (name, payload) SELECT :name, :payload WHERE NOT EXISTS (SELECT * FROM upsert)\n;" +
"COMMIT;" :
"INSERT INTO %s (name, payload) VALUES (:name, :payload) ON DUPLICATE KEY UPDATE payload = :payload", "INSERT INTO %s (name, payload) VALUES (:name, :payload) ON DUPLICATE KEY UPDATE payload = :payload",
configTable configTable
); );

View File

@ -177,7 +177,7 @@ public class DbConnector
} }
} }
protected static Boolean isPostgreSQL(final IDBI dbi) public static Boolean isPostgreSQL(final IDBI dbi)
{ {
return dbi.withHandle( return dbi.withHandle(
new HandleCallback<Boolean>() new HandleCallback<Boolean>()