Last active
June 2, 2016 02:10
-
-
Save rafaelhenrique/d2f05d61e1d251cbce68 to your computer and use it in GitHub Desktop.
db_create on sqlalchemy-migrate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
from migrate.versioning import api | |
import os.path | |
from my_project_here import db | |
from my_project_here import app | |
SQLALCHEMY_DATABASE_URI = app.config['SQLALCHEMY_DATABASE_URI'] | |
SQLALCHEMY_MIGRATE_REPO = app.config['SQLALCHEMY_MIGRATE_REPO'] | |
db.create_all() | |
try: | |
db.session.commit() | |
db.session.remove() | |
except: | |
db.session.rollback() | |
db.session.remove() | |
if not os.path.exists(SQLALCHEMY_MIGRATE_REPO): | |
api.create(SQLALCHEMY_MIGRATE_REPO, 'database repository') | |
api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) | |
else: | |
api.version_control( | |
SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, | |
api.version(SQLALCHEMY_MIGRATE_REPO)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from migrate.versioning import api | |
from my_project_here import app | |
SQLALCHEMY_DATABASE_URI = app.config['SQLALCHEMY_DATABASE_URI'] | |
SQLALCHEMY_MIGRATE_REPO = app.config['SQLALCHEMY_MIGRATE_REPO'] | |
v = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) | |
api.downgrade(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, v - 1) | |
print 'Current database version: ' + \ | |
str(api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import imp | |
from migrate.versioning import api | |
from my_project_here import db | |
from my_project_here import app | |
SQLALCHEMY_DATABASE_URI = app.config['SQLALCHEMY_DATABASE_URI'] | |
SQLALCHEMY_MIGRATE_REPO = app.config['SQLALCHEMY_MIGRATE_REPO'] | |
migration = SQLALCHEMY_MIGRATE_REPO + '/versions/%03d_migration.py' % (api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) + 1) | |
tmp_module = imp.new_module('old_model') | |
old_model = api.create_model(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) | |
exec old_model in tmp_module.__dict__ | |
script = api.make_update_script_for_model(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, tmp_module.meta, db.metadata) | |
open(migration, "wt").write(script) | |
api.upgrade(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) | |
print 'New migration saved as ' + migration | |
print 'Current database version: ' + str(api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from migrate.versioning import api | |
from my_project_here import app | |
SQLALCHEMY_DATABASE_URI = app.config['SQLALCHEMY_DATABASE_URI'] | |
SQLALCHEMY_MIGRATE_REPO = app.config['SQLALCHEMY_MIGRATE_REPO'] | |
api.upgrade(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) | |
print 'Current database version: ' + \ | |
str(api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
echo "O que quer fazer?" | |
echo "1. db_create" | |
echo "2. db_migrate" | |
echo "3. db_downgrade" | |
echo "4. db_upgrade" | |
read OPT | |
case $OPT in | |
1) | |
echo "db_create..." | |
cd ../.. | |
python -m my_project_here.db_migrate_stuff.db_create | |
;; | |
2) | |
echo "db_migrate..." | |
cd ../.. | |
python -m my_project_here.db_migrate_stuff.db_migrate | |
;; | |
3) | |
echo "db_downgrade..." | |
cd ../.. | |
python -m my_project_here.db_migrate_stuff.db_downgrade | |
;; | |
4) | |
echo "db_upgrade..." | |
cd ../.. | |
python -m my_project_here.db_migrate_stuff.db_upgrade | |
;; | |
*) | |
echo "ERRO: Selecione a opção correta 1,2,3 ou 4." | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment