📺 Develpreneur YouTube Episode

Video + transcript

SQL Sync Part 23

2022-11-01 •Youtube

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
10.7

thank you

18.89

[Music]

27

well hello and welcome back we're

28.8

continuing our series of tutorials on

31.74

SQL and python

33.559

MySQL primarily and this episode I was

37.62

going to dig into enumerated arguments

40.14

enumerated types in the database I think

43.5

that's less of an issue so we're going

46.14

to actually skip around a little bit

47.76

here and we're going to look at command

50.34

line parameters as far as setting some

52.62

stuff up for what to synchronize

55.02

so first uh I want to revisit command

58.5

line parameters a little bit in Python

60.66

so if we go in and we say python3 main

63.239

which is our python script

65.64

and we look at sys.rgv and we have to

69.659

import CIS to do this if we look at the

73.14

length that are it's which is an array

74.88

it's got one item in it and if we look

76.56

at what is in item one it is main.py

80.82

so pretty straightforward now what we

83.52

want to do

84.479

is we want to be able to do something

86.939

that says we're going to walk through

90.72

arguments so we're going to do like four

93.619

parm in

95.939

this Dot org V

100.56

and let's just do that let's just do

102

print

102.96

par

104.579

because what we want to do here and

107.34

we'll leave this as equipped what we

109.439

want to do is we want to do something

110.34

like this is so we're going to say hey

111.479

if we've got a question mark then this

113.46

can be help

114.899

and

118.5

it doesn't like that

121.979

just do it like that

127.68

okay so that's my one why is it not

129.66

picking that up

135.239

uh let's do it this way oh there we go

137.76

okay

138.66

oh it's because it doesn't like that

140.34

question mark

142.44

so we would have to do it as a this

146.7

there we go

148.62

because we have to actually

150.86

simplify those things out so now if we

153.239

do that we can see where we get these

155.94

two different uh parameters you know

159.12

zero the item and zero the first one is

161.64

just the name of the file so we can

162.9

ignore that and then after that we're

164.16

going to get some stuff

165.78

now what we could do is

168.36

we could do

170.459

something a little more

172.92

funky about this but we're going to keep

174.959

it simple so what we're going to do is

177.18

we're going to say

178.739

um

180.3

we're going to do it this way so we're

181.5

going to say

183.06

if

185.18

parm equals

188.7

or actually let's just do

191.879

if a question mark

193.98

and parm

197.459

and then we're gonna do print

200.4

uh let's say DB sync

203.64

version 1.0

208.81

[Music]

210.5

and this will be something about like

212.94

our help or something along those lines

217.68

be there we're going to do

223.14

uh application help

225.239

and then we're gonna come down and we're

226.5

going to say

233.28

I'm gonna say here

235.68

Please wrap all parameters

241.5

and parameters

244.019

and quote or

246.84

double quote

253.019

and then we're going to come through

253.98

each of these and we're going to say

255.48

this one oh

258

I think I hit that right there we go

260.22

and it's going to be this one

263.4

I'll do it like this

266.82

and we're going to say whoops

268.919

I'm going to say display

271.44

this help screen

274.08

whoops I have that caps lock let's not

276.06

yell at them

282.18

and then

283.86

so if we do this

286.74

uh so if we do like uh let's do the same

290.58

thing let's do one it's not going to do

292.5

anything if we do a

295.32

there then it says hey we're going to

297.66

display so we've got our little bit of

298.919

our our help kind of thing there

301.5

what I want to do is going through each

303.36

of these like key items and we're going

306.12

to add some parameters for that

309.18

so what we want to do is let's start

310.8

with we're going to have uh let's call

312.78

this feature Flags

317.28

and

320.58

we're just going to do this

323.58

and what we're going to do is we're

324.9

going to come in here and so let's say

326.52

we're going to do

327.72

print

330.24

feature Flags

335.94

then we'll come through and let's go

337.259

look at each of these so let's see the

338.58

first one we want to do is

341.22

let's say uh

345.06

so tables

352.44

so if t for table

359.039

is going to be

360.479

if you have a t

365.16

that's going to be synchronized tables

369.3

so synchronize tables

373.02

and then if T is in the palm

377.4

then all we're going to do is we're

378.96

going to say

379.86

a feature Flags Dot

383.94

append

385.979

and this will be T we'll do a capital T

388.199

and then we come down here

390.84

and then we're going to say if

393.419

t

394.8

n

396.479

feature Flags

398.28

then

399.479

we're going to do that we're going to do

401.16

the same thing for columns

403.5

this is pretty easy to do

405.6

actually let's do it there

409.259

so we can note that we're going to do it

412.38

only if we're actually going to do it

416.1

just same thing for columns

419.16

and then let's just say here if

423.9

