IOS database upgrade FMDBMigrationManager

FMDBMigrationManager is a combination of FMDB and the use of a third party, you can record the database version number and upgrade the database database operations.
first to integrate FMDB and FMDBMigrationManager, it is recommended to use cocoapods, here is no longer say.
according to the official document interpretation, there are two ways to upgrade, we explain one by one.
said the first, add the file to record version and upgrade operation, create a new project, and create a database, it is what we are going to upgrade the database.
associates the database with our FMDBMigrationManager

FMDBMigrationManager * manager=[FMDBMigrationManager managerWithDatabaseAtPath:DBPath migrationsBundle:[NSBundle mainBundle]]; //DBPath is to upgrade the database address / / [NSBundle mainBundle] is stored in a database upgrade file location according to their file location is below what will say the upgrade file

The following version of the table will be created, this table will be saved in our database, the database version number records, and upgrade our database to the highest version.

BOOL resultState=NO; NSError * error=nil; if ((manager.hasMigrationsTable) {resultState=[manager createMigrationsTable:& error];})

Execute the statement, and then go to our database view, you will find more than a table schema_migrations

IOS database upgrade FMDBMigrationManager
database.Png

this table is used to store the version number, the current table version number (version) is 0.

The following is the most important update statement

BOOL resultState=NO; resultState=[manager progress:nil error:& error]; //UINT64_MAX represents the upgrade to the highest version of migrateDatabaseToVersion:UINT64_MAX

The overall upgrade function is

FMDBMigrationManager * manager=[FMDBMigrationManager managerWithDatabaseAtPath:DBPath migrationsBundle:[NSBundle mainBundle]]; BOOL resultState=NO; NSError * error=nil; if (! Manager.hasMigrationsTable) {resultState=[manager createMigrationsTable:& error] resultState=[manager;} migrateDatabaseToVersion:UINT64_MAX progress:nil error:& error];

At this point, you have not added the upgrade file, so there is nothing to do with this code database.
upgrade file, is a number of SQL files, in which to write some statements on the database operation

IOS database upgrade FMDBMigrationManager
SQL file.Png

file format is fixed (digital) _ (.Sql description language), the numbers in front of the so-called official version, recommend the use of timestamps, you can also use the 1, 2, 3, 4, 5…… Upgrade, to maintain a monotonic increase can be.
file to write to the database to do the operation

IOS database upgrade FMDBMigrationManager
SQL file content

then drag the file into the project

IOS database upgrade FMDBMigrationManager
drag works

FMDBMigrationManager will be based on the creation of the NSBundle into the SQL file to find their own, compared to the version number. After adding the SQL file, restart the project, and then run the upgrade code to view the database

IOS database upgrade FMDBMigrationManager
upgrade database.Png

found that the addition of a User table, and then add a new database field files (commonly used in the project is to upgrade the operation of the database field)

IOS database upgrade FMDBMigrationManager
add field file.Png

2_AddEmail.sql content

IOS database upgrade FMDBMigrationManager
2_AddEmail file content.Png

restart the project to run the upgrade code to view the database

IOS database upgrade FMDBMigrationManager
database.Png

found that email has been added to the

IOS database upgrade FMDBMigrationManager
view version number.Png

storage version number has been recorded to 2, and then want to upgrade, then add the 3_*.sql file to the project.

The following second ways to upgrade the use of custom class form.
is the first method, each upgrade to establish a file, and most of the time, file write only a word, emotionally difficult to accept, so we introduce a relatively simple way.
first defines a new class: Migration

/ / / / Migration.h / / FMDB / / Created / / by Alan on 16/5/23. Copyright alan. All rights of 2016 / / reserved. / / #import < Foundation/Foundation.h> #import "FMDBMigrationManager.h" @interface Migration: NSObject< FMDBMigrating> @ property (nonatomic, readonly) NSString *name; @property (nonatomic, readonly) uint64_t version; (BOOL) - migrateDatabase: (FMDatabase * database error: (out) NSError *__autoreleasing * error @end);

