mirror of
https://github.com/djrobstep/migra.git
synced 2024-09-11 13:56:05 +03:00
support EMPTY parameter
This commit is contained in:
parent
811e581a77
commit
e8e94728a2
@ -3,11 +3,21 @@ from __future__ import unicode_literals, print_function
|
||||
from sqlbag import S
|
||||
import argparse
|
||||
import sys
|
||||
from contextlib import contextmanager
|
||||
|
||||
from .migra import Migration
|
||||
from .statements import UnsafeMigrationException
|
||||
|
||||
|
||||
@contextmanager
|
||||
def arg_context(x):
|
||||
if x == 'EMPTY':
|
||||
yield None
|
||||
else:
|
||||
with S(x) as s:
|
||||
yield s
|
||||
|
||||
|
||||
def parse_args(args):
|
||||
parser = argparse.ArgumentParser(
|
||||
description='Generate a database migration.')
|
||||
@ -36,8 +46,10 @@ def run(args, out=None, err=None):
|
||||
if not err:
|
||||
err = sys.stderr # pragma: no cover
|
||||
|
||||
with S(args.dburl_from) as s0, S(args.dburl_target) as s1:
|
||||
m = Migration(s0, s1)
|
||||
with \
|
||||
arg_context(args.dburl_from) as ac0, \
|
||||
arg_context(args.dburl_target) as ac1:
|
||||
m = Migration(ac0, ac1)
|
||||
|
||||
if args.unsafe:
|
||||
m.set_safety(False)
|
||||
|
@ -1,23 +1,29 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from sqlbag import raw_execute
|
||||
from schemainspect import get_inspector
|
||||
from schemainspect import get_inspector, DBInspector
|
||||
from .changes import Changes
|
||||
from .statements import Statements
|
||||
|
||||
|
||||
class Migration(object):
|
||||
def __init__(self, s_from, s_target):
|
||||
self.s_from = s_from
|
||||
self.s_target = s_target
|
||||
|
||||
def __init__(self, x_from, x_target):
|
||||
self.statements = Statements()
|
||||
self.changes = Changes(None, None)
|
||||
self.inspect_from()
|
||||
self.inspect_target()
|
||||
self.statements = Statements()
|
||||
|
||||
def clear(self):
|
||||
self.statements = Statements()
|
||||
if isinstance(x_from, DBInspector):
|
||||
self.changes.i_from = x_from
|
||||
else:
|
||||
self.changes.i_from = get_inspector(x_from)
|
||||
if x_from:
|
||||
self.s_from = x_from
|
||||
|
||||
if isinstance(x_target, DBInspector):
|
||||
self.changes.i_target = x_target
|
||||
else:
|
||||
self.changes.i_target = get_inspector(x_target)
|
||||
if x_target:
|
||||
self.s_target = x_target
|
||||
|
||||
def inspect_from(self):
|
||||
self.changes.i_from = get_inspector(self.s_from)
|
||||
@ -25,11 +31,14 @@ class Migration(object):
|
||||
def inspect_target(self):
|
||||
self.changes.i_target = get_inspector(self.s_target)
|
||||
|
||||
def clear(self):
|
||||
self.statements = Statements()
|
||||
|
||||
def apply(self):
|
||||
for stmt in self.statements:
|
||||
raw_execute(self.s_from, stmt)
|
||||
|
||||
self.inspect_from()
|
||||
self.changes.i_from = get_inspector(self.s_from)
|
||||
safety_on = self.statements.safe
|
||||
self.clear()
|
||||
self.set_safety(safety_on)
|
||||
|
2
setup.py
2
setup.py
@ -8,7 +8,7 @@ readme = io.open('README.rst').read()
|
||||
|
||||
setup(
|
||||
name='migra',
|
||||
version='0.1.1470919405',
|
||||
version='0.1.1470919406',
|
||||
url='https://github.com/djrobstep/migra',
|
||||
description='Like diff but for PostgreSQL schemas',
|
||||
long_description=readme,
|
||||
|
@ -1,6 +1,6 @@
|
||||
create extension "citext" with schema "public" version '1.1';
|
||||
create extension "citext" with schema "public" version '1.3';
|
||||
|
||||
create extension "hstore" with schema "public" version '1.3';
|
||||
create extension "hstore" with schema "public" version '1.4';
|
||||
|
||||
create type "public"."bug_status" as enum ('new', 'open', 'closed');
|
||||
|
||||
|
@ -7,7 +7,7 @@ from pytest import raises
|
||||
from migra import Statements, UnsafeMigrationException, Migration
|
||||
from migra.command import run
|
||||
from sqlbag import temporary_database, S, load_sql_from_file
|
||||
|
||||
from schemainspect import get_inspector
|
||||
from migra.command import parse_args
|
||||
|
||||
SQL = """select 1;
|
||||
@ -70,6 +70,8 @@ def test_all():
|
||||
|
||||
with S(d0) as s0, S(d1) as s1:
|
||||
m = Migration(s0, s1)
|
||||
m.inspect_from()
|
||||
m.inspect_target()
|
||||
|
||||
with raises(AttributeError):
|
||||
m.changes.nonexist
|
||||
@ -89,3 +91,25 @@ def test_all():
|
||||
|
||||
out, err = outs()
|
||||
assert run(args, out=out, err=err) == 0
|
||||
|
||||
# test alternative parameters
|
||||
|
||||
with S(d0) as s0, S(d1) as s1:
|
||||
m = Migration(
|
||||
get_inspector(s0),
|
||||
get_inspector(s1)
|
||||
)
|
||||
|
||||
# test empty
|
||||
m = Migration(None, None)
|
||||
m.add_all_changes()
|
||||
|
||||
with raises(AttributeError):
|
||||
m.s_from
|
||||
|
||||
with raises(AttributeError):
|
||||
m.s_target
|
||||
|
||||
args = parse_args(['--unsafe', 'EMPTY', 'EMPTY'])
|
||||
out, err = outs()
|
||||
assert run(args, out=out, err=err) == 0
|
||||
|
Loading…
Reference in New Issue
Block a user