let's take one of those

426.479

just to be sure so we're gonna do the if

428.34

t

431.52

print them and then we're going to save

433.38

here print

437.94

sync the tables

441.84

this is going to be a c

447.66

synchronized columns

452.22

oops I didn't write that right that's

454.38

okay

456.18

oops so R will be rho c will be columns

464.16

we'll have to add all of these in

468.3

we also want to do what

472.139

well

473.88

we want to do columns we want to do

477.539

indexes

479.94

we don't want to do procedures

485.28

and there's probably a couple more which

486.72

like functions and stuff but let's just

488.22

go with this for now

490.38

because this is actually pretty easy to

492.3

do because it'll be we'll do an I will

494.16

do a p

497.22

and

500.94

we're gonna do it like that oh

505.199

but now for each of these

507.9

we can actually build these out real

509.34

quick because if there's a c

511.139

then there's going to be a c

513.18

if there's an i

516.06

it's going to be an i

518.099

if there is a p

521.88

is going to be a p

523.32

so let's go look at this

525.24

so now if we run it

527.7

let's run it with

532.88

let's see what this uh let's do with

535.26

that and then

538.08

um

539.58

Dash p

543.36

Dash

547.76

uh t

551.76

so if we do it okay since there's a help

554.519

in there let's get rid of our help

558.66

there we go so it does say sync the

560.58

tables now there's an IAP did I double

563.519

something up

565.98

uh if there is a uh oh

571.019

let's see I want to do

576.959

if

582.959

let's do that let's actually make it so

585.66

that way we won't

587.76

screw it up so we have to have

590.399

because what's happening is there is an

592.14

i in the name and it was picking that up

594.48

so now if we look at it there we go so

596.76

we have p and t

598.26

I sync the tables

600.72

uh here we go for

603.36

that is going to be sink Rose

612.3

we want triggers

616.98

which will be interesting to do

619.399

because we already had a t this is going

622.019

to be procedures

626.399

let's do a TR

628.92

for trigger

631.5

f for functions

636.2

I4 indexes

645.54

and so we can have every one of those or

650.519

so we have we had a t a c an i a p

655.32

an f and a TR

669.18

uh what else did we have t r c t f i

673.32

okay so that's everything

675.18

and let's add one

679.68

let's do or

684.3

she'll do it this way

686.7

oh

689.7

that's what we're going to do here

695.16

let's see if that works

700.079

there we go

707.94

we come in here

712.8

and we're gonna do that and if we get

715.019

all

716.459

so now if we do Dash all

719.279

there we go we get everything in there

723.12

now it's going to do this for each one

725.72

so uh let's say what we can do here is

730.32

if

732.3

uh if we find this

738.66

then we're just gonna quit so if we have

741.48

help we're just going to quit

745.279

and we're going to do uh oh we do want

748.5

to do a dash all

759.2

and then we do

770.16

let's just do how do we want to do this

772.68

this will be good let's do

775.019

all equals false

777.6

because what we're going to do here is

779.279

let's skip all of the alls

782.519

and we'll do that a little separately

784.079

just so it's not quite as confusing

786.18

whoops

789.72

and we will separately deal with all

802.2

because what we're going to do is we're

804.06

going to say

805.74

if

808.68

all in parm

815.339

then we're going to

818.899

uh do I want to do that

822.12

now because it's okay if we can put each

824.339

of these in here so let's just go back

825.42

to that let's not get too complicated

827.279

here

831.24

let's just keep it that way so if

832.8

there's an awl it's going to add it so

834.72

we will see some of these things show up

836.04

multiple times but that's okay

840.839

because then it's going to run through

842.519

each of these guys so if I come in and

846.12

say just tables

848.88

so now I can come through

851.76

and well I don't need to print the

854.04

feature Flags nor do I need to do this

857.88

so now if I want to do just my tables

862.2

back into a t

864

and boom it's going to do my table oh

865.86

but it is doing the others

868.56

interesting

883.86

uh

886.62

let's go look at that again did I just

888.66

do that with an almond there nope

893.459

print

895.019

feature Flags

897.839

what did we just do

909.779

uh

917.88

so we just did something funky somewhere

920.04

in here let's see what do we do here

921.959

oh see he's got the whole

937.88

interesting so we are adding something

940.74

somewhere in here that

944.459

we need to figure out why it's building

946.5

all of those out

948.36

so I think that's a good thing to pick

950.399

up next time we will track this sucker

952.26

down it's probably a simple fix but

954.18

we'll do that and then dive into the

956.399

next piece uh for now we'll wrap this

958.86

one up and we will be back next time

961.5

continuing our tutorials so go out there

963.42

and have yourself a great day a great

965.22

week and we will talk to you next time

969.54

[Music]