📺 Develpreneur YouTube Episode

Video + transcript

SQL Sync Part 5

2022-08-30 •Youtube

Detailed Notes

Focus for this episode: More details about columns and adding columns that are missing in the 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 hello and welcome back we are
continuing our series where we're
looking at uh sort of combining our
python and our sql tutorials
and we're building a database
synchronization script based on
uh it's written in python
now since the last time i do want to
catch up a little bit a couple things
that i'm a couple of changes i made
just so you know so one of them is
we were looking at default
functions you know you can either
default as a value or a function one of
your defaults uh and we basically said
if it's a if it's a function
then
if we have a parenthesis open
parenthesis
we're gonna assume there's a function in
there however
there is the
key phrase current underscore time stamp
which actually is a function
um and there may be some others but this
one i know i ran into so go ahead and
count that as a function another thing
was uh just for safety's sake
i went ahead and decided to wrap the
column names in uh these little tick
marks
in case it is some sort of reserved word
or something like that
i came across that in one of the one
things i was playing around with so i
figured it was probably best to do that
that being said
now so what we've got is we've been able
to come in and deal with our tables the
next thing we want to do
is we're going to go with the table
we're going to see if the columns match
up
so let's
let's see create missing tables let's
just pause that for right now so we'll
come back to that later
and for now
what we want to do is we're going to go
for in each
let's see where we do find here we go
missing tables
and now we're going to create one that's
called missing columns and basically
it's going to work the way as
missing tables
so there's going to be a source and a
target
and let's see we had somewhere down here
we did missing tables
uh here
and we'll just print them for now
in case there are any i don't think
there will be because we should have
everything going
and now we're going to have missing
columns
and uh
these table dot columns
are not in target
so this one's gonna be a little more
complicated
so for missing columns what we're gonna
do
is we're gonna go through
so for each table
and we're going to assume that they're
not missing so we actually don't need
well i guess we do need it in both of
them
that's a good question let's say
well we do still need source and target
and did we call those
oh no so we need actually this source so
we do missing columns
here we go
we actually need
uh dbc which is our source
and db2 is our target
beca and let's actually do tables which
is going to be let's just say source
tables
so we're going to assume that they're
all there at this point
and we'll see how that works out
so
back up here
so we have
uh let's do this db source
db destination
and tables
tables list
and so missing columns so now what we're
going to do is
for row and tables list and so now each
one
this is gonna be a table name
and so we're gonna have to do a query
so
we'll just take that one for row and
there
uh we'll return missing columns
and let's see so this is going to be our
column query
this will be our
source column query
and let's see in each one yep well
actually that can be our column query
and so we are going to we need two
cursors oops
so let's do that
let's go and create these cursors real
quick so that's going to be cursor
source is
db source
the destination is going to be
tv
dust
and so we're going to do with each case
we're going to describe the table so
we're going to get two set specs we're
going to do
source
and destination
and so we want source and we want
destination
so now we're going to have source
columns
equals
we're going to have uh we'll call it
destination columns
equals
and
[Music]
oops um
wait that's how i want i'm sorry
we just want to execute them first
execute the two and then we're going to
be
source columns
equals cursor source stuff actually
there we go
destination columns
equals destination fetch all
and now what we're going to do here
is do something along these lines so
what we're going to do
so
so for
column row
in
target
destination columns
then for each one
let's see so name is going to be equal
to row 0
because that is where our names are for
our
columns and if name is not in
oh so what we want to do
okay so first we go to generate names so
let's call it
uh source names
equals this
and then for
conroe
and source columns
then for each one of those source names
dot a pin
uh see
row
zero so we're gonna go into each of
these first we're gonna do is we're
gonna build a list of the column names
and the source
and then we're gonna go through each one
of these uh which one of the
destinations and we're gonna say if
the name is not in source
names this is call row
so if the column if the name of the
destination is not in the source then
we're going to do
missing columns
and we're going to append
the name except for we're going to do
a table
which is
let's do this for table
there we go
it's gonna be table plus and we're just
going to do dot name so this is going to
give us all of the missing columns
and then we're going to run through
and see how that works now
so right now let's see if it even runs
whoops that's not what i want to do
so we didn't get any mix but let's go
ahead and break one here so let's go to
our source
uh
let's see so testing
was our source right
uh let's see tutorial
is our source okay
so
local
uh
see
uh is that right okay so then local
tutorial is our database
so let's go look here okay so if we go
here
let's get rid of a column somewhere like
it really doesn't matter so let's just
do with our address let's start with
that
let's get rid of our zip
so let's go in here
alter table
and we're going to delete this column
so we're going to drop columns up
and now what we should see is that
should show up
and
it does not
so let's
go in here
oh let's see these columns are not in
the target okay so we are
so now what we want to do is we're going
to go missing columns oh
did we do oh no that's right
so uh oh we did this wrong
we want to do
we want to do the destination columns
and
then we want to say
anything that's in source so this one
says
if the name is not in the source names
so if the source data is not in the
let's do this let's change this a little
bit
just to make it less
annoying
so here's the thing
so in this case we're going from a to b
and so we're going to say let's look at
all the columns in b
and then look at all the columns and
then for each of those we're going to
see which is here
and then for each of those
we're going to say
for everything that's in a we're going
to say hey does that already exist if it
doesn't then it's missing
and that should give us
uh here we go missing calls is not
defined
oh
it's columns
and so i bet now we'll see there we go
so address.zip is not there
so we have gone through and we're now
doing this and so we can find this is uh
as we did before uh this is
these columns are not in target we can
take the same thing
and say these columns are not in source
just in case there's some extras
and all we do here
is swap these two
so that's dvc
that's db2 so what we'll get is we
should get through here address that's
it but it's all fine the other way
and so that brings us to now what do we
do
so if we do this
uh
where did that go here
so we need to do in this case
is say that we have a missing column
which is good
because what we can do
is
um
we can actually do this
um
how do we want to do this this will be
good
let's just look at it this way
uh this won't give us a name but
what we can do is we could just say call
row and now we're going to see what the
definition of that is
and here we see that okay so this is
missing
and so all we need
is a table name so we probably want to
do this a little differently we're going
to probably want to build our sql right
here
and then go back to this
and then create out our sql
which will probably include the table
and the name
so it would be something along bike
let's do table sequel
equals
alter table
plus
name
plus
and column
uh
oops that's not name that's table
plus name
plus we'll do this plus a type which is
a call row
which we found before is the type is
uh one
so let's look at this for now uh and
then we'll print this
let's print uh call row
there we go
and so now we're gonna see oops we
didn't print that didn't we oh yes we
did
um
oh my mistake we want this to be
a table underscore sequel
bam
so
here we go so now we're starting to see
the start of our sequel
and uh we can see here
uh basically you know for this column
this sort of puts us a run to the end of
this one so the next episode we're going
to come back we're starting building out
that column sql and probably like we've
seen before where we built the table
we're going to have to you know
essentially
um
steal some of the same information so we
may do some sort of uh you know abstract
this out to a
build column sql something along those
lines
but i don't want to you know spoil it
too much
so for now go out there and have
yourself a great day a great week and we
will talk to you next time
you
Transcript Segments
0.43

