Amazon.CognitoSync.SyncManager.Internal.SQLiteLocalStorage.ChangeIdentityId C# (CSharp) Method

ChangeIdentityId() public method

Reparents all datasets from old identity id to a new one.
public ChangeIdentityId ( string oldIdentityId, string newIdentityId ) : void
oldIdentityId string Old identity identifier.
newIdentityId string New identity identifier.
return void
        public void ChangeIdentityId(string oldIdentityId, string newIdentityId)
        {
            _logger.DebugFormat("Reparenting datasets from {0} to {1}", oldIdentityId, newIdentityId);
            lock (sqlite_lock)
            {

                List<Statement> statements = new List<Statement>();
                // if oldIdentityId is unknown, aka the dataset is created prior to
                // having a cognito id, just reparent datasets from unknown to
                // newIdentityId
                if (DatasetUtils.UNKNOWN_IDENTITY_ID == oldIdentityId)
                {

                    HashSet<string> commonDatasetNames = GetCommonDatasetNames(oldIdentityId, newIdentityId);

                    // append UNKNOWN to the name of all non unique datasets
                    foreach (String oldDatasetName in commonDatasetNames)
                    {
                        string updateDatasetQuery = "UPDATE " + TABLE_DATASETS
                              + " SET " + DatasetColumns.DATASET_NAME + " = @" + DatasetColumns.DATASET_NAME
                              + " WHERE " + DatasetColumns.IDENTITY_ID + " = @" + DatasetColumns.IDENTITY_ID
                              + " AND " + DatasetColumns.DATASET_NAME + " = @old" + DatasetColumns.DATASET_NAME + " ";

                        string timestamp = AWSSDKUtils.ConvertToUnixEpochMilliSeconds(DateTime.UtcNow).ToString(CultureInfo.InvariantCulture);

                        Statement updateDatasetStatement = new Statement()
                        {
                            Query = updateDatasetQuery,
                            Parameters = new string[] { oldDatasetName + "." + oldIdentityId + "-" + timestamp, oldIdentityId, oldDatasetName }
                        };

                        statements.Add(updateDatasetStatement);

                        string updateRecordsQuery = "UPDATE " + TABLE_RECORDS
                            + " SET " + RecordColumns.DATASET_NAME + " = @" + RecordColumns.DATASET_NAME
                            + " WHERE " + RecordColumns.IDENTITY_ID + " = @" + RecordColumns.IDENTITY_ID
                            + " AND " + RecordColumns.DATASET_NAME + " = @old" + RecordColumns.DATASET_NAME + " ";

                        Statement updateRecordsStatement = new Statement()
                        {
                            Query = updateRecordsQuery,
                            Parameters = new string[] { oldDatasetName + "." + oldIdentityId + "-" + timestamp, oldIdentityId, oldDatasetName }
                        };
                        statements.Add(updateRecordsStatement);

                    }

                    string updateIdentityDatasetQuery = DatasetColumns.BuildUpdate(
                            new string[] { DatasetColumns.IDENTITY_ID },
                            DatasetColumns.IDENTITY_ID + " = @oldIdentityId "
                        );

                    Statement UpdateIdentityDatasetStatement = new Statement()
                    {
                        Query = updateIdentityDatasetQuery,
                        Parameters = new string[] { newIdentityId, oldIdentityId }
                    };

                    statements.Add(UpdateIdentityDatasetStatement);

                    string updateRecordsIdentityQuery = RecordColumns.BuildUpdate(
                            new string[] { RecordColumns.IDENTITY_ID },
                            RecordColumns.IDENTITY_ID + " = @oldIdentityId "
                        );

                    Statement UpdateIdentityRecordsStatement = new Statement()
                    {
                        Query = updateRecordsIdentityQuery,
                        Parameters = new string[] { newIdentityId, oldIdentityId }
                    };

                    statements.Add(UpdateIdentityRecordsStatement);
                }
                else
                {
                    // 1. copy oldIdentityId/dataset to newIdentityId/dataset
                    // datasets table
                    string copyDatasetToNewIdentity = "INSERT INTO " + TABLE_DATASETS + "("
                        + DatasetColumns.IDENTITY_ID + ","
                        + DatasetColumns.DATASET_NAME + ","
                        + DatasetColumns.CREATION_TIMESTAMP + ","
                        + DatasetColumns.STORAGE_SIZE_BYTES + ","
                        + DatasetColumns.RECORD_COUNT
                        // last sync count is reset to default 0
                        + ")"
                        + " SELECT "
                        + "'" + newIdentityId + "'," // assign new owner
                        + DatasetColumns.DATASET_NAME + ","
                        + DatasetColumns.CREATION_TIMESTAMP + ","
                        + DatasetColumns.STORAGE_SIZE_BYTES + ","
                        + DatasetColumns.RECORD_COUNT
                        + " FROM " + TABLE_DATASETS
                        + " WHERE " + DatasetColumns.IDENTITY_ID + " = @" + DatasetColumns.IDENTITY_ID + " ";

                    statements.Add(new Statement
                    {
                        Query = copyDatasetToNewIdentity,
                        Parameters = new string[] { oldIdentityId }
                    });

                    // records table
                    string copyRecordsToNewIdentity = "INSERT INTO " + TABLE_RECORDS + "("
                        + RecordColumns.IDENTITY_ID + ","
                        + RecordColumns.DATASET_NAME + ","
                        + RecordColumns.KEY + ","
                        + RecordColumns.VALUE + ","
                        // sync count is resset to default 0
                        + RecordColumns.LAST_MODIFIED_TIMESTAMP + ","
                        + RecordColumns.LAST_MODIFIED_BY + ","
                        + RecordColumns.DEVICE_LAST_MODIFIED_TIMESTAMP
                        // modified is reset to default 1 (dirty)
                        + ")"
                        + " SELECT "
                        + "'" + newIdentityId + "'," // assign new owner
                        + RecordColumns.DATASET_NAME + ","
                        + RecordColumns.KEY + ","
                        + RecordColumns.VALUE + ","
                        + RecordColumns.LAST_MODIFIED_TIMESTAMP + ","
                        + RecordColumns.LAST_MODIFIED_BY + ","
                        + RecordColumns.DEVICE_LAST_MODIFIED_TIMESTAMP
                        + " FROM " + TABLE_RECORDS
                        + " WHERE " + RecordColumns.IDENTITY_ID + " = @" + RecordColumns.IDENTITY_ID + " ";

                    statements.Add(new Statement
                    {
                        Query = copyRecordsToNewIdentity,
                        Parameters = new string[] { oldIdentityId }
                    });


                    // 2. rename oldIdentityId/dataset to
                    // newIdentityId/dataset.oldIdentityId
                    // datasets table
                    string updateDatasetToNewIdentityQuery = "UPDATE " + TABLE_DATASETS
                        + " SET "
                        + DatasetColumns.IDENTITY_ID + " = '" + newIdentityId + "', "
                        + DatasetColumns.DATASET_NAME + " = "
                        + DatasetColumns.DATASET_NAME + " || '." + oldIdentityId + "'"
                        + " WHERE " + DatasetColumns.IDENTITY_ID + " = @" + DatasetColumns.IDENTITY_ID + " ";

                    statements.Add(new Statement
                    {
                        Query = updateDatasetToNewIdentityQuery,
                        Parameters = new string[] { oldIdentityId }
                    });

                    // records table
                    string updateRecordsToNewIdentityQuery = "UPDATE " + TABLE_RECORDS
                        + " SET "
                        + RecordColumns.IDENTITY_ID + " = '" + newIdentityId + "', "
                        + RecordColumns.DATASET_NAME + " = "
                        + RecordColumns.DATASET_NAME + " || '." + oldIdentityId + "'"
                        + " WHERE " + RecordColumns.IDENTITY_ID + " = @" + RecordColumns.IDENTITY_ID + " ";

                    statements.Add(new Statement
                    {
                        Query = updateRecordsToNewIdentityQuery,
                        Parameters = new string[] { oldIdentityId }
                    });
                }

                //execute all of them
                ExecuteMultipleHelper(statements);

            }

        }

