📺 Develpreneur YouTube Episode

Video + transcript

SQL Sync Part 2

2022-08-18 •Youtube

Detailed Notes

Focus for this episode: Read in tables list Finding missing tables for source or Destination DB.

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
[Music]
well welcome back we are continuing
we're building our little database sync
script and learning a little bit about
sql along the way
uh this episode we're gonna keep on
going and we're gonna start looking at
some table comparisons so
uh this time around
i have changed some stuff up
and um
i've gone ahead and created a database
on my local
machine that has no tables in it so if
you go look here
um
i've got the tutorial database
tutorial
but if i just show tables
there's going to be none
so
uh i should see and what we do is we're
going to do a comparison we're going to
start with that so i'm going to connect
to the source database
and i'm going to have this second one
that i'm going to call db2
and i'm going to call it cursor 2
and this is going to be connect to db2
and that's going to be the db2
[Music]
cursor okay
so let's make sure it connects
it
does not
let's see
target database
uh
so we have to go take a look at this
okay i have a different connector i need
to deal with
um and this does sometimes happen that's
one of the fun things about things like
python where you have libraries so i had
to install this
connector and now if i do it
then i am able to let's come back here
connect to source database connect to
the target database
so i've got that um
and now i'm going to come through
and what i'm going to do is i'm going to
do two queries so first
and let's do this this is going to be um
compare tables
and so what i want to do my first one is
i'm going to
get my results i'm going to call it
source results
for show tables in my first database
and i'm going to come in and i'm going
to do the same thing
query 2 equals show tables uh it's gonna
be cursor two query two
actually i can keep it the same query
and then
uh this is gonna be target results
equals cursor two fetch all
and
i'm going to skip this
and so what i want to do is i'm going to
say
when i say see
i want to do this probably the easiest
way
is
i'm going to pull this so first i'm
going to do um
i'm going to create
a
let's just do a
uh list of
names
so
this will be source
and so what i do here is i'm going to
just do
let's call it tables
and i'm just going to make it an empty
array
and i'm going to do four row
in source results
so each row and remember that
column one is going to be the name so
i'm just going to do tables dot append
and it's gonna be uh what is that row
zero
and let me just do this i'm gonna do
print
uh tables so i'm gonna do the whole
thing for right now i'm just gonna print
out the result
and we're gonna see here is
all my tables
so now
i can take that
and i'm gonna say
we're going to call this missing
i'm going to create a missing array
and we're going to
see
these
tables are missing
and print missing
and so now i'm going to go through the
target results
and i am going well i don't wanna do
that
and uh name is equal to
row zero
and i'm actually going to do missing dot
append
the name but i only want to do it if
name
not in
tables
then i'm going to add it to the missing
so
i should see here oh it's not doing it
so
oh i'm sorry i want to do that reversed
i want to take
um so this would be oops this should be
not in source
so we're going to call this new tables
and it's going to be new tables
so i've got
a tables missing tables and new tables
so if they
exist in
the target but not in the source that
means that they are new guys new tables
and so we don't have any but we want to
do the reverse or the different
comparisons so now we're going to take
let's do this
let's do source tables
and i'm going to do target tables
just to be safe just to keep these so
i'm gonna do that and
source tables
but now i'm going to also do let's
create target tables
uh what did i call that i call that targ
results
let's go ahead and just
do it this way
just to keep it
consistent and easy to read
okay so now
i want that to be targettables.pin
okay so that gives me that but now for
the
missing
this is going to be missing
this is going to be missing
and let's see four row in
source results
src results
name is not in target tables
then i'm gonna append it
and let's see how this looks so now what
we've got is
between the two i connect out and then i
have none of the tables exist in the
target that are not in the
source but
i have all of these tables
that are in the target database but not
in
the
um not in the source so now what i want
to do
is i'm going to do a little search
and i'm going to say my sequel
[Music]
scribe table columns
let's say in detail
and i want to get
is probably what i want
okay so i can do this
show tables here we go
so i want to describe oh that's why i
want to do it so
for each of these
now
what i want to do is
uh we're gonna do so now um
for each
missing table
get details and so for now we're just
gonna work on that so we're gonna say
for
table because i don't think i'm gonna so
yeah okay for table in
missing
then i'm going to do uh
query uh let's say
uh let's call him query
let's do describe query i'll make that
easy
equals
describe and then plus
table
and now this is going to be since this
is missing i need to get it from the
source so i'm going to do it from cursor
1.
let's do that whoops
we'll just copy and paste that to make
it quick
so we're going to execute the describe
query
the
describe results
and then what i'm going to do here is
i'm just going to do for
uh
let's say column and oops
column
in
description results
print
the column
and i'm going to do let's do this um
whoops
so i want to print the table
just so we can see everything so now
what we're going to see is it's going to
get pretty complicated pretty quick but
here we go so here's each of our tables
and so now what we're going to want to
do is we're going to have to do a sort
of complicated kind of walk through
and create
so we're going to have
uh let's see
we're going to have this create sql
equals
and it's going to start with create
table
plus
table
and then we can
do this
so let's start with um
that
and at the end i know we're going to
create sql equals
create sql plus
we're going to close this thing out and
put our little semicolon there
i think that gives us a good stopping
point here because the next time we're
going to start walking through this and
actually build a table create
on the fly to be able to sync up our
tables
that being said i think it's a good time
to wrap it up as i said so we will go
out there
and get started into our day but as for
all of you go out there and have us a
great day a great week and we will talk
to you
next time
you
Transcript Segments
0.43

