Code Monkey home page Code Monkey logo

Comments (3)

groue avatar groue commented on June 2, 2024

Hello @zaplitny,

It looks like this could work, yes. But since grdb_migrations is an internal table, not even mentioned in the documentation, I would not recommend messing with it directly. Future GRDB versions may modify the grdb_migrations schema, and your code would stop working.

Instead, you can rely of the fact that GRDB never modifies user_version by itself. So if it is not zero, you know that the app opens a database that was previously handled by FMDB.

You can thus register one GRDB migration per FMDB migration:

var migrator = ...

migrator.registerMigration("fmdb-1") { db in
    guard try db.userVersion() < 1 else {
        // Migration already applied by FMDB
        return
    }

    // apply script 1
}

migrator.registerMigration("fmdb-2") { db in
    guard try db.userVersion() < 2 else {
        // Migration already applied by FMDB
        return
    }
    
    // apply script 2
}

...

migrator.registerMigration("first-post-fmdb-migration") { db in
    ...
}

Also possible: just register one GRDB migration for all FMDB migrations:

var migrator = ...

migrator.registerMigration("fmdb") { db in
    let version = try db.userVersion()
    
    if version < 1 {
        // apply script 1
    }
    
    if version < 2 {
        // apply script 2
    }

    ...
}

migrator.registerMigration("first-post-fmdb-migration") { db in
    ...
}

Both sample codes use this little Database extension:

extension Database {
    func userVersion() -> Int {
        // Force-unwrap is ok: user_version is always returned.
        try Int.fetchOne(db, sql: "PRAGMA user_version")!
    }
}

from grdb.swift.

zaplitny avatar zaplitny commented on June 2, 2024

Thanks a lot, @groue . It's much better solution

from grdb.swift.

groue avatar groue commented on June 2, 2024

Cool! Happy GRDB :-)

from grdb.swift.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.