From ba0e707785be246db51e0702c22e99a85d469405 Mon Sep 17 00:00:00 2001 From: Igor Justino Date: Thu, 19 Nov 2015 23:21:02 +0000 Subject: [PATCH] Add mysql db schema if not present --- src/main/java/com/zendesk/maxwell/schema/Schema.java | 4 ++++ .../java/com/zendesk/maxwell/schema/SchemaStore.java | 8 ++++++++ src/test/java/com/zendesk/maxwell/SchemaStoreTest.java | 10 ++++++++++ 3 files changed, 22 insertions(+) diff --git a/src/main/java/com/zendesk/maxwell/schema/Schema.java b/src/main/java/com/zendesk/maxwell/schema/Schema.java index 4da0e63ff..2832898f7 100644 --- a/src/main/java/com/zendesk/maxwell/schema/Schema.java +++ b/src/main/java/com/zendesk/maxwell/schema/Schema.java @@ -33,6 +33,10 @@ public Database findDatabase(String string) { return null; } + public void addDatabase(Database d) { + this.databases.add(d); + } + public Schema copy() { ArrayList newDBs = new ArrayList<>(); for ( Database d : this.databases ) { diff --git a/src/main/java/com/zendesk/maxwell/schema/SchemaStore.java b/src/main/java/com/zendesk/maxwell/schema/SchemaStore.java index aacbdbbe9..ce465226a 100644 --- a/src/main/java/com/zendesk/maxwell/schema/SchemaStore.java +++ b/src/main/java/com/zendesk/maxwell/schema/SchemaStore.java @@ -235,6 +235,14 @@ private void restoreFrom(BinlogPosition targetPosition) this.schema_id = schemaRS.getLong("id"); + if ( this.schema.findDatabase("mysql") == null ) { + LOGGER.info("Could not find mysql db, adding it to schema"); + SchemaCapturer sc = new SchemaCapturer(connection, "mysql"); + Database db = sc.capture().findDatabase("mysql"); + this.schema.addDatabase(db); + shouldResave = true; + } + p = connection.prepareStatement("SELECT * from `maxwell`.`databases` where schema_id = ? ORDER by id"); p.setLong(1, this.schema_id); diff --git a/src/test/java/com/zendesk/maxwell/SchemaStoreTest.java b/src/test/java/com/zendesk/maxwell/SchemaStoreTest.java index c3a6f64a9..24513f5d5 100644 --- a/src/test/java/com/zendesk/maxwell/SchemaStoreTest.java +++ b/src/test/java/com/zendesk/maxwell/SchemaStoreTest.java @@ -12,6 +12,7 @@ import org.junit.Before; import org.junit.Test; +import com.zendesk.maxwell.schema.Database; import com.zendesk.maxwell.schema.Schema; import com.zendesk.maxwell.schema.SchemaCapturer; import com.zendesk.maxwell.schema.SchemaStore; @@ -93,4 +94,13 @@ public void testMasterChange() throws Exception { rs = server.getConnection().createStatement().executeQuery("SELECT * from `maxwell`.`positions`"); assertThat(rs.next(), is(false)); } + + @Test + public void testRestoreMysqlDb() throws Exception { + Database db = this.schema.findDatabase("mysql"); + this.schema.getDatabases().remove(db); + this.schemaStore.save(); + SchemaStore restoredSchema = SchemaStore.restore(server.getConnection(), server.SERVER_ID, this.binlogPosition); + assertThat(restoredSchema.getSchema().findDatabase("mysql"), is(not(nullValue()))); + } }