📺 Develpreneur YouTube Episode

Video + transcript

SQL Sync Part 21

2022-10-25 •Youtube

Detailed Notes

Focus for this episode: This episode steps into some issues we can see with related data and cleaning up tables when the records are referenced elsewhere.

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 are
continuing our SQL python combo tutorial
series and we went through a longer one
last time I apologize but it was one of
those it does take a little bit of time
to go do some of that cleanup
and uh did get that stuff cleaned up so
now we have our uh sort of an organized
DB sync class that has stuff sitting in
it to sort of help us out and call it
from different places and then we're
going to run everything from our main
script
now looking at our to do thing
um let's see so we basically got
uh how to import related data multiple
ways generalize extract key functions
okay so we got the first part of this so
we're going to do
this like this because we're going to
come back to it we're going to provide a
way to walk multiple databases
we're not going to do that today
we are going to deal with uh we're going
to start dealing with our data issues a
little bit someone we can run into this
is going to start us on walking the
foreign key hierarchy just a little bit
but also
it highlighted to me that we have not
pulled across foreign Keys we deal with
primary keys
but we actually do not create any other
references so that's something we're
going to end up picking up as well
probably not this episode because we're
going to keep it simple this time
so I want to show you what I'm I'm
running into here uh what got us going
so when we go through when we go uh
see where's that uh Rose there we go so
we do sync rows we come in and we just
do a truncate of each table so every
table we just go through and empty it
and start pulling rows across now we may
not want to do that so this is an option
you know there's something else that
we're going to start exploring as we go
further on but right now let's just work
with this trunk cut truncate
now let's just take two tables here
address and lookup type
if I truncate them no problem I can go
through them however
if I suddenly add a foreign key
relationship well that's a drop
so if I add to the table address a
constraint where now its type ID is
referenced in lookup type
and then we'll see what that looks like
real quick if you look at address you
can see here instead of PRI it's got
this mul
for the key
and then if I try to do a truncate bam I
get an error cannot truncate a table
referenced in a foreign keyword
constraint so if I have that there when
I'm running through trying to do my
simple processes
so if I come through here let's see
where's Maine
let's just do this
so if I do it then it's not going to
like it because it's going to blow up
right here foreign key constraint fails
so what we need to do is we need to be
able to First adjust those constraints
and what we really want to do
is for that given one we want to check
so we're going to do that here
when we come in here the first thing we
want to do before we do this truncate
is we're going to check if this
is referenced
in a foreign key
so what we're going to do is we're going
to see if this table which in this case
it would be lookup type we've got to see
does something reference it
now it's not that address references it
so we can't find it if we look back at
your description of address we don't see
that also if we look at description of
lookup type
we don't see that we don't see where
it's it's referenced anywhere so
we have to go back into our information
schema
and this is actually going to be in
referential constraints
and so what we're going to do is let's
just do that and again where the table
is tutorial if we look
then we're going to see here because we
don't notice we didn't even have a name
before or I'm sorry here
that's a drop when we do add constraint
we didn't give it a name it just has a
name
so
we have this constraint name we can see
here
that the reference table is lookup type
which is the that's what we care about
and then the table name that references
it we've got that so between that
because if you remember the way we get
rid of a foreign key is it has to be
alter table
the table name that it's on not the one
that reference the one that references
it not the table and then the name of
the foreign key so we would have the
constraint name so we have the
constraint name
we have the table name and we have the
thing that says hey we need to care
about this so
what we want to do first
and we probably don't need all of these
so we're going to probably clean this up
a little bit let's go with our query so
what we want to do is we want to do a
first
uh let's just do a
net we'll go ahead and pull all of them
so we really don't need anything other
than let's go look at this real quick
the shortened version and we really
don't need we don't care what the unique
constraint name
uh we do care about the unique
constraint name we don't care about the
match option we don't care about the
update rule we don't care about the
delete rule although we could tweak it
there
uh let's see and so we have our oh we
don't need our unique constraint name so
constraint name table name reference
table
and really what we need to do here and
reference table name
equals and in this case uh lkp
underscore typ
and so we're going to get
is Bam we have those and we really don't
even need that second one so now that's
all we need
and so from there we're going to take
that and we're going to do an altered
table because we're going to go ahead
and we're just this sort of again sort
of a Brute Force
approach is we're going to go through
and we're going to say hey if it's there
we're going to drop it and then we're
going to be able to truncate so what we
need to do here
is this is going to be in the
destination database so this is going to
be our check SQL equals
and what we need to do here
is
we're going to pull all these and what
we're going to do is we're just going to
make this
painfully simple to do so uh let's see
so we're constrained
schema equals
and that's going to be our table
which whoops
I already have it down there
we've already loaded it up
actually I guess it we loaded it right
there for table in
oh I'm sorry that's constraint that's
not the table whoops
so here we're going to need our
oh this is where so we're going to
change this a bit because before we
would have places where we would send
let me see here
we would send our database name
so we're going to have to call what yep
where we are here so we're going to have
to change this and we're going to have
to call this
um
destination DB
um let's do destination DB name just to
be safe
and then when we do sync rows
now we're going to have to call it
whatever our database name is
this is something we're going to clean
up a little bit later as well by the way
but for now
so this is going to be a destination
DB name
let's just do it like this
um
do that to sort of clean it up a little
bit from its length and then the
reference table name this is where
we're gonna do
that's just going to be table
and then
I don't need this part that comes later
so let's see so close that out so first
what we want to do is we're going to go
through uh where is our this is our
cursor destination
and we're going to do a DOT execute
um
oh good question how do we want to do
this let's do
uh we're going to do this here first
so that's going to be the check SQL
but and then we're going to come in and
we're going to have to get rid of that
thing but we want to take the
descriptive query
let's see do we want to do that
uh yes so first I'm going to do is we're
going to try to get rid of that so
uh get rid of our data so we come in
here blah blah blah
Okay so
so first we empty our table we don't
need to know anything but the table name
there so what we can do is we come in
here and we can say four row
in
let's call that for foreign key row
in
and this will be
um
let's call it FK rows
then we're going to go through here
and then for each one we're going to do
this uh FK SQL
equals and then we're gonna have to
clean all this thing up
this way
this is one of those exciting parts of
the tutorial where we will watch Rob
type so we go there
we go there we go there
so we come in here we're going to alter
table
and we need to give it our table name
and that's going to be our table and
then we're going to drop foreign key
oh actually nope this is going to be in
FK row zero
because that's our
oop our table name is one I'm sorry
and then we're going to drop
foreign whoops
Plus
uh FK row zero
and then we're going to execute it so
we're going to do uh cursor
destination
dot execute
hopefully this won't blow up because
we're using same cursor but I think
we're okay because we always have our
rows and now we should be able to do it
so
running this same thing
before when we got rid of this all right
let me pull that back out just to verify
it's still there so before it's going to
blow up
there it blows up now if I come up here
and I put our code in
to take care of that
uh let me just do this real quick just
to make sure we show see what we're
building here
uh
that's FK SQL let's make sure I got that
right
yeah okay
so now if we look at it
bam up now we're getting a nun type
object is not iterable oh
if
okay rows
oh
um
oh my mistake I need to do a fetch all
I knew there was something simple I was
missing there uh let's see
so that's going to be cursor destination
whoops
that's where the FK Rose is
put my S there
now
we should see that
and so now we're seeing cannot add or
update a childhood foreign key
constraint
fails
and
a good question so if we come through
here
before we get rid of it
oh we missed something we did not get
that right
uh let's see data sync for address
cannot update
uh what did we do here
cfk SQL check SQL
[Music]
and we did something not quite right
here so
let's see for table and Source tables
then I'm going to take
let's make sure we got that right maybe
I missed something there I probably did
uh print the check SQL
and if we look let's see select
constraint name
reference table name equals address
oh you know what
oh okay so that's none which is good uh
I wonder if it's uh giving me this
so he should come through
and we're missing something here so okay
let's figure out what we got in our
little typos
um
cannot add or updated child row
constraint
which should be fine
mean 20 but it's in DB sync 402
cannot add or update a child row
oh because we're sinking the rows at a
time we need to actually okay
what we need to do
is we actually need to go through here
and first uh
see
we're gonna do the same thing we've got
to get rid of all of these before we do
our truncate
so let's do this
uh first we're going to do that so we're
going to come through here we don't need
to know we have to do any of the
truncated stuff because we've already
done that
okay
and this is going to be
data clean for
and we're going to do that and then
we're going to do the whoops
let's take the that part
to where we were and so we're going to
come in here and we're going to go for
each table and first we're going to
clean out all the data because what was
happening is we're running into some
issues where we were pulling data in but
we had a foreign key that was blowing us
up so now let's see if that'll work
uh yeah we still have our prints and
we're still running into something here
so we're not finding any so when we find
one
let's make sure I got that right
I don't think I did I think I lost one
something there
um because I don't think it's foreign
key yeah that's why
that's what I thought I missed something
a typo okay so if I go in here and I do
4 key
now we're going to do it and now it's
going to bring all our data across
so once again we've
found a couple you know bumped our head
against something and been able to make
a correction for it now what we have
also found is that we did not you know
we had to go in and manually
create our
um our constraint here
what we're going to do next time around
is we need to be able to
uh we're going to be like let's say sync
constraints
sync non-primary constraints and that is
what we're going to tackle next time
that being said we can wrap this one up
while this thing is running in the
background trying to get some data moved
across
but while we're waiting for that go out
there and have yourself a great day a
great week and we will talk to you next
time
foreign
Transcript Segments
10.7

