sqflite_auto_migration 1.0.2 copy "sqflite_auto_migration: ^1.0.2" to clipboard
sqflite_auto_migration: ^1.0.2 copied to clipboard

Use source_gen and build_runner to auto generate migration function for sqlite by using override migration class and annotated with version number

sqflite_auto_migration #

use code-gen to generate sqflite OpenDatabaseOptions

Getting Started #

install #

Add to you pubspec.yaml

dependencies:
    sqflite_auto_migration:

dev_dependencies:
    build_runner: '>=0.9.1'

Write a migration class like

import 'package:sqflite_auto_migration/sqflite_auto_migration.dart';
import 'package:sqflite/sqflite.dart';

@MigrationVersion(1)
class TestMigrationOne extends MigrationBase {
  @override
  Future down(Batch batch) async {
    batch.execute('DROP TABLE IF EXISTS Company');
    batch.execute('''CREATE TABLE Company (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT)''');
  }

  @override
  Future up(Batch batch) async {
    batch.execute('DROP TABLE Company');
    return null;
  }
}

generate migration function file #

flutter packages pub run build_runner build --delete-conflicting-outputs

And you will see in you lib/generated/migration_function.dart

OpenDatabaseOptions get options => OpenDatabaseOptions(
    //latest version define in the migrations
    version: 3,
    onCreate: (db, version) async {
      var batch = db.batch();
      for (var m in migrations.entries) {
        await m.value.up(batch);
      }
    },
    onUpgrade: (db, oldVersion, newVersion) async {
      var batch = db.batch();
      for (var m in migrations.entries) {
        if (m.key > oldVersion && m.key <= newVersion) await m.value.up(batch);
      }
    },
    onDowngrade: (db, oldVersion, newVersion) async {
      var batch = db.batch();
      for (var m in migrations.entries) {
        if (m.key <= oldVersion && m.key > newVersion)
          await m.value.down(batch);
      }
    });

Use this option to open your sqlite

This project is highly inspired by annotation_route

0
likes
30
pub points
0%
popularity

Publisher

unverified uploader

Use source_gen and build_runner to auto generate migration function for sqlite by using override migration class and annotated with version number

Repository (GitHub)
View/report issues

License

MIT (LICENSE)

Dependencies

build, flutter, mustache4dart, source_gen, sqflite

More

Packages that depend on sqflite_auto_migration