Skip to content

Commit

Permalink
Add mysql db schema if not present
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Justino committed Nov 19, 2015
1 parent e882333 commit ba0e707
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/zendesk/maxwell/schema/Schema.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public Database findDatabase(String string) {
return null;
}

public void addDatabase(Database d) {
this.databases.add(d);
}

public Schema copy() {
ArrayList<Database> newDBs = new ArrayList<>();
for ( Database d : this.databases ) {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/zendesk/maxwell/schema/SchemaStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
10 changes: 10 additions & 0 deletions src/test/java/com/zendesk/maxwell/SchemaStoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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())));
}
}

0 comments on commit ba0e707

Please sign in to comment.