-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
718eed7
commit 93ec9e2
Showing
15 changed files
with
188 additions
and
603 deletions.
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": minor | ||
--- | ||
|
||
Revamp migrations system |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,52 +1,34 @@ | ||
import type { AstroConfig } from 'astro'; | ||
import type { Arguments } from 'yargs-parser'; | ||
import type { DBConfig } from '../../../types.js'; | ||
import { getMigrationQueries } from '../../migration-queries.js'; | ||
import { | ||
MIGRATIONS_NOT_INITIALIZED, | ||
MIGRATIONS_UP_TO_DATE, | ||
MIGRATION_NEEDED, | ||
getMigrationStatus, | ||
} from '../../migrations.js'; | ||
import { getMigrationQueries, | ||
createCurrentSnapshot, | ||
createEmptySnapshot, | ||
getProductionCurrentSnapshot, } from '../../migration-queries.js'; | ||
import { getManagedAppTokenOrExit } from '../../../tokens.js'; | ||
|
||
export async function cmd({ | ||
astroConfig, | ||
dbConfig, | ||
flags, | ||
}: { | ||
astroConfig: AstroConfig; | ||
dbConfig: DBConfig; | ||
flags: Arguments; | ||
}) { | ||
const status = await getMigrationStatus({ dbConfig, root: astroConfig.root }); | ||
const { state } = status; | ||
if (flags.json) { | ||
if (state === 'ahead') { | ||
const { queries: migrationQueries } = await getMigrationQueries({ | ||
oldSnapshot: status.oldSnapshot, | ||
newSnapshot: status.newSnapshot, | ||
}); | ||
const newFileContent = { | ||
diff: status.diff, | ||
db: migrationQueries, | ||
}; | ||
status.newFileContent = JSON.stringify(newFileContent, null, 2); | ||
} | ||
console.log(JSON.stringify(status)); | ||
process.exit(state === 'up-to-date' ? 0 : 1); | ||
} | ||
switch (state) { | ||
case 'no-migrations-found': { | ||
console.log(MIGRATIONS_NOT_INITIALIZED); | ||
process.exit(1); | ||
} | ||
case 'ahead': { | ||
console.log(MIGRATION_NEEDED); | ||
process.exit(1); | ||
} | ||
case 'up-to-date': { | ||
console.log(MIGRATIONS_UP_TO_DATE); | ||
return; | ||
} | ||
const appToken = await getManagedAppTokenOrExit(flags.token); | ||
const productionSnapshot = await getProductionCurrentSnapshot({ appToken: appToken.token }); | ||
const currentSnapshot = createCurrentSnapshot(dbConfig); | ||
const { queries: migrationQueries } = await getMigrationQueries({ | ||
oldSnapshot: JSON.stringify(productionSnapshot) !== '{}' ? productionSnapshot : createEmptySnapshot(), | ||
newSnapshot: currentSnapshot, | ||
}); | ||
|
||
if (migrationQueries.length === 0) { | ||
console.log(`Database schema is up to date.`); | ||
} else { | ||
console.log(`Database schema is out of date.`); | ||
console.log(`Run 'astro db push' to push up your latest changes.`); | ||
} | ||
|
||
await appToken.destroy(); | ||
} |
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
Oops, something went wrong.