Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c6837a9e73 | |||
| b365d12a2e | |||
| 4455fa48c4 | |||
| 96534ccf30 |
@@ -259,6 +259,12 @@ web2py will attempt to run a GUI to ask for it when starting the web server
|
|||||||
'(default is %(default)s), see -S above. NOTE: when the APP_ENV '
|
'(default is %(default)s), see -S above. NOTE: when the APP_ENV '
|
||||||
'argument of -S include a controller c automatic import of '
|
'argument of -S include a controller c automatic import of '
|
||||||
'models is always enabled')
|
'models is always enabled')
|
||||||
|
g.add_argument('--force_migrate',
|
||||||
|
default=False,
|
||||||
|
action='store_true',
|
||||||
|
help=
|
||||||
|
'force DAL to migrate all tables that should be migrated when enabled; '
|
||||||
|
'monkeypatch in the DAL class to force _migrate_enabled=True')
|
||||||
g.add_argument('-R', '--run',
|
g.add_argument('-R', '--run',
|
||||||
type=existing_file,
|
type=existing_file,
|
||||||
metavar='PYTHON_FILE', help=
|
metavar='PYTHON_FILE', help=
|
||||||
|
|||||||
+24
-1
@@ -216,7 +216,8 @@ def run(
|
|||||||
bpython=False,
|
bpython=False,
|
||||||
python_code=None,
|
python_code=None,
|
||||||
cron_job=False,
|
cron_job=False,
|
||||||
scheduler_job=False):
|
scheduler_job=False,
|
||||||
|
force_migrate=False):
|
||||||
"""
|
"""
|
||||||
Start interactive shell or run Python script (startfile) in web2py
|
Start interactive shell or run Python script (startfile) in web2py
|
||||||
controller environment. appname is formatted like:
|
controller environment. appname is formatted like:
|
||||||
@@ -246,6 +247,19 @@ def run(
|
|||||||
os.mkdir(adir)
|
os.mkdir(adir)
|
||||||
fileutils.create_app(adir)
|
fileutils.create_app(adir)
|
||||||
|
|
||||||
|
if force_migrate:
|
||||||
|
import_models = True
|
||||||
|
from gluon.dal import DAL
|
||||||
|
orig_init = DAL.__init__
|
||||||
|
|
||||||
|
def custom_init(*args, **kwargs):
|
||||||
|
kwargs['migrate_enabled'] = True
|
||||||
|
kwargs['migrate'] = True
|
||||||
|
logger.info('Forcing migrate_enabled=True')
|
||||||
|
orig_init(*args, **kwargs)
|
||||||
|
|
||||||
|
DAL.__init__ = custom_init
|
||||||
|
|
||||||
if c:
|
if c:
|
||||||
import_models = True
|
import_models = True
|
||||||
extra_request = {}
|
extra_request = {}
|
||||||
@@ -298,6 +312,15 @@ def run(
|
|||||||
print(traceback.format_exc())
|
print(traceback.format_exc())
|
||||||
if import_models:
|
if import_models:
|
||||||
BaseAdapter.close_all_instances('rollback')
|
BaseAdapter.close_all_instances('rollback')
|
||||||
|
elif force_migrate:
|
||||||
|
try:
|
||||||
|
execfile("scripts/migrator.py", _env)
|
||||||
|
if import_models:
|
||||||
|
BaseAdapter.close_all_instances('commit')
|
||||||
|
except:
|
||||||
|
print(traceback.format_exc())
|
||||||
|
if import_models:
|
||||||
|
BaseAdapter.close_all_instances('rollback')
|
||||||
else:
|
else:
|
||||||
if not plain:
|
if not plain:
|
||||||
if bpython:
|
if bpython:
|
||||||
|
|||||||
+1
-1
@@ -741,7 +741,7 @@ def start():
|
|||||||
sys.argv = [options.run or ''] + options.args
|
sys.argv = [options.run or ''] + options.args
|
||||||
run(options.shell, plain=options.plain, bpython=options.bpython,
|
run(options.shell, plain=options.plain, bpython=options.bpython,
|
||||||
import_models=options.import_models, startfile=options.run,
|
import_models=options.import_models, startfile=options.run,
|
||||||
cron_job=options.cron_job)
|
cron_job=options.cron_job, force_migrate=options.force_migrate)
|
||||||
return
|
return
|
||||||
|
|
||||||
if options.cron_run:
|
if options.cron_run:
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
'''
|
||||||
|
To use, e.g. python .\web2py.py -S APPNAME --force_migrate
|
||||||
|
'''
|
||||||
|
|
||||||
|
import logging
|
||||||
|
|
||||||
|
logger = logging.getLogger("web2py")
|
||||||
|
|
||||||
|
|
||||||
|
def get_databases(request):
|
||||||
|
dbs = {}
|
||||||
|
global_env = globals()
|
||||||
|
for (key, value) in global_env.items():
|
||||||
|
try:
|
||||||
|
cond = isinstance(value, GQLDB)
|
||||||
|
except:
|
||||||
|
cond = isinstance(value, SQLDB)
|
||||||
|
if cond:
|
||||||
|
dbs[key] = value
|
||||||
|
return dbs
|
||||||
|
|
||||||
|
|
||||||
|
logger.debug('Getting all databases')
|
||||||
|
databases = get_databases(None)
|
||||||
|
logger.debug('databases = %s', databases)
|
||||||
|
for db_name in databases:
|
||||||
|
logger.debug('Migrating %s', db_name)
|
||||||
|
db = databases[db_name]
|
||||||
|
tables = db.tables
|
||||||
|
for table_name in tables:
|
||||||
|
# Force migration of lazy tables
|
||||||
|
logger.debug("Ensuring migration of table '%s'", table_name)
|
||||||
|
db(db[table_name]).isempty()
|
||||||
|
db.commit()
|
||||||
Reference in New Issue
Block a user