Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to clean schema that contain a Computed Column Defined with a User-Defined Function #1193

Closed
mdaquel opened this issue Feb 2, 2016 · 2 comments

Comments

@mdaquel
Copy link

@mdaquel mdaquel commented Feb 2, 2016

When you try to drop all objects in the configured schema using Flyway command-line tool, it will fail and output an exception Unable to clean schema [dbo].

Similar issue has been (reported and closed) [https://github.com//issues/472]

Cause

This issue will occur as observed in the following conditions:

Steps to Reproduce

  1. Download the Flyway Command-Line tool for Windows.

  2. Create a Test Microsoft SQL server database.

  3. Configure Flyway to use the Test database created in previous step.

  4. Create the migration in /sql directory called V1__Create initial schema.sql

    CREATE FUNCTION [dbo].[svfCommandName]
    (
        @xml Xml
    )
    RETURNS nvarchar(50)
    WITH SCHEMABINDING
    AS
    BEGIN
     return  @xml.value('local-name(/*[1])','nvarchar(max)')
    END
    GO
    
    CREATE TABLE [dbo].[EventStream](
     [EventId] [uniqueidentifier] NOT NULL,
    [EventData] [xml] NULL,
    [EventDate] [datetime] NOT NULL,
    [EventState] [int] NULL,
    [CommandName]  AS ([dbo].[svfCommandName]([EventData])),
    CONSTRAINT [PK_EventStream] PRIMARY KEY CLUSTERED
    (
     [EventId] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    
    GO
    
    ALTER TABLE [dbo].[EventStream] ADD  CONSTRAINT [DF_EventStream_EventDate]  DEFAULT (getdate()) FOR [EventDate]
    GO

    Note: the schema above has an EventStream table that contain a computed column defined with a _svfCommandName_ user-defined function.

  5. Execute Flyway to migrate our database.

    flyway migrate
    

    If all went well, you should see the following output

    Flyway 3.2.1 by Boxfuse
    
    Database: jdbc:jtds:sqlserver://localhost/Test (Microsoft SQL Server 10.0)
    Validated 1 migration (execution time 00:00.010s)
    Creating Metadata table: [dbo].[schema_version]
    Current version of schema [dbo]: << Empty Schema >>
    Migrating schema [dbo] to version 1 - Create initial schema.sql
    Successfully applied 1 migration to schema [dbo] (execution time 00:00.045s).
    
  6. Execute Flyway to prints the details and status information about all the migration

    flyway info
    

    Just to confirm the migration is a success.

    Flyway 3.2.1 by Boxfuse
    
    Database: jdbc:jtds:sqlserver://localhost/Test (Microsoft SQL Server 10.0)
    
    +---------+---------------------------+---------------------+---------+
    | Version | Description               | Installed on        | State   |
    +---------+---------------------------+---------------------+---------+
    | 1       | Create initial schema.sql | 2016-02-02 22:21:04 | Success |
    +---------+---------------------------+---------------------+---------+
    
  7. Finally, try to drop all objects in the configured schema by execute Flyway clean adding -X to the argument list to also print the debug output.

    flyway clean -X
    

    Boom! you should see the output below with error.

    Flyway 3.2.1 by Boxfuse
    
    Database: jdbc:jtds:sqlserver://localhost/Test (Microsoft SQL Server 10.0)
    DEBUG: DDL Transactions Supported: true
    DEBUG: Schema: dbo
    DEBUG: Scanning for filesystem resources at 'sql' (Prefix: '', Suffix: '.sql')
    DEBUG: Scanning for resources in path: sql (sql)
    DEBUG: Found filesystem resource: sql\V1__Create initial schema.sql.sql
    DEBUG: Cleaning schema [dbo] ...
    DEBUG: Rolling back transaction...
    DEBUG: Transaction rolled back
    ERROR: Unexpected error
    org.flywaydb.core.api.FlywayException: Unable to clean schema [dbo]
            at org.flywaydb.core.internal.dbsupport.Schema.clean(Schema.java:150)
            at org.flywaydb.core.internal.command.DbClean$4.doInTransaction(DbClean.java:154)
            at org.flywaydb.core.internal.command.DbClean$4.doInTransaction(DbClean.java:152)
            at org.flywaydb.core.internal.util.jdbc.TransactionTemplate.execute(TransactionTemplate.java:72)
            at org.flywaydb.core.internal.command.DbClean.cleanSchema(DbClean.java:152)
            at org.flywaydb.core.internal.command.DbClean.clean(DbClean.java:106)
            at org.flywaydb.core.Flyway$3.execute(Flyway.java:1125)
            at org.flywaydb.core.Flyway$3.execute(Flyway.java:1121)
            at org.flywaydb.core.Flyway.execute(Flyway.java:1418)
            at org.flywaydb.core.Flyway.clean(Flyway.java:1121)
            at org.flywaydb.commandline.Main.executeOperation(Main.java:115)
            at org.flywaydb.commandline.Main.main(Main.java:91)
    Caused by: java.sql.SQLException: Cannot DROP FUNCTION 'dbo.svfCommandName' because it is being referenced by object 'EventStream'.
            at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:372)
            at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2988)
            at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2421)
            at net.sourceforge.jtds.jdbc.TdsCore.getMoreResults(TdsCore.java:671)
            at net.sourceforge.jtds.jdbc.JtdsStatement.processResults(JtdsStatement.java:613)
            at net.sourceforge.jtds.jdbc.JtdsStatement.executeSQL(JtdsStatement.java:572)
            at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.execute(JtdsPreparedStatement.java:784)
            at org.flywaydb.core.internal.dbsupport.JdbcTemplate.execute(JdbcTemplate.java:219)
            at org.flywaydb.core.internal.dbsupport.sqlserver.SQLServerSchema.doClean(SQLServerSchema.java:84)
            at org.flywaydb.core.internal.dbsupport.Schema.clean(Schema.java:148)
            ... 11 more
    
@axelfontaine
Copy link
Contributor

@axelfontaine axelfontaine commented Feb 2, 2016

Awesome bug report. Thanks! Fixed.

axelfontaine added a commit to flyway/flywaydb.org that referenced this issue Feb 2, 2016
@mdaquel
Copy link
Author

@mdaquel mdaquel commented Feb 2, 2016

Wow fixed already! 👍 Much appreciated. Cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.