[Music]

27.119

well welcome back we are continuing

29.199

we're building our little database sync

31.439

script and learning a little bit about

32.96

sql along the way

34.48

uh this episode we're gonna keep on

35.92

going and we're gonna start looking at

37.52

some table comparisons so

40.32

uh this time around

42.16

i have changed some stuff up

44.559

and um

46.559

i've gone ahead and created a database

48.96

on my local

50.719

machine that has no tables in it so if

53.36

you go look here

55.12

um

56.32

i've got the tutorial database

59.199

tutorial

60.8

but if i just show tables

62.879

there's going to be none

64.4

so

65.28

uh i should see and what we do is we're

67.119

going to do a comparison we're going to

68.159

start with that so i'm going to connect

69.36

to the source database

70.96

and i'm going to have this second one

72.24

that i'm going to call db2

74.72

and i'm going to call it cursor 2

77.52

and this is going to be connect to db2

81.68

and that's going to be the db2

83.67

[Music]

84.84

cursor okay

87.439

so let's make sure it connects

89.759

it

90.799

does not

94.64

let's see

99.28

target database

101.2

uh

105.119

so we have to go take a look at this

107.759

okay i have a different connector i need

110.479

to deal with

114.88

um and this does sometimes happen that's

117.52

one of the fun things about things like

119.6

python where you have libraries so i had

121.6

to install this

123.36

connector and now if i do it

126.64

then i am able to let's come back here

130.239

connect to source database connect to

131.92

the target database

134.16

so i've got that um

138.239

and now i'm going to come through

139.92

and what i'm going to do is i'm going to

141.68

do two queries so first

144.64

and let's do this this is going to be um

148.319

compare tables

151.519

and so what i want to do my first one is

153.36

i'm going to

155.36

get my results i'm going to call it

157.36

source results

159.2

for show tables in my first database

161.92

and i'm going to come in and i'm going

163.36

to do the same thing

166.319

query 2 equals show tables uh it's gonna

170.239

be cursor two query two

173.76

actually i can keep it the same query

177.2

and then

178.159

uh this is gonna be target results

181.68

equals cursor two fetch all

187.28

and

188.319

i'm going to skip this

190.48

and so what i want to do is i'm going to

192.159

say

193.599

when i say see

196

i want to do this probably the easiest

198.4

way

200.319

is

202.239

i'm going to pull this so first i'm

204.239

going to do um

205.92

i'm going to create

207.92

a

213.92

let's just do a

215.84

uh list of

219.599

names

220.879

so

223.519

this will be source

226.4

and so what i do here is i'm going to

227.84

just do

229.12

let's call it tables

233.92

and i'm just going to make it an empty

235.28

array

236.56

and i'm going to do four row

238.84

in source results

243.76

so each row and remember that

246.4

column one is going to be the name so

248

i'm just going to do tables dot append

252.879

and it's gonna be uh what is that row

256.88

zero

261.199

and let me just do this i'm gonna do

263.199

print

264.479

uh tables so i'm gonna do the whole

266.479

thing for right now i'm just gonna print

267.68

out the result

270.16

and we're gonna see here is

272.08

all my tables

275.12

so now

276.32

i can take that

278.4

and i'm gonna say

282.96

we're going to call this missing

285.84

i'm going to create a missing array

288.56

and we're going to

290.639

see

292.479

these

296.16

tables are missing

301.44

and print missing

305.759

and so now i'm going to go through the

307.36

target results

310.639

and i am going well i don't wanna do

313.039

that

314.4

