drizzle-kit 0.9.0 → 0.9.1

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.
Files changed (60) hide show
  1. package/.eslintrc +3 -1
  2. package/config.yml +4 -0
  3. package/data/_prev/multi.ts +43 -0
  4. package/{examples → data/_prev}/tables/authOtpTable.ts +3 -3
  5. package/{examples → data/_prev}/tables/cityTable.ts +3 -3
  6. package/{examples → data/_prev}/tables/usersTable.ts +1 -1
  7. package/data/_prev/types/types.ts +11 -0
  8. package/data/v1/tables/authOtpTable.ts +18 -0
  9. package/data/v1/tables/deletedTable.ts +9 -0
  10. package/data/v1/tables/usersTable.ts +15 -0
  11. package/data/v1/types/types.ts +6 -0
  12. package/data/v2/tables/authOtpTable.ts +22 -0
  13. package/data/v2/tables/cityTable.ts +10 -0
  14. package/data/v2/tables/usersTable.ts +19 -0
  15. package/data/v2/types/types.ts +11 -0
  16. package/data/v3/tables/authOtpTable.ts +21 -0
  17. package/data/v3/tables/cityTable.ts +17 -0
  18. package/data/v3/tables/usersTable.ts +18 -0
  19. package/data/v3/types/types.ts +11 -0
  20. package/factory.ts +224 -0
  21. package/fstest.ts +50 -0
  22. package/index.ts +16 -0
  23. package/package.json +34 -13
  24. package/readme.md +9 -0
  25. package/serializer.ts +139 -0
  26. package/src/cli/commands/migration/index.tsx +1 -1
  27. package/src/cli/commands/migration/migrate/index.tsx +155 -17
  28. package/src/cli/components-api/ComponentsList.tsx +20 -0
  29. package/src/cli/components-api/CreateApp.tsx +19 -0
  30. package/src/cli/components-api/components/PromptColumnsConflicts.tsx +171 -0
  31. package/src/cli/components-api/components/PromptTablesConflicts.tsx +180 -0
  32. package/src/cli/components-api/components/Task.tsx +85 -0
  33. package/src/cli/components-api/index.tsx +76 -0
  34. package/src/cli/enq.ts +41 -0
  35. package/src/cli/machines/resolveColumnsMachine.ts +179 -0
  36. package/src/cli/machines/resolveTablesMachine.ts +169 -0
  37. package/src/cli/utils/formatDataForTable.ts +36 -0
  38. package/src/cli/utils/valuesForPrompts.ts +35 -0
  39. package/src/diff.ts +272 -0
  40. package/src/differ.js +135 -0
  41. package/src/jsonStatements.js +176 -0
  42. package/src/migrationPreparator.ts +47 -0
  43. package/src/serilizer/factory.ts +340 -0
  44. package/src/serilizer/index.ts +25 -0
  45. package/src/simulator.ts +85 -0
  46. package/src/sqlgenerator.js +373 -0
  47. package/test-build.js +26 -0
  48. package/test.ts +57 -0
  49. package/testFactory.js +3 -0
  50. package/testFactory.ts +26 -0
  51. package/tsconfig.json +1 -1
  52. package/webpack.config.js +42 -45
  53. package/result.json +0 -207
  54. package/src/cli/commands/migration/migrate/components/Progress/index.tsx +0 -98
  55. package/src/cli/commands/migration/migrate/components/Prompts/index.tsx +0 -37
  56. package/src/cli/commands/migration/migrate/components/index.tsx +0 -132
  57. package/src/common/components/Prompt.tsx +0 -106
  58. package/src/common/components/SelectTable.tsx +0 -134
  59. package/src/index.ts +0 -10
  60. package/src/serializer.ts +0 -135
