📺 Develpreneur YouTube Episode

Video + transcript

SQL Sync Part 19

2022-10-18 •Youtube

Detailed Notes

Focus for this episode: We step back and do 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
thank you
[Music]
well hello and welcome back we're
continuing our look at Python and SQL
and this time we're going to focus a
little more on the python side I guess
because we've made a pretty solid
progress we have a few things left that
we need to do
some of these are non-trivial but I
think at this point I want to pull this
one here is clean this thing up a little
bit
because I think as we move forward it'll
be a little easier to do so
now what we're going to do is we're
going to take uh we've got a couple
things here so we're going to start with
this main.py that we run and we're just
going to duplicate it and we're going to
somewhere in here I think I just do a
copy paste
there we go and we're going to call this
uh dbsync .py
and we don't have to worry about that
right now so what we're going to do is
we're going to have Main
and it's going to use dbsync
so what Maine's going to do
um and actually let's clean this up a
little bit so we go to DB sync what
we're going to do is we're going to
create a class called my SQL sync uh
we'll do synchronize
and then it's going to have a couple
different things let's have a bunch of
stuff in it so what we can do is let's
see we're going to keep all our
procedures so we're going to get rid of
the code at the end
here that's a cleanup here we go
um and let's actually let's go back here
because what we'll probably do is
actually end up sort of keeping the
connections
uh
do we have a oh we don't have compare
tables
do we
so we're gonna have to create compare
tables
yeah we have missing tables
but we need compare tables okay so let's
do this first
let's take that compare tables
and
when we do that here we go
so the first thing we do is we're going
to do this compare tables and we're
going to build out
a tables list
actually we're going to do it twice I
think because what we're going to do
is we're going to do let's go look at
these real quick and let's do it this
way we're going to take
connections
and what we're going to have here is
we're going to have a source DB
and we're going to have destination DB
and we'll just do
there's none
equals none
I can do it this way
and then let's see what we're gonna have
here
is we're going to have this is going to
be self dot Source DB equals
connection
so we're going to change this stuff up
quite a bit in a sense
um and we're going to call this connect
to destination
this is going to be connect to source
what we'll probably end up doing
like that
um
oh itself
whoops
that's gonna be self
and then this is going to be
self dot destination DB equals
so missing tables
whoop okay so missing tables
is simply going to take
uh this is
we're going to do two things we're going
to have uh Source tables
and we'll just start with that we'll
have Target tables
and so now here
this is just going to be self
and then for Row in
self Dot
uh is that Source tables
oops Target tables
and if the name is not in
self.source tables
then
and we're going to do this we're going
to go ahead and take
missing tables
so I have to pass those around
and we're going to do self Dot
missing tables
uh let's do this uh this just needs to
be
we're going to make sure that we
synchron that we set him to nothing and
then we're going to come in here and do
self-doubt missing tables so when we run
missing tables we should be in good
shape what's you complaining about
oh
um
let's call him
uh let's see this is going to be
let's do
set
missing tables
so now
oops
um let's do it like this let's be
consistent
and we're gonna do Source tables Target
tables
and missing tables
so that gets us
Target tables
Source tables missing tables there we go
and Row from Outer scope
I don't know what that is okay so we
should be good so here we're going to be
there's going to be uh Define
let's see compare tables
and it's going to be a self there we go
and then we're going to take this
like that
so we need a cursor which is going to be
probably from let's see if we can go
back here where we created our little
cursor we got it
is
okay
so we need these cursors
um
let's go ahead
we're going to create those each time so
we're going to come into our DB Sync
here and we're going to do
cursor equals
and this will be
um
self.source
uh what did we call it
Source DB okay
so this should be
oh except for I call it yeah edit with
an SRC my mistake okay and so that gives
me a cursor
and then this one
uh cursor two
is going to be whoops
from the destination
so we're going to come in here and we're
going to go call him the
destinationdb.cursor
so I've got our cursors we're going to
create these uh create a list
of each of these we don't need missing
at this point we do need self Dot
Source tables
and we're going to do self dot Target
tables
and then we're gonna have to do self dot
Source tables
and we're going to do self dot Target
tables
and so now we've built out here we've
built out the things that we're going to
need so there is going to be you know
some
some work to be done here uh we have to
make sure that we call things in order
but
we're going to go ahead and do this so
we compare tables missing procedures
is going to be pretty straightforward if
we can get all the way into that
oh and we're gonna let's take our
cursors
here and do the same thing up here
I'm going to make sure we close it so we
have a cursor and a cursor too
so we're gonna do a cursor close and a
cursor 2 close
I assume what is it complaining about
there
oh there's some sort of outer scope we
don't need to worry about that too much
we don't need import CIS I don't think
well probably not
okay
uh let's see so this is going to be
uh self Dot
Source DB
and this is going to be
self.destination DB
so we're good
and we're gonna go through here let's
see do we have oh but we do need for
missing procedures
we need self
and we need a source name and a
destination name
which is
okay so this yeah this is our database
so we're going to go ahead and keep that
and then we should be good through all
of this
um not seeing anything that should
matter here
oh yeah it is because here we've got to
do okay so this is self dot Source DB
whoops
and then here is going to be self.source
DB
and
by using destination it probably just
says that's a spelling typo okay
and then self-source uh close everything
we can probably
let's get rid of some of this output
um now we'll keep that output for now
so we're going to do the same thing so
we've got that
we're gonna come in here we're going to
do the same thing we're going to do that
we don't need our sourcing oops it's a
comma
um pretty much the same of everything
let's go back up here where we create
these two
and those are probably the same names
and we're going to take this whole thing
to hear
tab that up
and let's see so again we have
self.source DB
and here self.sourcesdb
and let's see do we use it again doesn't
look like we do okay so we should be
good same thing missing triggers
we're going to come we don't need our
source our databases we got those we get
our source and our destination
I have that yep there we go
so we got those fixed
uh let's see we'll do this here
I don't need this these comments anymore
so we should be good there
I think that's all good
it's gonna be yes in the self and notice
we're not having to do all of this
um
I don't know what that table's list
missing columns
let's look at what do we use for missing
columns when we call missing columns
we're actually using it on Source tables
so we should be good
um
vsync so here this should be that tables
list is actually Source columns
the set's missing columns and table in
self Dot
uh source
oh it's gonna push this up one so I can
make sure it picks it up right and uses
its little autocomplete stuff properly
I get that so now this is going to be
self dot Source tables
and then Source execute execute
oh interesting I don't know that I need
to do
and missing columns
oh that's for each table okay so we're
good yeah so we're going to come in
we're going to find our missing columns
uh see destination names because we only
need
the tables that are in the source
because we've already built them into
the destination okay so we're good
we could technically do only destination
and see if they exist in Source but uh
that could cause some problems if we
have extra tables in our destination so
we're good there so we're going through
here we are gonna let's get rid of this
print column row
and we go through and we've got
everything so we should be good there
I went a little bit long there so this
seems like a good point to sort of cut
away and wrap up for today we will dive
right right back in tomorrow uh next
episode and we're going to continue
right into this
um
it is a little bit tedious it is
something that you may want to you know
Skip ahead and just take a look at the
how things have changed however there
are several little comments and things
that are noted as we go through and do
this cleanup that may be useful to you
particularly from the python side the
SQL side not so much we've sort of you
know this is really sort of a couple of
python heavy focused episodes but then
we will get back into it so we've got
one more we're doing some cleanup and
then we're going to dive into our next
round of issues to tackle
that being said let's get out there and
get back to your day 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.18

