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

Added "Deploy to Heroku with PostgreSQL" chapter including way to automatically run pending migrations on Heroku #49

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

mattwelke
Copy link

Hope my first contribution with new content looks good! I tried to follow the style and tone of the book as best as I could (friendly, conversational, but still short and to the point).

Every step I instruct the reader to do in the book is assuming they've completed the book so far at least to the point where they add the TodoItem model. I also tested each step as I went to make sure it worked properly, and it worked on my machine (Windows 10, PostgreSQL 10.2 via Docker).

I was able to get my own fork of the book hosted (https://welkie.gitbooks.io/littleaspnetcorebookfork/content/chapters/alternative-databases/postgresql.html) if you want to take a look and see how this would render if the pull request is accepted. I welcome your input on the way the code examples are laid out, etc.

@nbarbettini
Copy link
Owner

Thanks @Welkie! FYI, I'm currently super busy helping organize a conference in SF (https://iterateconf.io), so I won't be able to take a look right away. I'm excited to get contributions though! 😄

@mattwelke
Copy link
Author

@nbarbettini Sounds good. If you don't mind me bugging you for some advice though in the mean time, I was going to contribute another chapter or fix a few up according to the issues that we've got logged right now. But I'm new to submitting pull requests to other projects, so I'm not sure how to go about doing that. If I added to this pull request, it wouldn't be ideal because accepting all changes would be all or nothing. But if I do new branches for each addition I do, with individual pull requests, then the 2nd 3rd etc wouldn't be able to be merged after the 1st is merged (I'd have to manually update the branches for them with the latest code now that the 1st was merged). Unless my understanding of that isn't good? I'd appreciate some advice if you know the best way to do contributions like that.

@sadqiang
Copy link

Your link goes to a 404 page. Please kindly check it again. Thank you!

@mattwelke
Copy link
Author

@sadqiang GitBook unfortunately disabled my account after I created that book. They classified it as spam. I think they got confused assuming I was intending to commit copyright infringement or something. Unfortunately, we can't see my changes rendered unless we know how to simulate GitBook locally or the pull request is accepted. You can see the changes by checking the "Files changed" in this pull request though.

@nbarbettini
Copy link
Owner

@Welkie Good question - I'd definitely prefer to keep pull requests separate instead of adding to one giant PR.

If you create a new branch other-changes off master and make edits that don't depend on anything in this branch, then other-changes can be PRed and merged to master independently of this one. You won't have to wait for this PR to be merged.

Also, you can always merge or rebase the latest changes from master into a branch you are working on to keep it "fresh". My typical process for working on a branch is:

  • Create branch off master: git checkout -b other-changes
  • Edit or add content, push up to Github
  • Open a PR against master when it's ready for review
  • Make additional changes/updates
  • If necessary, rebase against master so everything in the branch sits on top of the latest commits to master
  • Merge via Github

@nbarbettini
Copy link
Owner

Hopefully I understood your question and scenario. If you need some help or just want to see how it works, shoot me an email and we can do a quick screenshare call.

@nbarbettini nbarbettini self-requested a review February 24, 2018 03:28
@nbarbettini
Copy link
Owner

@Welkie @sadqiang It is possible to use Gitbook locally: https://toolchain.gitbook.com/setup.html

That's super weird that Gitbook disabled your account. Other forks of the book have been hosted on Gitbook just fine. /shrug

@mattwelke
Copy link
Author

@nbarbettini That's basically how I planned to do it. Separate branches each with PRs. I didn't realize you could submit PRs for merging those branches into the origin repo's master. That helps and should keep the work for the origin repo's maintainers limited. They would just need to accept the individual PRs which probably won't conflict. I think that clears it up for me. I'll leave you alone to prepare for Iterate. Good luck! 👍

@nbarbettini nbarbettini added this to the 1.1 milestone Apr 6, 2018
@@ -0,0 +1,2 @@
# Alternative databases
Now that you've learned how to create and deploy a basic ASP.NET Core application using SQLite, you can explore other databases you can use.
Copy link
Owner

Choose a reason for hiding this comment

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

Let's add a note here explaining that this chapter is optional.

Copy link
Author

Choose a reason for hiding this comment

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

Added note indicating optional. This may change if chapter rearranged in future.

SUMMARY.md Outdated
@@ -31,4 +31,6 @@
* [Deploy the application](chapters/deploy-the-application/README.md)
* [Deploy to Azure](chapters/deploy-the-application/deploy-to-azure.md)
* [Deploy with Docker](chapters/deploy-the-application/deploy-with-docker.md)
* [Alternative databases](chapters/alternative-databases/README.md)
Copy link
Owner

Choose a reason for hiding this comment

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

I'm still trying to decide where this should go. I think it makes more sense before Deploy, but then the Deploy chapter might have to worry about which database people are using.

Copy link
Author

Choose a reason for hiding this comment

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

Awaiting feedback on proposal in comment below.


Using PostgreSQL for your application instead of SQLite will allow it to scale better and give you access to various PostgreSQL features like the [PostGIS extension](https://postgis.net/).

Thanks to the layer of abstraction we get from using Entity Framework Core, modifying our application to use a different database engine is less complex than you would think. We can use our existing models, only having to change the Entity Framework Core provider we use. We will add the [Npgsql EF Core provider](http://www.npgsql.org/efcore/index.html).
Copy link
Owner

Choose a reason for hiding this comment

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

I tried to avoid our/we in the book and stick with "you" - can you change the phrasing in this file to match?

Copy link
Author

Choose a reason for hiding this comment

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

Changed to "you" throughout chapter.


Thanks to the layer of abstraction we get from using Entity Framework Core, modifying our application to use a different database engine is less complex than you would think. We can use our existing models, only having to change the Entity Framework Core provider we use. We will add the [Npgsql EF Core provider](http://www.npgsql.org/efcore/index.html).

### Add provider Nuget package
Copy link
Owner

Choose a reason for hiding this comment

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

nit: NuGet

Copy link
Author

Choose a reason for hiding this comment

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

Changed to NuGet throughout chapter.


![pgAdmin showing the tables](pgadmin_aspnetcore_tables_created.png)

At this point, you can start your application, visit `localhost:5000` in your browser, and create and manipulate `Todo` items just like before, except this time the data is being saved in the PostgreSQL database. Notice how everything works as before, including:
Copy link
Owner

Choose a reason for hiding this comment

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

Can you rephrase this to avoid saying "at this point" twice?

Copy link
Author

Choose a reason for hiding this comment

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

Replaced all 'at this point' phrases with descriptive paragraph intros.

@nbarbettini
Copy link
Owner

Reviewed. I like it, thanks for the contribution! The tone fits the book well.

The only thing I'm still thinking through is

  • Where it should go chronologically. It currently feels a little weird to explore this after the application is deployed.
  • What other databases could be discussed in this chapter - I like PostgreSQL but it's lonely! 😄

@mattwelke
Copy link
Author

For sure. I'm thinking it's worth at least mentioning MongoDB, even though its provider is very beta right now. MySQL also has pretty good support.

I'll touch up the minor things from the review soon. In the mean time, if you have an idea for the best place chronologically for this topic, be sure to let me know.

@mattwelke
Copy link
Author

@nbarbettini I figured I'd look into cleaning this up now.

I was thinking about this point you made:

Where it should go chronologically. It currently feels a little weird to explore this after the application is deployed.

I figured what might be nice would be having it after the "Deploy to Azure" chapter, in a new chapter called "Deploy to Heroku". Heroku doesn't support SQLite, so this would be a good chance to show readers two new concepts:

  • Heroku hosting (including using the custom buildpack for hosting .NET Core on Heroku)
  • Switching database to PostgreSQL

It gives PostgreSQL more relevance as opposed to it being randomly recommended like my existing chapter seems to do, since PostgreSQL is the simplest RDBMS to use on Heroku.

Alternatively, I was also thinking it would make sense to have the database switched to PostgreSQL before the deploy to Azure. This would also give it a relevant role, showing users that SQLite usually only makes sense in development, and that we would use something heftier in production.

@mattwelke
Copy link
Author

I left the arrangement alone for now, but cleaned it up using your feedback. You can see the changes in 817bbf6.

@nbarbettini
Copy link
Owner

@Welkie I really like the idea of a Deploy to Heroku chapter! Would you be willing to write that?

This logical progression makes the most sense to me so far:

  • Use a database (note that it's SQLite, good for development)
  • ...
  • Deploy to Azure
    • Use Azure SQL Server
  • Deploy to Heroku
    • Use PostgreSQL
  • Deploy with Docker (with SQLite, mention that Postgres works too)

@mattwelke
Copy link
Author

mattwelke commented Apr 14, 2018

That sounds good to me! I played around with the Heroku buildpack before and I noticed some rough edges. Not sure if it was me or the buildpack itself. I'm going to look more into it and if I find that it works smoothly, I'll make that new chapter and merge the PostgreSQL content in.

I'm prepping for exams right now, so that will be something I get started with later.

@mattwelke
Copy link
Author

@nbarbettini I was able to get my Heroku deploy working.

This thread details what I had to do to get around the inability to run migrations via the Heroky CLI right now (at this point I'm not sure if that's a bug with the buildpack or if that's just me being dumb).

I don't think it's that bad actually now that I've done it. I think it can be explained simply in a Heroku chapter. Let me know what you think of that thread and if you give me the go ahead, I'll work on the Heroku chapter shortly and integrate my PostgreSQL stuff into it.

@nbarbettini
Copy link
Owner

If the author of the buildpack doesn't get back to you soon, I think it makes sense to go ahead and doc the workaround. 👍

@nbarbettini nbarbettini modified the milestones: 1.1, 2.0 May 3, 2018
@nbarbettini
Copy link
Owner

FYI I'm releasing version 1.1 today and planning on releasing another large update around the time that ASP.NET Core 2.1 is final. The new stuff we've been discussing here fits in well with the next update I think. 👍

@mattwelke
Copy link
Author

@nbarbettini I agree. I've been busy lately starting my first post grad job so this hasn't been on my radar. It will be soon though. :)

@mattwelke
Copy link
Author

Wanted to provide an update here. I was coordinating with the maintainers behind the .NET Core buildpack to find out the best way to go about deploying .NET Core to Heroku. There ended up being some disagreement on the best way to handle db migrations. There were two solutions found:

  • Adding code to manually grab a DbContext instance during program startup and running pending migrations.
  • Using a "Target" (<Exec Command="dotnet ef database update" />) to run the SDK during the deploy step to run pending migrations.

And another user put forth an idea, to use the official Heroku "Release" system to do migrations.

I was waiting for them to sort out what they think would be the best option but it hasn't gone anywhere and I'd like to finish the PR since I sent it in February and I think it would be useful for users. I'd welcome some input here and then I can finish the chapter with how we'll instruct our readers to deploy on Heroku.

@mattwelke
Copy link
Author

@nbarbettini
Rebased and redid the chapter to be a new "Deploy to Heroku with PostgreSQL" chapter. The debate on the best way to handle Heroku side migrations was never solved, so I decided to go with the solution where the source code is changed to run pending migrations automatically as the application starts up if it detects that it's running on Heroku. You can take a look at the commits to see this. Despite 2.1 having been released, which includes some slight differences in how the Program.cs code would look, I kept this based on 2.0, since we can tackle the 2.1 update later on.

@mattwelke mattwelke changed the title Added section on alternative databases, with chapter on PostgreSQL Added "Deploy to Heroku with PostgreSQL" chapter including way to automatically run pending migrations on Heroku Jul 11, 2018
@mattwelke
Copy link
Author

@nbarbettini Was wondering if you've had a chance to look at this? Also, one thing I was thinking was that this might be too much for this book, since its goal is to be focused on just introducing ASP.NET Core to people, and it already covers one deployment method. Curious what your thoughts are.

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.

4 participants