Usage Example

        public void SQliteDatasetsTests()
        {
            string dbPath = Path.Combine(PCLStorage.FileSystem.Current.LocalStorage.Path, DB_FILE_NAME);

            string randomId = "old";
            string randomDataset = Guid.NewGuid().ToString();
            using (SQLiteLocalStorage storage = new SQLiteLocalStorage())
            {
                storage.WipeData();
                storage.CreateDataset(randomId, randomDataset);
                storage.PutValue(randomId, randomDataset, "Voldemort", "He who must not be named");

                using (SQLiteConnection connection = new SQLiteConnection(dbPath))
                {
                    string query = "select count(*) from datasets where dataset_name = @dataset_name and identity_id = @identity_id ";
                    using (var sqliteStatement = connection.Prepare(query))
                    {
                        BindData(sqliteStatement, randomDataset, randomId);
                        if(sqliteStatement.Step()==SQLiteResult.ROW)
                        {
                            var count = sqliteStatement.GetInteger(0);
                            Assert.IsTrue(count == 1);
                        }
                        else
                        {
                            Assert.Fail();
                        }

                    }

                    query = "select count(*) from records where dataset_name = @dataset_name and identity_id = @identity_id ";

                    using( var sqliteStatement = connection.Prepare(query))
                    {
                        BindData(sqliteStatement, randomDataset, randomId);
                        if (sqliteStatement.Step() == SQLiteResult.ROW)
                        {
                            var count = sqliteStatement.GetInteger(0);
                            Assert.IsTrue(count == 1);
                        }
                        else
                        {
                            Assert.Fail();
                        }
                    }
                }

                var datasets = storage.GetDatasetMetadata(randomId);
                Assert.IsTrue(datasets.Count == 1);

                var Id = "new";
                storage.ChangeIdentityId(randomId, Id);
                randomId = Id;

                using (SQLiteConnection connection = new SQLiteConnection(dbPath))
                {
                    var query = "select count(*) from datasets where dataset_name = @dataset_name and identity_id = @identity_id ";
                    using (var sqliteStatement = connection.Prepare(query))
                    {
                        BindData(sqliteStatement, randomDataset, randomId);
                        if (sqliteStatement.Step() == SQLiteResult.ROW)
                        {
                            var count = sqliteStatement.GetInteger(0);
                            Assert.IsTrue(count == 1);
                        }
                        else
                        {
                            Assert.Fail();
                        }
                    }


                    query = "select count(*) from records where dataset_name = @dataset_name and identity_id = @identity_id ";
                    using (var sqliteStatement = connection.Prepare(query))
                    {
                        BindData(sqliteStatement, randomDataset, randomId);
                        if (sqliteStatement.Step() == SQLiteResult.ROW)
                        {
                            var count = sqliteStatement.GetInteger(0);
                            Assert.IsTrue(count == 1);
                        }
                        else
                        {
                            Assert.Fail();
                        }
                    }
                }

                storage.DeleteDataset(randomId, randomDataset);

                using (SQLiteConnection connection = new SQLiteConnection(dbPath))
                {
                    var query = "select last_sync_count from datasets where dataset_name = @dataset_name and identity_id = @identity_id";

                    using (var sqliteStatement = connection.Prepare(query))
                    {
                        BindData(sqliteStatement, randomDataset, randomId);
                        if (sqliteStatement.Step() == SQLiteResult.ROW)
                        {
                            var count = sqliteStatement.GetInteger(0);
                            Assert.IsTrue(count == -1);
                        }
                        else
                        {
                            Assert.Fail();
                        }
                    }

                }

                Assert.IsNotNull(storage.GetDatasetMetadata(randomId)[0]);

            }
        }
All Usage Examples Of Amazon.CognitoSync.SyncManager.Internal.SQLiteLocalStorage::ChangeIdentityId