def write_migration(name, comment, prefix_dir, ostr):
    """
    Writes a new migration.
    It generates a new version, constructs the correct filename and path
    Updates the App and Version tables and writes ostr to the new filen.
    @param name:    Name of the new migration. 
    @param ostr:    Content that will be written to the new migration.
    """
    version = get_new_version()
    verstring = powlib.version_to_string(version)
    # will be saved in the versions table and used to load the module by do_migrate
    modulename = verstring +"_" + name 
    filename = modulename + ".py"
    
    #update the app table with the new version
    update_app_and_version(version, modulename, version, comment )
    
    ofile = open(  os.path.normpath(os.path.join(prefix_dir + "/migrations/", filename)) , "w+") 
    ofile.write(ostr)
    ofile.close()
    return filename
示例#2
0
def render_migration(name, model, comment, col_defs = "", PARTS_DIR = powlib.PARTS_DIR, prefix_dir = "./"):
    """
    Renders a database migration file.
    @param name:        A Name for the migration. By default the modelname is taken.
    @param model:       Modelname for this migration (typically defining the model's base table)
    @param comment:     a Comment for this migration
    @param col_defs:    pre defined column definitions of the form NAME TYPE OPTIONS, NAME1 TYPE1 OPTIONS1, ...
    @param PARTS_DIR:   A relative path to the stubs/partials dir from the executing script.
    @param prefix_dir:  A prefix path to be added to migrations making prefix_dir/migrations the target dir
    """
    
    # add the auto generated (but can be safely edited) warning to the outputfile
    infile = open (os.path.normpath(PARTS_DIR + "/db_migration_stub.py"), "r")
    ostr = infile.read()
    infile.close()

    # Replace the TAGGED Placeholders with the actual values
    ostr = ostr.replace( "#DATE", str(datetime.date.today() ))
    pluralname = powlib.plural(model)
    ostr = ostr.replace("#TABLENAME", pluralname)
    
    #
    # Add / Replace the column definitions with the given ones by -d (if there were any)
    # 
    if col_defs != "None":
        ostr = transform_col_defs( ostr, col_defs )

    # generate the new version
    version = get_new_version()
    verstring = powlib.version_to_string(version)

    print "generate_migration: " + name + " for model: " + model

    # really write the migration now
    write_migration(name, model, comment, prefix_dir, ostr)

    return
def render_migration(name, model, comment, col_defs = "", PARTS_DIR = powlib.PARTS_DIR, prefix_dir = "./"):
    # 
    #print "generate_migration: " + name + "  for model: " + model
    #
    
    # add the auto generated (but can be safely edited) warning to the outputfile
    infile = open (os.path.normpath(PARTS_DIR + "/can_be_edited.txt"), "r")
    ostr = infile.read()
    infile.close()
    
    # add a creation date
    ostr = ostr + os.linesep
    ostr = ostr + "# date created: \t" + str(datetime.date.today())
    ostr = ostr + os.linesep
    
    # Add the model_stub part1 content to the newly generated file. 
    infile = open (os.path.normpath( PARTS_DIR + "db_migration_stub2_part1.py"), "r")
    ostr = ostr + infile.read()
    infile.close()
    
    pluralname = powlib.plural(model)
    ostr += powlib.tab +  "table_name=\"" + pluralname + "\""
    ostr += powlib.linesep
    #print "modelname was: " + model + "  pluralized table_name is:" + pluralname
    
    # Add the model_stub part2 content to the newly generated file. 
    infile = open (os.path.normpath( PARTS_DIR + "db_migration_stub2_part2.py"), "r")
    ostr = ostr + infile.read()
    infile.close()
    
    #
    # Add / Replace the column definitions with the given ones by -d (if there were any)
    # 
    if col_defs != "None":
        ostr = transform_col_defs( ostr, col_defs )
        
    app = powlib.load_class( "App", "App")
    app_versions = powlib.load_class( "Version", "Version")
    sess = app.pbo.getSession()
    app = sess.query(App.App).first()
    
    version = app.maxversion
    oldmaxversion = version
    version += 1
    
    verstring = powlib.version_to_string(version)
    print "generate_migration: " + name + " for model: " + model
    #print "version: " + str(version)
    #print "version2string: " + verstring
    filename = os.path.normpath ( "./migrations/" + verstring +"_" + name +".py" )
    
    #update the app table with the new version
    #appTable.update().values(maxversion= str(version) ).execute()
    app.maxversion = str(version)
    app.update()
    app_versions.filename = str(verstring +"_" + name )
    app_versions.version = str(version)
    app_versions.comment = str(comment)
    app_versions.update()
    print " -- maxversion (old,new): (" + str(oldmaxversion) + "," + str(app.maxversion) +")"
    ofile = open(  os.path.normpath(os.path.join(prefix_dir,filename)) , "w+") 
    print  " -- created file:" + str(os.path.normpath(os.path.join(prefix_dir,filename)))
    ofile.write( ostr )
    ofile.close()
    return
示例#4
0
def render_migration(name, model, comment):
    # 
    
    #print "generate_migration: " + name + "  for model: " + model
    
    # add the auto generated warning to the outputfile
    infile = open (os.path.normpath(PARTS_DIR + "/can_be_edited.txt"), "r")
    ostr = infile.read()
    infile.close()
    
    # add a creation date
    ostr = ostr + os.linesep
    ostr = ostr + "# date created: \t" + str(datetime.date.today())
    ostr = ostr + os.linesep
    
    # Add the model_stub part1 content to the newly generated file. 
    infile = open (os.path.normpath( PARTS_DIR + "db_migration_stub2_part1.py"), "r")
    ostr = ostr + infile.read()
    infile.close()
    
    pluralname = powlib.plural(model)
    ostr += powlib.tab +  "table_name=\"" + pluralname + "\""
    ostr += powlib.linesep
    #print "modelname was: " + model + "  pluralized table_name is:" + pluralname
    
    # Add the model_stub part2 content to the newly generated file. 
    infile = open (os.path.normpath( PARTS_DIR + "db_migration_stub2_part2.py"), "r")
    ostr = ostr + infile.read()
    infile.close()
    
    #ostr += powlib.tab + powlib.tab + powlib.tab +  "Column('id', Integer, Sequence('" + model +"_id_seq'), primary_key=True),"
    #ostr += powlib.newline
    
    app = powlib.load_class( "Appinfo", "Appinfo")
    app_versions = powlib.load_class( "Version", "Version")
    sess = app.pbo.getSession()
    app = sess.query(Appinfo.Appinfo).first()
    
    version = app.maxversion
    oldmaxversion = version
    version += 1
    
    verstring = powlib.version_to_string(version)
    print "generate_migration: " + name + " for model: " + model
    #print "version: " + str(version)
    #print "version2string: " + verstring
    filename = os.path.normpath ( "./migrations/" + verstring +"_" + name +".py" )
    
    #update the app table with the new version
    #appTable.update().values(maxversion= str(version) ).execute()
    app.maxversion = str(version)
    app.update()
    app_versions.filename = str(verstring +"_" + name )
    app_versions.version = str(version)
    app_versions.comment = str(comment)
    app_versions.update()
    print " -- maxversion (old,new): (" + str(oldmaxversion) + "," + str(app.maxversion) +")"
    ofile = open(  filename , "w+") 
    print  " -- created file:" + str(filename)
    ofile.write( ostr )
    ofile.close()
    return