@@ -0,0 +1,180 @@
1
+ import React from 'react';
2
+ import { Box, Text } from 'ink';
3
+ import SelectInput from 'ink-select-input';
4
+ import Table, { Header } from 'ink-table';
5
+ import { useMachine } from '@xstate/react';
6
+ import { Item } from 'ink-select-input/build/SelectInput';
7
+
8
+ import { Named, TablesResultData } from 'cli/commands/migration/migrate';
9
+ import createResolveTablesMachine from 'cli/machines/resolveTablesMachine';
10
+ import formatDataForTable from 'cli/utils/formatDataForTable';
11
+ import {
12
+ Action,
13
+ Confirmation,
14
+ actions,
15
+ confirmations,
16
+ } from 'cli/utils/valuesForPrompts';
17
+
18
+ export interface RenderTablesSelectProps<T extends Named> {
19
+ missingTables: T[],
20
+ newTables: T[],
21
+ }
22
+
23
+ interface Props<T extends Named> extends RenderTablesSelectProps<T> {
24
+ onDone: (value: TablesResultData<T>) => void;
25
+ }
26
+
27
+ const PromptTablesConflicts: React.FC<Props<any>> = ({
28
+ onDone,
29
+ newTables: newProps,
30
+ missingTables: missingProps,
31
+ }) => {
32
+ const [current, send] = useMachine(createResolveTablesMachine({
33
+ missingTables: missingProps,
34
+ newTables: newProps,
35
+ }));
36
+
37
+ const {
38
+ missingItemIndex,
39
+ newTables,
40
+ missingTables,
41
+ createdTables,
42
+ renamedTables,
43
+ deletedTables,
44
+ } = current.context;
45
+
46
+ const selectItemIndex = (item: Item<number>) => {
47
+ send({ type: 'CHOICE_ITEM', itemIndex: item.value });
48
+ };
49
+
50
+ const selectNewItemIndex = (item: Item<number>) => {
51
+ send({ type: 'CHOICE_NEW_ITEM', itemIndex: item.value });
52
+ };
53
+
54
+ const selectAction = (action: Item<Action>) => {
55
+ send({ type: action.value });
56
+ };
57
+
58
+ const confirm = (confirmationItem: Item<Confirmation>) => {
59
+ send({ type: confirmationItem.value });
60
+ };
61
+
62
+ const renderDOM = () => {
63
+ if (current.matches('table')) {
64
+ return (
65
+ <>
66
+ <Box display="flex" flexDirection="column">
67
+ <Box display="flex">
68
+ <Box flexDirection="column">
69
+ <Header>Missing tables:</Header>
70
+ <SelectInput
71
+ items={missingTables!.map((table, i: number) => ({
72
+ key: String(i),
73
+ label: table.name,
74
+ value: i,
75
+ }))}
76
+ onSelect={selectItemIndex}
77
+ />
78
+ </Box>
79
+ {!!newTables?.length && (
80
+ <Box flexDirection="column" paddingLeft={5}>
81
+ <Header>New tables:</Header>
82
+ <Box display="flex" flexDirection="column">
83
+ {newTables.map(({ name }, i) => <Text key={name + i}>{name}</Text>)}
84
+ </Box>
85
+ </Box>
86
+ )}
87
+ </Box>
88
+ </Box>
89
+ </>
90
+ );
91
+ }
92
+
93
+ if (current.matches('action.actionChoice')) {
94
+ const tempActions = !newTables!.length
95
+ ? actions.filter(({ value }) => value !== Action.RENAME)
96
+ : actions;
97
+
98
+ return (
99
+ <>
100
+ <Header>{`${missingTables![missingItemIndex]?.name} is:`}</Header>
101
+ <SelectInput items={tempActions} onSelect={selectAction} />
102
+ </>
103
+ );
104
+ }
105
+
106
+ if (current.matches('confirmationDelete')) {
107
+ return (
108
+ <>
109
+ <Header>!!! Data in table will be lost !!!</Header>
110
+ <Text>Are you sure?</Text>
111
+ <SelectInput items={confirmations} onSelect={confirm} />
112
+ </>
113
+ );
114
+ }
115
+
116
+ if (current.matches('confirmationRename')) {
117
+ return (
118
+ <>
119
+ <Header>Are you sure?</Header>
120
+ <SelectInput items={confirmations} onSelect={confirm} />
121
+ </>
122
+ );
123
+ }
124
+
125
+ if (current.matches('action.rename')) {
126
+ return (
127
+ <>
128
+ <Header>{`${missingTables![missingItemIndex]?.name} was renamed to:`}</Header>
129
+ <SelectInput
130
+ items={newTables!.map((table, i: number) => ({
131
+ key: String(i),
132
+ label: table.name,
133
+ value: i,
134
+ }))}
135
+ onSelect={selectNewItemIndex}
136
+ />
137
+ </>
138
+ );
139
+ }
140
+
141
+ if (current.matches('done')) {
142
+ onDone({
143
+ created: createdTables,
144
+ renamed: renamedTables,
145
+ deleted: deletedTables,
146
+ });
147
+ }
148
+
149
+ return (<></>);
150
+ };
151
+
152
+ return (!!deletedTables.length
153
+ || !!renamedTables.length
154
+ || !!createdTables.length) ? (
155
+ <Box flexDirection="column" margin={1}>
156
+ <Box flexDirection="column" marginBottom={0}>
157
+ <Header>Tables: </Header>
158
+ <Table
159
+ data={formatDataForTable([
160
+ {
161
+ title: 'Deleted',
162
+ values: deletedTables,
163
+ },
164
+ {
165
+ title: 'Renamed',
166
+ values: renamedTables,
167
+ },
168
+ {
169
+ title: 'Created',
170
+ values: createdTables,
171
+ },
172
+ ])}
173
+ />
174
+ </Box>
175
+ {renderDOM()}
176
+ </Box>
177
+ ) : renderDOM();
178
+ };
179
+
180
+ export default PromptTablesConflicts;
@@ -0,0 +1,85 @@
1
+ import React, { useEffect, useState } from 'react';
2
+ import { Box, Newline, Text } from 'ink';
3
+ import Spinner from 'ink-spinner';
4
+ import { CreateTaskParams } from 'cli/components-api/index';
5
+
6
+ enum StepState {
7
+ IN_PROGRESS,
8
+ SUCCESS,
9
+ FAIL
10
+ }
11
+
12
+ export interface CallbackProps {
13
+ setTitle: React.Dispatch<React.SetStateAction<string>>
14
+ setError: React.Dispatch<React.SetStateAction<any>>
15
+ }
16
+
17
+ interface Props<T> {
18
+ func: (props: CallbackProps) => T;
19
+ onDone: (result: T) => T;
20
+ titleStr: string;
21
+ }
22
+
23
+ const Task: React.FC<Props<any>> = <T extends unknown>(props: Props<T>) => {
24
+ const {
25
+ func,
26
+ onDone,
27
+ titleStr,
28
+ } = props;
29
+ const [title, setTitle] = useState<string>(titleStr);
30
+ const [state, setState] = useState<StepState>(StepState.IN_PROGRESS);
31
+ const [error, setError] = useState<any>(null);
32
+
33
+ useEffect(() => {
34
+ (async () => {
35
+ try {
36
+ const result = await func({ setTitle, setError });
37
+ setState(StepState.SUCCESS);
38
+ onDone(result);
39
+ } catch (e) {
40
+ setError(e);
41
+ setState(StepState.FAIL);
42
+ }
43
+ })();
44
+ }, []);
45
+
46
+ return (
47
+ <Box flexDirection="row">
48
+ <Box marginRight={1}>
49
+ {state === StepState.IN_PROGRESS ? (
50
+ <Text>
51
+ [
52
+ <Spinner type="dots" />
53
+ ]
54
+ </Text>
55
+ ) : state === StepState.SUCCESS ? (
56
+ <Text>
57
+ [
58
+ <Text color="green">✓</Text>
59
+ ]
60
+ </Text>
61
+ ) : (
62
+ <Text>
63
+ [
64
+ <Text color="red">×</Text>
65
+ ]
66
+ </Text>
67
+ )}
68
+
69
+ {state === StepState.FAIL ? (
70
+ <Text>
71
+ {title}
72
+ <Newline />
73
+ <Text color="red">{error!.message}</Text>
74
+ </Text>
75
+ ) : (
76
+ <>
77
+ <Text>{title}</Text>
78
+ </>
79
+ )}
80
+ </Box>
81
+ </Box>
82
+ );
83
+ };
84
+
85
+ export default Task;
@@ -0,0 +1,76 @@
1
+ import React from 'react';
2
+ import { proxy } from 'valtio';
3
+
4
+ import {
5
+ ColumnsResultData,
6
+ Named,
7
+ TablesResultData,
8
+ } from 'cli/commands/migration/migrate';
9
+ import Task, { CallbackProps } from 'cli/components-api/components/Task';
10
+ import createApp from 'cli/components-api/CreateApp';
11
+ import { TableWithColumn } from 'cli/machines/resolveColumnsMachine';
12
+ import PromptTablesConflicts, { RenderTablesSelectProps } from 'cli/components-api/components/PromptTablesConflicts';
13
+ import PromptColumnsConflicts from 'cli/components-api/components/PromptColumnsConflicts';
14
+
15
+ const componentsList = proxy<React.ReactElement<any, any>[]>([]);
16
+ let app: ReturnType<typeof createApp>;
17
+
18
+ export interface CreateTaskParams<In, Cbs> {
19
+ input: In,
20
+ run: (cbs: Cbs) => void
21
+ }
22
+
23
+ const prepareApp = () => {
24
+ if (!app) {
25
+ app = createApp(componentsList);
26
+ componentsList.length = 0;
27
+ }
28
+ };
29
+
30
+ export const task = <T extends unknown>(
31
+ titleStr: string,
32
+ func: (props: CallbackProps) => T,
33
+ ) => new Promise<T>((
34
+ resolve,
35
+ ) => {
36
+ prepareApp();
37
+
38
+ componentsList.push(
39
+ <Task
40
+ titleStr={titleStr}
41
+ func={func}
42
+ onDone={(val: T) => resolve(val)}
43
+ />,
44
+ );
45
+ });
46
+
47
+ export const promptTablesConflicts = <T extends Named>(
48
+ { missingTables, newTables }: RenderTablesSelectProps<T>,
49
+ ) => new Promise<TablesResultData<T>>((
50
+ resolve,
51
+ ) => {
52
+ prepareApp();
53
+
54
+ componentsList.push(
55
+ <PromptTablesConflicts
56
+ onDone={(val) => resolve(val)}
57
+ missingTables={missingTables}
58
+ newTables={newTables}
59
+ />,
60
+ );
61
+ });
62
+
63
+ export const promptColumnsConflicts = (
64
+ tableWithColumns: TableWithColumn,
65
+ ) => new Promise<ColumnsResultData<Named>>((
66
+ resolve,
67
+ ) => {
68
+ prepareApp();
69
+
70
+ componentsList.push(
71
+ <PromptColumnsConflicts
72
+ onDone={(val) => resolve(val)}
73
+ tableWithColumns={tableWithColumns}
74
+ />,
75
+ );
76
+ });
package/src/cli/enq.ts ADDED
@@ -0,0 +1,41 @@
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()
@@ -0,0 +1,179 @@
1
+ import { assign, createMachine, send } from 'xstate';
2
+ import { Named, RenamedObject } from 'cli/commands/migration/migrate';
3
+
4
+ type Event =
5
+ | { type: 'CHOICE_ITEM', itemIndex: number }
6
+ | { type: 'DELETE' }
7
+ | { type: 'RENAME' }
8
+ | { type: 'CANCEL' }
9
+ | { type: 'CONFIRM' }
10
+ | { type: 'NEXT' }
11
+ | { type: 'CHOICE_NEW_ITEM'; itemIndex: number };
12
+
13
+ export interface TableWithColumn {
14
+ name: string;
15
+ added: Named[],
16
+ deleted: Named[],
17
+ }
18
+
19
+ interface Props {
20
+ tableWithColumns: TableWithColumn,
21
+ }
22
+
23
+ interface Context {
24
+ tableName: string,
25
+ addedColumns: Named[],
26
+ deletedColumns: Named[],
27
+ missingItemIndex: number,
28
+ newItemIndex: number,
29
+ created: Named[];
30
+ renamed: RenamedObject<Named>[];
31
+ deleted: Named[];
32
+ }
33
+
34
+ const createResolveColumnsMachine = (props: Props) => (
35
+ createMachine<Context, Event>({
36
+ id: 'resolveColumns',
37
+ initial: 'table',
38
+ context: {
39
+ tableName: props.tableWithColumns.name,
40
+ addedColumns: props.tableWithColumns.added,
41
+ deletedColumns: props.tableWithColumns.deleted,
42
+ missingItemIndex: 0,
43
+ newItemIndex: 0,
44
+ created: [],
45
+ renamed: [],
46
+ deleted: [],
47
+ },
48
+ states: {
49
+ table: {
50
+ entry: send({ type: 'NEXT' }),
51
+ on: {
52
+ NEXT: [
53
+ {
54
+ target: 'done',
55
+ cond: 'isMissingColumnsResolved',
56
+ actions: ['resolveRemaining'],
57
+ },
58
+ {
59
+ target: 'done',
60
+ cond: 'isNewColumnsResolved',
61
+ actions: ['resolveMissing'],
62
+ },
63
+ ],
64
+ CHOICE_ITEM: { target: 'action', actions: ['choseItem'] },
65
+ },
66
+ },
67
+ action: {
68
+ initial: 'actionChoice',
69
+ states: {
70
+ actionChoice: {
71
+ on: { DELETE: '#resolveColumns.confirmationDelete', RENAME: 'rename' },
72
+ },
73
+ rename: {
74
+ on: {
75
+ CHOICE_NEW_ITEM: { target: '#resolveColumns.confirmationRename', actions: ['choseNewItem'] },
76
+ },
77
+ },
78
+ },
79
+ },
80
+ confirmationDelete: {
81
+ on: {
82
+ CANCEL: 'action.actionChoice',
83
+ CONFIRM: [
84
+ { target: 'final', actions: ['delete'] },
85
+ ],
86
+ },
87
+ },
88
+ confirmationRename: {
89
+ on: {
90
+ CANCEL: 'action.actionChoice',
91
+ CONFIRM: [
92
+ { target: 'final', actions: ['rename'] },
93
+ ],
94
+ },
95
+ },
96
+ final: {
97
+ entry: send({ type: 'NEXT' }),
98
+ on: {
99
+ NEXT: [
100
+ {
101
+ target: 'done',
102
+ cond: 'isMissingColumnsResolved',
103
+ actions: ['resolveRemaining'],
104
+ },
105
+ {
106
+ target: 'table',
107
+ },
108
+ ],
109
+ },
110
+ },
111
+ done: {},
112
+ },
113
+ },
114
+ {
115
+ guards: {
116
+ isMissingColumnsResolved: ({ deletedColumns }) => !deletedColumns.length,
117
+ isNewColumnsResolved: ({ addedColumns }) => !addedColumns.length,
118
+ },
119
+ actions: {
120
+ choseItem: assign({
121
+ missingItemIndex: (context, event) => (event.type === 'CHOICE_ITEM' ? event.itemIndex : 0),
122
+ }),
123
+
124
+ choseNewItem: assign({
125
+ newItemIndex: (context, event) => (event.type === 'CHOICE_NEW_ITEM' ? event.itemIndex : 0),
126
+ }),
127
+
128
+ delete: assign({
129
+ deleted: ({
130
+ missingItemIndex,
131
+ deleted,
132
+ deletedColumns,
133
+ }) => [...deleted, deletedColumns[missingItemIndex]],
134
+ deletedColumns: ({
135
+ missingItemIndex,
136
+ deletedColumns,
137
+ }) => deletedColumns.filter((_, index) => index !== missingItemIndex),
138
+ }),
139
+
140
+ rename: assign({
141
+ renamed: ({
142
+ missingItemIndex,
143
+ newItemIndex,
144
+ renamed,
145
+ addedColumns,
146
+ deletedColumns,
147
+ }) => [
148
+ ...renamed,
149
+ { old: deletedColumns[missingItemIndex], new: addedColumns[newItemIndex] },
150
+ ],
151
+ deletedColumns: ({
152
+ missingItemIndex,
153
+ deletedColumns,
154
+ }) => deletedColumns.filter((_, index) => index !== missingItemIndex),
155
+ addedColumns: ({
156
+ newItemIndex,
157
+ addedColumns,
158
+ }) => addedColumns.filter((_, index) => index !== newItemIndex),
159
+ }),
160
+
161
+ resolveRemaining: assign({
162
+ created: ({
163
+ addedColumns,
164
+ created,
165
+ }) => [...created, ...addedColumns],
166
+ addedColumns: (context) => [],
167
+ }),
168
+
169
+ resolveMissing: assign({
170
+ deleted: ({
171
+ deletedColumns,
172
+ deleted,
173
+ }) => [...deleted, ...deletedColumns],
174
+ deletedColumns: (context) => [],
175
+ }),
176
+ },
177
+ }));
178
+
179
+ export default createResolveColumnsMachine;