[Music]

27.039

well hello and welcome back we are

29.119

continuing our series where we're

31.279

looking at uh sort of combining our

34.16

python and our sql tutorials

36.559

and we're building a database

38.16

synchronization script based on

41.2

uh it's written in python

43.6

now since the last time i do want to

45.92

catch up a little bit a couple things

48.32

that i'm a couple of changes i made

50.96

just so you know so one of them is

53.28

we were looking at default

56.719

functions you know you can either

58.32

default as a value or a function one of

60.719

your defaults uh and we basically said

62.96

if it's a if it's a function

65.519

then

66.88

if we have a parenthesis open

68.96

parenthesis

70.08

we're gonna assume there's a function in

71.36

there however

73.84

there is the

75.2

key phrase current underscore time stamp

77.759

which actually is a function

80.479

um and there may be some others but this

82.4

one i know i ran into so go ahead and

84.799

count that as a function another thing

87.04

was uh just for safety's sake

90.159

i went ahead and decided to wrap the

93.04

column names in uh these little tick

96.32

marks

97.68

in case it is some sort of reserved word

99.68

or something like that

101.36

i came across that in one of the one

103.36

things i was playing around with so i

105.28

figured it was probably best to do that

108.159

that being said

109.6

now so what we've got is we've been able

112

to come in and deal with our tables the

115.04

next thing we want to do

116.96

is we're going to go with the table

118.24

we're going to see if the columns match

120.159

up

121.52

so let's

123.28

let's see create missing tables let's

125.36

just pause that for right now so we'll

127.52

come back to that later

130.16

and for now

131.76

what we want to do is we're going to go

133.599

for in each

135.44

let's see where we do find here we go

137.44

