Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

In writing "migration-register", I realized that I needed some of the…

… same functionality as in "migration-init", so I refactored to make that available. I thought that I'd need the "get_HASH()" method from "migrations-create", as well, and moved that into the parent class, but turns out not so.
  • Loading branch information...
commit 969e2fc3b8d8bbd371fbbaca64bb4c1289017025 1 parent 106e309
@al-the-x al-the-x authored
View
40 bin/migration-create
@@ -1,20 +1,20 @@
#!/usr/bin/env python2.7
-import os.path, hashlib, migrations
+import os.path, migrations
MIGRATION_TEMPLATE = """
CREATE OR REPLACE FUNCTION %(migration_name)s ( varchar ) RETURNS VOID AS
$BODY$
DECLARE
- _direction varchar := UPPER($1)
+ _direction varchar := UPPER($1);
BEGIN
CASE
WHEN _direction = 'UP' THEN
- PERFORM SELECT 1; -- TODO: Write UP migration...
+ PERFORM 1; -- TODO: Write UP migration...
WHEN _direction = 'DOWN' THEN
- PERFORM SELECT 1; -- TODO: Write DOWN migration...
+ PERFORM 1; -- TODO: Write DOWN migration...
END CASE;
END;
@@ -23,12 +23,19 @@ MIGRATION_TEMPLATE = """
"""
-class MigrationCreate(migrations.Command):
- def get_HASH ( self, schema_file, long_hash=False ):
- with open(schema_file) as f:
- HASH = hashlib.sha256(f.read()).hexdigest()
+class MigrationCreate(migrations.MigrationCommand):
+ def init_args ( self ):
+ super(MigrationCreate, self).init_args(
+ ).add_argument('-s', '--schema-file', dest='schema_file', default='etc/schema.pg.sql',
+ help='the schema file to mark migrations from'
+ ).with_migration_path(
+ ).with_migration_name(
+ ).add_argument('--long-hash', action='store_true',
+ help='use the full 40-character HASH in MIGRATION_NAME if "--migration-name" is not provided'
+ ).add_argument('-f', '--force', action='store_true', dest='force',
+ help='force overwrite of existing migration file'
+ )
- return HASH if long_hash else HASH[0:7]
def main ( self, schema_file, migration_path, migration_name, long_hash=False, **kwargs ):
@@ -46,19 +53,6 @@ class MigrationCreate(migrations.Command):
if __name__ == '__main__':
- command = MigrationCreate(
- ).add_argument('-s', '--schema-file', dest='schema_file', default='etc/schema.pg.sql',
- help='the schema file to mark migrations from'
- ).add_argument('-M', '--migration-path', dest='migration_path', default='etc/migrations/',
- help='the path in which to store migrations'
- ).add_argument('-m', '--migration-name', type=str, dest='migration_name',
- help='the name of the migration to create (use the current hash of the schema file by default)'
- ).add_argument('--long-hash', action='store_true',
- help='use the full 40-character HASH in MIGRATION_NAME if "--migration-name" is not provided'
- ).add_argument('-f', '--force', action='store_true', dest='force',
- help='force overwrite of existing migration file'
- )
-
- command()
+ MigrationCreate()()
## vim: filetype=python
View
12 bin/migration-init
@@ -24,19 +24,17 @@ CREATE INDEX "history_applied_idx" ON "migrations"."history" ( "applied" );
'DOWN' : 'DROP SCHEMA "migrations" CASCADE;'
}
-class MigrationInit(migrations.Command):
- def main ( self, arguments=(), uninstall=False, **kwargs ):
- command = ('psql',) + arguments + ('-c',) + ('BEGIN TRANSACTION;\n%s\nCOMMIT TRANSACTION;' % (
- MIGRATION_SCHEMA['DOWN'] if uninstall else MIGRATION_SCHEMA['UP']
- ),)
+class MigrationInit(migrations.MigrationCommand):
+ def main ( self, uninstall, DBNAME, DBUSER, **kwargs ):
+ migration = MIGRATION_SCHEMA['DOWN'] if uninstall else MIGRATION_SCHEMA['UP']
- subprocess.call(command)
+ subprocess.call(('psql', DBNAME, DBUSER, '-1', '-c', migration))
if __name__ == '__main__':
command = MigrationInit(
).add_argument('--uninstall', action='store_true', help='uninstall the migrations schema instead'
- ).add_argument('ARGUMENTS', nargs='*', help='additional arguments to pass to the "psql" command, i.e. DBUSER, DBNAME, etc.'
+ ).with_psql_arguments(
)
command()
View
34 bin/migration-register
@@ -0,0 +1,34 @@
+#!/usr/bin/env python2.7
+
+import migrations, subprocess, os.path
+
+
+class MigrationRegister(migrations.MigrationCommand):
+ def init_args ( self ):
+ super(MigrationRegister, self).init_args(
+ ).with_migration_path(
+ ).with_psql_arguments(
+ ).add_argument('MIGRATION_FILE',
+ help='the path to the migration file to register'
+ )
+
+
+ def main ( self, migration_path, MIGRATION_FILE, DBNAME, DBUSER, **kwargs ):
+ migration_path = migrations.path(migration_path)
+
+ try: MIGRATION_FILE = migrations.path(MIGRATION_FILE)
+
+ except IOError:
+ MIGRATION_FILE = migrations.path(
+ migration_path, MIGRATION_FILE
+ )
+
+ migration_name = os.path.basename(MIGRATION_FILE).split('.', 1).pop(0)
+
+ subprocess.call(('psql', DBNAME, DBUSER, '-1', '-f', MIGRATION_FILE))
+
+
+if __name__ == '__main__':
+ MigrationRegister()()
+
+## vim: filetype=python
View
63 bin/migrations.py
@@ -1,10 +1,15 @@
-import argparse, os.path, sys
+import argparse, os.path, sys, hashlib
-def path ( value ):
- abspath = os.path.abspath(value)
+MIGRATION_EXTENSION = '.pg.sql'
- if not os.path.exists(abspath): raise Exception(
- 'Specified path does not exist: %s' % value
+
+def path ( *paths ):
+ path = os.path.join(*paths)
+
+ abspath = os.path.abspath(path)
+
+ if not os.path.exists(abspath): raise IOError(
+ 'Specified path does not exist: %s' % path
)
return abspath
@@ -14,13 +19,7 @@ class Command(argparse.ArgumentParser):
def __init__ ( self, *args, **kwargs ):
super(Command, self).__init__(*args, **kwargs)
- self.add_argument('-d', '--debug', action='store_true', dest='debug',
- help='display the full stack trace of errors instead of just the message'
- ).add_argument('-c', '--config', dest='config_file', default='.pg-migrate',
- help='the configuration file to use (command flags override config options)'
- ).add_argument('--no-config', dest='config_file', action='store_const', const=None,
- help='do not use a configuration file at all'
- )
+ self.init_args()
def add_argument ( self, *args, **kwargs ):
@@ -29,13 +28,20 @@ def add_argument ( self, *args, **kwargs ):
return self
+ def init_args ( self ):
+ return self.add_argument('-d', '--debug', action='store_true', dest='debug',
+ help='display the full stack trace of errors instead of just the message'
+ )
+
+
def __call__ ( self, args=None ):
args = None
try:
args = self.parse_args(args)
- if args.config_file: pass ## TODO: Load / save args to config_file...?
+ ## TODO: Add config file support...
+ #if args.config_file: pass ## TODO: Load / save args to config_file...?
self.main(**vars(args))
@@ -50,3 +56,34 @@ def __call__ ( self, args=None ):
def main ( self, *args, **kwargs ):
pass
+
+
+class MigrationCommand(Command):
+ def with_migration_path ( self ):
+ return self.add_argument('-M', '--migration-path', dest='migration_path', default='etc/migrations/',
+ help='the path in which to store migrations, default: etc/migrations/')
+
+
+ def with_migration_name ( self, **kwargs ):
+ defaults = dict(type=str, dest='migration_name',
+ help='the name of the migration to create (use the current hash of the schema file by default)'
+ )
+
+ defaults.update(kwargs); kwargs = defaults
+
+ return self.add_argument('-m', '--migration-name', **kwargs)
+
+
+ def with_psql_arguments ( self ):
+ return self.add_argument('DBNAME', nargs='?', default='',
+ ).add_argument('DBUSER', nargs='?', default='',
+ )
+
+
+ def get_HASH ( self, schema_file, long_hash=False ):
+ with open(schema_file) as f:
+ HASH = hashlib.sha256(f.read()).hexdigest()
+
+ return HASH if long_hash else HASH[0:7]
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.