Follow the FMDBMigrating protocol, and the first way to drag the file the same way, name is the upgrade description, version is the version number, the last one method, the operation.
because name and version are read-only, so we want to customize a init method, describe the incoming version and update, update incoming best with an array of ways, because there may be more than one update statement.

/ / / / Migration.h / / FMDB / / Created / / by Alan on 16/5/23. Copyright alan. All rights of 2016 / / reserved. / / #import < Foundation/Foundation.h> #import "FMDBMigrationManager.h" @interface Migration: NSObject< FMDBMigrating> - (instancetype) initWithName: (NSString *) name andVersion: (uint64_t) version andExecuteUpdateArray: (NSArray * updateArray); / / @property (custom method nonatomic readonly, NSString *name); @property (nonatomic, readonly) uint64_t version; (BOOL) - migrateDatabase: (FMDatabase *) database error: (out NSError *__autoreleasing * error @end);
/ / / / Migration.m / / FMDB / / Created / / by Alan on 16/5/23. Copyright alan. All rights of 2016 / / reserved. / / #import "Migration.h" @interface (Migration) @property (nonatomic, copy) NSString * myName (nonatomic, assign); @property uint64_t myVersion; @property (nonatomic, strong) NSArray * updateArray @end @implementation (instancetype - Migration;) initWithName: (NSString *) name andVersion: (uint64_t) version andExecuteUpdateArray: (NSArray * updateArray) {if (self=[super init]) {_myName=name}; _myVersion=version; _updateArray=updateArray; return self;} - (NSString * name) {return _myName;} - (uint64_t) version {return _myVersion;} - (BOOL) migrateDatabase: (FMDatabase * database error: (OU) T NSError *__autoreleasing * error {for (NSString * updateStr in _updateArray) {[database executeUpdate:updateStr];} return YES;}

The method is also very simple, the custom class object can be added to the manager.

FMDBMigrationManager * manager=[FMDBMigrationManager managerWithDatabaseAtPath:DBPath migrationsBundle:[NSBundle mainBundle]]; Migration * migration_1=[[Migration alloc]initWithName:@ "USer andVersion:1 andExecuteUpdateArray:@[@" new form "create table User (name text, age integer)"; "Migration * migration_2=[[Migration alloc]initWithName:@" USer email andVersion:2 "new field" andExecuteUpdateArray:@[@ alter table User add email text "[manager"; addMigration:migration_1]; BOOL [manager addMigration:migration_2]; resultState=NO NSError; * error=nil; if (! Manager.hasMigrationsTable) {resultState=[manager createMigrationsTable:& error] resultState=[manager migrateDatabaseToVersion:UINT64_;} MAX progress:nil error:& error];

Later also want to upgrade, add a new custom object, pay attention to!!! Version number to keep increasing

FMDBMigrationManager * manager=[FMDBMigrationManager managerWithDatabaseAtPath:DBPath migrationsBundle:[NSBundle mainBundle]]; Migration * migration_1=[[Migration alloc]initWithName:@ "USer andVersion:1 andExecuteUpdateArray:@[@" new form "create table User (name text, age integer)"; "Migration * migration_2=[[Migration alloc]initWithName:@" USer email andVersion:2 "new field" andExecuteUpdateArray:@[@ alter table User add email text Migration * migration_3=[[Migration alloc]initWithName:@ ";" USer table add field "address andVersion:3 andExecuteUpdateArray:@[@" alter table User add address text "[manager"; addMigration:migration_1]; [manager addMigration:migration_2]; [manager addMigration:migration_3]; BOOL res UltState=NO; NSError * error=nil; if (! Manager.hasMigrationsTable) {resultState=[manager createMigrationsTable:& error] resultState=[manager;} migrateDatabaseToVersion:UINT64_MAX progress:nil error:& error];