📺 Develpreneur YouTube Episode

Video + transcript

SQL Sync Part 20

2022-10-20 •Youtube

Detailed Notes

Focus for this episode: We wrap up some code cleanup in this episode. This will position us for a more user friendly approach moving forward.

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
foreign
[Music]
our Series where we're looking at we're
basically going through our SQL and
Python tutorial combining those into our
application and this episode We are
continuing doing some some cleanup we
essentially did a pretty much Brute
Force raw kind of script and now we're
going through and cleaning things up
extracting things out into a separate
class this MySQL synchronized that
theoretically we could do it with you
know have at some point an oracle or a
SQL Server
do it very SQL specific and we're doing
some cleanup here and we are getting
into our missing indexes uh is basically
where we left off last time so let's uh
dive right back into it
I can do our missing indexes
same thing we don't need a tables list
now
because it's actually going to be whoop
let's take this
and pop that there
and this is going to be do I still have
that yep I do okay so I've got that
built
this is going to be self.source tables
and should be good there
so we're going to do this again
here
and I'll do one more though oops
there we go
take this and
actually let's go ahead down to sink
Rose great missing column tables there
we go
I'm going to take that whole kit and
caboodle there
I don't know if we need all of it we're
going to find out
um so we did missing triggers
right
yep
now we've got missing columns
is good
um I don't know we need to return
missing columns but we will anyways
missing indexes
is I think all good
sync columns
okay so that's not going to be what we
need
so he's gonna be self dot Source tables
we need our two
um
interesting we created these queries
again each time so let's do that
Christian destination
pressure destination there we go
and we're gonna do it again this time
we're going to call him update cursor
I know this is not the most uh glamorous
of
our tutorials but
let's go ahead and get rid of those I
don't think we need those right now
uh let's see we don't need all those
prints we're good we're good we're good
we're coming through we found our
differences we building out our stuff
Bam Bam Bam we are good to go what is
this another string complaining about
now it might be referenced before
assignment
uh where is
let's look at whoops null string
equals not null
and let's do here
um let's make him equal to nothing right
here
there
so we should be good uh he's gonna come
around we're gonna do that and we get to
sink rows which again is going to be uh
self
we probably don't need a table yet
because we don't need tables this we do
want our Max rows
did I keep that I did not keep that my
mistake so let's go back up here
oh one more
and we come down here
the sink Rose
that's going to be
self.source
tables and
[Music]
we're all good in here do we need
anything else
oh we don't need that extra there
create missing tables
it's going to be the same thing
well
okay
so this just needs to be self
uh sorry
self.source
and let me get rid of that little
comment
looking good and then so here we go so
that's the end of our class
right
that should work
so let's keep the comments here
and we'll go over to Main
and now we really don't need any of that
oh
I don't know we're gonna need any of
that other than we're going to do import
um
all right let's see
from
DB sync
import
MySQL synchronize okay
and then we're going to come here
basically call it the same way but we're
going to come in here
[Music]
and we're going to do a uh I'm just
going to call sync equals
my SQL synchronize
and then we're going to do
sync Dot
connect to source
sync dot connect to destination
We compare the tables which is
probably here
compare tables
we don't need this and then we're going
to walk through these dot missing tables
uh let's actually create missing tables
I believe
oh
first we need sync Dot
do we need
I guess we do need missing tables so we
need missing tables
let's go look at that real quick
oh because we did
oh we need to set this in tables first
okay
uh let's see compare tables
set missing tables is that how that
works
so we set the missing compare
is based off of these so we have to get
the yeah we have to generate compare
first okay so that makes sense
the pair set create
um
we don't really need that we don't need
create missing tables here
now we're going to do Source uh sync dot
create whoop
missing columns
and uh
so what we're going to need here is when
we do missing columns we need to
actually execute it so we weren't
executing that I don't think so if we go
to
uh DB sync
missing columns
we were returning that let's see let's
go ahead
and do this
and missing columns
I print command
uh really I can just take the cursor
destination
destination.execute
I can probably do it like that okay so
we're gonna do it like that we're gonna
clean that up a little bit uh let's see
so that's columns
rows
dot sync rows
and that's going to be my X rows
and then sync.missing triggers
and he's going to have the two names
and then
sync Dot
missing procedures
there it is
I don't give it those two
uh see this is going to be sync.m mixing
functions
way back here
missing indexes
let's go ahead yeah we'll get these last
couple okay
sync dot missing indexes
doesn't need anything
um
some missing indexes is doing the same
I'm going to need to actually build this
out
and then we're good
uh and so we're going to print uh
application complete
and we're going to go over here oh I'm
going to spell it right
and we're going to go to missing indexes
and we're going to throw that little
thing in because it apparently is
creating our
yeah so it just does it here so we're
going to come through here for index in
indexes
uh let's just do this we'll do the same
thing here so we're going to do
um
self Dot destinationdb.cursor
execute failed to create
cursor 2 close boom
and sync columns is that not being
called
uh uh oh too many blank lines oh well
too many blank lines too bad
um
there we go
oh I wonder if it's supposed to be like
that oh okay my mistake sometimes it
complains it complains in different ways
okay this is a silly little cleanup
thing so we now have everything and it's
a very short script it's going to call
this if we call it it should run the
same
but it's going to give us some issues so
uh sync.connect to Source oh
because I probably didn't put oh I did
put itself
um
not sure
let's see
oh okay so these need at least that so
all right
so
it's a neat little thing but um so we'll
get that taken care of and next time
around we can continue looking at a
little bit of our cleanup here and
moving into the next piece apologies if
we went a little bit long this time but
hey that's you know somehow we got to go
through a couple of these sort of boring
things to clean some stuff up and make
sure we're you know crushing all the t's
dotting all the eyes
but until next time go out there have
yourself a great day a great week and we
will talk to you next time
[Music]
Transcript Segments
0.359

