Skip to content

Commit

Permalink
Merge pull request #469 from maskarade/fix_436
Browse files Browse the repository at this point in the history
Fix #436: ManualStepMigration not working
  • Loading branch information
gfx authored Oct 29, 2018
2 parents 495dc34 + c36a3c7 commit 836d15f
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 36 deletions.
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2015 FUJI Goro (gfx).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.github.gfx.android.orma.test;

import com.github.gfx.android.orma.BuildConfig;
import com.github.gfx.android.orma.test.model.OrmaDatabase;

import org.junit.Test;
import org.junit.runner.RunWith;

import android.content.Context;

import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;

@RunWith(AndroidJUnit4.class)
public class Issue463_ForOrmaDatabaseBuilder {

Context getContext() {
return ApplicationProvider.getApplicationContext();
}

@Test
public void dbVersionIsUpToDate() {
OrmaDatabase orma = OrmaDatabase
.builder(getContext())
.name(null)
.build();

assertThat(orma.getConnection().getReadableDatabase().getVersion(), is(not(0)));
assertThat(orma.getConnection().getReadableDatabase().getVersion(), is(BuildConfig.VERSION_CODE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.util.SparseArray;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
* <p>
* Version based, step-by-step migration engine with {@link Database#getVersion()},
Expand All @@ -44,6 +45,9 @@ public class ManualStepMigration extends AbstractMigrationEngine {

public static final String MIGRATION_STEPS_TABLE = "orma_migration_steps";

@Nullable
public static final String MIGRATION_COMPLETED = null;

static final String kId = "id";

static final String kVersion = "version";
Expand Down Expand Up @@ -103,10 +107,12 @@ public void start(@NonNull Database db, @NonNull List<? extends MigrationSchema>

if (dbVersion == 0) {
db.setVersion(version);
trace("set version from 0 to %d", version);
return;
}

if (dbVersion == version) {
trace("nothing tdo (version=%d)", version);
return;
}

Expand Down Expand Up @@ -147,6 +153,7 @@ public void run() {
}
}
runTasksInTransaction(db, tasks);
saveStep(db, newVersion, MIGRATION_COMPLETED);
}

public void downgrade(@NonNull Database db, int oldVersion, int newVersion) {
Expand All @@ -170,11 +177,11 @@ public void run() {
}
}
runTasksInTransaction(db, tasks);
saveStep(db, newVersion, MIGRATION_COMPLETED);
}

private void runTasksInTransaction(@NonNull Database db, @NonNull final List<Runnable> tasks) {
if (tasks.isEmpty()) {
saveStep(db, version, null);
return;
}

Expand All @@ -196,6 +203,7 @@ public void saveStep(@NonNull Database db, int version, @Nullable String sql) {
values.put(kSql, sql);
db.insertOrThrow(MIGRATION_STEPS_TABLE, null, values);
db.setVersion(version);
trace("set version to %d, creating a migration log for %s", version, sql);
}

public void execStep(@NonNull Database db, int version, @Nullable String sql) {
Expand Down
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright (c) 2015 FUJI Goro (gfx).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.github.gfx.android.orma.migration.test;

import com.github.gfx.android.orma.core.Database;
import com.github.gfx.android.orma.core.DefaultDatabase;
import com.github.gfx.android.orma.migration.ManualStepMigration;
import com.github.gfx.android.orma.migration.test.util.SchemaData;

import org.junit.Test;
import org.junit.runner.RunWith;

import android.content.Context;

import java.util.ArrayList;

import androidx.annotation.NonNull;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;

@RunWith(AndroidJUnit4.class)
public class Issue436_ManualStepMigrationNotInvoked {
static {
System.setProperty("robolectric.logging", "stderr");
}

Context getContext() {
return ApplicationProvider.getApplicationContext();
}

Database createDatabase() {
return new DefaultDatabase.Provider().provideOnMemoryDatabase(getContext());
}

@Test
public void invokedWithoutSteps() {
int initialVersion = 5;
int secondVersion = 7;

Database db = createDatabase();
new ManualStepMigration(getContext(), initialVersion, true)
.start(db, new ArrayList<SchemaData>());
assertThat(db.getVersion(), is(initialVersion));


ManualStepMigration m = new ManualStepMigration(getContext(), secondVersion, true);
m.start(db, new ArrayList<SchemaData>());
assertThat(db.getVersion(), is(secondVersion));

db.close();
}

@Test
public void invokedWithSteps() {
int initialVersion = 1;
int secondVersion = 3;

Database db = createDatabase();
new ManualStepMigration(getContext(), initialVersion, true)
.start(db, new ArrayList<SchemaData>());
assertThat(db.getVersion(), is(initialVersion));

ManualStepMigration m = new ManualStepMigration(getContext(), secondVersion, true);
m.addStep(secondVersion, new ManualStepMigration.ChangeStep() {
@Override
public void change(@NonNull ManualStepMigration.Helper helper) {
// nop
}
});
m.start(db, new ArrayList<SchemaData>());
assertThat(db.getVersion(), is(secondVersion));

db.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public void testIdempotenceWithNop() throws Exception {
public void upgradeFull() throws Exception {
migration.upgrade(db, 1, 100);

assertThat(migration.fetchDbVersion(db), is(16));
assertThat(migration.fetchDbVersion(db), is(100));

assertThat(seq.size(), is(4));

Expand Down

0 comments on commit 836d15f

Please sign in to comment.