
root problem is a slow migration
unique connectiontype id constraint wasn’t a problem bc it didn’t crash server
models.sequelize.sync crashed server bc it didn’t have a catch
so the belongsto relation between connection and connectionType wasn’t being enforced, so it was possible to assign a connection’s connectionType id to a type that didn’t exist in the db
which would cause crashes
lucas wrote a script to look for connections with non-existent type. and then deleted those
there was also a similar error on the notifications table
this allowed the server to start, and to properly enforce the connection type constraint
the notification cardids foreignkey constraint was missing / not enforced
server crashed bc sync took very long, bc of foreign key constraints
started on the 17th, notification’s are created without proper card id
so i need to prevent this from happening
add time measurement to models.sequelize.sync
Remove cardid constraint
1. look into finding the clientside cause for notifications sending wrong cardids
add console.logs to each route
2. run the script and remove the busted notifications (can you send it?)
heroku kills server if this doesn’t complete after 30s
This is the script to find corrupted notifications:
select notifications.* from notifications
left outer join cards on notifications."cardId" = cards.id
where notifications."cardId" is not null and cards.id is null order by "createdAt" desc;
Now
3. and then re-run the sync
4. i’ll rewatch the part of the vid where you cover how to spot the sync error so i can validate whether it actually synced without new errors
long running process, from sync alter:true
ALTER TABLE "cards" ALTER COLUMN "x" DROP NOT NULL;
ALTER TABLE "cards" ALTER COLUMN "x" DROP DEFAULT;
ALTER TABLE "cards" ALTER COLUMN "x" TYPE INTEGER;
check foreignkey constraints in connections and notification
This is the script to create the foreign key constraint: ALTER TABLE “notifications” ADD FOREIGN KEY (“cardId”) REFERENCES “cards” (“id”) ON DELETE SET NULL ON UPDATE CASCADE; feel free to set ‘ON DELETE CASCADE’
purge old notifications. scheduled task
add notification email w no recipients null soak
see screenshot for stacktrace
“I would recommend to initialize all the existing tables in your first script and from then on just use it to alter stuff”
And associations. Equalize assisiations now do nothing and can be removed from models.js
so the current live version has alter: false
if needed, may not be needed after sync success
w alter: true
app starts by dropping all constraints and rebuilding thenm
DROP CONSTRAINT
followed by ALTER
s
edit readme w migration instructions
return response/error on operations so client knows when db fails
alter: false
write a migration that specifies all the existing table names, columns, contraints, and indexes
edit models to create new dbs
this gets real slow eventually
initialize all the existing tables in your first script and from then on just use it to alter stuff
migrations needed for new columns
so when you git pull for the first time you can just run the migrations and start developing
so the model files are like a bridge: the migrations change the actual db, the model files tell sequlize about the current state of the db
investigate h12 timeouts
identify specific slow queries for perf investigation
basically it’s just that extra migration step on top, but the model parts are the same as before right?
space.findDetailed
get user favs?
get user tags?
add new indexes
🌱 operation createConnection
SequelizeForeignKeyConstraintError:
Key (connectionTypeId)=(rqRRanHIIPSdvWutc5LU5) is not present in table "connectionTypes
fix operation: notifications saving w non-existing cardids
SequelizeForeignKeyConstraintError:
where
migration plan / questions
on sign up / sign in err using prev anon user id instead of new one
find a way to run npx sequelize-cli db:migrate on app start, runs new migrations
mar 25 12pm POST /space/multiple
sequelize CLI will create a new table where it will keep a log of all migrations it already executed. This will ensure that no migration is executed twice.
migration is a requirement before we can add new indexes?
mon 25, 4am GET /user/favorites
on operation createConnectionType
I would develop this script with a completely empty database.
make a 2ndary blank local db
redo queue system to support multiple batches with max size
> does anything get deleted or overwritten ?
maybe there is a way to duplicate heroku dbs for prod testing
maintains order
need v7?
It shouldn’t because the migration script will usually use a ‘create table’ query which will fail and stop the whole migration.
handles fails without removing failed items from queue?
Once the script is complete we can figure out how to fake the migration log in the prod DB so it doesn’t start with the first initial migration.
create all basic tables w basic columns
add associations
1 migration file per table
create blank db
add existing indexes
server uses it
spaces_collaboratorKey
recheck associations
run migrations on blank db
personalBlogSubscribers_email_key
in personalBlogSubscribers table
eyeball test that tables, columns, associations, indexes matches prod
table names (check plurals)
users table: duplicate users_email_key and users_appleAppAccountToken_key indexes
manually copy the SequelizeMeta table to local dev and prod dbs
table structures
prod
run migration after SequelizeMeta to ensure nothing runs
desktop
laptop
note to lucas w screenshot of sequelizeMeta
readme:
new migrations should match model.js and modelfiles
merge pr to prod
CREATE TABLE IF NOT EXISTS "SequelizeMeta" (
name VARCHAR(255) PRIMARY KEY
);
INSERT INTO "SequelizeMeta" (name)
VALUES
('000-create-table-user.js'),
('001-create-table-box.js'),
('002-create-table-card.js'),
('003-create-table-connection.js'),
('004-create-table-connectionType.js'),
('005-create-table-journalDailyPrompt.js'),
('006-create-table-journalPrompt.js'),
('007-create-table-meta.js'),
('008-create-table-notification.js'),
('009-create-table-personalBlogComment.js'),
('010-create-table-personalBlogSubscriber.js'),
('011-create-table-sessionToken.js'),
('012-create-table-shouldNotSendToNonUser.js'),
('013-create-table-shipping.js'),
('014-create-table-space.js'),
('015-create-table-tag.js'),
('016-create-table-userAIGeneratedImage.js'),
('017-create-table-userFavoriteColor.js'),
('018-create-table-userSpace.js'),
('022-create-table-spaceCollaboratorUser.js'),
('023-create-table-notificationUnsubscribe.js'),
('024-create-table-userVisitSpace.js'),
('025-initial-foreign-key-associations.js'),
('025-initial-indexes.js');
add info about how migrations are run