thank you

18.89

[Music]

27.119

well hello and welcome back we are

29.22

continuing our SQL python combo tutorial

32.34

series and we went through a longer one

35.579

last time I apologize but it was one of

38.219

those it does take a little bit of time

39.719

to go do some of that cleanup

41.64

and uh did get that stuff cleaned up so

44.52

now we have our uh sort of an organized

47.46

DB sync class that has stuff sitting in

50.579

it to sort of help us out and call it

52.14

from different places and then we're

53.76

going to run everything from our main

54.899

script

55.86

now looking at our to do thing

59.219

um let's see so we basically got

63.48

uh how to import related data multiple

66.479

ways generalize extract key functions

68.52

okay so we got the first part of this so

70.979

we're going to do

73.08

this like this because we're going to

74.76

come back to it we're going to provide a

76.02

way to walk multiple databases

78.72

we're not going to do that today

80.52

we are going to deal with uh we're going

83.04

to start dealing with our data issues a

84.78

little bit someone we can run into this

87.299

is going to start us on walking the

89.22

foreign key hierarchy just a little bit

91.22

but also

93.6

it highlighted to me that we have not

95.759

pulled across foreign Keys we deal with

98.1

primary keys

99.42

but we actually do not create any other

103.28

references so that's something we're

105.119

