SQLiteMigrationManager.swift
SQLiteMigrationManager.swift 是一个用于 SQLite.swift 的模式管理系统。它受到了 FMDBMigrationManager 的极大启发。
概念
SQLiteMigrationManager.swift 通过将 schema_migrations
表引入数据库
CREATE TABLE "schema_migrations" (
"version" INTEGER NOT NULL UNIQUE
);
schema_migrations
表中的每一行都对应着一个已应用的迁移,表示了模式的一个唯一版本。此模式支持任何基于整数的版本控制方案,但建议使用编码时间戳的整数。
用法
请参阅 示例项目。
创建迁移表
let db = try Connection("path/to/store.sqlite")
let manager = SQLiteMigrationManager(db: self.db)
if !manager.hasMigrationsTable() {
try manager.createMigrationsTable()
}
创建 SQL 文件迁移
在你的迁移包中创建一个迁移文件
$ touch "`ruby -e "puts Time.now.strftime('%Y%m%d%H%M%S').to_i"`"_name.sql
SQLiteMigrationManager.swift 将只识别形式为 <version>_<name>.sql
的文件名。以下文件名是有效的
1.sql
2_add_new_table.sql
3_add-new-table.sql
4_add new table.sql
创建 Swift 迁移
可以通过实现 Migration
协议来创建基于 Swift 的迁移
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
}
}
迁移数据库
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()
}
检查模式状态
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())")
安装
Swift 包管理器
SQLiteMigrationManager.swift 可通过 Swift 包管理器 获取。要安装它,请在您的 Package.swift
文件中添加以下依赖项:
.package(url: "https://github.com/garriguv/SQLiteMigrationManager.swift.git", from: "0.8.1")
CocoaPods
SQLiteMigrationManager.swift 可通过 CocoaPods 获取。安装它,请在您的 Podfile
中添加以下行:
pod "SQLiteMigrationManager.swift"
Carthage
SQLiteMigrationManager.swift 可通过 Carthage 获取。安装它,请在您的 Cartfile
中添加以下行:
github "garriguv/SQLiteMigrationManager.swift"
贡献
- 分叉它( https://github.com/garriguv/SQLiteMigrationManager.swift/fork )
- 安装开发依赖(
bin/setup
) - 创建您的功能分支(
git checkout -b my-new-feature
) - 提交您的更改(
git commit -am 'Add some feature'
) - 推送到分支(
git push origin my-new-feature
) - 创建一个新的Pull Request
- 您做得很好!
👍
作者
文森特·加里格斯,[email protected]
许可证
SQLiteMigrationManager.swift 密码库可用MIT许可证。有关更多信息,请参阅LICENSE文件。