drizzle-kit 0.9.41 → 0.9.44

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.
package/readme.rst DELETED
@@ -1,2 +0,0 @@
1
- ## Documentation
2
-
@@ -1,79 +0,0 @@
1
- import { task, promptTablesConflicts, promptColumnsConflicts } from '../components-api';
2
- import fs from 'fs';
3
- import yaml from 'js-yaml'
4
- import prepareMigration from '../../migrationPreparator';
5
- import { applySnapshotsDiff, Column, ColumnsResolverInput, ColumnsResolverOutput, Table, TablesResolverInput, TablesResolverOutput } from '../../snapshotsDiffer';
6
-
7
- export interface Config {
8
- dataFolder: string;
9
- migrationRootFolder?: string;
10
- }
11
-
12
- export const prepareAndMigrate = async () => {
13
- const drizzleConfig: Config = yaml.load(fs.readFileSync('drizzle.config.yml', { encoding: 'utf-8' })) as Config;
14
- const migrationRootFolder = drizzleConfig.migrationRootFolder || 'drizzle' // or from config/params
15
- const dataFolder = drizzleConfig.dataFolder
16
-
17
- try {
18
- const { prev, cur } = await prepareJsonSnapshots(migrationRootFolder, dataFolder)
19
- freeeeeeze(prev);
20
- freeeeeeze(cur);
21
- const sql = await prepareSQL(prev, cur)
22
- // todo: save results to a new migration folder
23
- const folderName = prepareSnapshotFolderName()
24
- const migrationFolderPath = `./${migrationRootFolder}/${folderName}`
25
- fs.mkdirSync(migrationFolderPath)
26
- fs.writeFileSync(`${migrationFolderPath}/snapshot.json`, JSON.stringify(cur, null, 2))
27
- fs.writeFileSync(`${migrationFolderPath}/migration.sql`, sql)
28
- } catch (e) {
29
- console.error(e)
30
- }
31
- }
32
-
33
- const freeeeeeze = (obj: any) => {
34
- Object.freeze(obj)
35
- for (let key in obj) {
36
- if (obj.hasOwnProperty(key) && typeof obj[key] === 'object') {
37
- freeeeeeze(obj[key]);
38
- }
39
- }
40
- }
41
-
42
- const prepareJsonSnapshots = async (migrationRootFolder: string, dataFolder: string) => {
43
- return await task('Preparing data schema json snapshot', async ({ setTitle }) => {
44
- return prepareMigration(migrationRootFolder, dataFolder)
45
- });
46
- }
47
-
48
- export const prepareSQL = async (prev: object, cur: object) => {
49
- const tablesResolver = async (input: TablesResolverInput<Table>): Promise<TablesResolverOutput<Table>> => {
50
- try {
51
- const { created, deleted, renamed } = await promptTablesConflicts({
52
- newTables: input.created,
53
- missingTables: input.deleted,
54
- })
55
-
56
- return { created: created, deleted: deleted, renamed: renamed };
57
-
58
- } catch (e) {
59
- console.error(e)
60
- throw e
61
- }
62
- }
63
-
64
- const columnsResolver = async (input: ColumnsResolverInput<Column>): Promise<ColumnsResolverOutput<Column>> => {
65
- const result = await promptColumnsConflicts({ name: input.tableName, added: input.created, deleted: input.deleted })
66
- return { tableName: input.tableName, created: result.created, deleted: result.deleted, renamed: result.renamed }
67
- }
68
-
69
- return await applySnapshotsDiff(prev, cur, tablesResolver, columnsResolver);
70
- };
71
-
72
- const prepareSnapshotFolderName = () => {
73
- const now = new Date()
74
- return `${now.getFullYear()}${two(now.getUTCMonth() + 1)}${two(now.getUTCDate())}${two(now.getUTCHours())}${two(now.getUTCMinutes())}${two(now.getUTCSeconds())}`
75
- }
76
-
77
- const two = (input: number): string => {
78
- return input.toString().padStart(2, '0')
79
- }
@@ -1,287 +0,0 @@
1
- import { interpret } from 'xstate';
2
-
3
- import createResolveTablesMachine, { CreateTablesMachineProps } from '../machines/resolveTablesMachine';
4
- import createResolveColumnsMachine, { CreateColumnsMachineProps } from '../machines/resolveColumnsMachine';
5
- import formatDataForTable from '../utils/formatDataForTable';
6
-
7
- const loading = require('loading-cli');
8
- const { Select, Confirm } = require('enquirer');
9
- const Table = require('cli-table');
10
-
11
- export interface CallbackProps {
12
- setTitle: (title: string) => void;
13
- setError: (error: any) => void;
14
- }
15
-
16
- export interface Named {
17
- name: string
18
- }
19
-
20
- export interface RenamedObject<T> {
21
- from: T,
22
- to: T
23
- }
24
-
25
- interface TablesResultData<T extends Named> {
26
- created: T[],
27
- renamed: RenamedObject<T>[],
28
- deleted: T[],
29
- }
30
-
31
- interface ColumnsResultData<T extends Named> {
32
- created: T[],
33
- renamed: RenamedObject<T>[],
34
- deleted: T[],
35
- }
36
-
37
- const actions = ['Renamed', 'Deleted'];
38
-
39
- const bold = '\x1b[1m';
40
- const red = '\u001B[31m';
41
- const blue = '\u001B[34m';
42
- const reset = '\u001B[0m';
43
-
44
- const getElementIndexByName = (
45
- array: Named[],
46
- elementName: string,
47
- ) => array.findIndex(({ name }) => name === elementName);
48
-
49
- const clearLine = () => {
50
- process.stdout.moveCursor(0, -1);
51
- process.stdout.clearLine(1);
52
- }
53
-
54
- export const task = async <T extends unknown>(
55
- titleStr: string,
56
- func: (props: CallbackProps) => T,
57
- ): Promise<T> => {
58
- const progress = loading({
59
- frames: ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'],
60
- text: `${bold}${titleStr}`,
61
-
62
- }).start();
63
- try {
64
- const result = await func({
65
- setTitle: (title: string) => { progress.text = `${bold}${title}`; },
66
- setError: (error: any) => { progress.fail(error.message); },
67
- });
68
- progress.succeed();
69
- return result;
70
- } catch (error: any) {
71
- progress.fail(`${bold}${error.message}`);
72
- throw Error(error);
73
- }
74
- };
75
-
76
- export const promptTablesConflicts = <T extends Named>(
77
- { missingTables: m, newTables: n }: CreateTablesMachineProps<T>,
78
- ) => new Promise<TablesResultData<T>>((
79
- resolve,
80
- ) => {
81
- const resolveTablesMachine = createResolveTablesMachine({ missingTables: m, newTables: n });
82
- const service = interpret(resolveTablesMachine).start();
83
- service.subscribe(async (state) => {
84
- try {
85
- const {
86
- missingItemIndex,
87
- newItemIndex,
88
- newTables,
89
- missingTables,
90
- createdTables,
91
- renamedTables,
92
- deletedTables,
93
- } = state.context;
94
-
95
- if (state.matches('idle')) {
96
- service.send({ type: 'NEXT' });
97
- }
98
-
99
- if (state.changed && state.matches('table')) {
100
- const tableName = await new Select({
101
- name: 'tableName',
102
- message: 'Chose missing table:',
103
- choices: missingTables.map(({ name }) => name),
104
- }).run();
105
- clearLine();
106
- service.send({ type: 'CHOICE_ITEM', itemIndex: getElementIndexByName(missingTables, tableName) });
107
- }
108
-
109
- if (state.changed && state.matches('action.actionChoice')) {
110
- const actionName = await new Select({
111
- name: 'tableName',
112
- message: `Table "${missingTables[missingItemIndex].name}" was:`,
113
- choices: actions,
114
- }).run();
115
- clearLine();
116
- service.send({ type: actionName.toUpperCase() });
117
- }
118
-
119
- if (state.changed && state.matches('confirmationDelete')) {
120
- const confirmation = await new Confirm({
121
- name: 'confirm',
122
- message: 'Are you sure?',
123
- initial: true,
124
- }).run();
125
- clearLine();
126
-
127
- if (confirmation) console.log(`${bold}Deleted "${missingTables[missingItemIndex].name}" table, ${red}all data will be lost!`)
128
- service.send({ type: confirmation ? 'CONFIRM' : 'CANCEL' });
129
- }
130
-
131
- if (state.changed && state.matches('confirmationRename')) {
132
- const confirmation = await new Confirm({
133
- name: 'confirm',
134
- message: 'Are you sure?',
135
- initial: true,
136
- }).run();
137
- clearLine();
138
-
139
- if (confirmation) console.log(`${bold}Renamed "${missingTables[missingItemIndex].name}" -> "${newTables[newItemIndex].name}"`)
140
-
141
- service.send({ type: confirmation ? 'CONFIRM' : 'CANCEL' });
142
- }
143
-
144
- if (state.changed && state.matches('action.rename')) {
145
- const tableName = await new Select({
146
- name: 'tableName',
147
- message: `Table "${missingTables![missingItemIndex]?.name}" was renamed to:`,
148
- choices: newTables.map(({ name }) => name),
149
- }).run();
150
- clearLine();
151
-
152
- service.send({ type: 'CHOICE_NEW_ITEM', itemIndex: getElementIndexByName(newTables, tableName) });
153
- }
154
-
155
- if (state.changed && state.matches('done')) {
156
- if (createdTables.length || deletedTables.length || renamedTables.length) {
157
- const table = new Table({
158
- head: [`${blue}Created`, `${blue}Renamed`, `${blue}Deleted`],
159
- rows: formatDataForTable(createdTables, renamedTables, deletedTables)
160
- });
161
-
162
- console.log(`${reset}${bold}Tables:`);
163
- console.log(table.toString());
164
- }
165
-
166
- resolve({
167
- created: createdTables,
168
- deleted: deletedTables,
169
- renamed: renamedTables,
170
- });
171
-
172
- service.stop();
173
- }
174
- } catch (e) {
175
- console.error(e);
176
- }
177
- });
178
- });
179
-
180
- export const promptColumnsConflicts = <T extends Named>(
181
- props: CreateColumnsMachineProps<T>,
182
- ) => new Promise<ColumnsResultData<T>>((
183
- resolve,
184
- ) => {
185
- const resolveColumnsMachine = createResolveColumnsMachine(props);
186
- const service = interpret(resolveColumnsMachine).start();
187
-
188
- service.subscribe(async (state) => {
189
- try {
190
- const {
191
- tableName,
192
- addedColumns,
193
- deletedColumns,
194
- missingItemIndex,
195
- newItemIndex,
196
- created,
197
- renamed,
198
- deleted,
199
- } = state.context;
200
-
201
- if (state.matches('idle')) {
202
- service.send({ type: 'NEXT' });
203
- }
204
-
205
- if (state.changed && state.matches('table')) {
206
- const columnName = await new Select({
207
- name: 'columnName',
208
- message: `Table "${tableName}" missing columns:`,
209
- choices: deletedColumns.map(({ name }) => name),
210
- }).run();
211
- clearLine();
212
-
213
- service.send({ type: 'CHOICE_ITEM', itemIndex: getElementIndexByName(deletedColumns, columnName) });
214
- }
215
-
216
- if (state.changed && state.matches('action.actionChoice')) {
217
- const actionName = await new Select({
218
- name: 'tableName',
219
- message: `Column "${deletedColumns[missingItemIndex].name}" was:`,
220
- choices: actions,
221
- }).run();
222
- clearLine();
223
-
224
- service.send({ type: actionName.toUpperCase() });
225
- }
226
-
227
- if (state.changed && state.matches('confirmationDelete')) {
228
- const confirmation = await new Confirm({
229
- name: 'confirm',
230
- message: 'Are you sure?',
231
- initial: true,
232
- }).run();
233
- clearLine();
234
-
235
- if (confirmation) console.log(`${bold}Deleted column "${deletedColumns[missingItemIndex].name}" from "${tableName}" table, ${red}all data will be lost!`)
236
-
237
- service.send({ type: confirmation ? 'CONFIRM' : 'CANCEL' });
238
- }
239
-
240
- if (state.changed && state.matches('confirmationRename')) {
241
- const confirmation = await new Confirm({
242
- name: 'confirm',
243
- message: 'Are you sure?',
244
- initial: true,
245
- }).run();
246
- clearLine();
247
-
248
- if (confirmation) console.log(`${bold}Renamed "${deletedColumns[missingItemIndex].name} -> ${addedColumns[newItemIndex].name}" in "${tableName}" table`)
249
-
250
- service.send({ type: confirmation ? 'CONFIRM' : 'CANCEL' });
251
- }
252
-
253
- if (state.changed && state.matches('action.rename')) {
254
- const columnName = await new Select({
255
- name: 'tableName',
256
- message: `Column "${deletedColumns![missingItemIndex]?.name}" was renamed to:`,
257
- choices: addedColumns.map(({ name }) => name),
258
- }).run();
259
- clearLine();
260
-
261
- service.send({ type: 'CHOICE_NEW_ITEM', itemIndex: getElementIndexByName(addedColumns, columnName) });
262
- }
263
-
264
- if (state.changed && state.matches('done')) {
265
- if (created.length || deleted.length || renamed.length) {
266
- const table = new Table({
267
- head: [`${blue}Created`, `${blue}Renamed`, `${blue}Deleted`],
268
- rows: formatDataForTable(created, renamed, deleted)
269
- });
270
-
271
- console.log(`${reset}${bold}Table "${tableName}" columns:`);
272
- console.log(table.toString());
273
- }
274
-
275
- resolve({
276
- created,
277
- deleted,
278
- renamed,
279
- });
280
-
281
- service.stop();
282
- }
283
- } catch (e) {
284
- console.error(e);
285
- }
286
- });
287
- });
package/src/cli/enq.ts DELETED
@@ -1,41 +0,0 @@
1
- import { Command } from 'commander'
2
- import fs from 'fs'
3
-
4
- const main = async () => {
5
- const program = new Command()
6
- .arguments("[input]")
7
-
8
- // const addCommand = new Command('add')
9
- // program.addCommand(addCommand)
10
- program.parse(process.argv)
11
-
12
-
13
- if (program.args.length === 0) {
14
- return
15
- }
16
-
17
- console.log(program.args)
18
- const [input] = program.args
19
- if (input === 'add') {
20
- //todo add item
21
- // // const exampleLink = terminalLink('here', 'https://google.com')
22
- // console.log(`Example see ${exampleLink}`)
23
- // console.log(exampleLink)
24
- // try {
25
- // const newEntry = await prepareNewEntry()
26
- // newEntry.secret
27
- // } catch (_) { }
28
- return
29
- }
30
-
31
- if (fs.existsSync(input)) {
32
- // handle file
33
- console.log('handle file')
34
- return
35
- }
36
-
37
- console.log('is encoded secret',)
38
- }
39
-
40
-
41
- main()
package/src/cli/index.ts DELETED
@@ -1,14 +0,0 @@
1
- #!/usr/bin/env node
2
- import { Command, program } from 'commander';
3
- import { prepareAndMigrate } from './commands/migrate';
4
- // import 'source-map-support/register';
5
- import 'pretty-error/start';
6
- import './commands/migrate';
7
-
8
- const migrationCommand = new Command('migrate')
9
- .alias('mg')
10
- .description('Migration')
11
- .action(prepareAndMigrate);
12
-
13
- program.addCommand(migrationCommand)
14
- program.parse();
@@ -1,173 +0,0 @@
1
- import { assign, createMachine, send } from 'xstate';
2
- import { Named, RenamedObject } from '../components-api';
3
-
4
- type Event =
5
- | { type: 'CHOICE_ITEM', itemIndex: number }
6
- | { type: 'DELETED' }
7
- | { type: 'RENAMED' }
8
- | { type: 'CANCEL' }
9
- | { type: 'CONFIRM' }
10
- | { type: 'NEXT' }
11
- | { type: 'CHOICE_NEW_ITEM'; itemIndex: number };
12
-
13
- export interface CreateColumnsMachineProps<T> {
14
- name: string;
15
- added: T[],
16
- deleted: T[],
17
- }
18
-
19
- interface Context<T> {
20
- tableName: string,
21
- addedColumns: T[],
22
- deletedColumns: T[],
23
- missingItemIndex: number,
24
- newItemIndex: number,
25
- created: T[];
26
- renamed: RenamedObject<T>[];
27
- deleted: T[];
28
- }
29
-
30
- const createResolveColumnsMachine = <T extends Named>(props: CreateColumnsMachineProps<T>) => (
31
- createMachine<Context<T>, Event>({
32
- id: 'resolveColumns',
33
- initial: 'idle',
34
- context: {
35
- tableName: props.name,
36
- addedColumns: props.added,
37
- deletedColumns: props.deleted,
38
- missingItemIndex: 0,
39
- newItemIndex: 0,
40
- created: [],
41
- renamed: [],
42
- deleted: [],
43
- },
44
- states: {
45
- idle: {
46
- entry: send({ type: 'NEXT' }),
47
- on: {
48
- NEXT: [
49
- {
50
- target: 'done',
51
- cond: 'isResolved',
52
- actions: ['resolveRemaining'],
53
- },
54
- {
55
- target: 'table',
56
- },
57
- ],
58
- },
59
- },
60
- table: {
61
- on: {
62
- CHOICE_ITEM: { target: 'action', actions: ['choseItem'] },
63
- },
64
- },
65
- action: {
66
- initial: 'actionChoice',
67
- states: {
68
- actionChoice: {
69
- on: { DELETED: '#resolveColumns.confirmationDelete', RENAMED: 'rename' },
70
- },
71
- rename: {
72
- on: {
73
- CHOICE_NEW_ITEM: { target: '#resolveColumns.confirmationRename', actions: ['choseNewItem'] },
74
- },
75
- },
76
- },
77
- },
78
- confirmationDelete: {
79
- on: {
80
- CANCEL: 'action.actionChoice',
81
- CONFIRM: [
82
- { target: 'check', actions: ['delete'] },
83
- ],
84
- },
85
- },
86
- confirmationRename: {
87
- on: {
88
- CANCEL: 'action.actionChoice',
89
- CONFIRM: [
90
- { target: 'check', actions: ['rename'] },
91
- ],
92
- },
93
- },
94
- check: {
95
- entry: send({ type: 'NEXT' }),
96
- on: {
97
- NEXT: [
98
- {
99
- target: 'done',
100
- cond: 'isResolved',
101
- actions: ['resolveRemaining'],
102
- },
103
- {
104
- target: 'table',
105
- },
106
- ],
107
- },
108
- },
109
- done: {},
110
- },
111
- },
112
- {
113
- guards: {
114
- isResolved: ({ deletedColumns, addedColumns }) => !deletedColumns.length || !addedColumns.length,
115
- },
116
- actions: {
117
- choseItem: assign({
118
- missingItemIndex: (context, event) => (event.type === 'CHOICE_ITEM' ? event.itemIndex : 0),
119
- }),
120
-
121
- choseNewItem: assign({
122
- newItemIndex: (context, event) => (event.type === 'CHOICE_NEW_ITEM' ? event.itemIndex : 0),
123
- }),
124
-
125
- delete: assign({
126
- deleted: ({
127
- missingItemIndex,
128
- deleted,
129
- deletedColumns,
130
- }) => [...deleted, deletedColumns[missingItemIndex]],
131
- deletedColumns: ({
132
- missingItemIndex,
133
- deletedColumns,
134
- }) => deletedColumns.filter((_, index) => index !== missingItemIndex),
135
- }),
136
-
137
- rename: assign({
138
- renamed: ({
139
- missingItemIndex,
140
- newItemIndex,
141
- renamed,
142
- addedColumns,
143
- deletedColumns,
144
- }) => [
145
- ...renamed,
146
- { from: deletedColumns[missingItemIndex], to: addedColumns[newItemIndex] },
147
- ],
148
- deletedColumns: ({
149
- missingItemIndex,
150
- deletedColumns,
151
- }) => deletedColumns.filter((_, index) => index !== missingItemIndex),
152
- addedColumns: ({
153
- newItemIndex,
154
- addedColumns,
155
- }) => addedColumns.filter((_, index) => index !== newItemIndex),
156
- }),
157
-
158
- resolveRemaining: assign({
159
- created: ({
160
- addedColumns,
161
- created,
162
- }) => [...created, ...addedColumns],
163
- deleted: ({
164
- deletedColumns,
165
- deleted,
166
- }) => [...deleted, ...deletedColumns],
167
- deletedColumns: (context) => [],
168
- addedColumns: (context) => [],
169
- }),
170
- },
171
- }));
172
-
173
- export default createResolveColumnsMachine;