going to end up picking up as well

106.86

probably not this episode because we're

108.84

going to keep it simple this time

110.52

so I want to show you what I'm I'm

112.92

running into here uh what got us going

115.079

so when we go through when we go uh

119.159

see where's that uh Rose there we go so

122.52

we do sync rows we come in and we just

124.92

do a truncate of each table so every

127.259

table we just go through and empty it

128.64

and start pulling rows across now we may

132.42

not want to do that so this is an option

134.34

you know there's something else that

135.84

we're going to start exploring as we go

137.459

further on but right now let's just work

139.62

with this trunk cut truncate

142.26

now let's just take two tables here

144.959

address and lookup type

147.36

if I truncate them no problem I can go

149.819

through them however

151.56

if I suddenly add a foreign key

153.72

relationship well that's a drop

157.14

so if I add to the table address a

160.319

constraint where now its type ID is

162.66

referenced in lookup type

165.599

and then we'll see what that looks like

167.22

real quick if you look at address you

168.72

can see here instead of PRI it's got

171.12

this mul

172.86

for the key

174.9

and then if I try to do a truncate bam I

178.739

get an error cannot truncate a table

181.019

referenced in a foreign keyword

182.519

constraint so if I have that there when

185.76

I'm running through trying to do my

187.98

simple processes

