|
37 | 37 |
|
38 | 38 | import androidx.test.ext.junit.runners.AndroidJUnit4; |
39 | 39 | import androidx.test.platform.app.InstrumentationRegistry; |
| 40 | + |
| 41 | +import io.realm.annotations.PrimaryKey; |
| 42 | +import io.realm.annotations.RealmClass; |
40 | 43 | import io.realm.entities.AllTypes; |
41 | 44 | import io.realm.entities.AnnotationTypes; |
42 | 45 | import io.realm.entities.CatOwner; |
|
62 | 65 | import io.realm.entities.Thread; |
63 | 66 | import io.realm.entities.embedded.EmbeddedSimpleChild; |
64 | 67 | import io.realm.entities.embedded.EmbeddedSimpleParent; |
| 68 | +import io.realm.entities.migration.HandleBackLinksChild1; |
| 69 | +import io.realm.entities.migration.HandleBackLinksChild2; |
| 70 | +import io.realm.entities.migration.HandleBackLinksParent1; |
| 71 | +import io.realm.entities.migration.HandleBackLinksParent2; |
65 | 72 | import io.realm.entities.migration.MigrationClassRenamed; |
66 | 73 | import io.realm.entities.migration.MigrationCore6PKStringIndexedByDefault; |
67 | 74 | import io.realm.entities.migration.MigrationFieldRenameAndAdd; |
@@ -1712,6 +1719,135 @@ public void migrate(DynamicRealm realm, long oldVersion, long newVersion) { |
1712 | 1719 | realm = Realm.getInstance(realmConfig); |
1713 | 1720 | } |
1714 | 1721 |
|
| 1722 | + @Test |
| 1723 | + public void migrate_embeddedObject_deleteOrphans() { |
| 1724 | + RealmConfiguration config1 = configFactory.createConfigurationBuilder() |
| 1725 | + .schema(HandleBackLinksParent1.class, HandleBackLinksChild1.class) |
| 1726 | + .schemaVersion(1) |
| 1727 | + .build(); |
| 1728 | + |
| 1729 | + Realm realm1 = Realm.getInstance(config1); |
| 1730 | + realm1.executeTransaction(realm -> { |
| 1731 | + HandleBackLinksChild1 child = new HandleBackLinksChild1(); |
| 1732 | + HandleBackLinksParent1 parent = new HandleBackLinksParent1(); |
| 1733 | + parent.child = child; |
| 1734 | + HandleBackLinksChild1 orphan = new HandleBackLinksChild1(); |
| 1735 | + realm.insertOrUpdate(parent); |
| 1736 | + realm.insertOrUpdate(orphan); |
| 1737 | + }); |
| 1738 | + long parentCountV1 = realm1.where(HandleBackLinksParent1.class) |
| 1739 | + .count(); |
| 1740 | + assertEquals(1L, parentCountV1); |
| 1741 | + long childCountV1 = realm1.where(HandleBackLinksChild1.class) |
| 1742 | + .count(); |
| 1743 | + assertEquals(2L, childCountV1); |
| 1744 | + realm1.close(); |
| 1745 | + |
| 1746 | + RealmMigration migration = (realm, oldVersion, newVersion) -> { |
| 1747 | + RealmSchema schema = realm.getSchema(); |
| 1748 | + if (oldVersion == 1L) { |
| 1749 | + RealmObjectSchema parent = schema.get("HandleBackLinksParent1"); |
| 1750 | + assertNotNull(parent); |
| 1751 | + RealmObjectSchema child = schema.get("HandleBackLinksChild1"); |
| 1752 | + assertNotNull(child); |
| 1753 | + try { |
| 1754 | + child.setEmbedded(true); |
| 1755 | + } catch (Exception e) { |
| 1756 | + assertEquals(IllegalStateException.class, e.getClass()); |
| 1757 | + assertEquals("Cannot convert 'HandleBackLinksChild1' to embedded: at least one object has no incoming links and would be deleted.", e.getMessage()); |
| 1758 | + } |
| 1759 | + child.setEmbedded(true, true); |
| 1760 | + |
| 1761 | + // Rename classes to avoid conflicts with all other tests |
| 1762 | + parent.setClassName("HandleBackLinksParent2"); |
| 1763 | + child.setClassName("HandleBackLinksChild2"); |
| 1764 | + } |
| 1765 | + }; |
| 1766 | + |
| 1767 | + // Create schema v2 |
| 1768 | + RealmConfiguration config2 = configFactory.createConfigurationBuilder() |
| 1769 | + .schema(HandleBackLinksParent2.class, HandleBackLinksChild2.class) |
| 1770 | + .schemaVersion(2) |
| 1771 | + .migration(migration) |
| 1772 | + .build(); |
| 1773 | + |
| 1774 | + // The orphan child is erased |
| 1775 | + Realm realm2 = Realm.getInstance(config2); |
| 1776 | + long parentCountV2 = realm2.where(HandleBackLinksParent2.class) |
| 1777 | + .count(); |
| 1778 | + assertEquals(1L, parentCountV2); |
| 1779 | + long childCountV2 = realm2.where(HandleBackLinksChild2.class) |
| 1780 | + .count(); |
| 1781 | + assertEquals(1L, childCountV2); |
| 1782 | + realm2.close(); |
| 1783 | + } |
| 1784 | + |
| 1785 | + @Test |
| 1786 | + public void migrate_embeddedObject_clonesChildWhenReferencedMoreThanOnce() { |
| 1787 | + RealmConfiguration config1 = configFactory.createConfigurationBuilder() |
| 1788 | + .schema(HandleBackLinksParent1.class, HandleBackLinksChild1.class) |
| 1789 | + .schemaVersion(1) |
| 1790 | + .build(); |
| 1791 | + |
| 1792 | + Realm realm1 = Realm.getInstance(config1); |
| 1793 | + HandleBackLinksChild1 child = new HandleBackLinksChild1(); |
| 1794 | + HandleBackLinksParent1 parent1 = new HandleBackLinksParent1(); |
| 1795 | + HandleBackLinksParent1 parent2 = new HandleBackLinksParent1(); |
| 1796 | + realm1.executeTransaction(realm -> { |
| 1797 | + // Copy child and set it as children for both parents. |
| 1798 | + // Now the managed child has two parents. This will not be allowed after converting it |
| 1799 | + // to an embedded object and will result into the child object being duplicated so that |
| 1800 | + // both parents can have a reference to a separate and different embedded object. |
| 1801 | + HandleBackLinksChild1 managedChild = realm.copyToRealm(child); |
| 1802 | + parent1.id = 1L; |
| 1803 | + parent1.child = managedChild; |
| 1804 | + realm.insert(parent1); |
| 1805 | + parent2.id = 2L; |
| 1806 | + parent2.child = managedChild; |
| 1807 | + realm.insert(parent2); |
| 1808 | + }); |
| 1809 | + long parentCountV1 = realm1.where(HandleBackLinksParent1.class) |
| 1810 | + .count(); |
| 1811 | + assertEquals(2L, parentCountV1); |
| 1812 | + long childCountV1 = realm1.where(HandleBackLinksChild1.class) |
| 1813 | + .count(); |
| 1814 | + assertEquals(1L, childCountV1); |
| 1815 | + realm1.close(); |
| 1816 | + |
| 1817 | + RealmMigration migration = (realm, oldVersion, newVersion) -> { |
| 1818 | + RealmSchema schema = realm.getSchema(); |
| 1819 | + if (oldVersion == 1L) { |
| 1820 | + RealmObjectSchema parentSchema = schema.get("HandleBackLinksParent1"); |
| 1821 | + assertNotNull(parentSchema); |
| 1822 | + RealmObjectSchema childSchema = schema.get("HandleBackLinksChild1"); |
| 1823 | + assertNotNull(childSchema); |
| 1824 | + childSchema.setEmbedded(true, true); |
| 1825 | + |
| 1826 | + // Rename classes to avoid conflicts with all other tests |
| 1827 | + parentSchema.setClassName("HandleBackLinksParent2"); |
| 1828 | + childSchema.setClassName("HandleBackLinksChild2"); |
| 1829 | + } |
| 1830 | + }; |
| 1831 | + |
| 1832 | + // Create schema v2 |
| 1833 | + RealmConfiguration config2 = configFactory.createConfigurationBuilder() |
| 1834 | + .schema(HandleBackLinksParent2.class, HandleBackLinksChild2.class) |
| 1835 | + .schemaVersion(2) |
| 1836 | + .migration(migration) |
| 1837 | + .build(); |
| 1838 | + |
| 1839 | + // After the migration the only child class is embedded and will not be allowed to have two parents |
| 1840 | + // so the child object will be duplicated |
| 1841 | + Realm realm2 = Realm.getInstance(config2); |
| 1842 | + long parentCountV2 = realm2.where(HandleBackLinksParent2.class) |
| 1843 | + .count(); |
| 1844 | + assertEquals(2L, parentCountV2); |
| 1845 | + long childCountV2 = realm2.where(HandleBackLinksChild2.class) |
| 1846 | + .count(); |
| 1847 | + assertEquals(2L, childCountV2); |
| 1848 | + realm2.close(); |
| 1849 | + } |
| 1850 | + |
1715 | 1851 | // TODO Add unit tests for default nullability |
1716 | 1852 | // TODO Add unit tests for default Indexing for Primary keys |
1717 | 1853 | } |
0 commit comments