Skip to content

Commit

Permalink
fix #436 again; migrationEngine is always OrmaMigration by default!
Browse files Browse the repository at this point in the history
  • Loading branch information
gfx committed Oct 29, 2018
1 parent 74e6411 commit c36a3c7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 32 deletions.
Expand Up @@ -25,6 +25,7 @@

import android.content.Context;
import android.content.pm.ApplicationInfo;

import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand All @@ -46,22 +47,27 @@ public abstract class OrmaDatabaseBuilderBase<T extends OrmaDatabaseBuilderBase<
@NonNull
DatabaseProvider databaseProvider;

@Nullable
MigrationEngine migrationEngine;

@Nullable
TraceListener traceListener;

boolean foreignKeys = true;

boolean wal = true;

boolean trace;

TraceListener migrationTraceListener;

boolean tryParsingSql;

OrmaMigration.Builder ormaMigrationBuilder;
@Nullable
OrmaMigration.Builder ormaMigrationBuilder = null;

@NonNull
AccessThreadConstraint readOnMainThread;

@NonNull
AccessThreadConstraint writeOnMainThread;

public OrmaDatabaseBuilderBase(@NonNull Context context) {
Expand Down Expand Up @@ -126,17 +132,12 @@ public T provider(@NonNull DatabaseProvider databaseProvider) {
*/
public T migrationEngine(@NonNull MigrationEngine migrationEngine) {
if (ormaMigrationBuilder != null) {
throw new IllegalArgumentException("migrationStep() is already set");
throw new IllegalArgumentException("migrationEngine is already installed via fields like migrationStep()");
}
this.migrationEngine = migrationEngine;
return (T) this;
}

public T migrationTraceListener(@NonNull TraceListener traceListener) {
migrationTraceListener = traceListener;
return (T) this;
}

/**
* Controls write-ahead logging in SQLite. The default is {@code true}.
*
Expand All @@ -161,13 +162,15 @@ public T foreignKeys(boolean foreignKeys) {
return (T) this;
}

private void prepareOrmaMigrationBuilder() {
@NonNull
private OrmaMigration.Builder prepareOrmaMigrationBuilder() {
if (migrationEngine != null) {
throw new IllegalArgumentException("migrationEngine() is already set");
throw new IllegalArgumentException("migrationEngine is already set");
}
if (ormaMigrationBuilder == null) {
ormaMigrationBuilder = OrmaMigration.builder(context);
}
return ormaMigrationBuilder;
}

/**
Expand All @@ -178,8 +181,8 @@ private void prepareOrmaMigrationBuilder() {
* @return the receiver itself
*/
public T migrationStep(@IntRange(from = 1) int schemaVersion, @NonNull ManualStepMigration.Step step) {
prepareOrmaMigrationBuilder();
ormaMigrationBuilder.step(schemaVersion, step);
prepareOrmaMigrationBuilder()
.step(schemaVersion, step);
return (T) this;
}

Expand All @@ -190,8 +193,8 @@ public T migrationStep(@IntRange(from = 1) int schemaVersion, @NonNull ManualSte
* @return the receiver itself
*/
public T versionForManualStepMigration(@IntRange(from = 1) int schemaVersion) {
prepareOrmaMigrationBuilder();
ormaMigrationBuilder.versionForManualStepMigration(schemaVersion);
prepareOrmaMigrationBuilder()
.versionForManualStepMigration(schemaVersion);
return (T) this;
}

Expand All @@ -218,12 +221,15 @@ public T tryParsingSql(boolean tryParsingSql) {
*/
public T trace(boolean trace) {
this.trace = trace;
if (migrationTraceListener == null) {
migrationTraceListener = trace ? TraceListener.LOGCAT : TraceListener.EMPTY;
}
return (T) this;
}

public T migrationTraceListener(@NonNull TraceListener traceListener) {
prepareOrmaMigrationBuilder().trace(traceListener);
return (T) this;
}


/**
* Sets {@link AccessThreadConstraint} for reading.
*
Expand All @@ -250,16 +256,12 @@ public T writeOnMainThread(AccessThreadConstraint writeOnMainThread) {
protected abstract String getSchemaHash();

protected T fillDefaults() {
if (migrationTraceListener == null) {
migrationTraceListener = trace ? TraceListener.LOGCAT : TraceListener.EMPTY;
}
if (ormaMigrationBuilder != null) {
migrationEngine = ormaMigrationBuilder
.trace(migrationTraceListener)
if (migrationEngine == null) {
migrationEngine = prepareOrmaMigrationBuilder()
.trace(traceListener)
// versionForManualStepMigration is extracted from the app's Build.VERSION
.schemaHashForSchemaDiffMigration(getSchemaHash())
.build();
} else if (migrationEngine == null) {
migrationEngine = new SchemaDiffMigration(context, getSchemaHash(), migrationTraceListener);
}

return (T) this;
Expand Down
Expand Up @@ -18,13 +18,14 @@
import com.github.gfx.android.orma.core.Database;

import android.content.Context;
import android.util.SparseArray;

import java.util.List;

import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import android.util.SparseArray;

import java.util.List;

/**
* <p>
Expand Down Expand Up @@ -150,12 +151,12 @@ public Builder schemaHashForSchemaDiffMigration(@NonNull String schemaHash) {
}

public Builder trace(boolean value) {
traceListener = value ? TraceListener.LOGCAT : TraceListener.EMPTY;
trace(value ? TraceListener.LOGCAT : TraceListener.EMPTY);
return this;
}

public Builder trace(@NonNull TraceListener traceListener) {
this.traceListener = traceListener;
public Builder trace(@Nullable TraceListener traceListener) {
this.traceListener = traceListener != null ? traceListener : TraceListener.EMPTY;
return this;
}

Expand Down

0 comments on commit c36a3c7

Please sign in to comment.