well hello and welcome back we're

29.039

continuing our look at Python and SQL

32.16

and this time we're going to focus a

34.98

little more on the python side I guess

36.6

because we've made a pretty solid

39.3

progress we have a few things left that

41.7

we need to do

42.92

some of these are non-trivial but I

46.32

think at this point I want to pull this

49.079

one here is clean this thing up a little

51.6

bit

52.8

because I think as we move forward it'll

54.66

be a little easier to do so

57.48

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

58.92

going to take uh we've got a couple

61.079

things here so we're going to start with

62.16

this main.py that we run and we're just

65.22

going to duplicate it and we're going to

69.119

somewhere in here I think I just do a

71.159

copy paste

72.659

there we go and we're going to call this

75.42

uh dbsync .py

80.64

and we don't have to worry about that

81.96

right now so what we're going to do is

83.759

we're going to have Main

85.38

and it's going to use dbsync

88.56

so what Maine's going to do

91.14

um and actually let's clean this up a

92.939

little bit so we go to DB sync what

94.74

we're going to do is we're going to

95.64

create a class called my SQL sync uh

101.22

we'll do synchronize

104.88

and then it's going to have a couple

107.52

different things let's have a bunch of

109.68

stuff in it so what we can do is let's

111.299

see we're going to keep all our

112.2

procedures so we're going to get rid of

114

the code at the end

116.399

here that's a cleanup here we go

122.7

um and let's actually let's go back here

126.42

because what we'll probably do is

128.34

actually end up sort of keeping the

130.38

connections

136.8

uh

141.18

do we have a oh we don't have compare

143.58

tables

145.14

do we

