📺 Develpreneur YouTube Episode

Video + transcript

SQL Sync Part 6

2022-09-01 •Youtube

Detailed Notes

Focus for this episode: We continue creating columns that are missing in the destination.

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 hello and welcome back we're
continuing our
tutorial series here where we are
building a
python script to synchronize tables and
columns and maybe even eventually data
from one database to another
at this point we have got our table
sinking in so if we have a table that
exists in our source but does not exist
in our target
then we can create that we will execute
that sql and create that table and we
did that to basically build a
sort of like a sample database on
another server
now
last episode we we dropped off where we
were comparing columns so now we're
seeing where there is
for a table that exists in both cases in
the source and the target
then which
we're going to find cases where there's
a column that does not exist in both
and if we were to run this script then
that would make sense the way we're
doing is we're going to first get all
our tables figured out
and then that means that we should be
good so that we're going to have every
table that exists in the source now
exists in the target because we will
have corrected that and now we're going
to go in and we're going to fix those
columns
and actually what we would want to do
here is move this guy up
ah let's see so i'm going to get our
missing tables first but we're not going
to call that right now
and then we're going to do
call
and i think we're fixing right there
yeah create oops
missing columns and we're just going to
go ahead and create it there so missing
columns
is going to tell us what we're not there
but we're we probably don't even need to
do that but we'll do that for now we're
going to do the
create it as we go because otherwise
it's just a pain to
do so what we looked at last time we
were printing out the missing column now
based on that we want it to do
this
uh
format basically so we started with this
so it's going to be
alter table
and we're going to give it the table
name
and the uh column
wait let's see
alter table table and so we're going to
do table dot
oh that's i'm sorry that's
database.table which is fine because
we're going to be in the
uh given table we don't really need that
database
prefix so alter table table which is
going to be the table name and let's go
ahead and just because
we put those little tick marks just to
protect the table name and then we do a
space
and then we're going to do add
and then we're going to open up our
parentheses
because then we're going to add
if we look here
the name
and then the type
the name we already got for the column
and the type is going to be
here in this row uh there and then we
need to
close it out
now we do need to do
as we did here because this is a very
simple one we need to do
values
and we probably need to do
no
so let's go ahead and get those two so
here let's see so handle default values
no
that's what we really want this to be is
so we're going to add this
if it can be null
then it's going to be
no
and if not it's going to be
not null but since we're doing an ad i
guess it always has to be a note so we
don't even have to worry about that
because you really can't add
a not null
column
so let's throw that out for now
we do want to allow for a
default
and so this is going to be it's not call
anymore it is callrow
conroe
so we need to do that before we do the
other piece
oops
sorry we didn't want to get rid of all
of that from the other ones let's go
back and see where we okay
um let's do that
let's get back to where we were
there we go
so first
uh wait we're gonna do it here
and we don't need the null but we do
need
uh says great column type
plus
default plus
close it out
oops and we don't need a semicolon there
uh so now it's gonna say here's a row
here's our thing we're going to let's go
ahead and print our table sql oh
actually we did it so we can just do we
still print that we do
so now we're going to add it we're going
to give a default value if there is a
one and all should be well so let's take
a look at what that looks like
because we should see our address
uh so here we're going to do
alter table address add zip
and what we want to do probably is our
little type thing
so let's do this
let's fix for our type which is going to
be here as well
uh so let's see call type equals column
row
one
and then
uh there and then we want the column
type to be down here which is here
so that's not going to be called row
that's going to call
type
now let's look at it
and now we see
alter table address add zip int and i
bet if we execute that
let's go here and just execute it just
for the fun of it
works fine
so
we're going to see it here
refresh
and boom now we have our column in there
so we've been able to come in and add
columns
now what we're not doing is doing any
changes to the source
so let's uh which we want to make sure
so let's go ahead and just i don't know
let's go back and alter that
sorry let's alter this table
and let's drop zip out again
uh
let's see oh there we go
and let's see if we can take just to be
safe let's take uh one that has
a default
whoops wrong one
here we go so we have this type
timestamp
uh although it says it can't be noble
what's going to be an issue
well let's try to drop it anyways
let's see what happens
okay
so now we're going to see two
this isn't executing anything yet so
we're gonna see two
two cases where it doesn't come in
uh let's see so type timestamp
uh here we go
here we go
so
and the default is current timestamp
because it is coming in as a
function so let's see let's go ahead and
turn this on so here
uh we don't need this
we really don't need
uh how do we do this is really so let's
do this
and let's do four
uh let's do cmd
in
this in columns
so reach one of these we're gonna get
this little sql back that's what we
wanna do in each of these
is we're going to execute on
db2
right
yeah so we're going to execute in db2
which is our second database that's the
target
and then we're going to do let's do it
this way
uh
here we go
we don't really need all of that but
let's just do that anyways
so we get our cursor
our query is equal well actually it's
just our command so we have to worry
about that
execute command
and we're good so let's see what happens
and
it gets us these and it gets us these
executions and so now let's see if it
runs it again
oh it does okay so
let's go look at
oh did i not return columns
return missing columns i did because it
definitely
shows up because we print it
and then here
let's print the command
and let's not let's get rid of this part
ah let's see
oops
oopsie oopsie oopsie oopsie
um
[Music]
here we go my mistake
so these are not in the target oops i
did it for both of them
we just want to see if they're
additionals in the source which we don't
want to do so this is a missing column
for that which is good i'm glad we
didn't do that
and then we're going to write to there
that makes sense
and
let's just get rid of that for now
interesting
no it doesn't want to connect to the
database
interesting interesting interesting
so we just did something we don't want
to do for some reason so
oh it didn't want to connect to the
database
connecting to the source database it did
not want to do
so let's do this real quick
i don't know if i can kill that or not
uh here we go i can
uh
that guy
oh
three
two
one
okay there we killed it
okay
so now if we try to connect to that test
database let's go test there real quick
uh
here
okay
so that worked so let's go see what you
do
okay there we go so now he runs through
it's just sort of a minor little issue
there let's see what we got here
uh let's see duplicate column name zip
so it was already there
uh
so he must have already run through
somewhere so if i go here oh
there
i got a local
uh tutorial
tables
refresh the objects tree just to be sure
oh he's not there
so let's go look at this again
so
these columns are not on the target
which is db2
he comes out and he prints that
oh shoot
that's a problem now if we look at it
there we go
let's run it again and now we're good
okay
sorry about that
so now we've got our column syncing up
next time around we are going to
consider
some options for data i don't know we're
going to go all the way deep into it but
it's something we can talk about at
least that being said it's a good time
to wrap this one up so go out there and
have yourself a great day a great week
and we will talk to you
next time
[Music]
you
Transcript Segments
0.43

