Swiftpack.co - Package - garriguv/SQLiteMigrationManager.swift

SQLiteMigrationManager.swift

CI Status Version License Platform Carthage compatible

SQLiteMigrationManager.swift is a schema management system for SQLite.swift. It is heavily inspired by FMDBMigrationManager.

Concept

SQLiteMigrationManager.swift works by introducing a schema_migrations table into the database:

CREATE TABLE "schema_migrations" (
  "version" INTEGER NOT NULL UNIQUE
);

Each row in schema_migrations corresponds to a single migration that has been applied and represents a unique version of the schema. This schema supports any versioning scheme that is based on integers, but it is recommended that you utilize an integer that encodes a timestamp.

Usage

Have a look at the example project.

Creating the Migrations Table

let db = try Connection("path/to/store.sqlite")

let manager = SQLiteMigrationManager(db: self.db)

if !manager.hasMigrationsTable() {
  try manager.createMigrationsTable()
}

Creating a SQL File Migrations

Create a migration file in your migration bundle:

$ touch "`ruby -e "puts Time.now.strftime('%Y%m%d%H%M%S').to_i"`"_name.sql

SQLiteMigrationManager.swift will only recognize filenames of the form <version>_<name>.sql. The following filenames are valid:

  • 1.sql
  • 2_add_new_table.sql
  • 3_add-new-table.sql
  • 4_add new table.sql

Creating a Swift Migration

Swift based migrations can be implemented by conforming to the Migration protocol:

import Foundation
import SQLiteMigrationManager
import SQLite

struct SwiftMigration: Migration {
  var version: Int64 = 2016_01_19_13_12_06

  func migrateDatabase(db: Connection) throws {
    // perform the migration here
  }
}

Migrating a Database

let db = try Connection("path/to/store.sqlite")

let manager = SQLiteMigrationManager(db: self.db, migrations: [ SwiftMigration() ], bundle: NSBundle.mainBundle())

if manager.needsMigration() {
  try manager.migrateDatabase()
}

Inspecting the Schema State

let db = try Connection("path/to/store.sqlite")

let manager = SQLiteMigrationManager(db: self.db, migrations: [ SwiftMigration() ], bundle: NSBundle.mainBundle())

print("hasMigrationsTable() \(manager.hasMigrationsTable())")
print("currentVersion()     \(manager.currentVersion())")
print("originVersion()      \(manager.originVersion())")
print("appliedVersions()    \(manager.appliedVersions())")
print("pendingMigrations()  \(manager.pendingMigrations())")
print("needsMigration()     \(manager.needsMigration())")

Installation

CocoaPods

SQLiteMigrationManager.swift is available through CocoaPods. To install it, add the following line to your Podfile:

pod "SQLiteMigrationManager.swift"

Carthage

SQLiteMigrationManager.swift is available through Carthage. To install it, add the following line to your Cartfile:

github "garriguv/SQLiteMigrationManager.swift"

Swift Package Manager

SQLiteMigrationManager.swift is availabel through Swift Package Manager. To install it, add the following dependency to your Package.swift file:

.package(url: "https://github.com/garriguv/SQLiteMigrationManager.swift.git", from: "0.6.0")

Contributing

  1. Fork it ( https://github.com/garriguv/SQLiteMigrationManager.swift/fork )
  2. Install the development dependencies (bin/setup)
  3. Create your feature branch (git checkout -b my-new-feature)
  4. Commit your changes (git commit -am 'Add some feature')
  5. Push to the branch (git push origin my-new-feature)
  6. Create a new Pull Request
  7. You're awesome! :+1:

Author

Vincent Garrigues, vincent.garrigues@gmail.com

License

SQLiteMigrationManager.swift is available under the MIT license. See the LICENSE file for more info.

Github

link
Stars: 114
Help us keep the lights on

Used By

Total: 0

Releases

0.6.0 - Apr 24, 2019

  • Update SQLite.swift to 0.11.6.
  • Support for Xcode 10.2.
  • Swift 5.0 migration.

0.5.0 - Feb 2, 2019

  • Add macOS support (@nickgzzjr & @misteral).
  • Reorganize project to support additional platforms in the future.

0.4.0 - Nov 13, 2018

  • Swift 4.2 support.
  • Upgrage example app.
  • Swift Package Manager support.

0.3.1 - Nov 13, 2018

  • Set APPLICATION_EXTENSION_API_ONLY to YES

0.3.0 - Nov 23, 2017

Add support for Swift 4.0