public void VerifyThatOverridesAndForeignKeyConstraintsWork()
        {
            var fromObject = new User
            {
                Uuid = Guid.NewGuid(),
                Name = "John Doe"
            };

            var toObject = new Usergroup
            {
                Iid  = 1,
                Name = "Admin"
            };

            fromObject.Usergroup_Id = toObject.Iid;

            DatabaseSession.Instance.Connector.CreateTableWithColumns(toObject);
            DatabaseSession.Instance.Connector.CreateTableWithColumns(fromObject);

            Assert.Throws <InvalidDataException>(() => DatabaseSession.Instance.Connector.CreateForeignKeyConstraint(fromObject.GetType().GetProperty("Usergroup_Id"), fromObject, toObject.GetType().GetProperty(toObject.PrimaryKey), toObject));

            DatabaseSession.Instance.Connector.CreatePrimaryKeyConstraint(toObject);
            Assert.Throws <InvalidDataException>(
                () =>
                DatabaseSession.Instance.Connector.DeleteForeignKeyConstraint(
                    fromObject.GetType().GetProperty("Usergroup_Id"), fromObject,
                    toObject.GetType().GetProperty(toObject.PrimaryKey), toObject));

            Assert.DoesNotThrow(() => DatabaseSession.Instance.Connector.CreateForeignKeyConstraint(fromObject.GetType().GetProperty("Usergroup_Id"), fromObject, toObject.GetType().GetProperty(toObject.PrimaryKey), toObject));

            toObject.Save();

            Assert.DoesNotThrow(() => fromObject.Save());

            DatabaseSession.Instance.Connector.DeleteForeignKeyConstraint(fromObject.GetType().GetProperty("Usergroup_Id"), fromObject, toObject.GetType().GetProperty(toObject.PrimaryKey), toObject);

            Assert.DoesNotThrow(() => toObject.Delete());
            DatabaseSession.Instance.Connector.DeleteTable(new User());
            DatabaseSession.Instance.Connector.DeleteTable(new Usergroup());
        }
        public void VerifyThatTransactionSafeDeleteTableWorks()
        {
            var fromObject = new User
            {
                Uuid = Guid.NewGuid(),
                Name = "John Doe"
            };

            var toObject = new Usergroup();

            Assert.Throws <InvalidDataException>(() => DatabaseSession.Instance.Connector.DeleteTable(fromObject));

            DatabaseSession.Instance.Connector.CreateTableWithColumns(fromObject);
            DatabaseSession.Instance.Connector.CreateTableWithColumns(toObject);
            DatabaseSession.Instance.Connector.CreatePrimaryKeyConstraint(fromObject);
            DatabaseSession.Instance.Connector.CreatePrimaryKeyConstraint(toObject);

            var transaction = DatabaseSession.Instance.CreateTransaction();

            DatabaseSession.Instance.Connector.CreateForeignKeyConstraint(fromObject.GetType().GetProperty("Usergroup_Id"), fromObject, toObject.GetType().GetProperty(toObject.PrimaryKey), toObject, FkDeleteBehaviorKind.CASCADE, true, transaction);
            DatabaseSession.Instance.Connector.DeleteForeignKeyConstraint(fromObject.GetType().GetProperty("Usergroup_Id"), fromObject, toObject.GetType().GetProperty(toObject.PrimaryKey), toObject, transaction);
            DatabaseSession.Instance.Connector.DeleteTable(fromObject, transaction);

            Assert.IsTrue(DatabaseSession.Instance.Connector.CheckTableExists(fromObject));

            DatabaseSession.Instance.Connector.DeleteTable(toObject, transaction);

            Assert.IsTrue(DatabaseSession.Instance.Connector.CheckTableExists(toObject));

            DatabaseSession.Instance.CommitTransaction(transaction);

            Assert.IsFalse(DatabaseSession.Instance.Connector.CheckTableExists(fromObject));
            Assert.IsFalse(DatabaseSession.Instance.Connector.CheckTableExists(toObject));
        }
        /// <summary>
        /// The method that executes to apply a migration.
        /// </summary>
        public override void Migrate()
        {
            var claim = new Claim();
            var usergroup = new Usergroup();
            var user = new User();

            var transaction = DatabaseSession.Instance.CreateTransaction();

            DatabaseSession.Instance.Connector.CreateTableWithColumns(claim, transaction);
            DatabaseSession.Instance.Connector.CreatePrimaryKeyConstraint(claim, transaction);
            DatabaseSession.Instance.Connector.CreateTableWithColumns(usergroup, transaction);
            DatabaseSession.Instance.Connector.CreatePrimaryKeyConstraint(usergroup, transaction);
            DatabaseSession.Instance.Connector.CreateTableWithColumns(user, transaction);
            DatabaseSession.Instance.Connector.CreatePrimaryKeyConstraint(user, transaction);
            DatabaseSession.Instance.Connector.CreateUniquenessConstraint(new []{ user.GetType().GetProperty("UserName") }, user, transaction);
            DatabaseSession.Instance.Connector.CreateUniquenessConstraint(new[] { user.GetType().GetProperty("Email") }, user, transaction);

            DatabaseSession.Instance.Connector.CreateForeignKeyConstraint(user.GetType().GetProperty("UsergroupId"),user, usergroup.GetType().GetProperty("Id"), usergroup, transaction);

            DatabaseSession.Instance.CommitTransaction(transaction);
        }
        public void VerifyThatTransactionSafeDeleteTableWorks()
        {
            var fromObject = new User
            {
                Id = Guid.NewGuid(),
                Name = "John Doe"
            };

            var toObject = new Usergroup();

            Assert.Throws<InvalidDataException>(() => DatabaseSession.Instance.Connector.DeleteTable(fromObject));

            DatabaseSession.Instance.Connector.CreateTableWithColumns(fromObject);
            DatabaseSession.Instance.Connector.CreateTableWithColumns(toObject);
            DatabaseSession.Instance.Connector.CreatePrimaryKeyConstraint(fromObject);
            DatabaseSession.Instance.Connector.CreatePrimaryKeyConstraint(toObject);

            var transaction = DatabaseSession.Instance.CreateTransaction();

            DatabaseSession.Instance.Connector.CreateForeignKeyConstraint(fromObject.GetType().GetProperty("Usergroup_Id"), fromObject, toObject.GetType().GetProperty(toObject.PrimaryKey), toObject, transaction);
            DatabaseSession.Instance.Connector.DeleteForeignKeyConstraint(fromObject.GetType().GetProperty("Usergroup_Id"), fromObject, toObject.GetType().GetProperty(toObject.PrimaryKey), toObject, transaction);
            DatabaseSession.Instance.Connector.DeleteTable(fromObject, transaction);

            Assert.IsTrue(DatabaseSession.Instance.Connector.CheckTableExists(fromObject));

            DatabaseSession.Instance.Connector.DeleteTable(toObject, transaction);

            Assert.IsTrue(DatabaseSession.Instance.Connector.CheckTableExists(toObject));

            DatabaseSession.Instance.CommitTransaction(transaction);

            Assert.IsFalse(DatabaseSession.Instance.Connector.CheckTableExists(fromObject));
            Assert.IsFalse(DatabaseSession.Instance.Connector.CheckTableExists(toObject));
        }
        public void VerifyThatOverridesAndForeignKeyConstraintsWork()
        {
            var fromObject = new User
            {
                Id = Guid.NewGuid(),
                Name = "John Doe"
            };

            var toObject = new Usergroup
            {
                Iid = 1,
                Name = "Admin"
            };

            fromObject.Usergroup_Id = toObject.Iid;

            DatabaseSession.Instance.Connector.CreateTableWithColumns(toObject);
            DatabaseSession.Instance.Connector.CreateTableWithColumns(fromObject);

            Assert.Throws<InvalidDataException>(() => DatabaseSession.Instance.Connector.CreateForeignKeyConstraint(fromObject.GetType().GetProperty("Usergroup_Id"), fromObject, toObject.GetType().GetProperty(toObject.PrimaryKey), toObject));

            DatabaseSession.Instance.Connector.CreatePrimaryKeyConstraint(toObject);
            Assert.Throws<InvalidDataException>(
                () =>
                    DatabaseSession.Instance.Connector.DeleteForeignKeyConstraint(
                        fromObject.GetType().GetProperty("Usergroup_Id"), fromObject,
                        toObject.GetType().GetProperty(toObject.PrimaryKey), toObject));

            Assert.DoesNotThrow(() => DatabaseSession.Instance.Connector.CreateForeignKeyConstraint(fromObject.GetType().GetProperty("Usergroup_Id"), fromObject, toObject.GetType().GetProperty(toObject.PrimaryKey), toObject));

            Assert.Throws<InvalidDataException>(() => fromObject.Save());

            toObject.Save();

            Assert.DoesNotThrow(() => fromObject.Save());

            Assert.Throws<InvalidDataException>(() => toObject.Delete());

            DatabaseSession.Instance.Connector.DeleteForeignKeyConstraint(fromObject.GetType().GetProperty("Usergroup_Id"), fromObject, toObject.GetType().GetProperty(toObject.PrimaryKey), toObject);

            Assert.DoesNotThrow(() => toObject.Delete());
            DatabaseSession.Instance.Connector.DeleteTable(new User());
            DatabaseSession.Instance.Connector.DeleteTable(new Usergroup());
        }
        /// <summary>
        /// The method that executes to apply a migration.
        /// </summary>
        public override void Migrate()
        {
            var claim     = new Claim();
            var usergroup = new Usergroup();
            var user      = new User();

            var transaction = DatabaseSession.Instance.CreateTransaction();

            DatabaseSession.Instance.Connector.CreateTableWithColumns(claim, transaction);
            DatabaseSession.Instance.Connector.CreatePrimaryKeyConstraint(claim, transaction);
            DatabaseSession.Instance.Connector.CreateTableWithColumns(usergroup, transaction);
            DatabaseSession.Instance.Connector.CreatePrimaryKeyConstraint(usergroup, transaction);
            DatabaseSession.Instance.Connector.CreateTableWithColumns(user, transaction);
            DatabaseSession.Instance.Connector.CreatePrimaryKeyConstraint(user, transaction);
            DatabaseSession.Instance.Connector.CreateUniquenessConstraint(new [] { user.GetType().GetProperty("UserName") }, user, transaction);
            DatabaseSession.Instance.Connector.CreateUniquenessConstraint(new[] { user.GetType().GetProperty("Email") }, user, transaction);

            DatabaseSession.Instance.Connector.CreateForeignKeyConstraint(user.GetType().GetProperty("UsergroupId"), user, usergroup.GetType().GetProperty("Id"), usergroup, transaction);

            DatabaseSession.Instance.CommitTransaction(transaction);
        }