[Music]

27.439

well hello and welcome back we're

29.119

continuing our

30.72

tutorial series here where we are

32.96

building a

34.8

python script to synchronize tables and

37.36

columns and maybe even eventually data

39.92

from one database to another

42.8

at this point we have got our table

45.44

sinking in so if we have a table that

47.039

exists in our source but does not exist

49.52

in our target

51.199

then we can create that we will execute

53.28

that sql and create that table and we

55.6

did that to basically build a

58.559

sort of like a sample database on

60.64

another server

62.16

now

63.359

last episode we we dropped off where we

66.24

were comparing columns so now we're

68.56

seeing where there is

70.32

for a table that exists in both cases in

73.119

the source and the target

75.28

then which

76.96

we're going to find cases where there's

78.4

a column that does not exist in both

81.759

and if we were to run this script then

83.6

that would make sense the way we're

85.28

doing is we're going to first get all

86.56

our tables figured out

88.32

and then that means that we should be

90.24

good so that we're going to have every

91.759

table that exists in the source now

94.479

exists in the target because we will

97.04

have corrected that and now we're going

98.72

to go in and we're going to fix those

100

columns

102.479

and actually what we would want to do

104.24

here is move this guy up

108.479

ah let's see so i'm going to get our

111.04

missing tables first but we're not going

113.439

to call that right now

115.119

and then we're going to do

116.56

call

118.64

and i think we're fixing right there

119.759

yeah create oops

122.399

missing columns and we're just going to

123.84

go ahead and create it there so missing

126.399

columns

