question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SQL Migrations Not Working?

See original GitHub issue

So I know you wrote a test to test that ported sqlite migration code, saw that test in one of your commits. But when I try this in my app, it’s simply not working:

InMemoryDB.ts

const inMemoryDb = newDb();
inMemoryDb.public.none(fs.readFileSync(path.resolve(__dirname, 'migrations/001-initial.sql'), 'utf8'));
(async () =>await inMemoryDb.public.migrate())();
const skills = inMemoryDb.public.many("select * from skills where name = 'Swift'");
console.log('skills', skills); // prints []

the migrations folder is at the same level as InMemoryDB.ts and in that folder is:

001-initial.sql
002-add-skills.sql

001-initial.sql - definitely adds a skills table seeded with some initial skills

002-add-skills.sql

INSERT INTO skills VALUES ('Swift', 0);
INSERT INTO skills VALUES ('Objective-C', 0);

no idea here, thoughts?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
oguimbalcommented, Jan 12, 2021

Tests

Some kind of building (transpiling, actually) is necessary given that it’s TS, so no, it is not possible to run mocha directly on ts files. When you do that on your side, I suppose that you’re using ts-node or something equivalent, which is transpiling TS for you. That’s also what does Webpack. Kind of an equivalent way to do the same thing.

Moreover, the current setup of pg-mem tests is using webpack because it’s way faster to code that way for me. That’s a matter of choice.

Ts compilation error

I dont see this problem on my side …

1reaction
oguimbalcommented, Jan 8, 2021

No need to read 001-initial.sql, the migration will do that for you.

Moreover, the line (async () =>await inMemoryDb.public.migrate())(); is effectively scheduling a migration, but triggers it “for later” (it dettaches a promise… use a linter, and you will get a warning on this line). Dettached promises is a pattern that should be avoided as much as possible (and which is, in this case, the cause of your problem).

Thus, the 4th line is effectively executed BEFORE the migration.

This should work:

const inMemoryDb = newDb();
await inMemoryDb.public.migrate();
const skills = inMemoryDb.public.many("select * from skills where name = 'Swift'");
console.log('skills', skills);

You’ll have to execute this from an async method, though.

Read more comments on GitHub >

github_iconTop Results From Across the Web

sql server - Migration not working - Stack Overflow
I can't get migrations to work. When I try Add-Migration InitialCreate I get the following error: Startup project 'docker-compose' is a Docker ...
Read more >
Common issues - Azure Database Migration Service
Learn about how to troubleshoot common known issues/errors associated with using Azure Database Migration Service.
Read more >
SQL Migration does not run in docker · Issue #2952 - GitHub
Migrations do not automatically run when your application begins. Running dotnet App.dll will only start the web server. A separate action is ...
Read more >
Code First Migrations are not applied during publish
When I publish, everything reports success, yet none of my migrations are being applied to the specified database(s). This deployment and application has...
Read more >
Migrations - Django documentation
For migrations to work, you must make the initial migration first and then make changes, as Django compares changes against migration files, not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found