missing tables

139.76

and now we're going to create one that's

140.959

called missing columns and basically

143.28

it's going to work the way as

145.12

missing tables

147.84

so there's going to be a source and a

149.04

target

152.64

and let's see we had somewhere down here

154.8

we did missing tables

158.08

uh here

160.319

and we'll just print them for now

162.8

in case there are any i don't think

164.319

there will be because we should have

167.44

everything going

171.36

and now we're going to have missing

172.56

columns

177.36

and uh

179.04

these table dot columns

182.4

are not in target

185.519

so this one's gonna be a little more

186.8

complicated

192.159

so for missing columns what we're gonna

193.92

do

195.92

is we're gonna go through

203.44

so for each table

205.36

and we're going to assume that they're

207.2

not missing so we actually don't need

210.48

well i guess we do need it in both of

212.319

them

214.959

that's a good question let's say

218.959

well we do still need source and target

220.72

and did we call those

226.239

oh no so we need actually this source so

229.599

we do missing columns

231.68

here we go

232.72

we actually need

234.48

uh dbc which is our source

239.439

and db2 is our target

244.72

beca and let's actually do tables which

248.08

is going to be let's just say source

250.319

tables

252.959

so we're going to assume that they're

254.4

all there at this point

257.44

and we'll see how that works out

259.519

so

260.72

back up here

263.199

so we have

264.4

uh let's do this db source

268.08

db destination

270.56

and tables

272.96

tables list

275.759

and so missing columns so now what we're

277.52

going to do is

286.56

for row and tables list and so now each

289.6

one

290.8

this is gonna be a table name

292.479

and so we're gonna have to do a query

298.24

so

299.44

we'll just take that one for row and

301.6

there

304.08

uh we'll return missing columns

312.479

and let's see so this is going to be our

314.24

column query

316.96

this will be our

319.44

source column query

323.6

and let's see in each one yep well

325.6

actually that can be our column query

330.16

and so we are going to we need two

332

cursors oops

335.6

so let's do that

338.72

let's go and create these cursors real

340.24

quick so that's going to be cursor

341.84

source is

344.16

db source

346.4

the destination is going to be

350

tv

350.84

dust

352.639

and so we're going to do with each case

354

we're going to describe the table so

355.36

we're going to get two set specs we're

357.039

going to do

359.44

source

363.12

and destination

368.72

and so we want source and we want

371.199

destination

373.919

so now we're going to have source

375.36

columns

376.72

equals

378.4

we're going to have uh we'll call it

380.16

destination columns

382.72

equals

386.24

and

387.34

[Music]

393.28

oops um

399.919

wait that's how i want i'm sorry

402.88

we just want to execute them first

405.6

execute the two and then we're going to

407.919

be

411.759

source columns

414.319

equals cursor source stuff actually

416.56

there we go

418.4

destination columns

422

equals destination fetch all

426.24

and now what we're going to do here

432

is do something along these lines so

433.599

what we're going to do

438.24

so

440.8

so for

444.479

column row

445.759

in

448

target

449.199

destination columns

452.96

then for each one

455.039

let's see so name is going to be equal

457.039

to row 0

459.039

because that is where our names are for

460.72

our

462.84

columns and if name is not in

468.639

oh so what we want to do

475.44

okay so first we go to generate names so

478.879

let's call it

480.72

uh source names

484.16

equals this

486.319

and then for

488.479

conroe

490.84

and source columns

494.72

then for each one of those source names

500.72

dot a pin

504

uh see

505.44

row

506.639

zero so we're gonna go into each of

508.319

these first we're gonna do is we're

509.12

gonna build a list of the column names

511.36

and the source

513.599

and then we're gonna go through each one

515.519

of these uh which one of the

516.959

destinations and we're gonna say if

520.8

the name is not in source

524.48

names this is call row

528.24

so if the column if the name of the

530.56

destination is not in the source then

533.36

we're going to do

534.48

missing columns

538.88

and we're going to append

540.72

the name except for we're going to do

544.64

a table

546.64

which is

550.56

let's do this for table

553.6

there we go

559.44

it's gonna be table plus and we're just

561.12

going to do dot name so this is going to

562.88

give us all of the missing columns

565.519

and then we're going to run through

567.12

and see how that works now

572.08

so right now let's see if it even runs

576.16

whoops that's not what i want to do

582.399

so we didn't get any mix but let's go

584.32

ahead and break one here so let's go to

588.16

our source

593.2

uh

594.32

let's see so testing

597.519

was our source right

601.12

uh let's see tutorial

606.399

is our source okay

608.32

so

609.12

local

615.519

uh

616.72

see

620.959

uh is that right okay so then local

624

tutorial is our database

626.32

so let's go look here okay so if we go

628.24

here

629.12

let's get rid of a column somewhere like

633.279

it really doesn't matter so let's just

634.56

do with our address let's start with

635.76

that

637.36

let's get rid of our zip

640

so let's go in here

643.279

alter table

645.519

and we're going to delete this column

650.959

so we're going to drop columns up

655.44

and now what we should see is that

657.6

should show up

659.6

and

660.56

it does not

664.399

so let's

667.68

go in here

670.48

oh let's see these columns are not in

672.48

the target okay so we are

676.64

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

678.079

to go missing columns oh

681.04

did we do oh no that's right

686.839

so uh oh we did this wrong

690.16

we want to do

695.92

we want to do the destination columns

699.519

and

700.839

then we want to say

704.72

anything that's in source so this one

706.88

says

708.24

if the name is not in the source names

715.2

so if the source data is not in the

717.839

let's do this let's change this a little

719.36

bit

721.519

just to make it less

723.68

annoying

725.92

so here's the thing

729.04

so in this case we're going from a to b

731.04

and so we're going to say let's look at

732.399

all the columns in b

734.16

and then look at all the columns and

736.079

then for each of those we're going to

737.2

see which is here

739.279

and then for each of those

741.2

we're going to say

743.519

for everything that's in a we're going

745.04

to say hey does that already exist if it

746.88

doesn't then it's missing

748.959

and that should give us

751.04

uh here we go missing calls is not

753.12

defined

754.56

oh

756.48

it's columns

759.279

and so i bet now we'll see there we go

761.2

so address.zip is not there

764.72

so we have gone through and we're now

766.639

doing this and so we can find this is uh

769.6

as we did before uh this is

773.839

these columns are not in target we can

775.92

take the same thing

779.2

and say these columns are not in source

781.68

just in case there's some extras

784

and all we do here

789.2

is swap these two

792.399

so that's dvc

794.24

that's db2 so what we'll get is we

797.2

should get through here address that's

799.04

it but it's all fine the other way

802.48

and so that brings us to now what do we

804.639

do

805.6

so if we do this

808.079

uh

809.36

where did that go here

814

so we need to do in this case

817.04

is say that we have a missing column

819.04

which is good

820.48

because what we can do

823.519

is

824.56

um

826.079

we can actually do this

829.04

um

830.48

how do we want to do this this will be

831.76

good

834.959

let's just look at it this way

836.72

uh this won't give us a name but

839.04

what we can do is we could just say call

841.279

row and now we're going to see what the

843.12

definition of that is

845.92

and here we see that okay so this is

848

missing

849.36

and so all we need

851.279

is a table name so we probably want to

852.959

do this a little differently we're going

853.92

to probably want to build our sql right

855.36

here

856.72

and then go back to this

859.68

and then create out our sql

863.199

which will probably include the table

865.839

and the name

867.04

so it would be something along bike

869.279

let's do table sequel

871.92

equals

873.6

alter table

876

plus

877.519

name

880.24

plus

881.68

and column

884.8

uh

886.24

oops that's not name that's table

892.16

plus name

894.639

plus we'll do this plus a type which is

898.72

a call row

902.8

which we found before is the type is

907.68

uh one

910.32

so let's look at this for now uh and

911.92

then we'll print this

913.68

let's print uh call row

917.519

there we go

920.8

and so now we're gonna see oops we

922.72

didn't print that didn't we oh yes we

924.24

did

927.12

um

928.8

oh my mistake we want this to be

932.48

a table underscore sequel

936.24

bam

938.16

so

940.959

here we go so now we're starting to see

942.72

the start of our sequel

945.04

and uh we can see here

947.68

uh basically you know for this column

950.56

this sort of puts us a run to the end of

952.8

this one so the next episode we're going

954.32

to come back we're starting building out

955.6

that column sql and probably like we've

958.24

seen before where we built the table

959.68

we're going to have to you know

961.199

essentially

962.88

um

964

steal some of the same information so we

966.24

may do some sort of uh you know abstract

968.16

this out to a

969.839

build column sql something along those

972.079

lines

973.44

but i don't want to you know spoil it

975.36

too much

976.48

so for now go out there and have

978

yourself a great day a great week and we

980.399

will talk to you next time

998.959

you