129.039

is going to tell us what we're not there

130.879

but we're we probably don't even need to

132.239

do that but we'll do that for now we're

134.239

going to do the

136.319

create it as we go because otherwise

139.36

it's just a pain to

140.84

do so what we looked at last time we

143.12

were printing out the missing column now

146.08

based on that we want it to do

148.8

this

150.08

uh

150.879

format basically so we started with this

153.04

so it's going to be

154.16

alter table

156.879

and we're going to give it the table

158.08

name

160.16

and the uh column

163.84

wait let's see

165.2

alter table table and so we're going to

167.04

do table dot

168.959

oh that's i'm sorry that's

170.16

database.table which is fine because

172.4

we're going to be in the

174.16

uh given table we don't really need that

177.04

database

179.04

prefix so alter table table which is

181.92

going to be the table name and let's go

183.36

ahead and just because

185.599

we put those little tick marks just to

187.12

protect the table name and then we do a

189.12

space

190.08

and then we're going to do add

192.319

and then we're going to open up our

194

parentheses

195.92

because then we're going to add

198.8

if we look here

200.48

the name

201.92

and then the type

204.319

the name we already got for the column

207.12

and the type is going to be

210.319

here in this row uh there and then we

213.12

need to

215.12

close it out

218.239

now we do need to do

220.48

as we did here because this is a very

221.92

simple one we need to do

226.239

values

228.08

and we probably need to do

234.08

no

236

so let's go ahead and get those two so

238.84

here let's see so handle default values

243.439

no

245.28

that's what we really want this to be is

250.319

so we're going to add this

252.4

if it can be null

254.879

then it's going to be

258.16

no

260.88

and if not it's going to be

264.96

not null but since we're doing an ad i

266.639

guess it always has to be a note so we

268.08

don't even have to worry about that

269.759

because you really can't add

272.24

a not null

274.56

column

275.6

so let's throw that out for now

278.16

we do want to allow for a

280.96

default

283.199

and so this is going to be it's not call

284.96

anymore it is callrow

292.16

conroe

303.12

so we need to do that before we do the

304.72

other piece

306.24

oops

307.36

sorry we didn't want to get rid of all

309.52

of that from the other ones let's go

311.199

back and see where we okay

314.72

um let's do that

317.199

let's get back to where we were

319.12

there we go

320.4

so first

322.4

uh wait we're gonna do it here

326

and we don't need the null but we do

328.08

need

330.4

uh says great column type

334.4

plus

336.8

default plus

340

close it out

343.52

oops and we don't need a semicolon there

347.44

uh so now it's gonna say here's a row

350.4

here's our thing we're going to let's go

352.88

ahead and print our table sql oh

354.8

actually we did it so we can just do we

356.96

still print that we do

360.96

so now we're going to add it we're going

362

to give a default value if there is a

363.36

one and all should be well so let's take

365.6

a look at what that looks like

367.84

because we should see our address

370.479

uh so here we're going to do

373.36

alter table address add zip

377.6

and what we want to do probably is our

379.52

little type thing

381.199

so let's do this

384.08

let's fix for our type which is going to

388

be here as well

393.52

uh so let's see call type equals column

398.319

row

399.759

one

404.319

and then

405.52

uh there and then we want the column

408.08

type to be down here which is here

412

so that's not going to be called row

413.12

that's going to call

414.319

type

416.24

now let's look at it

418.72

and now we see

420.08

alter table address add zip int and i

422.4

bet if we execute that

427.039

let's go here and just execute it just

429.039

for the fun of it

434.56

works fine

435.919

so

437.199

we're going to see it here

440.08

refresh

441.36

and boom now we have our column in there

444.72

so we've been able to come in and add

446.479

columns

447.599

now what we're not doing is doing any

450.24

changes to the source

453.36

so let's uh which we want to make sure

455.759

so let's go ahead and just i don't know

457.759

let's go back and alter that

459.759

sorry let's alter this table

462.72

and let's drop zip out again

466.319

uh

467.52

let's see oh there we go

470.72

and let's see if we can take just to be

472.96

safe let's take uh one that has

478.08

a default

481.12

whoops wrong one

486.24

