Constraint Exists Check

1,118 views
Skip to first unread message

William Floor

unread,
Jan 24, 2014, 9:58:49 AM1/24/14
to fluentmigrato...@googlegroups.com
Before dropping a constraint, it would be handy if we can check if it exists. This is especially usefull when you start using migrations on an already existing database but due to historical reasons we cant garantee the constraint exists (i.e. a foreign key).

It would be great if there can be a method added like Schema.Constraint(name).exists(). Or is there another neat way to do it?

Or an ugly way? :)

Zidad

unread,
Jan 27, 2014, 4:10:03 AM1/27/14
to fluentmigrato...@googlegroups.com
Hi William,

Are you using SQL Server? If yes, I have some extension methods that do just that:

See the ForeignKeyExists, TableConstraintExists etc...

Let me know if you're running into any issues!

- Wiebe

Zidad

unread,
Jan 27, 2014, 4:40:37 AM1/27/14
to fluentmigrato...@googlegroups.com
I turned the previously mentioned code into a nuget package if you don't want to copy all the source.

Install-Package Net.FluentMigrator

Cheers!

Wiebe 

On Friday, January 24, 2014 3:58:49 PM UTC+1, William Floor wrote:

William Floor

unread,
Jan 28, 2014, 9:05:48 AM1/28/14
to fluentmigrato...@googlegroups.com
Thanks, this can be very usefull. We need to be able to support MSSQL as well as Oracle. But with this example I can make these extensions myself. I did not know where to start with this.
My first idea was to just use a simple dbcommand to do the trick, but this is a very nice way!


Op maandag 27 januari 2014 10:40:37 UTC+1 schreef Zidad:

William Floor

unread,
Jan 30, 2014, 8:15:44 AM1/30/14
to fluentmigrato...@googlegroups.com
Why isnt the SchemaConstraintQuery class implemented, just ilke the SchemaIndexQuery class?

i.e.:

ublic SchemaConstraintQuery(string schemaName, string tableName, string constraintName, IMigrationContext context)
        {
            _schemaName = schemaName;
            _tableName = tableName;
            _constraintName = constraintName;
            _context = context;
        }

        public bool Exists()
        {
            return _context.QuerySchema.ConstraintExists(_schemaName, _tableName, _constraintName);
        }

Op vrijdag 24 januari 2014 15:58:49 UTC+1 schreef William Floor:

Zidad

unread,
Jan 31, 2014, 6:41:53 AM1/31/14
to fluentmigrato...@googlegroups.com
Uhm Well probably I just didn't know about it when I wrote it, or it might have not been around yet (I think I wrote this code 2 years ago already)

The code worked fine for my purpose, as I wanted an easy way to check for the existence of data, and it was very to extend this with for schema queries.

But you're very welcome to submit a pull request :)

Wiebe

William Floor

unread,
Jan 31, 2014, 7:54:33 AM1/31/14
to fluentmigrato...@googlegroups.com
Okay, so there is not really a reason why it was not implemented :)

As soon as I've tested it and read how this pull request stuff works ;) I'll put in a request. Thanks.


Op vrijdag 24 januari 2014 15:58:49 UTC+1 schreef William Floor:
Before dropping a constraint, it would be handy if we can check if it exists. This is especially usefull when you start using migrations on an already existing database but due to historical reasons we cant garantee the constraint exists (i.e. a foreign key).

Wiebe

unread,
Jan 31, 2014, 7:56:08 AM1/31/14
to fluentmigrato...@googlegroups.com
By 'when I wrote that' I mean the code in my repository of course, not the one in FluentMigrator...

William Floor

unread,
Jan 31, 2014, 8:46:04 AM1/31/14
to fluentmigrato...@googlegroups.com
Well,

I've forked the fluentmigrator git, and added the contraint syntax and tested it. Works very well, so I did a pull request anyway :)

Op vrijdag 31 januari 2014 13:56:08 UTC+1 schreef Wiebe:

BobC5

unread,
May 20, 2015, 4:05:49 PM5/20/15
to fluentmigrato...@googlegroups.com
I'm unsuccessfully trying to guess how to use ForeignKeyExists.

I'd appreciate a code sample showing it in use.

Tom Marien

unread,
May 21, 2015, 3:55:12 AM5/21/15
to fluentmigrato...@googlegroups.com
Hi Bob

Suppose you have an account table where you want to have a foreignkey to:

Create.Table("TableWithForeignkeytoAccount")
                  .WithColumn(PrimaryKeyName).AsGuid().PrimaryKey().NotNullable().WithDefault(SystemMethods.NewGuid)
                  .WithColumn("ACCOUNT_ID").AsInt32().Nullable().ForeignKey("FK_ACCOUNT", "ACCOUNT", "ID").OnDelete(Rule.Cascade)

I hope this helps

Kind regards

Tom Marien

Wiebe Tijsma

unread,
May 21, 2015, 5:39:42 AM5/21/15
to fluentmigrato...@googlegroups.com
Hi Bob,

Yes there's some trickery to get the data context, can you try this:

public class AddGroupNameColumn : ForwardOnlyMigration
{
    public override void Up()
    {
        if (!this.GetData().ForeignKeyExists("FK_MyForeignKeyName"))
        {
            Execute.EmbeddedScript("CreateForeignKey.sql");
        }
    }
}

But you'll need some of the other dependencies from here too (available on nuget too, but it might just be as easy to include the code in your project):

Wiebe 

--
You received this message because you are subscribed to a topic in the Google Groups "FluentMigrator Google Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/fluentmigrator-google-group/euBgGm7z4Oc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to fluentmigrator-goog...@googlegroups.com.
To post to this group, send email to fluentmigrato...@googlegroups.com.
Visit this group at http://groups.google.com/group/fluentmigrator-google-group.
For more options, visit https://groups.google.com/d/optout.

BobC5

unread,
May 21, 2015, 9:25:30 AM5/21/15
to fluentmigrato...@googlegroups.com
Thank you very much for your help and for this useful set of extensions to FluentMigrator.

I was able to get it to work after pulling a number of extension methods from some of your other projects.

I'm sure this would have been much simpler if I used your NuGet package, but I wanted to use source code.
To unsubscribe from this group and all its topics, send an email to fluentmigrator-google-group+unsub...@googlegroups.com.

To post to this group, send email to fluentmigrato...@googlegroups.com.
Visit this group at http://groups.google.com/group/fluentmigrator-google-group.

Wiebe Tijsma

unread,
May 21, 2015, 9:52:51 AM5/21/15
to fluentmigrato...@googlegroups.com
Sure, welcome!

Feel free to submit a pull if make any useful additions/fixes, now it's just tailored to what I needed specifically...

Wiebe

To unsubscribe from this group and all its topics, send an email to fluentmigrator-goog...@googlegroups.com.
To post to this group, send email to fluentmigrato...@googlegroups.com.
Visit this group at http://groups.google.com/group/fluentmigrator-google-group.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "FluentMigrator Google Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/fluentmigrator-google-group/euBgGm7z4Oc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to fluentmigrator-goog...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages