drizzle-kit 0.9.1 → 0.9.5

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 (55) hide show
  1. package/drizzle.js +4 -0
  2. package/drizzle.js.map +1 -0
  3. package/package.json +5 -2
  4. package/.eslintrc +0 -21
  5. package/config.yml +0 -4
  6. package/data/_prev/multi.ts +0 -43
  7. package/data/_prev/tables/authOtpTable.ts +0 -22
  8. package/data/_prev/tables/cityTable.ts +0 -17
  9. package/data/_prev/tables/usersTable.ts +0 -18
  10. package/data/_prev/types/types.ts +0 -11
  11. package/data/v1/tables/authOtpTable.ts +0 -18
  12. package/data/v1/tables/deletedTable.ts +0 -9
  13. package/data/v1/tables/usersTable.ts +0 -15
  14. package/data/v1/types/types.ts +0 -6
  15. package/data/v2/tables/authOtpTable.ts +0 -22
  16. package/data/v2/tables/cityTable.ts +0 -10
  17. package/data/v2/tables/usersTable.ts +0 -19
  18. package/data/v2/types/types.ts +0 -11
  19. package/data/v3/tables/authOtpTable.ts +0 -21
  20. package/data/v3/tables/cityTable.ts +0 -17
  21. package/data/v3/tables/usersTable.ts +0 -18
  22. package/data/v3/types/types.ts +0 -11
  23. package/factory.ts +0 -224
  24. package/fstest.ts +0 -50
  25. package/index.ts +0 -16
  26. package/readme.md +0 -9
  27. package/serializer.ts +0 -139
  28. package/src/cli/commands/migration/index.tsx +0 -10
  29. package/src/cli/commands/migration/migrate/index.tsx +0 -164
  30. package/src/cli/components-api/ComponentsList.tsx +0 -20
  31. package/src/cli/components-api/CreateApp.tsx +0 -19
  32. package/src/cli/components-api/components/PromptColumnsConflicts.tsx +0 -171
  33. package/src/cli/components-api/components/PromptTablesConflicts.tsx +0 -180
  34. package/src/cli/components-api/components/Task.tsx +0 -85
  35. package/src/cli/components-api/index.tsx +0 -76
  36. package/src/cli/enq.ts +0 -41
  37. package/src/cli/index.tsx +0 -8
  38. package/src/cli/machines/resolveColumnsMachine.ts +0 -179
  39. package/src/cli/machines/resolveTablesMachine.ts +0 -169
  40. package/src/cli/utils/formatDataForTable.ts +0 -36
  41. package/src/cli/utils/valuesForPrompts.ts +0 -35
  42. package/src/diff.ts +0 -272
  43. package/src/differ.js +0 -135
  44. package/src/jsonStatements.js +0 -176
  45. package/src/migrationPreparator.ts +0 -47
  46. package/src/serilizer/factory.ts +0 -340
  47. package/src/serilizer/index.ts +0 -25
  48. package/src/simulator.ts +0 -85
  49. package/src/sqlgenerator.js +0 -373
  50. package/test-build.js +0 -26
  51. package/test.ts +0 -57
  52. package/testFactory.js +0 -3
  53. package/testFactory.ts +0 -26
  54. package/tsconfig.json +0 -28
  55. package/webpack.config.js +0 -53
@@ -1,164 +0,0 @@
1
- import { task, promptTablesConflicts, promptColumnsConflicts } from 'cli/components-api';
2
- import fs from 'fs';
3
- import * as differ from '../../../../differ';
4
- import prepareMigration from 'migrationPreparator';
5
-
6
- import { prepareAddValuesToEnumJson, prepareAlterTableColumnsJson, prepareCreateEnumJson, prepareCreateIndexesJson, prepareCreateTableJson, prepareDropIndexesJson, prepareDropTableJson, prepareRenameTableJson } from '../../../../jsonStatements';
7
- import { fromJson } from '../../../../sqlgenerator';
8
-
9
- export interface TablesResultData<T> {
10
- created: T[],
11
- renamed: RenamedObject<T>[],
12
- deleted: T[],
13
- }
14
-
15
- export interface ColumnsResultData<T extends Named> {
16
- created: T[],
17
- renamed: RenamedObject<T>[],
18
- deleted: T[],
19
- }
20
-
21
- export interface Named {
22
- name: string
23
- }
24
-
25
- export interface RenamedObject<T> {
26
- old: T,
27
- new: T
28
- }
29
-
30
- const two = (num: number) => {
31
- return num.toString().padStart(2, '0')
32
- }
33
-
34
- const migrate = async () => {
35
- const timeoutE = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)).then(() => {
36
- throw new Error('Checking error handling');
37
- });
38
-
39
- const timeout = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
40
-
41
- const migrationRootFolder = 'drizzle' // or from config/params
42
- const { prev, cur } = await task('preparing data schema json snapshot', async ({ setTitle }) => {
43
- return prepareMigration(migrationRootFolder, './data/v2')
44
- });
45
-
46
- // const j1 = JSON.parse(fs.readFileSync('./out/v1_1634546745488.json', 'utf8'));
47
- // const j2 = JSON.parse(fs.readFileSync('./out/v2_1634549601361.json', 'utf8'));
48
-
49
- const diff = await task('preparing schemas diff', async ({ setTitle }) => {
50
- return differ.differ(prev, cur);
51
- });
52
-
53
- const { created, renamed, deleted } = await promptTablesConflicts({
54
- newTables: diff.addedTables,
55
- missingTables: diff.deletedTables,
56
- });
57
-
58
- const jsonStatements: any[] = []
59
-
60
- const jsonCreateTables = created.map(it => {
61
- return prepareCreateTableJson(it)
62
- })
63
-
64
- const jsonCreateIndexesForCreatedTables = created.map(it => {
65
- return prepareCreateIndexesJson(it.name, it.indexes)
66
- }).flat()
67
-
68
- const jsonDropTables = deleted.map(it => {
69
- return prepareDropTableJson(it)
70
- })
71
-
72
- const jsonRenameTables = renamed.map(it => {
73
- return prepareRenameTableJson(it.old, it.new)
74
- })
75
-
76
- // const createNewTables = created.map(it => {
77
- // return prepareCreateTable(it)
78
- // })
79
- // console.log(createNewTables.join('\n').trim())
80
-
81
- // const deleteTables = deleted.map(it => {
82
- // return prepareDropTable(it)
83
- // })
84
- // console.log(deleteTables.join('\n').trim())
85
-
86
- const renamedWithAlternations = differ.diffForRenamed(renamed)
87
- const allAltered = diff.alteredTablesWithColumns.concat(renamedWithAlternations)
88
-
89
- const allAlteredResolved = []
90
- for (const table of allAltered) {
91
- const result = await promptColumnsConflicts(table);
92
-
93
- const { deleted, added, ...ini } = table
94
-
95
- allAlteredResolved.push({ ...ini, ...result })
96
- }
97
-
98
- // console.log(JSON.stringify(allAlteredResolved, null, 2))
99
-
100
- // const renameTables = renamed.map(it => {
101
- // return prepareRenameTable(it.from, it.to)
102
- // })
103
- // console.log(renameTables.join('\n').trim())
104
-
105
- const jsonAlterTables = allAlteredResolved.map(it => {
106
- return prepareAlterTableColumnsJson(it)
107
- }).flat()
108
-
109
- const jsonCreateIndexesForAllAlteredTables = allAlteredResolved.map(it => {
110
- return prepareCreateIndexesJson(it.name, it.addedIndexes || {})
111
- }).flat()
112
-
113
- const jsonDropIndexesForAllAlteredTables = allAlteredResolved.map(it => {
114
- return prepareDropIndexesJson(it.name, it.deletedIndexes || {})
115
- }).flat()
116
-
117
- // // Enums:
118
- // // - создание енама ✅
119
- // // - переименование енама (пока не делаю)⏳
120
- // // - добавление вэлью к енаму ✅
121
- // // - ренейм вейлью у енама (пока не делаю, это надо запрашивать опять же через слай)⏳
122
- // // - удаление енама -> чекать не используется ли где-то енам и сначала ранить миграции и в самом конце удаление енама⏳
123
- // // - удаление вэлью из енама -> блок ❌
124
- // const enums = result.addedEnums.map(it => {
125
- // return prepareCreateEnum(it.name, it.values)
126
- // })
127
-
128
- const createEnums = diff.addedEnums.map(it => {
129
- return prepareCreateEnumJson(it.name, it.values)
130
- })
131
-
132
- //todo: block enum rename, enum value rename and enun deletion for now
133
- const jsonAlterEnumsWithAddedValues = diff.alteredEnums.map(it => {
134
- return prepareAddValuesToEnumJson(it.name, it.addedValues)
135
- }).flat()
136
-
137
- jsonStatements.push(...createEnums)
138
- jsonStatements.push(...jsonAlterEnumsWithAddedValues)
139
- jsonStatements.push(...jsonCreateTables)
140
- jsonStatements.push(...jsonCreateIndexesForCreatedTables)
141
- jsonStatements.push(...jsonDropTables)
142
- jsonStatements.push(...jsonRenameTables)
143
- jsonStatements.push(...jsonAlterTables)
144
- jsonStatements.push(...jsonCreateIndexesForAllAlteredTables)
145
- jsonStatements.push(...jsonDropIndexesForAllAlteredTables)
146
-
147
- // console.log(JSON.stringify(jsonStatements, null, 2))
148
- // console.log(jsonStatements)
149
-
150
- const sqlStatements = fromJson(jsonStatements)
151
- console.log(sqlStatements.join('\n'))
152
-
153
- // todo: save results to a new migration folder
154
- const date = new Date()
155
- // const folderName = `${date.getFullYear()}-${two(date.getMonth())}-${two(date.getDate())} ${two(date.getHours())}:${two(date.getMinutes())}:${two(date.getSeconds())}`
156
- const folderName = date.toISOString()
157
- const migrationFolderPath = `./${migrationRootFolder}/${folderName}`
158
- fs.mkdirSync(migrationFolderPath)
159
- fs.writeFileSync(`${migrationFolderPath}/snapshot.json`, JSON.stringify(cur, null, 2))
160
- fs.writeFileSync(`${migrationFolderPath}/migration.sql`, sqlStatements.join('\n'))
161
-
162
- };
163
-
164
- export default migrate;
@@ -1,20 +0,0 @@
1
- import React from 'react';
2
- import { useSnapshot } from 'valtio';
3
-
4
- interface Props {
5
- componentsList: React.ReactElement<any, any>[];
6
- }
7
-
8
- const ComponentsList: React.FC<Props> = ({
9
- componentsList,
10
- }) => {
11
- useSnapshot(componentsList);
12
-
13
- return (
14
- <>
15
- {componentsList.map((component, i) => ({ ...component, key: i }))}
16
- </>
17
- );
18
- };
19
-
20
- export default ComponentsList;
@@ -1,19 +0,0 @@
1
- import { render } from 'ink';
2
- import React from 'react';
3
-
4
- import ComponentsList from 'cli/components-api/ComponentsList';
5
-
6
- const createApp = (componentsList: React.ReactElement<any, any>[]) => {
7
- const inkApp = render(<ComponentsList componentsList={componentsList} />);
8
-
9
- return {
10
- remove() {
11
- inkApp.rerender(null);
12
- inkApp.unmount();
13
- inkApp.clear();
14
- inkApp.cleanup();
15
- },
16
- };
17
- };
18
-
19
- export default createApp;
@@ -1,171 +0,0 @@
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 { ColumnsResultData, Named } from 'cli/commands/migration/migrate';
9
- import formatDataForTable from 'cli/utils/formatDataForTable';
10
- import {
11
- Action,
12
- Confirmation,
13
- actions,
14
- confirmations,
15
- } from 'cli/utils/valuesForPrompts';
16
- import createResolveColumnsMachine, { TableWithColumn } from 'cli/machines/resolveColumnsMachine';
17
-
18
- interface Props {
19
- onDone: (value: ColumnsResultData<Named>) => void
20
- tableWithColumns: TableWithColumn,
21
- }
22
-
23
- const PromptColumnsConflicts: React.FC<Props> = ({
24
- onDone,
25
- tableWithColumns: props,
26
- }) => {
27
- const [current, send] = useMachine(
28
- createResolveColumnsMachine({ tableWithColumns: props }),
29
- );
30
-
31
- const {
32
- tableName,
33
- addedColumns,
34
- deletedColumns,
35
- missingItemIndex,
36
- created,
37
- renamed,
38
- deleted,
39
- } = current.context;
40
-
41
- const selectItemIndex = (item: Item<number>) => {
42
- send({ type: 'CHOICE_ITEM', itemIndex: item.value });
43
- };
44
-
45
- const selectNewItemIndex = (item: Item<number>) => {
46
- send({ type: 'CHOICE_NEW_ITEM', itemIndex: item.value });
47
- };
48
-
49
- const selectAction = (action: Item<Action>) => {
50
- send({ type: action.value });
51
- };
52
-
53
- const confirm = (confirmationItem: Item<Confirmation>) => {
54
- send({ type: confirmationItem.value });
55
- };
56
-
57
- const renderDOM = () => {
58
- if (current.matches('table')) {
59
- return (
60
- <>
61
- <Box display="flex" flexDirection="column">
62
- <Box display="flex">
63
- <Box flexDirection="column">
64
- <Header>{`Table "${tableName}" missing columns:`}</Header>
65
- <SelectInput
66
- items={deletedColumns!.map((column, i: number) => ({
67
- key: String(i),
68
- label: column.name,
69
- value: i,
70
- }))}
71
- onSelect={selectItemIndex}
72
- />
73
- </Box>
74
- {!!addedColumns?.length && (
75
- <Box flexDirection="column" paddingLeft={5}>
76
- <Header>New tables:</Header>
77
- <Box display="flex" flexDirection="column">
78
- {addedColumns.map(({ name }, i) => <Text key={name + i}>{name}</Text>)}
79
- </Box>
80
- </Box>
81
- )}
82
- </Box>
83
- </Box>
84
- </>
85
- );
86
- }
87
-
88
- if (current.matches('action.actionChoice')) {
89
- const tempActions = !addedColumns!.length
90
- ? actions.filter(({ value }) => value !== Action.RENAME)
91
- : actions;
92
-
93
- return (
94
- <>
95
- <Header>{`${deletedColumns![missingItemIndex]?.name} is:`}</Header>
96
- <SelectInput items={tempActions} onSelect={selectAction} />
97
- </>
98
- );
99
- }
100
-
101
- if (current.matches('confirmationDelete')) {
102
- return (
103
- <>
104
- <Header>!!! Data in table will be lost !!!</Header>
105
- <Text>Are you sure?</Text>
106
- <SelectInput items={confirmations} onSelect={confirm} />
107
- </>
108
- );
109
- }
110
-
111
- if (current.matches('confirmationRename')) {
112
- return (
113
- <>
114
- <Header>Are you sure?</Header>
115
- <SelectInput items={confirmations} onSelect={confirm} />
116
- </>
117
- );
118
- }
119
-
120
- if (current.matches('action.rename')) {
121
- return (
122
- <>
123
- <Header>{`${deletedColumns![missingItemIndex]?.name} was renamed to:`}</Header>
124
- <SelectInput
125
- items={addedColumns!.map((table, i: number) => ({
126
- key: String(i),
127
- label: table.name,
128
- value: i,
129
- }))}
130
- onSelect={selectNewItemIndex}
131
- />
132
- </>
133
- );
134
- }
135
-
136
- if (current.matches('done')) {
137
- onDone({ created, renamed, deleted });
138
- }
139
-
140
- return (<></>);
141
- };
142
-
143
- return (!!deleted.length
144
- || !!renamed.length
145
- || !!created.length) ? (
146
- <Box flexDirection="column" margin={1}>
147
- <Box flexDirection="column" marginBottom={0}>
148
- <Header>{`${tableName} columns:`}</Header>
149
- <Table
150
- data={formatDataForTable([
151
- {
152
- title: 'Deleted',
153
- values: deleted,
154
- },
155
- {
156
- title: 'Renamed',
157
- values: renamed,
158
- },
159
- {
160
- title: 'Created',
161
- values: created,
162
- },
163
- ])}
164
- />
165
- </Box>
166
- {renderDOM()}
167
- </Box>
168
- ) : renderDOM();
169
- };
170
-
171
- export default PromptColumnsConflicts;
@@ -1,180 +0,0 @@
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;
@@ -1,85 +0,0 @@
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;