-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure --force-reset drops previous tables (#10506)
* Ensure --force-reset drops previous tables * Remove unused import * Do suggestions
- Loading branch information
Showing
4 changed files
with
83 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@astrojs/db": patch | ||
--- | ||
|
||
Ensure --force-reset drops previous tables |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { expect } from 'chai'; | ||
import { describe, it } from 'mocha'; | ||
import { | ||
getMigrationQueries, | ||
} from '../../dist/core/cli/migration-queries.js'; | ||
import { MIGRATION_VERSION } from '../../dist/core/consts.js'; | ||
import { tableSchema } from '../../dist/core/schemas.js'; | ||
import { column, defineTable } from '../../dist/runtime/config.js'; | ||
|
||
const TABLE_NAME = 'Users'; | ||
|
||
// `parse` to resolve schema transformations | ||
// ex. convert column.date() to ISO strings | ||
const userInitial = tableSchema.parse( | ||
defineTable({ | ||
columns: { | ||
name: column.text(), | ||
age: column.number(), | ||
email: column.text({ unique: true }), | ||
mi: column.text({ optional: true }), | ||
}, | ||
}) | ||
); | ||
|
||
describe('force reset', () => { | ||
describe('getMigrationQueries', () => { | ||
it('should drop table and create new version', async () => { | ||
const oldCollections = { [TABLE_NAME]: userInitial }; | ||
const newCollections = { [TABLE_NAME]: userInitial }; | ||
const { queries } = await getMigrationQueries({ | ||
oldSnapshot: { schema: oldCollections, version: MIGRATION_VERSION }, | ||
newSnapshot: { schema: newCollections, version: MIGRATION_VERSION }, | ||
reset: true, | ||
}); | ||
|
||
expect(queries).to.deep.equal([ | ||
`DROP TABLE "${TABLE_NAME}"`, | ||
`CREATE TABLE "${TABLE_NAME}" (_id INTEGER PRIMARY KEY, "name" text NOT NULL, "age" integer NOT NULL, "email" text NOT NULL UNIQUE, "mi" text)`, | ||
]); | ||
}); | ||
|
||
it('should not drop table when previous snapshot did not have it', async () => { | ||
const oldCollections = {}; | ||
const newCollections = { [TABLE_NAME]: userInitial }; | ||
const { queries } = await getMigrationQueries({ | ||
oldSnapshot: { schema: oldCollections, version: MIGRATION_VERSION }, | ||
newSnapshot: { schema: newCollections, version: MIGRATION_VERSION }, | ||
reset: true, | ||
}); | ||
|
||
expect(queries).to.deep.equal([ | ||
`CREATE TABLE "${TABLE_NAME}" (_id INTEGER PRIMARY KEY, "name" text NOT NULL, "age" integer NOT NULL, "email" text NOT NULL UNIQUE, "mi" text)`, | ||
]); | ||
}); | ||
}); | ||
}); |