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

"DROP VIEW IF EXISTS" not working #267

Closed
devpolant opened this issue Oct 30, 2017 · 4 comments
Closed

"DROP VIEW IF EXISTS" not working #267

devpolant opened this issue Oct 30, 2017 · 4 comments
Labels

Comments

@devpolant
Copy link

devpolant commented Oct 30, 2017

I try to get a list of all views in my database:

var config = Configuration()
            config.trace = { print($0) }

            let queue = try DatabaseQueue(path: downloadedDatabaseURL.path, configuration: config)
            try queue.inDatabase { db in
                let rows = try Row.fetchAll(db, """
                    SELECT name, type, sql FROM sqlite_master
                    WHERE type != 'index' AND name NOT IN ('sqlite_sequence') AND name NOT LIKE '%old%'
                    ORDER BY type, name
                    """
                )
                var views = [Row]()
                for row in rows {
                    let type: String = row["type"]
                    switch type {
                    case "view":
                        views.append(row)
                    default:
                        break
                    }
                }
                for view in views {
                    try updateView(view, in: db)
                }

But I can't drop the view:

private func updateView(_ viewInfo: Row, in db: Database) throws {
        let name: String = viewInfo["name"]
        try db.execute("DROP VIEW IF EXISTS \(name);")
       // ...
}

In trace I see the following SQL:
DROP VIEW IF EXISTS EMAIL_TABLE;

The error did not throw, but the view actually did not delete from the database file on the disk.

But then when I try to create a view with this name (EMAIL_TABLE), I receive an error:
[logging] table EMAIL_TABLE already exists

Maybe I missing something?
GRDB version 2.1.0
Tested on simulator and iPhone 5 (iOS 10.3.3)

Thanks!

@groue
Copy link
Owner

groue commented Oct 30, 2017

Hello @polant,

No, you are not missing anything, and you have found a bug. It will be fixed shortly: stay tuned.

You may wonder how it is possible that DROP VIEW statements could fail. I would raise an eyebrow as well...

Well, in order to notify transaction observers of deleted rows, GRDB has to prevent the SQLite truncate optimization. This requires a funny little dance with SQLite Compile-Time Authorization Callbacks. When misused, SQLite doesn't drop table, and views.

The missing tests have been added in a0e3d4a: there won't be any regression once the fix has shipped.

Thanks for the report!

@groue groue added the bug label Oct 30, 2017
@devpolant
Copy link
Author

Thanks!

@groue
Copy link
Owner

groue commented Oct 30, 2017

The bug is fixed. Until a new version is shipped, you can use the development branch.

@groue
Copy link
Owner

groue commented Oct 31, 2017

Version 2.2.0 has shipped with the fix. Happy GRDB!

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

No branches or pull requests

2 participants