here we go so we have this type

488.16

timestamp

490.08

uh although it says it can't be noble

491.759

what's going to be an issue

495.68

well let's try to drop it anyways

498.08

let's see what happens

501.28

okay

503.12

so now we're going to see two

504.96

this isn't executing anything yet so

506.4

we're gonna see two

507.68

two cases where it doesn't come in

509.84

uh let's see so type timestamp

515.2

uh here we go

517.2

here we go

518.56

so

522.159

and the default is current timestamp

528.08

because it is coming in as a

530.72

function so let's see let's go ahead and

532.88

turn this on so here

534.959

uh we don't need this

536.88

we really don't need

542.08

uh how do we do this is really so let's

544.72

do this

546.64

and let's do four

550

uh let's do cmd

551.92

in

553.68

this in columns

555.68

so reach one of these we're gonna get

556.88

this little sql back that's what we

558.56

wanna do in each of these

561.36

is we're going to execute on

565.2

db2

569.279

right

572.56

yeah so we're going to execute in db2

574.399

which is our second database that's the

576.24

target

578.56

and then we're going to do let's do it

580.32

this way

582.56

uh

583.36

here we go

585.12

we don't really need all of that but

586.72

let's just do that anyways

588.88

so we get our cursor

591.76

our query is equal well actually it's

593.6

just our command so we have to worry

594.64

about that

596.32

execute command

598.959

and we're good so let's see what happens

603.44

and

604.8

it gets us these and it gets us these

607.519

executions and so now let's see if it

610.56

runs it again

612

oh it does okay so

619.68

let's go look at

622.16

oh did i not return columns

627.68

return missing columns i did because it

629.76

definitely

632.24

shows up because we print it

634.399

and then here

640.32

let's print the command

642.88

and let's not let's get rid of this part

649.519

ah let's see

656.399

oops

657.519

oopsie oopsie oopsie oopsie

660.959

um

661.48

[Music]

662.88

here we go my mistake

665.839

so these are not in the target oops i

667.519

did it for both of them

669.2

we just want to see if they're

670

additionals in the source which we don't

671.44

want to do so this is a missing column

673.12

for that which is good i'm glad we

675.12

didn't do that

676.88

and then we're going to write to there

678.72

that makes sense

683.36

and

685.36

let's just get rid of that for now

698.16

interesting

699.68

no it doesn't want to connect to the

700.8

database

711.839

interesting interesting interesting

714.24

so we just did something we don't want

716.079

to do for some reason so

722.32

oh it didn't want to connect to the

723.6

database

727.839

connecting to the source database it did

730.72

not want to do

735.6

so let's do this real quick

743.36

i don't know if i can kill that or not

745.279

uh here we go i can

750.24

uh

754.24

that guy

756.72

oh

764

three

764.8

two

765.6

one

770.839

okay there we killed it

774.24

okay

776.88

so now if we try to connect to that test

779.519

database let's go test there real quick

782.48

uh

784.16

here

787.279

okay

788.24

so that worked so let's go see what you

790.56

do

794.88

okay there we go so now he runs through

797.2

it's just sort of a minor little issue

799.12

there let's see what we got here

802.16

uh let's see duplicate column name zip

804.959

so it was already there

811.279

uh

812.839

so he must have already run through

815.44

somewhere so if i go here oh

819.04

there

820.72

i got a local

825.199

uh tutorial

829.44

tables

830.959

refresh the objects tree just to be sure

835.6

oh he's not there

837.519

so let's go look at this again

844.399

so

845.76

these columns are not on the target

854.32

which is db2

860.24

he comes out and he prints that

868.88

oh shoot

875.36

that's a problem now if we look at it

877.199

there we go

878.24

let's run it again and now we're good

880.639

okay

881.519

sorry about that

883.68

so now we've got our column syncing up

885.68

next time around we are going to

888.32

consider

889.68

some options for data i don't know we're

892.079

going to go all the way deep into it but

893.44

it's something we can talk about at

895.6

least that being said it's a good time

898.32

to wrap this one up so go out there and

899.839

have yourself a great day a great week

902.48

and we will talk to you

904.48

next time

906.14

[Music]

921.68

you