Do you need to alter your database? Migrations provide a great way to do this, but the approach you'll need to use differs depending on what's already in production. In this lesson, we'll discuss two different flows you can use to alter your database. One, when working completely locally and another when a migration has already been executed on your production database.
👍 If You Enjoy This Video, Consider Hitting The Like Button!
🥁 Subscribe To Stay Notified For New Lessons
📺 View on YouTube
👨💻 Find The Code For This Lesson Here
📚 Chapters:
- Intro
- Altering A Local-Only Column
- Altering A Column Already In Production
- Adding A New Column To A Local-Only Table
- Adding A New Column To A Table Already In Production
- Seeding A New Column With Data After Migrating
- Outro
📜 Transcript:
in this lesson we're going to talk about
the flow of migrations and how that
differs whenever you're working with
just a local database versus a database
that already has production data on it
so the reason that this differs is
because whenever you roll back
migrations that's going to get rid of
any tables columns or whatever within
your database that that migration
changed as well as any data that that
table column or what have you might be
storing and it's very vital that you
never run the risk of accidentally
purging production data from your
database so let's say both our roles and
users migrations have already run on
production we would not want to make any
changes to either of these migrations
and instead create a separate migration
to alter either of these two tables if
we needed to so in this lesson we're
going to take a look at that in
particular so first let's start with the
easy stuff and say that we're still just
working locally for example if we wanted
to go about adding in a default value to
our role ID column here at present point
in time and we can confirm this by
taking a look at our database structure
the column default for our role ID is
null so if we do not explicitly set this
whenever you user is created it's just
going to be set to null and at the
database level we can set that via a
default to value and then specify a
value for this we can do roles off of
our enum and specify it as member now
since we're working locally and this
migration hasn't actually hit a
production database we're free to go
ahead and make the change directly
within our user's migration and in the
way that we would go about getting this
change actually applied to our database
is by jumping into our terminal here and
running node Ace migration and you can
run refresh to rowback and then run
again or you can actually roll back and
then node Ace migration run individually
so refresh would just run both of those
commands within one go but there you can
see it individually so we rolled back so
we reverted both of these migrations and
then we just re-ran them so that would
pick up that default to Value change
that we applied to our role ID so now if
we go take a look at our column default
for our roll ID let's give it a refresh
here back into the structure you can see
now it's set to one so that change did
get applied successfully just by
changing our existing migration however
if we were to undo that change roll back
and rerun so now we're back to that
default value being null and now let's
say that this has already run in
production so our users table and our
roles table already exist within
production at that point in time we
would not want to make that alteration
directly within this migration file and
instead we would want to create a
separate migration file to make that
change so we can jump back into our
terminal here and run node Ace make
migration to create a separate migration
and you can name this whatever you've
deemed applicable I'm going to call it
users underscore add underscore role ID
default and then we can also specify the
table that this should get applied to so
we can do hyphen hyphen table equals and
then specify it for our users and what
that will do is if we actually jump into
that migration file here it will default
the table name to users and give us an
alter table up and down method call so
if we contrast that with our previous
two migrations these were set with
create table by default this one will be
set to alter table and now you might be
thinking okay so all that I need to do
is reassign that column within this
alter and then reapply the default to or
whatever change it is that you want to
make and you should be okey dokey to go
ahead and just rerun your migration so
let's go ahead and try that out and see
what happens so node Ace migration run
and you'll see that we're greeted with
an error because it's trying to alter
the table by adding the column roll ID
with that default value so it's
attempting to add in another column
called roll ID and it's running into an
area because that column obviously
already exists from our previous
migration so if I was adding in an
additional column that did not exist on
my users table you would just Define
whatever column it is that you want to
exist within your database now and it
would run just fine but if you're
altering an existing column all you need
to do is Define what column it is that
you want to Target so in this case table
dot integer role underscore ID specify
that you want to alter that column and
then make the alteration so now if we
give this a save jump back into our
terminal here I'm going to clear that
out and let's run node migration run
again that will run successfully because
now it knows that it needs to alter a
column instead of creating a column so
if we jump back into our database here
refresh you can see now our default is
set back to one just as we'd like it so
that's the approach that you would want
to take if you're working with
migrations that have already run on
production because had we made the
change within our users migration we
would have had to have rolled back and
then rerun the migration in order to
pick that change up which would have
purged our data from this user's table
and obviously we don't want to delete
all of our users so next let's take a
look at the use case where maybe we want
to add in an additional column so for
example on our roles table here let's
say maybe we wanted an additional
description column or something of the
sort again if we were working locally
all that we'd need to do is table that
string description give it a limit and
set it to nullable or not nullable again
the default is going to be nullable so
if you want it to be knowledgeable you
can just leave that off and then we
would need to roll back so node Ace
migration roll back now let's pause here
for a second and take a look at our
Adana schema here let's take a look at
the actual data within it and let's note
the batch column here so our role and
users tables were both created within
batch one whereas our user roll ID
default value was created within batch
two remember a batch is incremented
every time that you run so whenever we
first ran that was creating our role and
users table and then whenever we second
ran that was adding in our default roll
ID value and the rollback command we'll
just go back a single batch so if we
were to just roll back by the default we
would just be undoing our default rule
ID setting if we take a look at the help
options for this you can see that we
have within here an option to specify
the batch number that we want to go back
to so we could do node Ace migration
rollback hyphen hyphen batch and you can
see in the a note here use zero to roll
back to the initial state so we could do
zero so in order to run all the way back
we can specify 0 for that and now we'll
undo everything there is an alternative
command that you can do for this as well
and that is migration reset that will
just roll back all the way to zero so
you can take advantage of both of those
options if you don't need to go all the
way back then you would want to use
migration row back and then specify the
specific batch number that you want to
go back to within your Adana schema
batches back to the point at hand though
in order to pick up the change of adding
our description onto our rules migration
here we would need to go ahead and just
node Ace migration run to recreate all
of these and now one last thing to note
here is now if we refresh our Dyna
schema all of these are within batch one
because now they're all created within
the same run but if we take a look at
our rules now we have a description
within here as well again let's go ahead
and run through the use case of that
being production so let's do node Ace
migration roll back get rid of that
change that we added to our roles
migration and now let's create another
migration to actually apply that change
because now we're taking a look at what
this use case would look like if roles
had already run within our production
database so node is make migration and
we can call this roles underscore add
description or whatever you feel is
applicable there hyphen hyphen table
roles and let's dive into there so since
it does not exist when they're in a
database all that we need to do is
Define it so table.string description
255 nullable and that should do it so
now we should be good to go ahead and do
node Ace migration run so now if we
refresh our roles and you'll see that
our description was added in despite it
being all the way in the back it does
indeed exist let's go ahead and do node
Ace migration roll back again and now
let's say that our roles already exist
within our production database we have
our member and our admin and now we want
to add the description column but we
also want to seed that with some data
for both our member and our ad so we can
make use of this defer again to do that
async DB and how you do this is up to
you and again this might look forward
since we haven't covered querying or
inserting or anything on the sort yet
beyond what we did within our previous
migration lesson but let's go ahead and
just await DB from this dot table name
dot where ID is roles Dot and let's
specify admin first I suppose and then
we can call update description and say
our
application
super user and we can do await DB from
this table name where ID roles dot
member update description our
application
default user and now after the
alterations made on a Rolls table to add
in the description column it will run
this with the fur method which will then
query our admin record and update it
with a description value and then it
will do the same for our member as well
and then lastly would also be good to
fill in our down method So within our
down method all that we would want to do
is undo whatever we're doing with an up
we don't need to worry about getting rid
of what we did within our this dot defer
method since getting rid of the column
as a whole would get rid of that value
there as well so we can do table dot
drop column and specify it to drop our
description we can do the same for our
default value as well this would look a
little bit different since we need to
alter it so table dot integer row ID dot
alter
default to
null so since we don't want to get rid
of the role ID column altogether we
still want it to exist and it would be
our user's migration that would get rid
of it all together if we needed to all
that we want to do within this migration
is just alter it back to having a
default value of null should this
migration get rolled back so now we can
give this a save I'm going to check node
Ace migration status to see where we
left off looks like none of them have
run so we can clear that and then run
node Ace migration run all right now
that all of those have run let's verify
that our roles now have a description
value so let's jump back into our roles
and there we go we can see our
application default user and our
application super user for our member
and admin have been populated now let's
go ahead and try roll back it's a node
Ace migration
roll back and all of those successfully
reverted so it looks like both of these
ran without a hitch as well so the
important takeaway from this lesson is
if you have tables or migrations that
have already executed within your
production environment don't alter those
migrations at all create separate
migrations to make those alterations
tables if you're working locally and
nothing has hit production yet then
you're A-Okay to make whatever changes
you need just roll it back rerun them
and you'll be good to go
[Music]
thank you
[Music]
Join The Discussion! (2 Comments)
Please sign in or sign up for free to join in on the dicussion.
raymondcamden
"Show Ful Transcript" is not working here.
Please sign in or sign up for free to reply
tomgobich
Thank you, Raymond! The selectors for this were scoped to
.body-copy
and I guess I accidentally removed that class while refactoring.Please sign in or sign up for free to reply