SQLiteMigrationManager.swift
Migration manager for SQLite.swift
Install / Use
/learn @garriguv/SQLiteMigrationManager.swiftREADME
SQLiteMigrationManager.swift
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.sql2_add_new_table.sql3_add-new-table.sql4_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
Swift Package Manager
SQLiteMigrationManager.swift is available 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.8.2")
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"
Contributing
- Fork it ( https://github.com/garriguv/SQLiteMigrationManager.swift/fork )
- Install the development dependencies (
bin/setup) - Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request
- You're awesome! :+1:
Author
Vincent Garrigues, vincent@garriguv.io
License
SQLiteMigrationManager.swift is available under the MIT license. See the LICENSE file for more info.
Related Skills
feishu-drive
339.3k|
things-mac
339.3kManage Things 3 via the `things` CLI on macOS (add/update projects+todos via URL scheme; read/search/list from the local Things database)
clawhub
339.3kUse the ClawHub CLI to search, install, update, and publish agent skills from clawhub.com
yu-ai-agent
2.0k编程导航 2025 年 AI 开发实战新项目,基于 Spring Boot 3 + Java 21 + Spring AI 构建 AI 恋爱大师应用和 ReAct 模式自主规划智能体YuManus,覆盖 AI 大模型接入、Spring AI 核心特性、Prompt 工程和优化、RAG 检索增强、向量数据库、Tool Calling 工具调用、MCP 模型上下文协议、AI Agent 开发(Manas Java 实现)、Cursor AI 工具等核心知识。用一套教程将程序员必知必会的 AI 技术一网打尽,帮你成为 AI 时代企业的香饽饽,给你的简历和求职大幅增加竞争力。