190.2

so if I come through here let's see

193.08

where's Maine

195

let's just do this

198.42

so if I do it then it's not going to

201.18

like it because it's going to blow up

202.98

right here foreign key constraint fails

205.56

so what we need to do is we need to be

208.14

able to First adjust those constraints

211.8

and what we really want to do

215.239

is for that given one we want to check

220.319

so we're going to do that here

222.48

when we come in here the first thing we

224.64

want to do before we do this truncate

226.92

is we're going to check if this

232.92

is referenced

234.959

in a foreign key

239.099

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

240.36

to see if this table which in this case

242.28

it would be lookup type we've got to see

245.159

does something reference it

247.739

now it's not that address references it

250.5

so we can't find it if we look back at

253.08

your description of address we don't see

256.44

that also if we look at description of

259.799

lookup type

264.06

we don't see that we don't see where

265.86

it's it's referenced anywhere so

269.52

we have to go back into our information

272.1

schema

273.479

and this is actually going to be in

274.919

referential constraints

276.96

and so what we're going to do is let's

278.699

just do that and again where the table

280.74

is tutorial if we look

284.16

then we're going to see here because we

285.96

don't notice we didn't even have a name

287.4

before or I'm sorry here

291

that's a drop when we do add constraint

293.52

we didn't give it a name it just has a

295.199

name

295.979

so

297.36

we have this constraint name we can see

300.419

here

301.919

that the reference table is lookup type

303.9

which is the that's what we care about

306.54

and then the table name that references

308.88

it we've got that so between that

311.4

because if you remember the way we get

315.12

rid of a foreign key is it has to be

318.36

alter table

319.68

the table name that it's on not the one

322.199

that reference the one that references

323.82

it not the table and then the name of

327.479

the foreign key so we would have the

329.52

constraint name so we have the

331.02

constraint name

332.28

we have the table name and we have the

334.56

thing that says hey we need to care

336.24

about this so

340.08

what we want to do first

343.979

and we probably don't need all of these

346.02

so we're going to probably clean this up

347.28

a little bit let's go with our query so

348.6

what we want to do is we want to do a

350.699

first

352.199

uh let's just do a

355.08

net we'll go ahead and pull all of them

357

so we really don't need anything other

359.46

than let's go look at this real quick

361.139

the shortened version and we really

363.24

don't need we don't care what the unique

364.62

constraint name

366.18

uh we do care about the unique

367.979

constraint name we don't care about the

370.08

match option we don't care about the

371.94

update rule we don't care about the

373.68

delete rule although we could tweak it

376.199

there

377.1

uh let's see and so we have our oh we

380.16

don't need our unique constraint name so

382.259

constraint name table name reference

383.819

table

385.56

and really what we need to do here and

388.44

reference table name

392.88

equals and in this case uh lkp

396.919

underscore typ

399.9

and so we're going to get

402.6

is Bam we have those and we really don't

404.4

even need that second one so now that's

405.96

all we need

409.08

and so from there we're going to take

410.819

that and we're going to do an altered

412.139

table because we're going to go ahead

413.16

and we're just this sort of again sort

415.8

of a Brute Force

418.02

approach is we're going to go through

419.639

and we're going to say hey if it's there

420.66

we're going to drop it and then we're

422.22

going to be able to truncate so what we

424.139

need to do here

426.12

is this is going to be in the

