Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

💾 Miis and the Pokemon were split into separate characters #7

Merged
merged 5 commits into from
Aug 13, 2020
Merged

💾 Miis and the Pokemon were split into separate characters #7

merged 5 commits into from
Aug 13, 2020

Conversation

agoodnap
Copy link
Collaborator

@agoodnap agoodnap commented Aug 11, 2020

I split the Pokemon and the Miis up as specified in issue #4 each with their own graphic and went ahead and removed Pokemon Trainer as I don't think they'll be needed anymore now that the 'mons are separate. (The files are still there in case we want to realize @ejbee3 's idea of having the trainer clickable and then the 'mons selectable) I also added the ReleaseOrder to the Character Model, and updated characters.sql to include it for each char. I just gave Echo characters a half-step (eg. Peach = 13, Daisy = 13.5), so they'll always be sorted after their 'original'.

When called from the api, all characters are now sorted by their ReleaseOrder before being shown.

I apologize to @ejbee3 in case this gets accepted, you said you were working on this but I got excited as this is my first project as a contributer and spent the last couple hours on this 😅

@agoodnap agoodnap changed the title 💾 Miis and the Pokemon were separated to separate characters 💾 Miis and the Pokemon were split into separate characters Aug 11, 2020
Copy link
Collaborator

@skittlz444 skittlz444 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On top of modifying the Models/characters.sql, which is useful for initialisation of new databases (i.e. new dev setup), you should modify the generated migration's "Up" method to use "migrationBuilder.Sql()" to set the values for the column. This means that when the migration is run, the characters database table doesn't need to be truncated to have the release order values set.

@Breadkenty
Copy link
Owner

Breadkenty commented Aug 12, 2020

@Sleeping-Dev this looks great and I'd like to accept but before I do, I wanna know a little more about what @skittlz444 is referring to. I usually never touch the migration files that dotnet ef migrations add ... creates but I'm assuming you are referring to this change?

protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<decimal>(
                name: "ReleaseOrder",
                table: "Characters",
                nullable: false,
                defaultValue: 0m);
        }

replacing the

migrationBuilder.AddColumn<decimal>(
                name: "ReleaseOrder",
                table: "Characters",
                nullable: false,
                defaultValue: 0m);

to

migrationBuilder.Sql(
                name: "ReleaseOrder",
                table: "Characters",
                nullable: false,
                defaultValue: 0m);

When you do migrations like this, you usually have to wipe the database to scratch to add things. But does this prevent you from wiping the database over to make changes to your models?

@agoodnap
Copy link
Collaborator Author

agoodnap commented Aug 12, 2020

migrationsBuilder.Sql(string command) allows you to run SQL Commands on the database as youre migrating, it's useful for situations like this, where we want to add additional columns and data to the database.

I believe what they meant was to do something along the lines of this:

migrationsBuilder.AddColumn<decimal>(
                name: "ReleaseOrder",
                table: "Characters",
                nullable: false,
                defaultValue: 0m);
migrationBuilder.Sql("<SQL COMMANDS FOR ADDING RELEASE ORDER TO EVERY ENTRY>");

so that when applying migrations, one wouldn't have to add the ReleaseOrder values manually. I'm thinking of solving this by just running an sql script ("addReleaseOrderValues.sql") that goes through each character entry and adds their respective ReleaseOrder value. It would look similar to "characters.sql", but of course just altering entries instead of adding new ones. In code it would look something like this:

migrationsBuilder.AddColumn<decimal>(
                name: "ReleaseOrder",
                table: "Characters",
                nullable: false,
                defaultValue: 0m);
migrationBuilder.Sql(File.ReadAllText("addReleaseOrderValues.sql"));

For this to work in prod I believe we'd need to add addReleaseOrderValues.sql to the publish directory like this:

In Smash_Combos.csproj:
<ItemGroup><Content Include="Models\addReleaseOrderValues.sql" CopyToPublishDirectory="PreserveNewest" /></ItemGroup>

Otherwise it won't be copied when deploying, then migrations can't be applied, etc. etc.

One drawback would be that this requires an additional .sql file to be added to prod. I could type the commands out by hand but that just... seems inefficient. On the other hand its just a one-time thing...

Thoughts on this?

Edit: Thinking about this more, I'll probably just type it out by hand. This really is a one time thing and adding an entire new file for just this migration feels very wrong. I'll check back in after work.

@skittlz444
Copy link
Collaborator

@Sleeping-Dev is right with what I meant. Since it is a once off, it usually is best to type it out into the migrate up method rather than adding a new file.

@agoodnap
Copy link
Collaborator Author

Alright, I'll write it up and commit after work 👍

@agoodnap
Copy link
Collaborator Author

agoodnap commented Aug 12, 2020

I finished the update statements in the migration and am pretty much ready to commit. But when testing it I noticed that currently when setting up, it is still required to manually run psql Smash_ComboDatabase --file=Models/characters.sql as the characters are not added automatically during a migration.

I tested the migration by dropping and recreating the database, and as I forgot to run psql Smash_ComboDatabase --file=Models/characters.sql there were no characters for the current migration to update.

We could avoid this by automatically inserting the characters into the database during the AllTables-Migration I think?
That would remove the need to keep a characters.sql file and make the setup for other devs easier.

I don't think there's any problems with that, since I'm only adding Insert statements to an existing migration, right? That shouldn't impact databases that already have this migration applied, since those probably already have the data inserted anyway, so they're not missing out on any structural changes.

@agoodnap
Copy link
Collaborator Author

agoodnap commented Aug 12, 2020

I went ahead and tried it out and it worked, couldn't find any problems! Basically what I changed:

  • Changed the Migration name to something more accurate
  • Added the data updates to the newest migration as we discussed. Instead of using migrationBuilder.Sql() I used migrationBuilder.UpdateData(), it looks a bit cleaner than a long string.
  • Changed the ReleaseOrder for Yoshi in characters.sql from 6 -> 5, that was a typo from the last commit.
  • Added the Character insertions from characters.sql into the Migration "AllTables", that way when setting up an environment for the app to run, there is no more need to run psql Smash_ComboDatabase --file=Models/characters.sql, as the characters are automatically inserted when running dotnet ef database update

characters.sql is now obsolete, as its only purpose was to fill the database with characters when setting up an environment, that now happens automatically, should be cleaner 👍

This probably could have been its own issue, but as I was editing migrations anyway I figured I'd just take care of it now, I'll split the commits up if necessary.

Thoughts?

@skittlz444
Copy link
Collaborator

@Sleeping-Dev Brilliant, I really like the changes to make running the characters sql obsolete. It all looks good, the only thing left to do is to update the contributions readme to reflect the changes of not needing to run characters sql anymore. Good work.

@skittlz444 skittlz444 linked an issue Aug 13, 2020 that may be closed by this pull request
5 tasks
@Breadkenty
Copy link
Owner

Wow @Sleeping-Dev, I'm still learning what you're doing here but this is awesome great job!

@agoodnap
Copy link
Collaborator Author

agoodnap commented Aug 13, 2020

Thanks guys 👍
Alright, the documentation is updated and I also went ahead and deleted characters.sql while I was at it! @skittlz444 I think this should reflect your requested changes, feel free to approve this one.

Ty @Breadkenty, if you have any questions I'll gladly help, just message me on discord!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Separate Mii and Pokemon Trainer into individual characters
3 participants