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()))); + } }