146.459

so we're gonna have to create compare

147.9

tables

152.879

yeah we have missing tables

155.34

but we need compare tables okay so let's

157.92

do this first

159.599

let's take that compare tables

162.66

and

168.3

when we do that here we go

171

so the first thing we do is we're going

173.7

to do this compare tables and we're

174.959

going to build out

176.4

a tables list

180.42

actually we're going to do it twice I

182.64

think because what we're going to do

184.62

is we're going to do let's go look at

186.66

these real quick and let's do it this

188.28

way we're going to take

192.9

connections

195.659

and what we're going to have here is

197.7

we're going to have a source DB

202.019

and we're going to have destination DB

209.48

and we'll just do

212.76

there's none

214.86

equals none

218.04

I can do it this way

221.58

and then let's see what we're gonna have

224.4

here

226.799

is we're going to have this is going to

229.739

be self dot Source DB equals

235.739

connection

238.379

so we're going to change this stuff up

240.18

quite a bit in a sense

244.019

um and we're going to call this connect

245.64

to destination

248.819

this is going to be connect to source

256.56

what we'll probably end up doing

262.019

like that

264.479

um

267.24

oh itself

269.4

whoops

271.259

that's gonna be self

274.199

and then this is going to be

278.22

self dot destination DB equals

284.22

so missing tables

289.02

whoop okay so missing tables

291.66

is simply going to take

296.52

uh this is

299.4

we're going to do two things we're going

301.08

to have uh Source tables

306.96

and we'll just start with that we'll

308.759

have Target tables

313.979

and so now here

319.44

this is just going to be self

322.32

and then for Row in

325.8

self Dot

328.8

uh is that Source tables

332.639

oops Target tables

337.919

and if the name is not in

342.02

self.source tables

345.18

then

347.34

and we're going to do this we're going

348.6

to go ahead and take

350.039

missing tables

352.919

so I have to pass those around

357.84

and we're going to do self Dot

363

missing tables

367.38

uh let's do this uh this just needs to

369.9

be

372.539

we're going to make sure that we

373.979

synchron that we set him to nothing and

376.08

then we're going to come in here and do

377.699

self-doubt missing tables so when we run

379.86

missing tables we should be in good

381.72

shape what's you complaining about

386.52

oh

387.84

um

391.74

let's call him

395.4

uh let's see this is going to be

401.88

let's do

403.979

set

406.86

missing tables

409.319

so now

417.419

oops

421.919

um let's do it like this let's be

423.6

consistent

425.22

and we're gonna do Source tables Target

427.139

tables

428.4

and missing tables

431.78

so that gets us

438.78

Target tables

442.319

Source tables missing tables there we go

449.16

and Row from Outer scope

453.78

I don't know what that is okay so we

456.3

should be good so here we're going to be

459.9

there's going to be uh Define

464.039

let's see compare tables

469.259

and it's going to be a self there we go

470.94

and then we're going to take this

475.919

like that

482.039

so we need a cursor which is going to be

487.979

probably from let's see if we can go

490.199

back here where we created our little

492.06

cursor we got it

494.28

is

499.379

okay

502.139

so we need these cursors

505.56

um

508.8

let's go ahead

511.56

we're going to create those each time so

513.719

we're going to come into our DB Sync

515.099

here and we're going to do

520.08

cursor equals

524.099

and this will be

526.56

um

527.459

self.source

535.2

uh what did we call it

538.38

Source DB okay

541.019

so this should be

544.92

oh except for I call it yeah edit with

546.54

an SRC my mistake okay and so that gives

549.36

me a cursor

552.3

and then this one

555.12

uh cursor two

558.48

is going to be whoops

563.88

from the destination

565.8

so we're going to come in here and we're

567.6

going to go call him the

569.82

destinationdb.cursor

572.04

so I've got our cursors we're going to

574.019

create these uh create a list

577.019

of each of these we don't need missing

578.58

at this point we do need self Dot

584.399

Source tables

589.5

and we're going to do self dot Target

591.959

tables

597.24

and then we're gonna have to do self dot

600.32

Source tables

604.08

and we're going to do self dot Target

606.54

tables

609.54

and so now we've built out here we've

612.66

built out the things that we're going to

613.86

need so there is going to be you know

615.48

some

616.68

some work to be done here uh we have to

619.56

make sure that we call things in order

620.76

but

621.899

we're going to go ahead and do this so

623.279

we compare tables missing procedures

627.779

is going to be pretty straightforward if

631.26

we can get all the way into that

633.42

oh and we're gonna let's take our

637.38

cursors

640.019

here and do the same thing up here

642.54

