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

mdaquel opened this issue Feb 2, 2016 · 2 comments


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) []


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)
     return  @xml.value('local-name(/*[1])','nvarchar(max)')
    CREATE TABLE [dbo].[EventStream](
     [EventId] [uniqueidentifier] NOT NULL,
    [EventData] [xml] NULL,
    [EventDate] [datetime] NOT NULL,
    [EventState] [int] NULL,
    [CommandName]  AS ([dbo].[svfCommandName]([EventData])),
     [EventId] ASC
    ) ON [PRIMARY]
    ALTER TABLE [dbo].[EventStream] ADD  CONSTRAINT [DF_EventStream_EventDate]  DEFAULT (getdate()) FOR [EventDate]

    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(
            at org.flywaydb.core.internal.command.DbClean$4.doInTransaction(
            at org.flywaydb.core.internal.command.DbClean$4.doInTransaction(
            at org.flywaydb.core.internal.util.jdbc.TransactionTemplate.execute(
            at org.flywaydb.core.internal.command.DbClean.cleanSchema(
            at org.flywaydb.core.internal.command.DbClean.clean(
            at org.flywaydb.core.Flyway$3.execute(
            at org.flywaydb.core.Flyway$3.execute(
            at org.flywaydb.core.Flyway.execute(
            at org.flywaydb.core.Flyway.clean(
            at org.flywaydb.commandline.Main.executeOperation(
            at org.flywaydb.commandline.Main.main(
    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(
            at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(
            at net.sourceforge.jtds.jdbc.TdsCore.nextToken(
            at net.sourceforge.jtds.jdbc.TdsCore.getMoreResults(
            at net.sourceforge.jtds.jdbc.JtdsStatement.processResults(
            at net.sourceforge.jtds.jdbc.JtdsStatement.executeSQL(
            at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.execute(
            at org.flywaydb.core.internal.dbsupport.JdbcTemplate.execute(
            at org.flywaydb.core.internal.dbsupport.sqlserver.SQLServerSchema.doClean(
            at org.flywaydb.core.internal.dbsupport.Schema.clean(
            ... 11 more
Copy link

@axelfontaine axelfontaine commented Feb 2, 2016

Awesome bug report. Thanks! Fixed.

axelfontaine added a commit to flyway/ that referenced this issue Feb 2, 2016
Copy link

@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
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.