Click here to Skip to main content
15,884,473 members
Articles / Programming Languages / XML

DBKeeperNet - Keeps Your DB Schema Up-to-date

Rate me:
Please Sign up or sign in to vote.
4.88/5 (14 votes)
26 Aug 2014BSD4 min read 50.4K   575   89  
An article describing a simple .NET library which simply keeps your database schema up-to-date.
using System;
using NUnit.Framework;

namespace DbKeeperNet.Engine.Tests.Extensions.DatabaseServices
{
    public abstract class DatabaseServiceForeignKeyTests<T> : DatabaseServiceTests<T>
        where T: IDatabaseService, new()
    {
        private const string TABLE_NAME = @"testing_tb_fk";
        private const string FOREIGN_KEY_NAME = @"FK_testing_tb_fk";
		private const string UNKNOWN_FOREIGN_KEY_NAME = @"FK_testing_tb_unknown_fk";

        protected DatabaseServiceForeignKeyTests(string connectionString)
            : base(connectionString)
        {
        }

        [SetUp]
        public void SetUp()
        {
            Cleanup();
        }

        [TearDown]
        public void Shutdown()
        {
            Cleanup();
        }
		
        [Test]
        public void TestForeignKeyNotExists()
        {
        	CreateTestForeignKeyInDatabase();

            TestForeignKeyExists(UNKNOWN_FOREIGN_KEY_NAME, TABLE_NAME);
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestForeignKeyNotExistsNullName()
        {
            TestForeignKeyExists(null, null);
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestForeignKeyNotExistsEmptyName()
        {
            TestForeignKeyExists("", "");
        }

        [Test]
        public void TestForeignKeyExists()
        {
        	CreateTestForeignKeyInDatabase();

        	Assert.That(TestForeignKeyExists(FOREIGN_KEY_NAME, TABLE_NAME), Is.True);
        }

    	private void CreateTestForeignKeyInDatabase()
    	{
    		using (IDatabaseService connectedService = CreateConnectedDbService())
    		{

    			CreateNamedForeignKey(connectedService, TABLE_NAME, FOREIGN_KEY_NAME);
    		}
    	}

    	protected bool TestForeignKeyExists(string key, string table)
		{
			bool result;

			using (IDatabaseService connectedService = CreateConnectedDbService())
			{
				result = connectedService.ForeignKeyExists(key, table);
			}
			return result;
		}

        protected abstract void CreateNamedForeignKey(IDatabaseService connectedService, string tableName, string foreignKeyName);
        protected abstract void DropNamedForeignKey(IDatabaseService connectedService, string tableName, string foreignKeyName);
		
		private void Cleanup()
		{
			using (IDatabaseService connectedService = CreateConnectedDbService())
			{
				DropNamedForeignKey(connectedService, TABLE_NAME, FOREIGN_KEY_NAME);
			}
		}
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The BSD License


Written By
Team Leader NCR
Czech Republic Czech Republic
I'm software developer since 1996. I started with assembler on Intel 8051 CPUs, during years I was interested in C, C++, Sybase PowerBuilder, PHP, Sybase Anywhere Database, MSSQL server and multiplatform development.

Currently I'm developing in C++ and C# (this is my favorit and I spent some time with MCPD achievement). I'm also interested in design patterns.

Comments and Discussions