428.1

destination database so this is going to

430.86

be our check SQL equals

435.3

and what we need to do here

439.639

is

441.3

we're going to pull all these and what

443.34

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

444.479

make this

446.599

painfully simple to do so uh let's see

451.319

so we're constrained

453.599

schema equals

459.3

and that's going to be our table

463.38

which whoops

465.479

I already have it down there

467.34

we've already loaded it up

469.38

actually I guess it we loaded it right

471

there for table in

474.66

oh I'm sorry that's constraint that's

475.979

not the table whoops

478.86

so here we're going to need our

481.979

oh this is where so we're going to

484.02

change this a bit because before we

486.06

would have places where we would send

488.3

let me see here

490.319

we would send our database name

493.86

so we're going to have to call what yep

496.919

where we are here so we're going to have

499.139

to change this and we're going to have

500.34

to call this

502.379

um

503.039

destination DB

507.12

um let's do destination DB name just to

510.36

be safe

511.56

and then when we do sync rows

515.099

now we're going to have to call it

516.659

whatever our database name is

518.76

this is something we're going to clean

519.899

up a little bit later as well by the way

521.52

but for now

524.099

so this is going to be a destination

528.899

DB name

535.019

let's just do it like this

537.3

um

541.08

do that to sort of clean it up a little

543

bit from its length and then the

545.399

reference table name this is where

550.1

we're gonna do

552.779

that's just going to be table

556.38

and then

559.86

I don't need this part that comes later

563.64

so let's see so close that out so first

566.399

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

568.5

through uh where is our this is our

572.48

cursor destination

576.839

and we're going to do a DOT execute

580.68

um

583.26

oh good question how do we want to do

585.36

this let's do

588.12

uh we're going to do this here first

592.14

so that's going to be the check SQL

595.019

but and then we're going to come in and

597.12

we're going to have to get rid of that

598.8

thing but we want to take the

601.74

descriptive query

610.44

let's see do we want to do that

612.92

uh yes so first I'm going to do is we're

616.2

going to try to get rid of that so

618.24

uh get rid of our data so we come in

620.1

here blah blah blah

622.14

Okay so

624.6

so first we empty our table we don't

626.339

need to know anything but the table name

627.779

there so what we can do is we come in

630.42

here and we can say four row

633.54

in

636.48

let's call that for foreign key row

641.459

in

644.64

and this will be

646.8

um

647.76

let's call it FK rows

652.98

then we're going to go through here

655.44

and then for each one we're going to do

657.72

this uh FK SQL

661.62

equals and then we're gonna have to

663.54

clean all this thing up

669.42

this way

676.32

this is one of those exciting parts of

677.88

the tutorial where we will watch Rob

680.579

type so we go there

683.279

we go there we go there

686.72

so we come in here we're going to alter

688.92

table

690.72

and we need to give it our table name

695.459

and that's going to be our table and

698.16

then we're going to drop foreign key

701.16

oh actually nope this is going to be in

706.019

FK row zero

709.26

because that's our

711.24

oop our table name is one I'm sorry

714.899

and then we're going to drop

718.86

foreign whoops

721.86

Plus

724.76

uh FK row zero

731.64

and then we're going to execute it so

734.04

we're going to do uh cursor

738.54

destination

740.82

dot execute

744.839

hopefully this won't blow up because

746.459

we're using same cursor but I think

748.38

we're okay because we always have our

749.579

rows and now we should be able to do it

752.22

so

754.079

running this same thing

756.12

before when we got rid of this all right

759.18

let me pull that back out just to verify

760.98

it's still there so before it's going to

762.959

blow up

764.04

there it blows up now if I come up here

766.74

and I put our code in

768.839

to take care of that

771.48

uh let me just do this real quick just

773.88

to make sure we show see what we're

776.279

building here

778.68

uh

781.2

that's FK SQL let's make sure I got that

784.86

right

787.74

yeah okay

789.779