foreign

7.28

[Music]

28.82

our Series where we're looking at we're

32.579

basically going through our SQL and

34.559

Python tutorial combining those into our

36.78

application and this episode We are

39.66

continuing doing some some cleanup we

42.12

essentially did a pretty much Brute

44.34

Force raw kind of script and now we're

46.8

going through and cleaning things up

48.44

extracting things out into a separate

51.6

class this MySQL synchronized that

54.6

theoretically we could do it with you

56.52

know have at some point an oracle or a

58.64

SQL Server

60.96

do it very SQL specific and we're doing

64.68

some cleanup here and we are getting

66.54

into our missing indexes uh is basically

70.2

where we left off last time so let's uh

74.159

dive right back into it

76.38

I can do our missing indexes

78.96

same thing we don't need a tables list

81.24

now

82.68

because it's actually going to be whoop

85.979

let's take this

89.54

and pop that there

92.1

and this is going to be do I still have

94.2

that yep I do okay so I've got that

96.54

built

98.46

this is going to be self.source tables

103.939

and should be good there

107.939

so we're going to do this again

109.979

here

114.72

and I'll do one more though oops

119.7

there we go

121.5

take this and

125.939

actually let's go ahead down to sink

128.34

Rose great missing column tables there

130.739

we go

132.06

I'm going to take that whole kit and

133.98

caboodle there

135.3

I don't know if we need all of it we're

136.5

going to find out

138.18

um so we did missing triggers

141.9

right

143.04

yep

144.66

now we've got missing columns

148.44

is good

150.9

um I don't know we need to return

152.34

missing columns but we will anyways

153.72

missing indexes

161.48

is I think all good

165.72

sync columns

168.54

okay so that's not going to be what we

171.12

need

173.099

so he's gonna be self dot Source tables

177.54

we need our two

181.68

um

185.04

interesting we created these queries

187.2

again each time so let's do that

193.26

Christian destination

196.26

pressure destination there we go

200.879

and we're gonna do it again this time

202.26

we're going to call him update cursor

209.04

I know this is not the most uh glamorous

212.04

of

214.62

our tutorials but

217.44

let's go ahead and get rid of those I

218.819

don't think we need those right now

220.7

uh let's see we don't need all those

222.9

prints we're good we're good we're good

224.459

we're coming through we found our

225.54

differences we building out our stuff

228.36

Bam Bam Bam we are good to go what is

230.76

this another string complaining about

233.58

now it might be referenced before

235.2

assignment

239.4

uh where is

246.299

let's look at whoops null string

251.459

equals not null

258.68

and let's do here

267.72

um let's make him equal to nothing right

269.58

here

272.46

there

273.78

so we should be good uh he's gonna come

276

around we're gonna do that and we get to

278.46

sink rows which again is going to be uh

281.4

self

284.58

we probably don't need a table yet

286.38

because we don't need tables this we do

287.699

want our Max rows

289.919

did I keep that I did not keep that my

292.259

mistake so let's go back up here

295.08

oh one more

298.08

and we come down here

301.919

the sink Rose

304.44

that's going to be

307.32

self.source

311.46

tables and

313.44

[Music]

315.36

we're all good in here do we need

317.16

anything else

321.3

oh we don't need that extra there

325.979

create missing tables

331.199

it's going to be the same thing

334.139

well

337.56

okay

339.12

so this just needs to be self

349.56

uh sorry

351.3

self.source

356.24

and let me get rid of that little

359.16

comment

363.539

looking good and then so here we go so

366.419

that's the end of our class

368.039

right

369.78

