Skip to content

Commit

Permalink
More work on migrations
Browse files Browse the repository at this point in the history
Remove sync_db
Added nsot-ctl commands for migrations (upgrade, downgrade, latest, revision)
Updated docs on migrations
Move alembic into the actual nsot package as package data
Renamed from alembic to migrations
Removed alembic.ini since we're doing everything programmatically
  • Loading branch information
gmjosack committed Jan 17, 2015
1 parent d596042 commit ac6f107
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 79 deletions.
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ on development instances.
```bash

# Setup the database.
nsot-ctl -vvv -c config/dev.yaml sync_db
nsot-ctl -vvv -c config/dev.yaml migrations latest

# Run the development reverse proxy
nsot-ctl -vv -c config/dev.yaml user_proxy
Expand All @@ -74,6 +74,27 @@ nsot-server --config=config/dev.yaml -vv

```

#### Working with migrations

If you make any changes to the models you'll want to generate a new migration.
We use alembic for migrations underneath but for general schema changes is
should be sufficient to just run

```bash

nsot-ctl -vvv -c config/dev.yaml migrations revision

```

This will generate a new schema version. You can then sync to the latest version
with

```bash

nsot-ctl -vvv -c config/dev.yaml migrations latest

```

#### Working with docs

Documentation is done with Sphinx. If you just want to build and view the docs you
Expand Down
68 changes: 0 additions & 68 deletions alembic.ini

This file was deleted.

74 changes: 69 additions & 5 deletions bin/nsot-ctl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

""" Command-line interface to various NSoT administrative commands."""

import alembic.config
import alembic.command
import argparse
import BaseHTTPServer
import code
Expand All @@ -25,9 +27,51 @@ def make_session():
return models.Session()


def sync_db_command(args):
db_engine = models.get_db_engine(settings["database"])
models.Model.metadata.create_all(db_engine)
def setup_migration_config():
config = alembic.config.Config()
config.set_main_option("script_location", "nsot:migrations")
config.set_main_option("sqlalchemy.url", settings["database"])
return config


def migrations_latest_command(args):
print "Upgrading to latest revision...",
config = setup_migration_config()
try:
alembic.command.upgrade(config, "head")
print "Done"
except alembic.util.CommandError as err:
print "Failed ({})".format(err)


def migrations_upgrade_command(args):
print "Upgrading to revision {}...".format(args.revision),
config = setup_migration_config()
try:
alembic.command.upgrade(config, args.revision)
print "Done"
except alembic.util.CommandError as err:
print "Failed ({})".format(err)


def migrations_downgrade_command(args):
print "Downgrading to revision {}...".format(args.revision),
config = setup_migration_config()
try:
alembic.command.downgrade(config, args.revision)
print "Done"
except alembic.util.CommandError as err:
print "Failed ({})".format(err)


def migrations_revision_command(args):
print "Generating New Revision...",
config = setup_migration_config()
try:
alembic.command.revision(config, autogenerate=True)
print "Done"
except alembic.util.CommandError as err:
print "Failed ({})".format(err)


def user_proxy_command(args):
Expand Down Expand Up @@ -82,9 +126,28 @@ def main():

subparsers = parser.add_subparsers(dest="command")

sync_db_parser = subparsers.add_parser("sync_db", help="Apply database schema to database.")
sync_db_parser.set_defaults(func=sync_db_command)
# Migration Parsers
migrations_parser = subparsers.add_parser(
"migrations", help="Create and apply database migrations.")
migrations_subparsers = migrations_parser.add_subparsers(
dest="migratrions_command"
)

migrations_latest_parser = migrations_subparsers.add_parser("latest")
migrations_latest_parser.set_defaults(func=migrations_latest_command)

migrations_upgrade_parser = migrations_subparsers.add_parser("upgrade")
migrations_upgrade_parser.add_argument("revision")
migrations_upgrade_parser.set_defaults(func=migrations_upgrade_command)

migrations_downgrade_parser = migrations_subparsers.add_parser("downgrade")
migrations_downgrade_parser.add_argument("revision")
migrations_downgrade_parser.set_defaults(func=migrations_downgrade_command)

migrations_revision_parser = migrations_subparsers.add_parser("revision")
migrations_revision_parser.set_defaults(func=migrations_revision_command)

# User Proxy Parsers
user_proxy_parser = subparsers.add_parser("user_proxy", help="Start a development reverse proxy.")
user_proxy_parser.set_defaults(func=user_proxy_command)
user_proxy_parser.add_argument("-p", "--listen-port", default=8888, type=int,
Expand All @@ -93,6 +156,7 @@ def main():
help="Port to proxy to.")
user_proxy_parser.add_argument("username", nargs="?", default=None)

# Shell Parsers
shell_parser = subparsers.add_parser("shell", help="Launch a shell with models imported.")
shell_parser.set_defaults(func=shell_command)

Expand Down
5 changes: 0 additions & 5 deletions alembic/env.py → nsot/migrations/env.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
from __future__ import with_statement
from alembic import context
from sqlalchemy import engine_from_config, pool
from logging.config import fileConfig

# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config

# Interpret the config file for Python logging.
# This line sets up loggers basically.
fileConfig(config.config_file_name)

# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
Expand Down
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def get_package_data(package, base_dir):

get_package_data("nsot", "nsot/static")
get_package_data("nsot", "nsot/templates")
get_package_data("nsot", "nsot/migrations")

kwargs = {
"name": "nsot",
Expand Down

0 comments on commit ac6f107

Please sign in to comment.