Coder Social home page Coder Social logo

caribou's Introduction

Caribou SQLite Migrations

Caribou

Caribou is a small, simple SQLite database migrations library for Python, built primarily to manage the evoluton of client side databases over multiple releases of an application.

Example

Here is a simple example illustrating how to use Caribou to manage your SQLite schema:

Create a Migration

Use Caribou's command line tool to create your first migration:

$ caribou create my_first_migration
created migration ./20091115140758_my_first_migration.py

Edit Your Migration

Let's create a table with some data in the upgrade step and reverse the changes in the downgrade step.

"""
An example of a Caribou migration file.
"""

def upgrade(connection):
    # connection is a plain old sqlite3 database connection
    sql = """
        create table animals
        ( name     TEXT
        , status   TEXT
        ) """
    connection.execute(sql)
    
    animals = [ ('caribou', 'least concerned')
              , ('bengal tiger', 'threatened')
              , ('eastern elk', 'extinct')
              ]
    sql = 'insert into animals values (:1, :2)'
    for name, status in animals:
        connection.execute(sql, [name, status])

    connection.commit()

def downgrade(connection):
    connection.execute('drop table animals')

Caribou migrations are flexible because they are plain Python files. Feel free to add logging, DDL transactions, anything at all.

Run Your Migration:

Caribou migrations can be run with the command line tool:

$ caribou upgrade my_sqlite_db .
upgrading db [my_sqlite_db] to most recent version
upgraded [my_sqlite_db] successfully to version [20091115140758]

# if you want to revert your changes, uses the downgrade command:

$ caribou downgrade my_sqlite_db . 0
downgrading db [my_sqlite_db] to version [0]
downgraded [my_sqlite_db] successfully to version [0]

Since Caribou is built to manage client side sqlite databases, it can also be run programmatically from within your application:

"""
An example illustrating how to run a migration programmatically.
"""

import caribou

db_path = 'my_sqlite_db' 
migrations_path = '/path/to/migrations/dir'
version = '20091115140758'

# upgrade to most recent version
caribou.upgrade(db_path, migrations_path)

# upgrade to a specific version
caribou.upgrade(db_path, migrations_path, version)

# downgrade to a specific version
caribou.downgrade(db_path, migrations_path, version)

That's it. You're rolling.

Installation

using setuptools:

sudo easy_install caribou

or, download and extract the most code in the repo, and run:

sudo python setup.py install

Licence

Caribou is in the public domain.

Development

Things to know, before you start hacking Caribou:

Unit Tests

The unit test suite requires the nose unit testing library. To install:

sudo easy_install nose

To execute the test suite, run:

python setup.py nosetests

or simply:

nosetests

Appendix

Haven't got enough?

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.