Skip to main content

Python package to migrate postgresql database

Project description

generate

Example usage:

dbmigrator generate add_id_to_users

generates a file called migrations/20151217170514_add_id_to_users.py with content:

def up(cursor):
    # TODO migration code
    pass

def down(cursor):
    # TODO rollback code
    pass

To set the migrations directory using an entry point, in mymodule setup.py:

setup(
    ...
    entry_points={
        'dbmigrator': [
            'migrations_directory = mymodule.main:migrations_directory',
            ],
        },
    )

Important note: For the settings from setup.py to be picked up, before running dbmigrator, first run python setup.py develop or python setup.py install.

Then in mymodule/main.py:

import os

migrations_directory = '{}/sql/migrations'.format(
    os.path.abspath(os.path.dirname(__file__)))

or:

import os

def migrations_directory():
    return '{}/sql/migrations'.format(
        os.path.abspath(os.path.dirname(__file__)))

init

Example usage:

dbmigrator --db-connection-string='postgres://dbuser@localhost/dbname' init

or with a config file, development.ini, that looks like this:

[app:main]
db-connection-string = postgres://dbuser@localhost/dbname

Run the command:

dbmigrator --config=development.ini init

list

Example usage:

$ dbmigrator list
name                      | is applied | date applied
----------------------------------------------------------------------
20151217170514_add_id_to_   True         2016-01-31 00:15:01.692570+01:00
20151218145832_add_karen_   False
20160107200351_blah         False

migrate

Example usage:

With two migrations in the migrations directory,

migrations/20151217170514_add_id_to_users.py:

def up(cursor):
    # TODO migration code
    pass

def down(cursor):
    # TODO rollback code
    pass

and

migrations/20151218145832_add_karen_to_users.py:

def up(cursor):
    cursor.execute('ALTER TABLE users ADD COLUMN karen TEXT')

def down(cursor):
    cursor.execute('ALTER TABLE users DROP COLUMN karen')

To run the migrations:

$ dbmigrator migrate
Running migration 20151217170514 add_id_to_users

Running migration 20151218145832 add_karen_to_users
---
+++
@@ -4005,21 +4005,22 @@
     first_name text,
     firstname text,
     last_name text,
     surname text,
     full_name text,
     fullname text,
     suffix text,
     title text,
     email text,
     website text,
-    is_moderated boolean
+    is_moderated boolean,
+    karen text
 );

 ALTER TABLE public.users OWNER TO rhaptos;

 --
 -- Name: abstractid; Type: DEFAULT; Schema: public; Owner: rhaptos
 --

 ALTER TABLE ONLY abstracts ALTER COLUMN abstractid SET DEFAULT nextval('abstracts_abstractid_seq'::regclass);

or to run migrations up to a specific version:

$ dbmigrator migrate version=20151217170514
Running migration 20151217170514 add_id_to_users

if all migrations have already been run:

$ dbmigrator migrate
No pending migrations.  Database is up to date.

rollback

Example usage:

With two migrations in the migrations directory,

migrations/20151217170514_add_id_to_users.py:

def up(cursor):
    # TODO migration code
    pass

def down(cursor):
    # TODO rollback code
    pass

and

migrations/20151218145832_add_karen_to_users.py:

def up(cursor):
    cursor.execute('ALTER TABLE users ADD COLUMN karen TEXT')

def down(cursor):
    cursor.execute('ALTER TABLE users DROP COLUMN karen')

Make sure the database is up to date:

$ dbmigrator migrate
No pending migrations.  Database is up to date.

Now rollback the last migration:

$ dbmigrator rollback
Rolling back migration 20151218145832 add_karen_to_users
---
+++
@@ -4005,22 +4005,21 @@
     first_name text,
     firstname text,
     last_name text,
     surname text,
     full_name text,
     fullname text,
     suffix text,
     title text,
     email text,
     website text,
-    is_moderated boolean,
-    karen text
+    is_moderated boolean
 );

 ALTER TABLE public.users OWNER TO rhaptos;

 --
 -- Name: abstractid; Type: DEFAULT; Schema: public; Owner: rhaptos
 --

 ALTER TABLE ONLY abstracts ALTER COLUMN abstractid SET DEFAULT nextval('abstracts_abstractid_seq'::regclass);

To rollback the last 2 migrations:

