sqflite_migrations 0.1.2

sqflite_migrations #

Migrations manager for sqflite.

  • Create migrations
  • Run migrations
  • Rollback migrations

Getting Started #

In your flutter project add the dependency:

dependencies:
  ...
  sqflite_migrations: ^0.1.0 

Usage example #

Import sqflite_migrations.dart

import 'package:sqflite_migrations/sqflite_migrations.dart';

Creating a migration #

Create a new instance of the Migration class you imported from the package.

Its attributes are:

  • The migration's name, which should be unique.
  • An up method that must return the query you want to run when the migration is run.
  • A down method that returns the query you want to run when the migration is rolledback.

Example:

Migration createUsersTable = new Migration(
  'create-users-table',
  () => '''
    CREATE TABLE users( 
      id INT PRIMARY KEY,
      name TEXT NOT NULL
    );
  ''',
  () => 'DROP TABLE users;',
);

Running migrations #

You will run every migration in your list that was not ran already.

Assuming you want to run migrations only when your database is updated, you should run the runMigrations method on the onUpgrade method of sqflite.

It accepts as parameters:

  • Your current db of type Database.
  • An int with the new version.
  • Your migrations list of type List<Migration>.

Example:

  onUpgrade(Database db, int previousVersion, int newVersion) {
    runMigrations(db, newVersion, migrations);
  }

And then you pass this method when you create the database:

 Database db = await openDatabase(path,
        version: _databaseVersion,
        onCreate: _onCreate,
        onUpgrade: onUpgrade);

Undoing migrations #

This functionality will undo every migrations that was implemented in a version above the current one.

Undoing migrations works the other way around of running migrations: you call the undoMigrations method in the method passed to the onDowngrade attribute.

It's parameters are:

  • The current db of type Database.
  • An int with the new version.
  • Your migrations list of type List<Migration>.

Example:

  onDowngrade(Database db, int previousVersion, int newVersion) {
    undoMigrations(db, newVersion, migrations);
  }

In case you passed all the parameters the openDatabase function should look like this:

    Database db = await openDatabase(path,
        version: _databaseVersion,
        onCreate: _onCreate,
        onUpgrade: onUpgrade,
        onDowngrade: onDowngrade);

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  sqflite_migrations: ^0.1.2

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:sqflite_migrations/sqflite_migrations.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
37
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
50
Overall:
Weighted score of the above. [more]
58
Learn more about scoring.

We analyzed this package on Nov 15, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.6.0
  • pana: 0.12.21

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health issues and suggestions

Document public APIs. (-1 points)

21 out of 21 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Maintenance issues and suggestions

Provide a file named CHANGELOG.md. (-20 points)

Changelog entries help developers follow the progress of your package. See the example generated by stagehand.

The package description is too short. (-20 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and sqflite_migrations.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
sqflite ^1.1.6 1.1.7+2
Transitive dependencies
collection 1.14.11 1.14.12
flutter 0.0.0
meta 1.1.7 1.1.8
path 1.6.4
sky_engine 0.0.99
synchronized 2.1.0+1
typed_data 1.1.6
vector_math 2.0.8