So let's create some migrations and to do that, the first thing I want to do is go back to the sort
documentation and see how this works.
So I want to create databases such as click on and see what happens.
So I'm not going to create a database this way.
What I'm going to do, first of all, is make sure that I have postgrads running.
I do.
And then I'm going to open Beevor and just clear out anything that's in there right now.
So I know I'm starting with an empty database, a database that has no tables.
So I am on here.
I want to go to.
Yeah, yeah.
There's a new I'll upgrade later.
I want to look and see what's inside my bookings database.
So I click on that and I look at it and I have under my schemas, under my public, under tables, I
have, I have three tables and I'm going to get rid of all of this.
So I'll right click on this and choose.
Delete.
Yes.
And the same thing with people.
I will delete that.
Yes.
And the same thing with fonts.
I will delete that.
Yes.
All right.
So I now have an empty database.
Great.
So let's Heidi Dvor and see what we do here.
How do we start working with us, getting started with pop.
That's where I want to be.
So gives me some documentation.
Supports postgrads.
Good.
We have it installed.
Good.
All right.
Let's go to configuration.
And this is what we need to do.
We need to actually create a configuration file for our database.
So I'm going to copy this.
And this needs to exist as a file copy under my code, right at the root level of my application.
I'll create a new file and I will call that database dot y y Emelle.
And I'll just I'm not going to add that to get and I'll tell you why later on.
I'm going to pass that in there.
So this describes how soda connects to our database and our dialect is postgrads.
That's great.
Our database name is actually called bookings I think.
Let's make sure.
Yep, bookings.
And the user is my name.
Your username will be your name, of course, and it has no password, so I'll leave that empty hostis
localhost.
That's one two seven zero zero one.
That's an IP address.
That is the Lookback address and always refers to the current machine.
And the pool is five.
And that's all I care about right now.
I'm not going to worry about test or production.
I just want this stuff set up.
So once this is created, I now can open a terminal window and clear out the screen and I can write
SOTA generate and I'm going to put the word filles because that's the language used to describe the
migration's used by SOTA and I'll create our users table, so I'll name my migration create user with
uppercase U table.
And when I do that it says step change to your file file.
What it's done for me and I'll just hide this screen is this created a new directory called Migration's
right here.
And inside of that it's created two new files.
One is called Create User Table Down.
That is the down migration and one is called create user table up.
And that's the up migration.
So let's open that one.
And that is an empty file.
And I don't want it to remain empty.
I actually want to do something with it.
And this file is where I can create a table and I think back to the documentation for soda, when you
look at things like Migration's right here, I want to create a migration's writing on migration.
So it says it tells me to do exactly what I just did.
So to generate this the name of the migration.
And then it creates two files.
They're both empty.
And I need to populate them with the things that I want.
I can use straight sequel migrations.
I can write a sequel by hand if I want to, but I'm not going to.
And I'll tell you why a little later on.
So let's look at example.
Let's look at an example of creating a fiz migration.
I want to create a table and here's an example.
So let's copy this one, because it's a great example and I'll paste it in to my my empty up migration
and make sure you have the up migration.
And I'm going to create a table users.
And I want this table to correspond to what I built when I described it.
So I have an idea.
First name and a last name in an email.
Let's do those four to start with Idy.
I'll manually put that in there.
I don't have to, but I'm going to tag column, Heidi.
It's going to be an integer.
And it will be primary true and what that does is say to it says Tiffy's, that this column Idy will
be of type integer and it's going to be my primary key.
OK, and the primary key is that ID field we used, what we used, what we described, our database
structure.
So what was the second thing?
I wanted to think it was first name, first name, last name and email.
So we already have an email.
Let's just duplicate that twice and changes to first name and change this to last name.
And it's that all that I want in there.
Well, no, in this case it's not.
I'm actually going to put something in these parentheses.
I want I'm going to say is the default value for this.
If nothing is specified, make that empty.
I'll do the same thing here.
Default, which has to be, in quotes, default, empty for email.
I don't want the email to be empty, so I'm going to leave that one as it is.
And what else do we have after email?
We have password created out and updated that.
So password, I had Barkha 255 here, but that's actually not what I'm going to use.
I'm going to make that a different length.
So let me go back here and change this to password.
It is of type string, but its size is going to be 60 and it's going to be 60 because the hashes we
generate instead of the password are always going to be exactly 60 characters long.
So make that one size of 60.
So that's password.
What else do we have?
We have created and updated at an access level.
OK, so we can leave, we can delete all of these and we can say access level and that will be an integer
and the default I'll make one.
And I'm not going to bother with created at an update about because one of the nice features of soda
is that it actually creates those columns for us.
All right.
So I've got this file created.
I seem to be connected to my database.
Everything set up properly in my database YAML.
Let's find out if it works.
So all I have to do is make sure I'm in the root level of my project and say, soda migrate.
And it says, all right, it said, created the user table, successfully applied one migration's the
info is the length of time it took and then it says server version is thirteen point one PPG dump version
is twelve point three.
All it's telling me is that the client I have for PGP dump is a different version than the client that
the server is using.
And that's just because I have two versions of postgrads installed, one for version twelve point three
and one for version thirteen point one.
That is an error I can safely ignore.
Now, what I have done this I've run this migration.
Let's go over to divert and let's refresh our public schema by clicking on it and choosing refresh.
And we should see two tables, one that we just created called user right here.
And there is user and I can look at the properties for columns the users has.
What let's refresh this.
They're there.
There's my first my ID, my first name, my last name, my email, my password access level.
And it automatically created those columns for us.
Perfect.
So what's this table schema migration?
Well, let's look at it.
This is one that SOTA creates.
If it doesn't exist the very first time it runs, it creates for you.
And if we look under data, you'll see that it has one one field called version.
And inside of that is a number.
And that number 20, 2011, and all those numbers following it, it corresponds to what you see in this
migration's folder, that number right there.
And that allows allows the sort of client to know the sort of program to know that migration has run.
Well, that's great.
What is the purpose of that down migration?
Let's have a look at that.
Inside of the down migration right here.
We currently have nothing.
And here's where you write the code to reverse the migration so that migration creates a user table
and the down migration is simply going to drop that table.
So I can actually run arbitrary sequel by putting the key word sequel and then in quotations, whatever
sequel I want to run, I want to drop the table users.
And that's all that I have to do.
So now if I come back here and I'll continue to get these warnings, I'll fix it.
After this election, I'll upgrade my PJI dump version so we don't see that error in the future.
But right now, just ignore it.
I can say sota migrate down and it will run the down script, which says drop the table users.
So if I go back to deliver and refresh my public schema, refresh that users table is now gone and if
I run, so to migrate, so to migrate again, it will create the user's table.
So now if I refresh this in here, refresh.
There's my users table, so the great thing about this is I can write all of the migration's for all
of my tables and rebuild my database, my development database right from scratch.
I can say drop everything by running all the down migration's and run up run all the migration's just
by saying migrate up and it will recreate my database structure.
And as I modified my database, as time goes on, I can just write Neumont a new migration's to add
a column, delete a column, change your columns, type, whatever it may be.
So this will be a really useful way for us to to manage our database structure.
Now a word about git.
I am never, ever going to commit this file database Whitemoor to get instead.
I will copy this.
I'll say copy all of this, this contents of that file and I'll create a new file under bookings called
New File and I'll call it database dot y database, the y amount dot example.
And I will add that one together and I will paste this in and I'll take out the username because you
never want to start a password and get and in this case this is an example.
So if I was to clone this repository, all I would do is copy database dot y melda example to database
AML and put in my username and password and database name and everything else.
So don't ever store passwords into git, particularly on a public git repository, because you know,
that might be a potential method for some attacker to get access to your database.
So what I'm going to do now is just go to my getting your file Dugit, ignore and add database y AML.
To my getting Nawfal, and that way it will never add add that file by mistake to get so what we'll
do in the next lecture is right, migration's for all of the other tables that we're going to be using.