<s> from flask import json <EOL> from flask . ext . script import Manager <EOL> from flask . ext . migrate import MigrateCommand <EOL> from project import create_app <EOL> from project . extensions import db <EOL> from sqlalchemy . exc import OperationalError <EOL> from sqlalchemy . ext . serializer import dumps , loads <EOL> manager = Manager ( create_app ) <EOL> manager . add_command ( '<STR_LIT>' , MigrateCommand ) <EOL> @ manager . command <EOL> def init ( ) : <EOL> """<STR_LIT>""" <EOL> syncdb ( ) <EOL> @ manager . command <EOL> def syncdb ( ) : <EOL> """<STR_LIT>""" <EOL> db . drop_all ( ) <EOL> db . create_all ( ) <EOL> @ manager . option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT:source>' , <EOL> default = '<STR_LIT>' , <EOL> required = False , help = '<STR_LIT>' ) <EOL> def restore ( source = '<STR_LIT>' ) : <EOL> print ( "<STR_LIT>" ) <EOL> with open ( source , '<STR_LIT:rb>' ) as f : <EOL> data = json . loads ( f . readline ( ) ) <EOL> for model_data in data : <EOL> try : <EOL> restored = loads ( model_data , db . metadata , db . session ) <EOL> except AttributeError as e : <EOL> print ( '<STR_LIT>' . format ( e ) ) <EOL> continue <EOL> if restored : <EOL> print ( '<STR_LIT>' . format ( restored [ <NUM_LIT:0> ] . __table__ . name ) ) <EOL> for item in restored : <EOL> db . session . merge ( item ) <EOL> db . session . commit ( ) <EOL> print ( '<STR_LIT>' ) <EOL> @ manager . option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , default = None , required = True , help = '<STR_LIT>' ) <EOL> def dump ( destination ) : <EOL> dump_models = [ ] <EOL> serialized = list ( ) <EOL> for model in dump_models : <EOL> print ( '<STR_LIT>' . format ( model ) ) <EOL> serialized . append ( unicode ( dumps ( db . session . query ( model ) . all ( ) ) , errors = '<STR_LIT:ignore>' ) ) <EOL> with open ( destination , '<STR_LIT:w>' ) as f : <EOL> f . writelines ( json . dumps ( serialized ) ) <EOL> print ( '<STR_LIT>' ) <EOL> manager . add_option ( '<STR_LIT:-c>' , '<STR_LIT>' , dest = "<STR_LIT>" , required = False , <EOL> help = "<STR_LIT>" ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> manager . run ( ) </s>
