Detailed Notes
Focus for this episode: This episode continues our cleanup and making the application more of a library while we add command line parameters to out main script for better usage.
This tutorial walks through the creation of a database synchronization tool in Python. It continues our Python and SQL Tutorials with a useful application that provides a deeper look at SQL structures and commands.
Repository For Code: git clone [email protected]:rbroadhead/dbsync.git
Transcript Text
thank you [Music] well hello and welcome back we're continuing our series of tutorials on SQL and python MySQL primarily and this episode I was going to dig into enumerated arguments enumerated types in the database I think that's less of an issue so we're going to actually skip around a little bit here and we're going to look at command line parameters as far as setting some stuff up for what to synchronize so first uh I want to revisit command line parameters a little bit in Python so if we go in and we say python3 main which is our python script and we look at sys.rgv and we have to import CIS to do this if we look at the length that are it's which is an array it's got one item in it and if we look at what is in item one it is main.py so pretty straightforward now what we want to do is we want to be able to do something that says we're going to walk through arguments so we're going to do like four parm in this Dot org V and let's just do that let's just do print par because what we want to do here and we'll leave this as equipped what we want to do is we want to do something like this is so we're going to say hey if we've got a question mark then this can be help and it doesn't like that just do it like that okay so that's my one why is it not picking that up uh let's do it this way oh there we go okay oh it's because it doesn't like that question mark so we would have to do it as a this there we go because we have to actually simplify those things out so now if we do that we can see where we get these two different uh parameters you know zero the item and zero the first one is just the name of the file so we can ignore that and then after that we're going to get some stuff now what we could do is we could do something a little more funky about this but we're going to keep it simple so what we're going to do is we're going to say um we're going to do it this way so we're going to say if parm equals or actually let's just do if a question mark and parm and then we're gonna do print uh let's say DB sync version 1.0 [Music] and this will be something about like our help or something along those lines be there we're going to do uh application help and then we're gonna come down and we're going to say I'm gonna say here Please wrap all parameters and parameters and quote or double quote and then we're going to come through each of these and we're going to say this one oh I think I hit that right there we go and it's going to be this one I'll do it like this and we're going to say whoops I'm going to say display this help screen whoops I have that caps lock let's not yell at them and then so if we do this uh so if we do like uh let's do the same thing let's do one it's not going to do anything if we do a there then it says hey we're going to display so we've got our little bit of our our help kind of thing there what I want to do is going through each of these like key items and we're going to add some parameters for that so what we want to do is let's start with we're going to have uh let's call this feature Flags and we're just going to do this and what we're going to do is we're going to come in here and so let's say we're going to do print feature Flags then we'll come through and let's go look at each of these so let's see the first one we want to do is let's say uh so tables so if t for table is going to be if you have a t that's going to be synchronized tables so synchronize tables and then if T is in the palm then all we're going to do is we're going to say a feature Flags Dot append and this will be T we'll do a capital T and then we come down here and then we're going to say if t n feature Flags then we're going to do that we're going to do the same thing for columns this is pretty easy to do actually let's do it there so we can note that we're going to do it only if we're actually going to do it just same thing for columns and then let's just say here if let's take one of those just to be sure so we're gonna do the if t print them and then we're going to save here print sync the tables this is going to be a c synchronized columns oops I didn't write that right that's okay oops so R will be rho c will be columns we'll have to add all of these in we also want to do what well we want to do columns we want to do indexes we don't want to do procedures and there's probably a couple more which like functions and stuff but let's just go with this for now because this is actually pretty easy to do because it'll be we'll do an I will do a p and we're gonna do it like that oh but now for each of these we can actually build these out real quick because if there's a c then there's going to be a c if there's an i it's going to be an i if there is a p is going to be a p so let's go look at this so now if we run it let's run it with let's see what this uh let's do with that and then um Dash p Dash uh t so if we do it okay since there's a help in there let's get rid of our help there we go so it does say sync the tables now there's an IAP did I double something up uh if there is a uh oh let's see I want to do if let's do that let's actually make it so that way we won't screw it up so we have to have because what's happening is there is an i in the name and it was picking that up so now if we look at it there we go so we have p and t I sync the tables uh here we go for that is going to be sink Rose we want triggers which will be interesting to do because we already had a t this is going to be procedures let's do a TR for trigger f for functions I4 indexes and so we can have every one of those or so we have we had a t a c an i a p an f and a TR uh what else did we have t r c t f i okay so that's everything and let's add one let's do or she'll do it this way oh that's what we're going to do here let's see if that works there we go we come in here and we're gonna do that and if we get all so now if we do Dash all there we go we get everything in there now it's going to do this for each one so uh let's say what we can do here is if uh if we find this then we're just gonna quit so if we have help we're just going to quit and we're going to do uh oh we do want to do a dash all and then we do let's just do how do we want to do this this will be good let's do all equals false because what we're going to do here is let's skip all of the alls and we'll do that a little separately just so it's not quite as confusing whoops and we will separately deal with all because what we're going to do is we're going to say if all in parm then we're going to uh do I want to do that now because it's okay if we can put each of these in here so let's just go back to that let's not get too complicated here let's just keep it that way so if there's an awl it's going to add it so we will see some of these things show up multiple times but that's okay because then it's going to run through each of these guys so if I come in and say just tables so now I can come through and well I don't need to print the feature Flags nor do I need to do this so now if I want to do just my tables back into a t and boom it's going to do my table oh but it is doing the others interesting uh let's go look at that again did I just do that with an almond there nope print feature Flags what did we just do uh so we just did something funky somewhere in here let's see what do we do here oh see he's got the whole interesting so we are adding something somewhere in here that we need to figure out why it's building all of those out so I think that's a good thing to pick up next time we will track this sucker down it's probably a simple fix but we'll do that and then dive into the next piece uh for now we'll wrap this one up and we will be back next time continuing our tutorials so go out there and have yourself a great day a great week and we will talk to you next time [Music]
Transcript Segments
thank you
[Music]
well hello and welcome back we're
continuing our series of tutorials on
SQL and python
MySQL primarily and this episode I was
going to dig into enumerated arguments
enumerated types in the database I think
that's less of an issue so we're going
to actually skip around a little bit
here and we're going to look at command
line parameters as far as setting some
stuff up for what to synchronize
so first uh I want to revisit command
line parameters a little bit in Python
so if we go in and we say python3 main
which is our python script
and we look at sys.rgv and we have to
import CIS to do this if we look at the
length that are it's which is an array
it's got one item in it and if we look
at what is in item one it is main.py
so pretty straightforward now what we
want to do
is we want to be able to do something
that says we're going to walk through
arguments so we're going to do like four
parm in
this Dot org V
and let's just do that let's just do
par
because what we want to do here and
we'll leave this as equipped what we
want to do is we want to do something
like this is so we're going to say hey
if we've got a question mark then this
can be help
and
it doesn't like that
just do it like that
okay so that's my one why is it not
picking that up
uh let's do it this way oh there we go
okay
oh it's because it doesn't like that
question mark
so we would have to do it as a this
there we go
because we have to actually
simplify those things out so now if we
do that we can see where we get these
two different uh parameters you know
zero the item and zero the first one is
just the name of the file so we can
ignore that and then after that we're
going to get some stuff
now what we could do is
we could do
something a little more
funky about this but we're going to keep
it simple so what we're going to do is
we're going to say
um
we're going to do it this way so we're
going to say
if
parm equals
or actually let's just do
if a question mark
and parm
and then we're gonna do print
uh let's say DB sync
version 1.0
[Music]
and this will be something about like
our help or something along those lines
be there we're going to do
uh application help
and then we're gonna come down and we're
going to say
I'm gonna say here
Please wrap all parameters
and parameters
and quote or
double quote
and then we're going to come through
each of these and we're going to say
this one oh
I think I hit that right there we go
and it's going to be this one
I'll do it like this
and we're going to say whoops
I'm going to say display
this help screen
whoops I have that caps lock let's not
yell at them
and then
so if we do this
uh so if we do like uh let's do the same
thing let's do one it's not going to do
anything if we do a
there then it says hey we're going to
display so we've got our little bit of
our our help kind of thing there
what I want to do is going through each
of these like key items and we're going
to add some parameters for that
so what we want to do is let's start
with we're going to have uh let's call
this feature Flags
and
we're just going to do this
and what we're going to do is we're
going to come in here and so let's say
we're going to do
feature Flags
then we'll come through and let's go
look at each of these so let's see the
first one we want to do is
let's say uh
so tables
so if t for table
is going to be
if you have a t
that's going to be synchronized tables
so synchronize tables
and then if T is in the palm
then all we're going to do is we're
going to say
a feature Flags Dot
append
and this will be T we'll do a capital T
and then we come down here
and then we're going to say if
t
n
feature Flags
then
we're going to do that we're going to do
the same thing for columns
this is pretty easy to do
actually let's do it there
so we can note that we're going to do it
only if we're actually going to do it
just same thing for columns
and then let's just say here if
let's take one of those
just to be sure so we're gonna do the if
t
print them and then we're going to save
here print
sync the tables
this is going to be a c
synchronized columns
oops I didn't write that right that's
okay
oops so R will be rho c will be columns
we'll have to add all of these in
we also want to do what
well
we want to do columns we want to do
indexes
we don't want to do procedures
and there's probably a couple more which
like functions and stuff but let's just
go with this for now
because this is actually pretty easy to
do because it'll be we'll do an I will
do a p
and
we're gonna do it like that oh
but now for each of these
we can actually build these out real
quick because if there's a c
then there's going to be a c
if there's an i
it's going to be an i
if there is a p
is going to be a p
so let's go look at this
so now if we run it
let's run it with
let's see what this uh let's do with
that and then
um
Dash p
Dash
uh t
so if we do it okay since there's a help
in there let's get rid of our help
there we go so it does say sync the
tables now there's an IAP did I double
something up
uh if there is a uh oh
let's see I want to do
if
let's do that let's actually make it so
that way we won't
screw it up so we have to have
because what's happening is there is an
i in the name and it was picking that up
so now if we look at it there we go so
we have p and t
I sync the tables
uh here we go for
that is going to be sink Rose
we want triggers
which will be interesting to do
because we already had a t this is going
to be procedures
let's do a TR
for trigger
f for functions
I4 indexes
and so we can have every one of those or
so we have we had a t a c an i a p
an f and a TR
uh what else did we have t r c t f i
okay so that's everything
and let's add one
let's do or
she'll do it this way
oh
that's what we're going to do here
let's see if that works
there we go
we come in here
and we're gonna do that and if we get
all
so now if we do Dash all
there we go we get everything in there
now it's going to do this for each one
so uh let's say what we can do here is
if
uh if we find this
then we're just gonna quit so if we have
help we're just going to quit
and we're going to do uh oh we do want
to do a dash all
and then we do
let's just do how do we want to do this
this will be good let's do
all equals false
because what we're going to do here is
let's skip all of the alls
and we'll do that a little separately
just so it's not quite as confusing
whoops
and we will separately deal with all
because what we're going to do is we're
going to say
if
all in parm
then we're going to
uh do I want to do that
now because it's okay if we can put each
of these in here so let's just go back
to that let's not get too complicated
here
let's just keep it that way so if
there's an awl it's going to add it so
we will see some of these things show up
multiple times but that's okay
because then it's going to run through
each of these guys so if I come in and
say just tables
so now I can come through
and well I don't need to print the
feature Flags nor do I need to do this
so now if I want to do just my tables
back into a t
and boom it's going to do my table oh
but it is doing the others
interesting
uh
let's go look at that again did I just
do that with an almond there nope
feature Flags
what did we just do
uh
so we just did something funky somewhere
in here let's see what do we do here
oh see he's got the whole
interesting so we are adding something
somewhere in here that
we need to figure out why it's building
all of those out
so I think that's a good thing to pick
up next time we will track this sucker
down it's probably a simple fix but
we'll do that and then dive into the
next piece uh for now we'll wrap this
one up and we will be back next time
continuing our tutorials so go out there
and have yourself a great day a great
week and we will talk to you next time
[Music]