sqflite_migrate 0.4.0 copy "sqflite_migrate: ^0.4.0" to clipboard
sqflite_migrate: ^0.4.0 copied to clipboard

SQFLite migration tool

sqflite_migrate #

GitHub Actions Workflow Status

example

Proper, and traditional way to do migrations in sqflite

  • Supports using plain sql syntax to create migration files
  • A working CLI with all the necessary commands
  • A programmatic approach for more fine grained control (not recommended for production)

Usage #

dart pub global activate sqflitemigrate

This will give you access to global cli from pub.dev website

Create your migrations folder #

Create migrations folder wherever you think its most suitable for your app, and fill it with migration files

-- UP --
CREATE TABLE IF NOT EXISTS my_table (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  name TEXT NOT NULL
);

-- DOWN --
DROP TABLE my_table;

-- UP -- and -- DOWN -- are just plain sql comments required to separate the up and down queries

You can write as many queries as you want in the up and down sections, as long as the statements are separated by ; delimiter


-- UP --
CREATE TABLE IF NOT EXISTS my_table (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  name TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS my_table2 (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  name TEXT NOT NULL
);

-- DOWN --
DROP TABLE my_table;
DROP TABLE my_table2;

Naming format #

The migration files should be named in the following format

<version>_<description>.sql

Currently the version format is just a simple integer, that should be incremental, and sorted in ascending order that you want to run the migrations to run

In next releases we will add create command in the cli, with options to create it with a timestamp, or a version number

Running the migrations #

sqflite_migrate cli has the following commands

  • migrate --database DB_PATH --path MIGRATIONS_FOLDER_PATH [OPTIONS] - Runs the migrations
  • rollback --database DB_PATH --path MIGRATIONS_FOLDER_PATH [OPTIONS] - Rolls back the migrations
  • status --database DB_PATH --path MIGRATIONS_FOLDER_PATH - Shows the current status of the migrations
  • clear --database DB_PATH - Clears all the records from migrations table
  • delete-db --database DB_PATH - Deletes the database file, and all the migrations records

Programmatic usage #

You can also use the MigrationRunner class to run the migrations programmatically, but it is not recommended to use it in production, as it is not properly tested for this approach

import 'package:sqflite_migrate/sqflite_migrate.dart';

void main() async {
  final migrator = MigrationRunner.init(
    database: 'path/to/db',
    migrationsPath: 'path/to/migrations',
  );

  await migrator.migrate()..rollback();
}

Coming up #

  • Add create command in the cli
  • Add timestamp and version options in the create command
  • Add --force option in the migrate command to force run all the migrations
  • Add --force option in the rollback command to force rollback all the migrations
1
likes
130
points
41
downloads

Publisher

unverified uploader

Weekly Downloads

SQFLite migration tool

Repository (GitHub)
View/report issues

Documentation

API reference

License

GPL-3.0 (license)

Dependencies

ansi, ansi_escapes, ansicolor, args, cli_util, path, sqflite_common_ffi

More

Packages that depend on sqflite_migrate