so now if we look at it

792.06

bam up now we're getting a nun type

795.3

object is not iterable oh

800.639

if

802.68

okay rows

808.26

oh

810.66

um

813.24

oh my mistake I need to do a fetch all

817.38

I knew there was something simple I was

818.94

missing there uh let's see

821.639

so that's going to be cursor destination

826.019

whoops

828.54

that's where the FK Rose is

831.36

put my S there

839.04

now

841.019

we should see that

842.7

and so now we're seeing cannot add or

845.339

update a childhood foreign key

846.959

constraint

848.16

fails

850.86

and

856.44

a good question so if we come through

859.019

here

863.399

before we get rid of it

866.76

oh we missed something we did not get

868.92

that right

869.94

uh let's see data sync for address

872

cannot update

878.579

uh what did we do here

880.92

cfk SQL check SQL

885.24

[Music]

890.22

and we did something not quite right

893.16

here so

898.32

let's see for table and Source tables

903.959

then I'm going to take

906.959

let's make sure we got that right maybe

909

I missed something there I probably did

912.06

uh print the check SQL

916.74

and if we look let's see select

920.76

constraint name

931.019

reference table name equals address

941.579

oh you know what

947.1

oh okay so that's none which is good uh

960.24

I wonder if it's uh giving me this

975.6

so he should come through

985.32

and we're missing something here so okay

987.779

let's figure out what we got in our

989.22

little typos

990.839

um

991.92

cannot add or updated child row

994.44

constraint

1000.38

which should be fine

1004.94

mean 20 but it's in DB sync 402

1019.459

cannot add or update a child row

1027.799

oh because we're sinking the rows at a

1030.5

time we need to actually okay

1032.959

what we need to do

1035.12

is we actually need to go through here

1038.66

and first uh

1041.6

see

1044.299

we're gonna do the same thing we've got

1045.679

to get rid of all of these before we do

1046.939

our truncate

1048.14

so let's do this

1050.6

uh first we're going to do that so we're

1052.34

going to come through here we don't need

1054.2

to know we have to do any of the

1055.64

truncated stuff because we've already

1056.96

done that

1058.76

okay

1061.34

and this is going to be

1065.419

data clean for

1067.72

and we're going to do that and then

1070.52

we're going to do the whoops

1074.539

let's take the that part

1081.08

to where we were and so we're going to

1083.48

come in here and we're going to go for

1084.62

each table and first we're going to

1086.36

clean out all the data because what was

1088.46

happening is we're running into some

1090.62

issues where we were pulling data in but

1092.66

we had a foreign key that was blowing us

1095

up so now let's see if that'll work

1097.88

uh yeah we still have our prints and

1101.24

we're still running into something here

1107.72

so we're not finding any so when we find

1110

one

1114.26

let's make sure I got that right

1116.539

I don't think I did I think I lost one

1118.28

something there

1119.9

um because I don't think it's foreign

1122.419

key yeah that's why

1124.7

that's what I thought I missed something

1126.14

a typo okay so if I go in here and I do

1129.2

4 key

1131.78

now we're going to do it and now it's

1133.52

going to bring all our data across

1135.74

so once again we've

1139.46

found a couple you know bumped our head

1141.679

against something and been able to make

1143.72

a correction for it now what we have

1146.6

also found is that we did not you know

1148.4

we had to go in and manually

1150.5

create our

1153.2

um our constraint here

1155.96

what we're going to do next time around

1157.66

is we need to be able to

1162.2

uh we're going to be like let's say sync

1164.72

constraints

1165.919

sync non-primary constraints and that is

1171.38

what we're going to tackle next time

1173.059

that being said we can wrap this one up

1174.919

while this thing is running in the

1176.539

background trying to get some data moved

1178.039

across

1178.88

but while we're waiting for that go out

1180.62

there and have yourself a great day a

1182.48

great week and we will talk to you next

1185.78

time

1198.5

foreign