rake-db 2.3.0 → 2.3.2

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.
@@ -5,6 +5,7 @@ import {
5
5
  ColumnsShape,
6
6
  ForeignKeyOptions,
7
7
  instantiateColumn,
8
+ singleQuote,
8
9
  } from 'pqb';
9
10
 
10
11
  const matchMap = {
@@ -14,8 +15,8 @@ const matchMap = {
14
15
  };
15
16
 
16
17
  const fkeyActionMap = {
17
- a: 'NO ACTION',
18
- r: undefined, // default
18
+ a: undefined, // default
19
+ r: 'RESTRICT',
19
20
  c: 'CASCADE',
20
21
  n: 'SET NULL',
21
22
  d: 'SET DEFAULT',
@@ -55,6 +56,8 @@ export const structureToAst = async (db: DbStructure): Promise<RakeDbAst[]> => {
55
56
  for (const table of tables) {
56
57
  const { schemaName, name } = table;
57
58
 
59
+ if (name === 'schemaMigrations') continue;
60
+
58
61
  const belongsToTable = makeBelongsToTable(schemaName, name);
59
62
 
60
63
  const columns = allColumns.filter(belongsToTable);
@@ -63,11 +66,17 @@ export const structureToAst = async (db: DbStructure): Promise<RakeDbAst[]> => {
63
66
  const tableForeignKeys = allForeignKeys.filter(belongsToTable);
64
67
 
65
68
  const shape: ColumnsShape = {};
66
- for (const item of columns) {
67
- const klass = columnsByType[item.type];
69
+ for (let item of columns) {
70
+ const isSerial = getIsSerial(item);
71
+ if (isSerial) {
72
+ item = { ...item, default: undefined };
73
+ }
74
+
75
+ const klass = columnsByType[getColumnType(item, isSerial)];
68
76
  if (!klass) {
69
77
  throw new Error(`Column type \`${item.type}\` is not supported`);
70
78
  }
79
+
71
80
  let column = instantiateColumn(klass, item);
72
81
 
73
82
  if (
@@ -194,3 +203,35 @@ const makeBelongsToTable =
194
203
  (schema: string | undefined, table: string) =>
195
204
  (item: { schemaName: string; tableName: string }) =>
196
205
  item.schemaName === schema && item.tableName === table;
206
+
207
+ const getIsSerial = (item: DbStructure.Column) => {
208
+ if (item.type === 'int2' || item.type === 'int4' || item.type === 'int8') {
209
+ const { default: def, schemaName, tableName, name } = item;
210
+ const seq = `${tableName}_${name}_seq`;
211
+ if (
212
+ def &&
213
+ (def === `nextval(${singleQuote(`${seq}`)}::regclass)` ||
214
+ def === `nextval(${singleQuote(`"${seq}"`)}::regclass)` ||
215
+ def === `nextval(${singleQuote(`${schemaName}.${seq}`)}::regclass)` ||
216
+ def === `nextval(${singleQuote(`"${schemaName}".${seq}`)}::regclass)` ||
217
+ def === `nextval(${singleQuote(`${schemaName}."${seq}"`)}::regclass)` ||
218
+ def === `nextval(${singleQuote(`"${schemaName}"."${seq}"`)}::regclass)`)
219
+ ) {
220
+ return true;
221
+ }
222
+ }
223
+
224
+ return false;
225
+ };
226
+
227
+ const getColumnType = (item: DbStructure.Column, isSerial: boolean) => {
228
+ if (isSerial) {
229
+ return item.type === 'int2'
230
+ ? 'smallserial'
231
+ : item.type === 'int4'
232
+ ? 'serial'
233
+ : 'bigserial';
234
+ }
235
+
236
+ return item.type;
237
+ };
@@ -0,0 +1,100 @@
1
+ import { rakeDb } from './rakeDb';
2
+ import { createDb, dropDb, resetDb } from './commands/createOrDrop';
3
+ import { migrate, rollback } from './commands/migrateOrRollback';
4
+ import { generate } from './commands/generate';
5
+ import { pullDbStructure } from './pull/pull';
6
+
7
+ jest.mock('./common', () => ({
8
+ getMigrationConfigWithDefaults: (config: unknown) => config,
9
+ }));
10
+
11
+ jest.mock('./commands/createOrDrop', () => ({
12
+ createDb: jest.fn(),
13
+ dropDb: jest.fn(),
14
+ resetDb: jest.fn(),
15
+ }));
16
+
17
+ jest.mock('./commands/migrateOrRollback', () => ({
18
+ migrate: jest.fn(),
19
+ rollback: jest.fn(),
20
+ }));
21
+
22
+ jest.mock('./commands/generate', () => ({
23
+ generate: jest.fn(),
24
+ }));
25
+
26
+ jest.mock('./pull/pull', () => ({
27
+ pullDbStructure: jest.fn(),
28
+ }));
29
+
30
+ const options = [
31
+ {
32
+ databaseURL: 'one',
33
+ },
34
+ {
35
+ databaseURL: 'two',
36
+ },
37
+ ];
38
+
39
+ const config = {
40
+ migrationsPath: 'migrations',
41
+ };
42
+
43
+ describe('rakeDb', () => {
44
+ test('create', async () => {
45
+ await rakeDb(options, config, ['create']);
46
+
47
+ expect(createDb).toBeCalledWith(options, config);
48
+ });
49
+
50
+ test('drop', async () => {
51
+ await rakeDb(options, config, ['drop']);
52
+
53
+ expect(dropDb).toBeCalledWith(options);
54
+ });
55
+
56
+ test('reset', async () => {
57
+ await rakeDb(options, config, ['reset']);
58
+
59
+ expect(resetDb).toBeCalledWith(options, config);
60
+ });
61
+
62
+ test('migrate', async () => {
63
+ await rakeDb(options, config, ['migrate', 'arg']);
64
+
65
+ expect(migrate).toBeCalledWith(options, config, ['arg']);
66
+ });
67
+
68
+ test('rollback', async () => {
69
+ await rakeDb(options, config, ['rollback', 'arg']);
70
+
71
+ expect(rollback).toBeCalledWith(options, config, ['arg']);
72
+ });
73
+
74
+ test('generate', async () => {
75
+ await rakeDb(options, config, ['g', 'arg']);
76
+
77
+ expect(generate).toBeCalledWith(config, ['arg']);
78
+
79
+ jest.clearAllMocks();
80
+
81
+ await rakeDb(options, config, ['generate', 'arg']);
82
+
83
+ expect(generate).toBeCalledWith(config, ['arg']);
84
+ });
85
+
86
+ test('pull', async () => {
87
+ await rakeDb(options, config, ['pull']);
88
+
89
+ expect(pullDbStructure).toBeCalledWith(options[0], config);
90
+ });
91
+
92
+ test('other', async () => {
93
+ const log = jest.fn();
94
+ console.log = log;
95
+
96
+ await rakeDb(options, config, ['other']);
97
+
98
+ expect(log).toBeCalled();
99
+ });
100
+ });
package/src/rakeDb.ts CHANGED
@@ -1,8 +1,9 @@
1
- import { AdapterOptions, MaybeArray } from 'pqb';
1
+ import { AdapterOptions, MaybeArray, toArray } from 'pqb';
2
2
  import { createDb, dropDb, resetDb } from './commands/createOrDrop';
3
3
  import { migrate, rollback } from './commands/migrateOrRollback';
4
4
  import { getMigrationConfigWithDefaults, RakeDbConfig } from './common';
5
5
  import { generate } from './commands/generate';
6
+ import { pullDbStructure } from './pull/pull';
6
7
 
7
8
  export const rakeDb = async (
8
9
  options: MaybeArray<AdapterOptions>,
@@ -25,6 +26,8 @@ export const rakeDb = async (
25
26
  await rollback(options, config, args.slice(1));
26
27
  } else if (command === 'g' || command === 'generate') {
27
28
  await generate(config, args.slice(1));
29
+ } else if (command === 'pull') {
30
+ await pullDbStructure(toArray(options)[0], config);
28
31
  } else {
29
32
  printHelp();
30
33
  }