Code Monkey home page Code Monkey logo

Comments (4)

anlaital-oura avatar anlaital-oura commented on June 11, 2024 1

I am closing the issue as I figured out the root cause. GRDB is working flawlessly (as expected) and we had some incorrect code that was deleting these events accidentally when running against the simulator target. I apologize for wasting your time but still thank you for pointing me to the recommended approach to doing such migrations. 😭

from grdb.swift.

groue avatar groue commented on June 11, 2024

Hello @anlaital-oura,

This is weird indeed, because well those GRDB statements translate into plain ALTER TABLE, CREATE TABLE, etc, without anything fancy. You can activate tracing to see them:

migrator.registerMigration("some_migration") { db in
    #warning("TODO: remove debugging SQL trace")
    db.trace { print("SQL> \($0)") }
    
    // Proceed with the rest of the migration
    ...
}

My only concern with your code is that it does not precisely follow the SQLite instructions for performing such changes to the database schema. The GRDB documentation translates it into Swift in the Defining the Database Schema from a Migration chapter.

According to those instructions, I would expect to read instead:

migrator.registerMigration("some_migration") { db in
    // Create a new table with a fixed schema.
    try db.create(table: "new_some_table") { t in
        ...
    }

    // Transfer values.
    // ⚠️ This SQL statement assumes that both tables have the same columns.
    try db.execute(sql: "INSERT INTO new_some_table SELECT * FROM some_table")
    
    // Replace old table with the new one.
    try db.drop(table: "some_table")
    try db.rename(table: "new_some_table", to: "some_table")
}

This will solve some potential problems (described by the linked SQLite documentation), but I do not know if this will fix the weird dataset changes. I hope this is just a glitch in your testing, or a wrong copy statement, and not an SQLite or GRDB bug.

Please report your findings!

from grdb.swift.

anlaital-oura avatar anlaital-oura commented on June 11, 2024

I modified the code to be exactly what you recommended. It did not help and still only 237,485 rows were inserted. Here are the SQL traces:

SQL> CREATE TABLE "new_ringevent" ("timestamp" INTEGER, "event" BLOB, "type" INTEGER, PRIMARY KEY ("timestamp", "type"))
SQL> INSERT INTO new_ringevent SELECT * FROM ringevent
SQL> DROP TABLE "ringevent"
SQL> ALTER TABLE "new_ringevent" RENAME TO "ringevent"
SQL> INSERT INTO grdb_migrations (identifier) VALUES (?)
SQL> PRAGMA foreign_key_check
SQL> COMMIT TRANSACTION
SQL> PRAGMA foreign_keys = ON
SQL> BEGIN DEFERRED TRANSACTION
SQL> DELETE FROM "ringevent" WHERE "timestamp" < ?
SQL> COMMIT TRANSACTION

I have verified that this happens also when no primary keys constraints are used in the new table. It also seems that the number of rows copied varies a little bit, for example just now I got 237,406 rows inserted. The original data set remained the same at 399,958 rows.

from grdb.swift.

groue avatar groue commented on June 11, 2024

😅 Don't worry! You had a good idea opening the issue anyway!

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.