Search code examples
c#.netoracle-databasefluent-migrator

How to check if sequence exists in Fluent Migrator


Is there any way to check if sequence exists before create in Fluent Migrator?

here is my code:

IfDatabase("Oracle").Create.Sequence("SEQ_TEST").MaxValue(1).MaxValue(99999).StartWith(1).IncrementBy(2);

error:

ORA-00955: name is already being used by existing object tips

I can check tables using this code:

Schema.Table("TableTest").Exists()

But i dont know how i can check sequences, is there any way? Any help is appreciated!


Solution

  • I Resolved my issue using SQL Command

                Execute.Sql(@"
                    DECLARE
                      VAR_SEQUENCE_EXIST VARCHAR(1);
                    BEGIN
    
                      SELECT COUNT(1)
                        INTO VAR_SEQUENCE_EXIST
                        FROM USER_SEQUENCES
                       WHERE SEQUENCE_NAME = 'SEQ_TEST';
    
                      IF VAR_SEQUENCE_EXIST = 0 THEN       
                        -- Create sequence 
                        EXECUTE IMMEDIATE 'create sequence SEQ_TEST
                                            minvalue 1
                                            maxvalue 9999999999
                                            start with 1
                                            increment by 1
                                            nocache';
                      END IF;
                    END;");