$ dbmigrator rollback --steps=2
Rolling back migration 20151218145832 add_karen_to_users
---
+++
@@ -4005,22 +4005,21 @@
     first_name text,
     firstname text,
     last_name text,
     surname text,
     full_name text,
     fullname text,
     suffix text,
     title text,
     email text,
     website text,
-    is_moderated boolean,
-    karen text
+    is_moderated boolean
 );

 ALTER TABLE public.users OWNER TO rhaptos;

 --
 -- Name: abstractid; Type: DEFAULT; Schema: public; Owner: rhaptos
 --

 ALTER TABLE ONLY abstracts ALTER COLUMN abstractid SET DEFAULT nextval('abstracts_abstractid_seq'::regclass);

Rolling back migration 20151217170514 add_id_to_users

CHANGELOG

0.0.5 (2016-02-08)

  • Include CHANGELOG in distribution’s manifest

0.0.4 (2016-02-08)

  • Show warning message instead of error if migrations directory is undefined

  • Add CHANGELOG

0.0.3 (2016-02-08)

  • Return error if migrations directory is undefined

0.0.2 (2016-02-03)

  • Fix invalid rst in README

  • Update setup.py description and long_description

  • Update setup.py to include README as the description and fix url

  • Update README and cli after removing default value for config file

  • Remove default config path (development.ini)

  • Add dbmigrator list command

  • Fix dbmigrator rollback to stop if there are no migrations to rollback

  • Print message after initializing schema migrations

  • Add note to run python setup.py install if using entry points

  • Add migrations directory setting from setup.py entry point in README

  • Update command names for init and generate in README

  • Get settings from setup.py entry points

  • Remove __init__.py generation in migrations directory

  • Add option version to dbmigrator init for setting the initial version

  • Rename “generate_migration” command to “generate”

  • Rename “init_schema_migrations” command to “init”

  • Change the way migrations are imported so it works in python2

  • Add “applied” timestamp to schema migrations table

  • Add # -*- coding: utf-8 -*- to the top of generated migration files

  • Add README

  • Add command “rollback” to rollback migrations

  • Add command “migrate” to run pending migrations

  • Add migrations to table when running init_schema_migrations

  • Add command for creating the schema migrations table

  • Create dbmigrator cli and “generate_migration” command

  • Create dbmigrator python package

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

db-migrator-0.0.5.zip (28.2 kB view details)

Uploaded Source

db-migrator-0.0.5.tar.gz (21.0 kB view details)

Uploaded Source

db-migrator-0.0.5.tar.bz2 (18.0 kB view details)

Uploaded Source

File details

Details for the file db-migrator-0.0.5.zip.

File metadata

  • Download URL: db-migrator-0.0.5.zip
  • Upload date:
  • Size: 28.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for db-migrator-0.0.5.zip
Algorithm Hash digest
SHA256 a599a80964b9ca5f9defc79e4ad2443b2531c9a9b2fb23b0a926d75651d08ccb
MD5 9ce74add67ea60ed23bbfcc77eb1c0e0
BLAKE2b-256 8fe98303992136389ffb0a21e1267792c78951e6dab9edef1e4c7fbaabad1798

See more details on using hashes here.

File details

Details for the file db-migrator-0.0.5.tar.gz.

File metadata

  • Download URL: db-migrator-0.0.5.tar.gz
  • Upload date:
  • Size: 21.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for db-migrator-0.0.5.tar.gz
Algorithm Hash digest
SHA256 7643b0365a4e1de1e221dd88f8d9198aa5a458e5ffac16212202f75e022848e7
MD5 cf6715e35ec2565bad27ae7f6016c992
BLAKE2b-256 d19dc8a493e20e452b9ce00fbc6d119c051178e2a65972ec77f64773cba5d06a

See more details on using hashes here.

File details

Details for the file db-migrator-0.0.5.tar.bz2.

File metadata

  • Download URL: db-migrator-0.0.5.tar.bz2
  • Upload date:
  • Size: 18.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for db-migrator-0.0.5.tar.bz2
Algorithm Hash digest
SHA256 663db6246fc834ccd4c419900610a789ea896609e62453005e5d595c8ab9e48e
MD5 96d58e0aee358435f172f4b01e531ae0
BLAKE2b-256 8d22610f36310f9851edf29a87d36be1dd97f5f517729ba99bbd02cad6f54959

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page