Created
July 5, 2018 19:31
-
-
Save aconz2/6b5750071057d8447746e48346a29ef8 to your computer and use it in GitHub Desktop.
peewee migrator
This file contains hidden or 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
# generic migration template | |
from playhouse.migrate import * | |
from models import * | |
migrator = SchemaMigrator.from_database(database) | |
def add_column(field): | |
return migrator.add_column(field.model._meta.table_name, field.column_name, field) | |
migrations = ( | |
add_column(Foo.field), | |
) | |
def main(): | |
with database.transaction(): | |
migrate( | |
*migrations | |
) | |
magic = 'yes please do the migration' | |
if __name__ == '__main__': | |
import sys | |
args = dict(enumerate(sys.argv)) | |
print('Connected to {}'.format(database.connect_params['host'])) | |
for m in migrations: | |
if m.method in ('add_column',): | |
table, column, field = m.args | |
print('{:<15} {:<20} {:<20} {:<10}'.format(m.method, table, column, str(Context().sql(field.ddl(None)).query()))) | |
else: | |
print('{:<10} {}'.format(m.method, m.args)) | |
if input('Enter "{}" to execute: '.format(magic)) != magic: | |
sys.exit(1) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment