Code Monkey home page Code Monkey logo

sqlitemigrations's Introduction

SQLiteMigrations

This library is in development stage. It does not yet implement every planned feature. Right now it only implements key features I needed for a project.

SQLiteMigrations is a .NET Portable Class Library (PCL) to enable database schema management for SQLitePCL.raw built on top of SQLitePCL.pretty.

This project was inspired by SQLiteMigrationManager.swift (from which I borrowed the README structure) and goose.

Concept

SQLiteMigrations works by introducing a __migrations table into the database:

CREATE TABLE __migrations (
  id INT NOT NULL
);

Each row in __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 tests.

Initializing the database

You would first have to implement the interface IMigrationsProvider in order to provide the migrations to the migrator. When you are done with that you can define your configuration instance.

var config = new Configuration
{
  DBPath = DBPath,
  MigrationsProvider = new MigrationsProviderImplementation()
};

You can also provide a IMigrationsLogger implementation for logging of profiling information on each executed statement.

From there the process is really simple.

var migrator = new SQLiteMigrator(config);
migrator.init();

The init method call will create the database at the specified path if it does not exist already. Then it will check for the __migrations table and create it if is missing. It will then check for the current database schema version (initially 0) and retrieve all the migrations with version code higher than the current database schema version and execute them in order.

Creating a migration

Create a migration file:

$ touch 201704011234_add_phone_field_to_user.sql

SQLiteMigrations will only recognize filenames of the form <version>_<name>.sql. The following filenames are valid:

  • 1.sql
  • 2_add_new_table.sql
  • 3_add-new-table.sql
  • 4_add new table.sql

The file must be structured as follows:

--- database id 201704011234

--- database up

-- Upgrade your database
ALTER TABLE users ADD COLUMN phone VARCHAR(25);

--- database down

-- Downgrade your database
CREATE TABLE users_backup(name VARCHAR(50));
INSERT INTO users_backup SELECT name FROM users;
DROP TABLE users;
ALTER TABLE users_backup RENAME TO users;

The migration is ran inside a transaction so it can be rolled back in case of failure. Be careful not to try to begin a transaction since this would cause an error.

Installation

NuGet

To install SQLiteMigrations, run the following command in the Package Manager Console

Install-Package MallaCreativa.SQLiteMigrations

Or just go to the NuGet package gallery and add it to your project.

TODO

  • Implement schema upgrading
  • Implement schema downgrading
  • Flexibility on schema migration process
  • Information of schema version (and if it has migrations table)

Helping hands are totally wellcome.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b feature/my-new-feature or git checkout -b hotfix/my-hotfix)
  3. Commit your changes (git commit -am 'Add some feature' or git commit -am 'Fix some error')
  4. Push to the branch (git push origin feature/my-new-feature or git push origin hotfix/my-hotfix)
  5. Create a new Pull Request
  6. You're awesome! ๐Ÿ‘

You can create the Pull Request before having completed the changes (or even before starting) to request feedback or ask for help for where to begin, just remember to keep [WIP] to let everyone know it is a Work In Progress before the name until your work is done.

Author

Jonatan Cardona Casas, [email protected]

License

Migrations is available under the MIT license. See the LICENSE file for more info.

sqlitemigrations's People

Contributors

jaceee avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

w3ss

sqlitemigrations's Issues

Initialization process is inflexible

The initialization process is currently just internal.
The process should let the user:

  • Check if the migrations table exists
  • Create the migrations table
  • Check the current schema version
  • Check if the database needs migration (e. g. current version is less than last migration)
  • Run all pending migrations
  • Migrate up or down to a specific schema version

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.