I'm going to make sure we close it so we

644.76

have a cursor and a cursor too

649.98

so we're gonna do a cursor close and a

652.38

cursor 2 close

654.72

I assume what is it complaining about

656.7

there

662.88

oh there's some sort of outer scope we

664.92

don't need to worry about that too much

666.18

we don't need import CIS I don't think

668.16

well probably not

670.2

okay

672.98

uh let's see so this is going to be

678.18

uh self Dot

680.579

Source DB

683.1

and this is going to be

686.3

self.destination DB

688.56

so we're good

690.48

and we're gonna go through here let's

692.22

see do we have oh but we do need for

694.92

missing procedures

697.92

we need self

701.22

and we need a source name and a

703.26

destination name

705.66

which is

708.12

okay so this yeah this is our database

709.92

so we're going to go ahead and keep that

712.86

and then we should be good through all

715.62

of this

719.279

um not seeing anything that should

720.959

matter here

724.519

oh yeah it is because here we've got to

727.5

do okay so this is self dot Source DB

730.92

whoops

736.26

and then here is going to be self.source

738.899

DB

742.92

and

746.04

by using destination it probably just

747.839

says that's a spelling typo okay

750.66

and then self-source uh close everything

754.74

we can probably

758.22

let's get rid of some of this output

762.72

um now we'll keep that output for now

766.32

so we're going to do the same thing so

767.519

we've got that

768.72

we're gonna come in here we're going to

769.8

do the same thing we're going to do that

771.42

we don't need our sourcing oops it's a

773.04

comma

774.66

um pretty much the same of everything

776.579

let's go back up here where we create

778.139

these two

782.1

and those are probably the same names

784.86

and we're going to take this whole thing

789.6

to hear

791.22

tab that up

793.519

and let's see so again we have

797.06

self.source DB

800.76

and here self.sourcesdb

807.36

and let's see do we use it again doesn't

809.1

look like we do okay so we should be

810.779

good same thing missing triggers

815.76

we're going to come we don't need our

816.899

source our databases we got those we get

818.94

our source and our destination

821.04

I have that yep there we go

823.079

so we got those fixed

825.68

uh let's see we'll do this here

829.86

I don't need this these comments anymore

834.12

so we should be good there

837.18

I think that's all good

840.54

it's gonna be yes in the self and notice

842.7

we're not having to do all of this

845.399

um

846.66

I don't know what that table's list

848.7

missing columns

851.16

let's look at what do we use for missing

853.38

columns when we call missing columns

855.12

we're actually using it on Source tables

866.12

so we should be good

870.839

um

871.68

vsync so here this should be that tables

875.88

list is actually Source columns

880.199

the set's missing columns and table in

882.899

self Dot

885.38

uh source

890.339

oh it's gonna push this up one so I can

893.699

make sure it picks it up right and uses

895.26

its little autocomplete stuff properly

900.839

I get that so now this is going to be

902.82

self dot Source tables

908.16

and then Source execute execute

916.98

oh interesting I don't know that I need

919.44

to do

922.26

and missing columns

928.44

oh that's for each table okay so we're

930.3

good yeah so we're going to come in

931.62

we're going to find our missing columns

934.279

uh see destination names because we only

938.04

need

942.24

the tables that are in the source

943.86

because we've already built them into

944.82

the destination okay so we're good

948.12

we could technically do only destination

951.54

and see if they exist in Source but uh

954.12

that could cause some problems if we

955.139

have extra tables in our destination so

956.76

we're good there so we're going through

957.959

here we are gonna let's get rid of this

960

print column row

961.82

and we go through and we've got

964.019

everything so we should be good there

967.079

I went a little bit long there so this

969.6

seems like a good point to sort of cut

972.6

away and wrap up for today we will dive

976.26

right right back in tomorrow uh next

978.899

episode and we're going to continue

980.339

right into this

982.019

um

982.74

it is a little bit tedious it is

985.019

something that you may want to you know

986.399

Skip ahead and just take a look at the

988.68

how things have changed however there

991.8

are several little comments and things

993.959

that are noted as we go through and do

996.36

this cleanup that may be useful to you

998.339

particularly from the python side the

1001.399

SQL side not so much we've sort of you

1003.92

know this is really sort of a couple of

1005.66

python heavy focused episodes but then

1009.56

we will get back into it so we've got

1011.36

one more we're doing some cleanup and

1014

then we're going to dive into our next

1015.32

round of issues to tackle

1018.139

that being said let's get out there and

1021.139

get back to your day so go out there and

1022.639

have yourself a great day a great week

1024.559

and we will talk to you

1026.6

next time

1028.69

[Music]