and uh name is equal to

318.08

row zero

322.24

and i'm actually going to do missing dot

324.56

append

327.28

the name but i only want to do it if

331.919

name

333.12

not in

335.199

tables

337.759

then i'm going to add it to the missing

341.919

so

343.919

i should see here oh it's not doing it

346.8

so

351.52

oh i'm sorry i want to do that reversed

355.039

i want to take

358.8

um so this would be oops this should be

365.759

not in source

370.72

so we're going to call this new tables

373.52

and it's going to be new tables

389.44

so i've got

392.96

a tables missing tables and new tables

394.8

so if they

396.08

exist in

399.6

the target but not in the source that

401.6

means that they are new guys new tables

403.84

and so we don't have any but we want to

405.759

do the reverse or the different

407.44

comparisons so now we're going to take

413.68

let's do this

415.44

let's do source tables

419.52

and i'm going to do target tables

425.68

just to be safe just to keep these so

428.08

i'm gonna do that and

430.16

source tables

435.199

but now i'm going to also do let's

436.72

create target tables

440.96

uh what did i call that i call that targ

442.96

results

444.72

let's go ahead and just

446.319

do it this way

448.319

just to keep it

451.039

consistent and easy to read

454

okay so now

455.36

i want that to be targettables.pin

460

okay so that gives me that but now for

462.24

the

463.36

missing

467.199

this is going to be missing

471.68

this is going to be missing

482.08

and let's see four row in

485.12

source results

488.16

src results

492.08

name is not in target tables

496.16

then i'm gonna append it

498.639

and let's see how this looks so now what

500.479

we've got is

502.8

between the two i connect out and then i

504.96

have none of the tables exist in the

507.039

target that are not in the

508.96

source but

510.639

i have all of these tables

512.959

that are in the target database but not

516.32

in

517.36

the

518.839

um not in the source so now what i want

522

to do

523.36

is i'm going to do a little search

528.16

and i'm going to say my sequel

531.23

[Music]

532.56

scribe table columns

537.04

let's say in detail

542.32

and i want to get

548.08

is probably what i want

558.24

okay so i can do this

562.16

show tables here we go

564.16

so i want to describe oh that's why i

566.16

want to do it so

568.24

for each of these

570.8

now

571.76

what i want to do is

576.48

uh we're gonna do so now um

579.36

for each

581.279

missing table

584.32

get details and so for now we're just

586.399

gonna work on that so we're gonna say

588.64

for

591.839

table because i don't think i'm gonna so

594.8

yeah okay for table in

597.68

missing

601.36

then i'm going to do uh

606.8

query uh let's say

612.079

uh let's call him query

616

let's do describe query i'll make that

618.32

easy

619.519

equals

621.44

describe and then plus

624.64

table

626.64

and now this is going to be since this

628.32

is missing i need to get it from the

630

source so i'm going to do it from cursor

633.6

1.

636.399

let's do that whoops

638.72

we'll just copy and paste that to make

640.399

it quick

646.56

so we're going to execute the describe

648.32

query

653.44

the

655.36

describe results

660.56

and then what i'm going to do here is

661.92

i'm just going to do for

664.16

uh

667.92

let's say column and oops

671.92

column

673.76

in

674.56

description results

679.04

print

680.32

the column

685.04

and i'm going to do let's do this um

688.88

whoops

690.48

so i want to print the table

693.839

just so we can see everything so now

695.2

what we're going to see is it's going to

696.079

get pretty complicated pretty quick but

697.44

here we go so here's each of our tables

700.72

and so now what we're going to want to

701.92

do is we're going to have to do a sort

704.079

of complicated kind of walk through

706.959

and create

708.959

so we're going to have

710.56

uh let's see

713.36

we're going to have this create sql

715.36

equals

717.68

and it's going to start with create

721.92

table

724.24

plus

726.079

table

731.6

and then we can

733.92

do this

736.24

so let's start with um

739.76

that

741.279

and at the end i know we're going to

744.88

create sql equals

748.24

create sql plus

750.24

we're going to close this thing out and

752

put our little semicolon there

754.639

i think that gives us a good stopping

756.24

point here because the next time we're

758.32

going to start walking through this and

759.92

actually build a table create

762.56

on the fly to be able to sync up our

765.36

tables

766.32

that being said i think it's a good time

767.6

to wrap it up as i said so we will go

769.279

out there

770.24

and get started into our day but as for

773.12

all of you go out there and have us a

774.48

great day a great week and we will talk

777.279

to you

778.48

next time

796.72

you