Skip to content

Commit

Permalink
[table] turn off automatic creation of tables with a flag. write test…
Browse files Browse the repository at this point in the history
… to ensure it stays off.
  • Loading branch information
agrosner committed Aug 26, 2017
1 parent 45b9762 commit 32dd5e1
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@
*/
boolean assignDefaultValuesFromCursor() default true;

/**
* @return When false, this table gets generated and associated with database, however it will not immediately
* get created upon startup. This is useful for keeping around legacy tables for migrations.
*/
boolean createWithDatabase() default true;

/**
* @return The cache size for this Table.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ class TableDefinition(manager: ProcessorManager, element: TypeElement) : BaseTab
var customCacheFieldName: String? = null
var customMultiCacheFieldName: String? = null

var createWithDatabase = true

var allFields = false
var useIsForPrivateBooleans: Boolean = false

Expand Down Expand Up @@ -128,6 +130,8 @@ class TableDefinition(manager: ProcessorManager, element: TypeElement) : BaseTab
orderedCursorLookUp = table.orderedCursorLookUp
assignDefaultValuesFromCursor = table.assignDefaultValuesFromCursor

createWithDatabase = table.createWithDatabase

allFields = table.allFields
useIsForPrivateBooleans = table.useBooleanGetterSetters

Expand Down Expand Up @@ -488,6 +492,13 @@ class TableDefinition(manager: ProcessorManager, element: TypeElement) : BaseTab
`return`("ALL_COLUMN_PROPERTIES")
}

if (!createWithDatabase) {
`override fun`(TypeName.BOOLEAN, "createWithDatabase") {
modifiers(public, final)
`return`(false.L)
}
}

if (cachingEnabled) {

val singlePrimaryKey = primaryColumnDefinitions.size == 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@ package com.raizlabs.android.dbflow

import com.raizlabs.android.dbflow.sql.Query
import org.junit.Assert.assertEquals
import org.junit.Assert.fail
import kotlin.reflect.KClass


fun assertEquals(string: String, query: Query) = assertEquals(string, query.query.trim())
fun assertEquals(string: String, query: Query) = assertEquals(string, query.query.trim())

fun assertThrowsException(expectedException: KClass<out Exception>, function: () -> Unit) {
try {
function()
fail("Expected call to fail. Unexpectedly passed")
} catch (e: Exception) {
if (e.javaClass != expectedException.java) {
fail("Expected $expectedException but got ${e.javaClass}")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.raizlabs.android.dbflow.models

import android.database.sqlite.SQLiteException
import com.raizlabs.android.dbflow.BaseUnitTest
import com.raizlabs.android.dbflow.assertThrowsException
import com.raizlabs.android.dbflow.kotlinextensions.from
import com.raizlabs.android.dbflow.kotlinextensions.list
import com.raizlabs.android.dbflow.kotlinextensions.select
import org.junit.Test

/**
* Description:
*/
class DontCreateModelTest : BaseUnitTest() {

@Test
fun testModelNotCreated() {
assertThrowsException(SQLiteException::class) {
(select from DontCreateModel::class).list
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ class CharModel(@PrimaryKey var id: Int = 0, @Column var exampleChar: Char? = nu
@Table(database = TestDatabase::class)
class TwoColumnModel(@PrimaryKey var name: String? = "", @Column var id: Int = 0)

@Table(database = TestDatabase::class, createWithDatabase = false)
class DontCreateModel(@PrimaryKey var id: Int = 0)

enum class Difficulty {
EASY,
MEDIUM,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
*/
@SuppressWarnings("NullableProblems")
public abstract class ModelAdapter<TModel> extends InstanceAdapter<TModel>
implements InternalAdapter<TModel> {
implements InternalAdapter<TModel> {

private DatabaseStatement insertStatement;
private DatabaseStatement compiledStatement;
Expand Down Expand Up @@ -295,9 +295,9 @@ public void updateAutoIncrement(@NonNull TModel model, @NonNull Number id) {
@Override
public Number getAutoIncrementingId(@NonNull TModel model) {
throw new InvalidDBConfiguration(
String.format("This method may have been called in error. The model class %1s must contain" +
"a single primary key (if used in a ModelCache, this method may be called)",
getModelClass()));
String.format("This method may have been called in error. The model class %1s must contain" +
"a single primary key (if used in a ModelCache, this method may be called)",
getModelClass()));
}

/**
Expand All @@ -307,9 +307,9 @@ public Number getAutoIncrementingId(@NonNull TModel model) {
@NonNull
public String getAutoIncrementingColumnName() {
throw new InvalidDBConfiguration(
String.format("This method may have been called in error. The model class %1s must contain " +
"an autoincrementing or single int/long primary key (if used in a ModelCache, this method may be called)",
getModelClass()));
String.format("This method may have been called in error. The model class %1s must contain " +
"an autoincrementing or single int/long primary key (if used in a ModelCache, this method may be called)",
getModelClass()));
}

public boolean hasAutoIncrement(TModel model) {
Expand Down Expand Up @@ -480,8 +480,8 @@ public int getCacheSize() {

public IMultiKeyCacheConverter<?> getCacheConverter() {
throw new InvalidDBConfiguration("For multiple primary keys, a public static IMultiKeyCacheConverter field must" +
"be marked with @MultiCacheField in the corresponding model class. The resulting key" +
"must be a unique combination of the multiple keys, otherwise inconsistencies may occur.");
"be marked with @MultiCacheField in the corresponding model class. The resulting key" +
"must be a unique combination of the multiple keys, otherwise inconsistencies may occur.");
}

public ModelCache<TModel, ?> createModelCache() {
Expand Down Expand Up @@ -537,18 +537,26 @@ public ConflictAction getInsertOnConflictAction() {
return ConflictAction.ABORT;
}

/**
* @return When false, this table gets generated and associated with database, however it will not immediately
* get created upon startup. This is useful for keeping around legacy tables for migrations.
*/
public boolean createWithDatabase() {
return true;
}

private void throwCachingError() {
throw new InvalidDBConfiguration(
String.format("This method may have been called in error. The model class %1s must contain" +
"an auto-incrementing or at least one primary key (if used in a ModelCache, this method may be called)",
getModelClass()));
String.format("This method may have been called in error. The model class %1s must contain" +
"an auto-incrementing or at least one primary key (if used in a ModelCache, this method may be called)",
getModelClass()));
}

private void throwSingleCachingError() {
throw new InvalidDBConfiguration(
String.format("This method may have been called in error. The model class %1s must contain" +
"an auto-incrementing or one primary key (if used in a ModelCache, this method may be called)",
getModelClass()));
String.format("This method may have been called in error. The model class %1s must contain" +
"an auto-incrementing or one primary key (if used in a ModelCache, this method may be called)",
getModelClass()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,12 @@ protected void executeTableCreations(@NonNull final DatabaseWrapper database){
database.beginTransaction();
List<ModelAdapter> modelAdapters = databaseDefinition.getModelAdapters();
for (ModelAdapter modelAdapter : modelAdapters) {
try {
database.execSQL(modelAdapter.getCreationQuery());
} catch (SQLiteException e) {
FlowLog.logError(e);
if (modelAdapter.createWithDatabase()) {
try {
database.execSQL(modelAdapter.getCreationQuery());
} catch (SQLiteException e) {
FlowLog.logError(e);
}
}
}
database.setTransactionSuccessful();
Expand Down

0 comments on commit 32dd5e1

Please sign in to comment.