Project

General

Profile

Revision ee8d6e13

IDee8d6e131e5dd889a38e893518dc2da2b23ae57c
Parent 19948d16
Child 4095ba5c

Added by John Spray about 10 years ago

cthulhu: Invoke migrations in "calamari-ctl initialize"

Fixes: #7123

View differences:

cthulhu/cthulhu/calamari_ctl.py
10 10
from django.core.management import execute_from_command_line
11 11
import pwd
12 12
from django.utils.crypto import get_random_string
13
from cthulhu.persistence import persister
14 13
from django.contrib.auth import get_user_model
15 14
from cthulhu.config import CalamariConfig, AlembicConfig
15
from sqlalchemy import create_engine
16
from cthulhu.persistence import Base
16 17

  
18
# Import sqlalchemy objects so that create_all sees them
19
from cthulhu.persistence.sync_objects import SyncObject  # noqa
20
from cthulhu.persistence.servers import Server, Service  # noqa
21
from cthulhu.persistence.event import Event  # noqa
17 22

  
18 23
log = logging.getLogger('calamari_ctl')
19 24
log.setLevel(logging.INFO)
......
57 62
        open(config.get('calamari_web', 'secret_key_path'), 'w').write(get_random_string(50, chars))
58 63

  
59 64
    # Cthulhu's database
60
    log.info("Initializing database...")
61
    # If database already exists, migrate forward with alembic
62
    # If database does not exist, create with create_all then stamp with alembic
63
    persister.initialize(config.get('cthulhu', 'db_path'))
65
    db_path = config.get('cthulhu', 'db_path')
66
    engine = create_engine(db_path)
67
    Base.metadata.reflect(engine)
64 68
    alembic_config = AlembicConfig()
65
    command.stamp(alembic_config, "head")
69
    if 'alembic_version' in Base.metadata.tables:
70
        log.info("Updating database...")
71
        # Database already populated, migrate forward
72
        command.upgrade(alembic_config, "head")
73
    else:
74
        log.info("Initializing database...")
75
        # Blank database, do initial population
76
        Base.metadata.create_all(engine)
77
        command.stamp(alembic_config, "head")
66 78

  
67 79
    # Django's database
68 80
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "calamari_web.settings")
......
125 137
    config = CalamariConfig()
126 138

  
127 139
    log.info("Dropping tables")
128
    persister.drop(config.get('cthulhu', 'db_path'))
140
    db_path = config.get('cthulhu', 'db_path')
141
    engine = create_engine(db_path)
142
    Base.metadata.drop_all(engine)
129 143
    log.info("Complete.  Now run `%s initialize`" % os.path.basename(sys.argv[0]))
130 144

  
131 145

  
cthulhu/cthulhu/persistence/persister.py
9 9
import gevent.queue
10 10
import gevent.event
11 11

  
12
from sqlalchemy import create_engine
13 12
from sqlalchemy.orm import sessionmaker
14 13
from cthulhu.manager import config
15 14

  
16
from cthulhu.persistence import Base
17 15
from cthulhu.persistence.sync_objects import SyncObject
18 16
from cthulhu.persistence.servers import Server, Service
19
# FIXME I have to import this to get create_all to see it :-/
20
from cthulhu.persistence.event import Event
21
Event.foo = ""  # STFU pyflakes
22 17

  
23 18
from cthulhu.util import now
24 19
from cthulhu.log import log
25 20

  
26 21
Session = sessionmaker()
27 22

  
28

  
29
def initialize(db_path):
30
    engine = create_engine(db_path)
31
    Base.metadata.create_all(engine)
32

  
33

  
34
def drop(db_path):
35
    engine = create_engine(db_path)
36
    Base.metadata.drop_all(engine)
37

  
38

  
39 23
DeferredCall = namedtuple('DeferredCall', ['fn', 'args', 'kwargs'])
40 24

  
41 25

  

Also available in: Unified diff