1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright (c) 2012 Red Hat, Inc.
6
# Licensed under the Apache License, Version 2.0 (the "License"); you may
7
# not use this file except in compliance with the License. You may obtain
8
# a copy of the License at
10
# http://www.apache.org/licenses/LICENSE-2.0
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
# License for the specific language governing permissions and limitations
18
from sqlalchemy import MetaData
21
def upgrade(migrate_engine):
23
meta.bind = migrate_engine
25
# NOTE(dprince): Remove unused snapshots/volumes sequences.
26
# These are leftovers from the ID --> UUID conversion for these tables
27
# that occurred in Folsom.
28
if migrate_engine.name == "postgresql":
29
base_query = """SELECT COUNT(*) FROM pg_class c
31
AND relname = '%s';"""
32
result = migrate_engine.execute(base_query % "snapshots_id_seq")
33
if result.scalar() > 0:
34
sql = "DROP SEQUENCE snapshots_id_seq CASCADE;"
35
migrate_engine.execute(sql)
37
result = migrate_engine.execute(base_query % "volumes_id_seq")
38
if result.scalar() > 0:
39
sql = "DROP SEQUENCE volumes_id_seq CASCADE;"
40
migrate_engine.execute(sql)
43
def downgrade(migrate_engine):
45
meta.bind = migrate_engine
47
if migrate_engine.name == "postgresql":
48
sql = """CREATE SEQUENCE snapshots_id_seq START WITH 1 INCREMENT BY 1
49
NO MINVALUE NO MAXVALUE CACHE 1;
50
ALTER SEQUENCE snapshots_id_seq OWNED BY snapshots.id;
51
SELECT pg_catalog.setval('snapshots_id_seq', 1, false);
52
ALTER TABLE ONLY snapshots ALTER COLUMN id SET DEFAULT
53
nextval('snapshots_id_seq'::regclass);"""
55
sql += """CREATE SEQUENCE volumes_id_seq START WITH 1 INCREMENT BY 1
56
NO MINVALUE NO MAXVALUE CACHE 1;
57
ALTER SEQUENCE volumes_id_seq OWNED BY volumes.id;
58
SELECT pg_catalog.setval('volumes_id_seq', 1, false);
59
ALTER TABLE ONLY volumes ALTER COLUMN id SET DEFAULT
60
nextval('volumes_id_seq'::regclass);"""
61
migrate_engine.execute(sql)