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

Migration: Table already exists #70

Open
gngrwzrd opened this issue Apr 9, 2020 · 0 comments
Open

Migration: Table already exists #70

gngrwzrd opened this issue Apr 9, 2020 · 0 comments

Comments

@gngrwzrd
Copy link

gngrwzrd commented Apr 9, 2020

First thank you for this very simple to use library.

I'm trying to use Migrations. It's very easy to setup. BUT one thing I cannot figure out is why exceptions are thrown for the table existing already. The migrations don't run since there's an exception.

I've setup a schema like this:

func schema() -> Schema {
        return Schema(identifier: "workouts") { (schemaBuilder:SchemaBuilder) in
            schemaBuilder.version(0) { (v1:VersionBuilder) in
                
                //WORKOUTS
                v1.createTable(workoutsTableName) { (workouts:TableBuilder) in
                    workouts.primaryKey("id", autoincrement: true)
                    workouts.column("title", .Text)
                    workouts.column("notes", .Text)
                    workouts.column("timerType", type: .Integer)
                    workouts.column("countdown", type: .Real)
                    workouts.column("rounds", type: .Integer)
                    workouts.column("work", type: .Real)
                    workouts.column("rest", type: .Real)
                    workouts.column("asPerscribed", type: .Integer)
                    workouts.column("asPerscribedPlus", type: .Integer)
                    workouts.column("completedRounds", type: .Integer)
                    workouts.column("completedReps", type: .Integer)
                    workouts.column("startDate", type: .Real)
                    workouts.column("endDate", type: .Real)
                }
                
                //LAPS
                v1.createTable(workoutLapsTableName) { (workoutLaps:TableBuilder) in
                    workoutLaps.primaryKey("id", autoincrement: true)
                    workoutLaps.column("workoutId", type: .Integer)
                    workoutLaps.column("lap", type: .Real)
                }
            }
            
            //ADD TOTAL DURATION COLUMN.
            schemaBuilder.version(1) { (v2:VersionBuilder) in

                v2.alterTable("workouts") { (workouts:TableAlterer) in
                    workouts.addColumn("totalDuration", .Real)
                }

            }
            
        }
    }

And my init code is this:

func initDatabase() throws {
        let paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
        guard paths.count > 0 else {
            throw GTDBError.noDocumentPath
        }
        let docPathURL = URL(fileURLWithPath: paths[0])
        let dbPath = docPathURL.appendingPathComponent(sqliteFileName)
        do {
            try db = Database(path: dbPath.absoluteString)
        } catch {
            throw GTDBError.noDatabaseFile
        }
        do {
            try _ = self.schema().migrate(db)
        } catch let error as NSError where error.domain == sqliteErrorDomain {
            var continueThrow = true
            
            if error.code == 1 { //table exists?
                continueThrow = false
            }
            
            if continueThrow {
                print(error)
                throw GTDBError.sqliteError
            }
            
        } catch let error {
            print(error)
            throw GTDBError.otherError
        }
    }

The issue I'm hitting is exceptions thrown for the "workouts" table already existing, which causes the migrations to not run:

[logging] table "workouts" already exists in "CREATE TABLE "workouts" ( "id" INTEGER PRIMARY KEY AUTOINCREMENT,"title" TEXT,"notes" TEXT,"timerType" INTEGER,"countdown" REAL,"rounds" INTEGER,"work" REAL,"rest" REAL,"asPerscribed" INTEGER,"asPerscribedPlus" INTEGER,"completedRounds" INTEGER,"completedReps" INTEGER,"startDate" REAL,"endDate" REAL )"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant