@astrojs/db 0.9.1 → 0.9.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -19,10 +19,11 @@ async function cmd({
19
19
  const appToken = await getManagedAppTokenOrExit(flags.token);
20
20
  const productionSnapshot = await getProductionCurrentSnapshot({ appToken: appToken.token });
21
21
  const currentSnapshot = createCurrentSnapshot(dbConfig);
22
- const isFromScratch = isForceReset || !productionSnapshot;
22
+ const isFromScratch = !productionSnapshot;
23
23
  const { queries: migrationQueries, confirmations } = await getMigrationQueries({
24
24
  oldSnapshot: isFromScratch ? createEmptySnapshot() : productionSnapshot,
25
- newSnapshot: currentSnapshot
25
+ newSnapshot: currentSnapshot,
26
+ reset: isForceReset
26
27
  });
27
28
  if (migrationQueries.length === 0) {
28
29
  console.log("Database schema is up to date.");
@@ -1,7 +1,8 @@
1
1
  import { type DBConfig, type DBSnapshot, type DBTable } from '../types.js';
2
- export declare function getMigrationQueries({ oldSnapshot, newSnapshot, }: {
2
+ export declare function getMigrationQueries({ oldSnapshot, newSnapshot, reset, }: {
3
3
  oldSnapshot: DBSnapshot;
4
4
  newSnapshot: DBSnapshot;
5
+ reset?: boolean;
5
6
  }): Promise<{
6
7
  queries: string[];
7
8
  confirmations: string[];
@@ -24,10 +24,16 @@ const sqlite = new SQLiteAsyncDialect();
24
24
  const genTempTableName = customAlphabet("abcdefghijklmnopqrstuvwxyz", 10);
25
25
  async function getMigrationQueries({
26
26
  oldSnapshot,
27
- newSnapshot
27
+ newSnapshot,
28
+ reset = false
28
29
  }) {
29
30
  const queries = [];
30
31
  const confirmations = [];
32
+ if (reset) {
33
+ const currentSnapshot = oldSnapshot;
34
+ oldSnapshot = createEmptySnapshot();
35
+ queries.push(...getDropTableQueriesForSnapshot(currentSnapshot));
36
+ }
31
37
  const addedCollections = getAddedCollections(oldSnapshot, newSnapshot);
32
38
  const droppedTables = getDroppedCollections(oldSnapshot, newSnapshot);
33
39
  const notDeprecatedDroppedTables = Object.fromEntries(
@@ -346,6 +352,14 @@ async function getProductionCurrentSnapshot({
346
352
  }
347
353
  return result.data;
348
354
  }
355
+ function getDropTableQueriesForSnapshot(snapshot) {
356
+ const queries = [];
357
+ for (const collectionName of Object.keys(snapshot.schema)) {
358
+ const dropQuery = `DROP TABLE ${sqlite.escapeName(collectionName)}`;
359
+ queries.unshift(dropQuery);
360
+ }
361
+ return queries;
362
+ }
349
363
  function createCurrentSnapshot({ tables = {} }) {
350
364
  const schema = JSON.parse(JSON.stringify(tables));
351
365
  return { version: MIGRATION_VERSION, schema };
@@ -27,7 +27,7 @@ function printHelp({
27
27
  message.push(
28
28
  linebreak(),
29
29
  ` ${bgGreen(black(` ${commandName} `))} ${green(
30
- `v${"0.9.1"}`
30
+ `v${"0.9.3"}`
31
31
  )} ${headline}`
32
32
  );
33
33
  }
@@ -94,7 +94,10 @@ function getStudioVirtualModContents({
94
94
  return `
95
95
  import {asDrizzleTable, createRemoteDatabaseClient} from ${RUNTIME_IMPORT};
96
96
 
97
- export const db = await createRemoteDatabaseClient(process.env.ASTRO_STUDIO_APP_TOKEN);
97
+ export const db = await createRemoteDatabaseClient(process.env.ASTRO_STUDIO_APP_TOKEN ?? ${JSON.stringify(
98
+ appToken
99
+ // Respect runtime env for user overrides in SSR
100
+ )}, import.meta.env.ASTRO_STUDIO_REMOTE_DB_URL ?? ${JSON.stringify(getRemoteDatabaseUrl())});
98
101
 
99
102
  export * from ${RUNTIME_CONFIG_IMPORT};
100
103
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrojs/db",
3
- "version": "0.9.1",
3
+ "version": "0.9.3",
4
4
  "description": "",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -81,8 +81,8 @@
81
81
  "mocha": "^10.2.0",
82
82
  "typescript": "^5.2.2",
83
83
  "vite": "^5.1.4",
84
- "astro": "4.5.8",
85
- "astro-scripts": "0.0.14"
84
+ "astro-scripts": "0.0.14",
85
+ "astro": "4.5.8"
86
86
  },
87
87
  "scripts": {
88
88
  "types:config": "tsc -p ./tsconfig.config-types.json",