From 107c2de9b6f72e381936f41f335c4cbf2d15356c Mon Sep 17 00:00:00 2001 From: Ari Lazier Date: Thu, 11 Jun 2015 12:17:55 -0700 Subject: [PATCH] add api to delete a table and remove the pk for an object type --- object_store.cpp | 27 +++++++++------------------ object_store.hpp | 5 ++++- 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/object_store.cpp b/object_store.cpp index dd7e43e1..65a47f3a 100644 --- a/object_store.cpp +++ b/object_store.cpp @@ -24,7 +24,6 @@ #include #include -#include #include using namespace realm; @@ -209,7 +208,6 @@ bool ObjectStore::create_tables(Group *group, ObjectStore::Schema &target_schema // first pass to create missing tables vector to_update; - set target_type_names; for (auto& object_schema : target_schema) { bool created = false; ObjectStore::table_for_object_type_create_if_needed(group, object_schema.name, created); @@ -219,9 +217,6 @@ bool ObjectStore::create_tables(Group *group, ObjectStore::Schema &target_schema to_update.push_back(&object_schema); changed = true; } - - // keep track of names to figure out what tables need deletion - target_type_names.insert(object_schema.name); } // second pass adds/removes columns for out of date tables @@ -278,19 +273,6 @@ bool ObjectStore::create_tables(Group *group, ObjectStore::Schema &target_schema changed = true; } } - - // remove primary key entries for deleted types - // FIXME - delete actual tables once we have proper testing - ObjectStore::Schema schema; - for (int i = (int)group->size() - 1; i >= 0 ; i--) { - string object_type = object_type_for_table_name(group->get_table_name(i)); - if (object_type.length()) { - if (target_type_names.find(object_type) == target_type_names.end()) { - set_primary_key_for_object(group, object_type, ""); - } - } - } - return changed; } @@ -421,3 +403,12 @@ void ObjectStore::validate_primary_column_uniqueness(Group *group, Schema &schem } } } + +void ObjectStore::delete_data_for_object(Group *group, const StringData &object_type) { + TableRef table = table_for_object_type(group, object_type); + if (table) { + group->remove_table(table->get_index_in_group()); + set_primary_key_for_object(group, object_type, ""); + } +} + diff --git a/object_store.hpp b/object_store.hpp index 0c5f547e..5d1967a7 100644 --- a/object_store.hpp +++ b/object_store.hpp @@ -71,6 +71,9 @@ namespace realm { // check if indexes are up to date - if false you need to call update_realm_with_schema static bool indexes_are_up_to_date(Group *group, Schema &schema); + // deletes the table for the given type + static void delete_data_for_object(Group *group, const StringData &object_type); + private: // set a new schema version static void set_schema_version(Group *group, uint64_t version); @@ -84,7 +87,7 @@ namespace realm { static bool create_metadata_tables(Group *group); // set references to tables on targetSchema and create/update any missing or out-of-date tables - // if update existing is true, updates existing tables, otherwise validates existing tables + // if update existing is true, updates existing tables, otherwise only adds and initializes new tables static bool create_tables(realm::Group *group, ObjectStore::Schema &target_schema, bool update_existing); // get primary key property name for object type