that should work

373.139

so let's keep the comments here

379.5

and we'll go over to Main

383.1

and now we really don't need any of that

385.199

oh

386.639

I don't know we're gonna need any of

388.02

that other than we're going to do import

392.4

um

393.84

all right let's see

396.84

from

399.18

DB sync

400.979

import

402.78

MySQL synchronize okay

405.3

and then we're going to come here

415.979

basically call it the same way but we're

417.84

going to come in here

418.9

[Music]

420

and we're going to do a uh I'm just

423.419

going to call sync equals

425.3

my SQL synchronize

429.96

and then we're going to do

434.1

sync Dot

436.139

connect to source

441.18

sync dot connect to destination

445.86

We compare the tables which is

449.88

probably here

452.819

compare tables

455.039

we don't need this and then we're going

457.44

to walk through these dot missing tables

464.46

uh let's actually create missing tables

466.62

I believe

471.36

oh

473.4

first we need sync Dot

478.08

do we need

483.36

I guess we do need missing tables so we

485.759

need missing tables

487.56

let's go look at that real quick

491.039

oh because we did

494.66

oh we need to set this in tables first

497.819

okay

502.44

uh let's see compare tables

508.68

set missing tables is that how that

511.02

works

512.399

so we set the missing compare

517.02

is based off of these so we have to get

518.76

the yeah we have to generate compare

520.2

first okay so that makes sense

522.539

the pair set create

527.22

um

531.66

we don't really need that we don't need

533.7

create missing tables here

537.06

now we're going to do Source uh sync dot

540.18

create whoop

542.82

missing columns

549.08

and uh

555.42

so what we're going to need here is when

557.1

we do missing columns we need to

558.36

actually execute it so we weren't

559.62

executing that I don't think so if we go

561.66

to

563.339

uh DB sync

567.8

missing columns

576.54

we were returning that let's see let's

578.64

go ahead

580.38

and do this

585.42

and missing columns

589.2

I print command

595.2

uh really I can just take the cursor

597.18

destination

602.58

destination.execute

610.86

I can probably do it like that okay so

613.14

we're gonna do it like that we're gonna

614.04

clean that up a little bit uh let's see

616.32

so that's columns

618.6

rows

622.56

dot sync rows

625.68

and that's going to be my X rows

631.32

and then sync.missing triggers

636.48

and he's going to have the two names

642.24

and then

645.24

sync Dot

646.98

missing procedures

652.74

there it is

655.079

I don't give it those two

660.6

uh see this is going to be sync.m mixing

663.12

functions

668.1

way back here

670.74

missing indexes

674.82

let's go ahead yeah we'll get these last

676.5

couple okay

678.6

sync dot missing indexes

681.48

doesn't need anything

684.72

um

689.76

some missing indexes is doing the same

691.38

I'm going to need to actually build this

692.7

out

693.66

and then we're good

695.16

uh and so we're going to print uh

700.5

application complete

705.12

and we're going to go over here oh I'm

707.1

going to spell it right

708.959

and we're going to go to missing indexes

713.88

and we're going to throw that little

715.2

thing in because it apparently is

716.76

creating our

718.56

yeah so it just does it here so we're

720.72

going to come through here for index in

725.339

indexes

727.44

uh let's just do this we'll do the same

729.779

thing here so we're going to do

731.459

um

732.66

self Dot destinationdb.cursor

736.92

execute failed to create

739.8

cursor 2 close boom

749.22

and sync columns is that not being

751.2

called

756.06

uh uh oh too many blank lines oh well

758.459

too many blank lines too bad

760.98

um

763.86

there we go

767.639

oh I wonder if it's supposed to be like

769.32

that oh okay my mistake sometimes it

772.56

complains it complains in different ways

777.36

okay this is a silly little cleanup

779.16

thing so we now have everything and it's

782.639

a very short script it's going to call

784.26

this if we call it it should run the

786

same

787.2

but it's going to give us some issues so

790.32

uh sync.connect to Source oh

794.339

because I probably didn't put oh I did

796.38

put itself

799.2

um

802.639

not sure

814.38

let's see

819.959

oh okay so these need at least that so

823.32

all right

825.899

so

828.899

it's a neat little thing but um so we'll

832.2

get that taken care of and next time

834.779

around we can continue looking at a

836.94

little bit of our cleanup here and

838.44

moving into the next piece apologies if

840.959

we went a little bit long this time but

842.88

hey that's you know somehow we got to go

845.279

through a couple of these sort of boring

846.42

things to clean some stuff up and make

848.639

sure we're you know crushing all the t's

850.56

dotting all the eyes

852.48

but until next time go out there have

854.279

yourself a great day a great week and we

857.22

will talk to you next time

860.86

[Music]