Coder Social home page Coder Social logo

Comments (4)

Daemach avatar Daemach commented on July 26, 2024

Additionally, schema.drop( "table" ) does not work consistently. Changing this to schema.drop( "table", {}, true ) did work.

from cfmigrations.

elpete avatar elpete commented on July 26, 2024

I need some more information on this before I can work on it. A reproducible example would be best, but it seems like that is part of the problem according to what you wrote above. If you can at least give the conditions and code this is happening with, even if it isn't reproducible 100% of the time, that would be something. I'd like:

  1. CFML Engine and Version
  2. Database Type and Version
  3. JDBC Driver and Version
  4. All migration files trying to be ran.
  5. ColdBox Version
  6. cfmigrations version

Additionally, schema.drop( "table" ) does not work consistently. Changing this to schema.drop( "table", {}, true ) did work.

This is especially bizarre to me since schema.drop( "table", {}, true ) is just putting in the default values.

from cfmigrations.

Daemach avatar Daemach commented on July 26, 2024
  • Lucee 5.3.5+9
  • MSSQL 2019
  • net.sourceforge.jtds.jdbc.Driver (maybe I should switch this to the microsoft driver...)
  • Coldbox 5.6.2+1148
  • cfmigrations (2.0.3)

I'm with you on the bizarre. I ran it repeatedly and it kept skipping the drop, causing errors. When I added true it started working...

And this needs a dropIfExists for columns too, (any drop really), to avoid having to put them in try/catch blocks which may miss other errors.

2020_04_15_092429_addGalleryTable:
component {

function up( SchemaBuilder schema, QueryBuilder query ) {
    try{
        schema.create( "Galleries", function( Blueprint table ) {
            table.increments( "id" );
            table.integer( "userID" );
            table.string( "subdomain" );
            table.string( "galleryName" );
            table.string( "about" );
            table.timestamp( "createdDate" ).default("getDate()");
            
        } );
    } catch (any e) {}

    try {
        schema.alter( "Images", function( table ) {
            table.addColumn( table.integer( "galleryID" ).nullable("true") );
        } );
    } catch (any e) {}

    try {
        schema.alter( "Users", function( table ) {
            table.addColumn( table.timestamp( "createdDate" ).default("getDate()") );
            table.addColumn( table.timestamp( "updatedDate" ).default("getDate()") );
            table.addColumn( table.timestamp( "lastLogin" ).default("getDate()") );
        } );
    } catch (any e) {}

}

function down( SchemaBuilder schema, QueryBuilder query ) {

    // schema.alter( "Galleries", function( table ) {
    //     table.dropConstraint( table.primaryKey() );
    // } );

   schema.dropIfExists( "Galleries",{}, true );
//    dump( pita.toSQL() )
//    abort;
    schema.alter( "Images", function( table ) {
        table.dropColumn( "galleryID" );
    } );
    schema.alter( "Users", function( table ) {
        table.dropConstraint( "df_Users_createdDate" );
        table.dropColumn( "createdDate" );
        table.dropConstraint( "df_Users_updatedDate" );
        table.dropColumn( "updatedDate" );
        table.dropConstraint( "df_Users_lastLogin" );
        table.dropColumn( "lastLogin" );
    } );

    // dump("wtf");abort;
}

}

2020_04_16_084055_updateGalleryData

component {

function up( schema, query ) {
    userList = query.newQuery().from( "users" )
            .orderBy("userID")
            .orderby("email")
            .get();

    lastEmail = "";
    currentUserID = 0;
    deleteList = [];

    for ( row in userList ){

        // If a user has multiple accounts currently, we're going to use the first one and delete the rest
        // We are sorted by email address so compare the last email with the current one to see if it's a duplicate
        if (lastEmail != row.email){

            // Fresh - init vars
            lastEmail = row.email;
            currentUserID = row.userID;

        } else {
            // Duplicate user. Add this ID to a list to delete when the updates are done
            deleteList.append(row.userID);
        }

        // Insert a new gallery record, returning the new galleryID for use in the next query
        // Each new gallery will be created with the currentUserID, AKA the first user account we came across
        newGalleryID = query.newQuery().from( "Galleries" )
                            .returning( "id" )
                            .insert({
                                "subdomain" = row.username,
                                "userID" = currentUserID,
                                "galleryName" = row.username,
                                "about" = ""
                            });

                            // dump(newGalleryID.query.ID);abort;

        // Update the images table, using the galleryID we just created.  
        // This gallery (new subdomain) belongs to a separate userID currently, though.
        // So we use the existing userID to find the right images to attach to the new gallery record.
        query.newQuery().from( "Images" )
                        .where("userID", row.userID)
                        .update({
                            "galleryID" = newGalleryID.query.ID
                        });


    } 
    // Delete the extra user records.
    // if (deleteList.len()){
    //     query.newQuery().from( "Users" )
    //                 .whereIn( "userID", deleteList )
    //                 .delete()
    // }

    // Clean up old columns
    // schema.alter( "Users", function( table ) {
    //     table.dropColumn( "username" );
    // } );
    
    // Must delete constraints before columns.
    // schema.alter( "Images", function( table ) {
    //     table.dropConstraint( "DF_images_userID" );
    // } );

    // schema.alter( "Images", function( table ) {
    //     table.dropColumn( "userID" );
    // } );

        // Users => Galleries => Images 
}

function down( schema, query ) {
    
}

}

2020_04_16_091359_changeIds

component {

function up( schema, query ) {
    schema.alter( "users", function( table ) {
        table.renameColumn( "userID", table.string( "id" ) );
    } );
    schema.alter( "images", function( table ) {
        table.renameColumn( "imageID", table.string( "id" ) );
    } );
    schema.alter( "images", function( table ) {
        table.renameColumn( "userID", table.string( "oldUser" ) );
    } );
}

function down( schema, query ) {
    try{
        schema.alter( "users", function( table ) {
            table.renameColumn( "id", table.string( "userID" ) );
        } );
        schema.alter( "images", function( table ) {
            table.renameColumn( "id", table.string( "imageID" ) );
        } );
        schema.alter( "images", function( table ) {
            table.renameColumn( "oldUser", table.string( "userID" ) );
        } );
    } catch (any e){}

}

}

from cfmigrations.

adael avatar adael commented on July 26, 2024

In my case, was because multiple migrations having same date in the name:

IE:
2021_04_28_170640_create_contact
2021_04_28_170640_create_post
2021_04_28_170640_create_contact_post

When running migrate down it tried to drop posts before contact_posts (which were not allowed by constraints)

Changing the date part in the migration filename, solved this error for me.

Your library is awesome BTW

from cfmigrations.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.