1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
from __future__ import with_statement
from tests import clear_staging_env, staging_env, \
_sqlite_testing_config, sqlite_db, eq_, write_script, \
assert_raises_message
from alembic import command, util
from alembic.script import ScriptDirectory
import unittest
import os
class VersioningTest(unittest.TestCase):
def test_001_revisions(self):
global a, b, c
a = util.rev_id()
b = util.rev_id()
c = util.rev_id()
script = ScriptDirectory.from_config(self.cfg)
script.generate_revision(a, None, refresh=True)
write_script(script, a, """
revision = '%s'
down_revision = None
from alembic import op
def upgrade():
op.execute("CREATE TABLE foo(id integer)")
def downgrade():
op.execute("DROP TABLE foo")
""" % a)
script.generate_revision(b, None, refresh=True)
write_script(script, b, """
revision = '%s'
down_revision = '%s'
from alembic import op
def upgrade():
op.execute("CREATE TABLE bar(id integer)")
def downgrade():
op.execute("DROP TABLE bar")
""" % (b, a))
script.generate_revision(c, None, refresh=True)
write_script(script, c, """
revision = '%s'
down_revision = '%s'
from alembic import op
def upgrade():
op.execute("CREATE TABLE bat(id integer)")
def downgrade():
op.execute("DROP TABLE bat")
""" % (c, b))
def test_002_upgrade(self):
command.upgrade(self.cfg, c)
db = sqlite_db()
assert db.dialect.has_table(db.connect(), 'foo')
assert db.dialect.has_table(db.connect(), 'bar')
assert db.dialect.has_table(db.connect(), 'bat')
def test_003_downgrade(self):
command.downgrade(self.cfg, a)
db = sqlite_db()
assert db.dialect.has_table(db.connect(), 'foo')
assert not db.dialect.has_table(db.connect(), 'bar')
assert not db.dialect.has_table(db.connect(), 'bat')
def test_004_downgrade(self):
command.downgrade(self.cfg, 'base')
db = sqlite_db()
assert not db.dialect.has_table(db.connect(), 'foo')
assert not db.dialect.has_table(db.connect(), 'bar')
assert not db.dialect.has_table(db.connect(), 'bat')
def test_005_upgrade(self):
command.upgrade(self.cfg, b)
db = sqlite_db()
assert db.dialect.has_table(db.connect(), 'foo')
assert db.dialect.has_table(db.connect(), 'bar')
assert not db.dialect.has_table(db.connect(), 'bat')
def test_006_upgrade_again(self):
command.upgrade(self.cfg, b)
# TODO: test some invalid movements
@classmethod
def setup_class(cls):
cls.env = staging_env()
cls.cfg = _sqlite_testing_config()
@classmethod
def teardown_class(cls):
clear_staging_env()
class VersionNameTemplateTest(unittest.TestCase):
def setUp(self):
self.env = staging_env()
self.cfg = _sqlite_testing_config()
def tearDown(self):
clear_staging_env()
def test_option(self):
self.cfg.set_main_option("file_template", "myfile_%%(slug)s")
script = ScriptDirectory.from_config(self.cfg)
a = util.rev_id()
script.generate_revision(a, "some message", refresh=True)
write_script(script, a, """
revision = '%s'
down_revision = None
from alembic import op
def upgrade():
op.execute("CREATE TABLE foo(id integer)")
def downgrade():
op.execute("DROP TABLE foo")
""" % a)
script = ScriptDirectory.from_config(self.cfg)
rev = script._get_rev(a)
eq_(rev.revision, a)
eq_(os.path.basename(rev.path), "myfile_some_message.py")
def test_lookup_legacy(self):
self.cfg.set_main_option("file_template", "%%(rev)s")
script = ScriptDirectory.from_config(self.cfg)
a = util.rev_id()
script.generate_revision(a, None, refresh=True)
write_script(script, a, """
down_revision = None
from alembic import op
def upgrade():
op.execute("CREATE TABLE foo(id integer)")
def downgrade():
op.execute("DROP TABLE foo")
""")
script = ScriptDirectory.from_config(self.cfg)
rev = script._get_rev(a)
eq_(rev.revision, a)
eq_(os.path.basename(rev.path), "%s.py" % a)
def test_error_on_new_with_missing_revision(self):
self.cfg.set_main_option("file_template", "%%(slug)s_%%(rev)s")
script = ScriptDirectory.from_config(self.cfg)
a = util.rev_id()
script.generate_revision(a, "foobar", refresh=True)
assert_raises_message(
util.CommandError,
"Could not determine revision id from filename foobar_%s.py. "
"Be sure the 'revision' variable is declared "
"inside the script." % a,
write_script, script, a, """
down_revision = None
from alembic import op
def upgrade():
op.execute("CREATE TABLE foo(id integer)")
def downgrade():
op.execute("DROP TABLE foo")
""")
|