1
1
import { consola } from 'consola'
2
2
import { join } from 'pathe'
3
+ import { existsSync } from 'node:fs'
3
4
import { createStorage } from 'unstorage'
4
5
import fsDriver from 'unstorage/drivers/fs'
5
6
import { $api } from './data.mjs'
6
7
import { $fetch } from 'ofetch'
7
8
8
-
9
9
export async function queryDatabase ( { env, url, token, query, params } ) {
10
10
if ( url ) {
11
11
return queryRemoteDatabase ( { url, token, query, params } )
@@ -40,20 +40,26 @@ export async function queryRemoteDatabase({ url, token, query, params }) {
40
40
} )
41
41
}
42
42
43
+ let _migrationsDir
44
+ export function getMigrationsDir ( ) {
45
+ if ( ! _migrationsDir ) {
46
+ const cwd = process . cwd ( )
47
+ _migrationsDir = existsSync ( join ( cwd , '.data/hub/database/migrations' ) ) ? join ( cwd , '.data/hub/database/migrations' ) : join ( cwd , 'server/database/migrations' )
48
+ }
49
+ return _migrationsDir
50
+ }
43
51
44
52
/**
45
53
* @type {import('unstorage').Storage }
46
54
*/
47
55
let _storage
48
56
export function useMigrationsStorage ( ) {
49
57
if ( ! _storage ) {
50
- const cwd = process . cwd ( )
51
- const migrationsDir = join ( cwd , 'server/database/migrations' )
52
58
_storage = createStorage ( {
53
59
driver : fsDriver ( {
54
- base : migrationsDir ,
60
+ base : getMigrationsDir ( ) ,
55
61
ignore : [ '.DS_Store' ]
56
- } ) ,
62
+ } )
57
63
} )
58
64
}
59
65
return _storage
@@ -72,24 +78,25 @@ export async function getNextMigrationNumber() {
72
78
. sort ( ( a , b ) => a - b )
73
79
. pop ( ) ?? 0
74
80
75
- return ( lastSequentialMigrationNumber + 1 ) . toString ( ) . padStart ( 4 , '0' )
81
+ return ( lastSequentialMigrationNumber + 1 ) . toString ( ) . padStart ( 4 , '0' )
76
82
}
77
83
78
- const CreateMigrationsTableQuery = `CREATE TABLE IF NOT EXISTS _hub_migrations (
84
+ export const CreateDatabaseMigrationsTableQuery = `CREATE TABLE IF NOT EXISTS _hub_migrations (
79
85
id INTEGER PRIMARY KEY AUTOINCREMENT,
80
86
name TEXT UNIQUE,
81
87
applied_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
82
88
);`
89
+ export const ListDatabaseMigrationsQuery = 'select "id", "name", "applied_at" from "_hub_migrations" order by "_hub_migrations"."id"'
90
+
83
91
export async function createMigrationsTable ( { env, url, token } ) {
84
- await queryDatabase ( { env, url, token, query : CreateMigrationsTableQuery } )
92
+ await queryDatabase ( { env, url, token, query : CreateDatabaseMigrationsTableQuery } )
85
93
}
86
94
87
95
/**
88
96
* @type {Promise<Array<{ id: number, name: string, applied_at: string }>> }
89
97
*/
90
98
export async function fetchRemoteMigrations ( { env, url, token } ) {
91
- const query = 'select "id", "name", "applied_at" from "_hub_migrations" order by "_hub_migrations"."id"'
92
- const res = await queryDatabase ( { env, url, token, query } ) . catch ( ( error ) => {
99
+ const res = await queryDatabase ( { env, url, token, query : ListDatabaseMigrationsQuery } ) . catch ( ( error ) => {
93
100
if ( error . response ?. _data ?. message . includes ( 'no such table' ) ) {
94
101
return [ ]
95
102
}
0 commit comments