revisium 2.2.0 → 2.3.0

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.
@@ -39,7 +39,7 @@ revisium migrate save --file ./migrations.json \
39
39
  Apply migrations from a JSON file to Revisium.
40
40
 
41
41
  ```bash
42
- revisium migrate apply --file <path> [--commit]
42
+ revisium migrate apply --file <path> [--commit] [--create-project]
43
43
  ```
44
44
 
45
45
  ### Options
@@ -48,6 +48,7 @@ revisium migrate apply --file <path> [--commit]
48
48
  |--------|-------------|----------|
49
49
  | `-f, --file <path>` | Migrations file path | Yes |
50
50
  | `-c, --commit` | Create revision after applying | No |
51
+ | `--create-project` | Auto-create project if it doesn't exist | No |
51
52
  | `--url <url>` | Revisium URL (see [URL Format](./url-format.md)) | No* |
52
53
 
53
54
  *If `--url` is not provided, uses `REVISIUM_URL` environment variable or prompts interactively.
@@ -165,6 +166,10 @@ git commit -m "Add user phone field migration"
165
166
  export REVISIUM_URL=revisium://cloud.revisium.io/myorg/myproject/master
166
167
  export REVISIUM_TOKEN=$DEPLOY_TOKEN
167
168
  revisium migrate apply --file ./migrations.json --commit
169
+
170
+ # Apply in CI/CD with auto-create (fresh instance)
171
+ revisium migrate apply --file ./migrations.json --commit --create-project \
172
+ --url revisium://admin:admin@localhost:8888/admin/billing/master:draft
168
173
  ```
169
174
 
170
175
  See [URL Format](./url-format.md) for complete URL syntax and [Authentication](./authentication.md) for auth options.
@@ -2,7 +2,11 @@ import * as fs from 'fs';
2
2
  import * as path from 'path';
3
3
  import * as os from 'os';
4
4
  import { runCli, buildUrl } from '../utils/cli-runner';
5
- import { createTestProject } from '../utils/test-project';
5
+ import {
6
+ createTestProject,
7
+ deleteTestProject,
8
+ generateProjectName,
9
+ } from '../utils/test-project';
6
10
  import { api } from '../utils/api-client';
7
11
  import { FIXTURES_PATH } from '../utils/constants';
8
12
 
@@ -88,6 +92,50 @@ describe('Migrate Commands', () => {
88
92
  expect(updatedProject.rootBranch.headRevisionId).not.toBe(beforeHeadId);
89
93
  });
90
94
 
95
+ it('creates project with --create-project when project does not exist', async () => {
96
+ const projectName = generateProjectName('e2e-create');
97
+
98
+ try {
99
+ const result = await runCli([
100
+ 'migrate',
101
+ 'apply',
102
+ '--url',
103
+ buildUrl(projectName, { token }),
104
+ '--file',
105
+ path.join(FIXTURES_PATH, 'migrations.json'),
106
+ '--create-project',
107
+ ]);
108
+
109
+ expect(result.exitCode).toBe(0);
110
+ expect(result.stdout).toContain('creating automatically');
111
+ expect(result.stdout).toContain('Successfully applied');
112
+
113
+ const project = await api.getProject('admin', projectName);
114
+ expect(project.name).toBe(projectName);
115
+
116
+ const tables = await api.getTables(project.rootBranch.draftRevisionId);
117
+ expect(tables.length).toBe(14);
118
+ } finally {
119
+ await deleteTestProject(projectName);
120
+ }
121
+ });
122
+
123
+ it('fails with hint when project not found without --create-project', async () => {
124
+ const projectName = generateProjectName('e2e-no-create');
125
+
126
+ const result = await runCli([
127
+ 'migrate',
128
+ 'apply',
129
+ '--url',
130
+ buildUrl(projectName, { token }),
131
+ '--file',
132
+ path.join(FIXTURES_PATH, 'migrations.json'),
133
+ ]);
134
+
135
+ expect(result.exitCode).toBe(1);
136
+ expect(result.stdout + result.stderr).toContain('--create-project');
137
+ });
138
+
91
139
  it('skips already applied migrations', async () => {
92
140
  const project = await createTestProject();
93
141
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "revisium",
3
- "version": "2.2.0",
3
+ "version": "2.3.0",
4
4
  "homepage": "https://revisium.io",
5
5
  "description": "A CLI tool for interacting with Revisium instances, providing migration management, schema export, and data export capabilities.",
6
6
  "author": "Anton Kashirov",
@@ -11,6 +11,7 @@ import { parseBooleanOption } from 'src/utils/parse-boolean.utils';
11
11
  type Options = BaseOptions & {
12
12
  file: string;
13
13
  commit?: boolean;
14
+ createProject?: boolean;
14
15
  };
15
16
 
16
17
  @SubCommand({
@@ -36,7 +37,10 @@ export class ApplyMigrationsCommand extends BaseCommand {
36
37
  const jsonData = await this.validateJsonFile(options.file);
37
38
  this.logger.success('Migration file validation passed');
38
39
 
39
- await this.connectionService.connect(options);
40
+ await this.connectionService.connect({
41
+ url: options.url,
42
+ createProject: options.createProject,
43
+ });
40
44
 
41
45
  const countAppliedMigrations = await this.applyMigration(jsonData);
42
46
 
@@ -133,4 +137,13 @@ export class ApplyMigrationsCommand extends BaseCommand {
133
137
  parseCommit(value?: string): boolean {
134
138
  return parseBooleanOption(value);
135
139
  }
140
+
141
+ @Option({
142
+ flags: '--create-project [boolean]',
143
+ description:
144
+ 'Automatically create the project if it does not exist (for CI/CD)',
145
+ })
146
+ parseCreateProject(value?: string): boolean {
147
+ return parseBooleanOption(value);
148
+ }
136
149
  }
@@ -12,6 +12,7 @@ describe('ConnectionFactoryService', () => {
12
12
  connecting: jest.Mock;
13
13
  connected: jest.Mock;
14
14
  authenticated: jest.Mock;
15
+ info: jest.Mock;
15
16
  };
16
17
 
17
18
  const mockDraftScope = {
@@ -55,6 +56,7 @@ describe('ConnectionFactoryService', () => {
55
56
  connecting: jest.fn(),
56
57
  connected: jest.fn(),
57
58
  authenticated: jest.fn(),
59
+ info: jest.fn(),
58
60
  };
59
61
 
60
62
  const MockApiClient = RevisiumApiClient as jest.MockedClass<
@@ -114,4 +116,143 @@ describe('ConnectionFactoryService', () => {
114
116
  expect(result.revisionScope).toBe(mockExplicitScope);
115
117
  });
116
118
  });
119
+
120
+ describe('createProject option', () => {
121
+ let mockOrgScope: { createProject: jest.Mock };
122
+
123
+ beforeEach(() => {
124
+ mockOrgScope = {
125
+ createProject: jest.fn().mockResolvedValue({ id: 'new-project-id' }),
126
+ };
127
+ });
128
+
129
+ it('creates project and retries when createProject is true and project not found', async () => {
130
+ const projectNotFoundError = new Error(
131
+ 'A project with this name does not exist in the organization',
132
+ );
133
+
134
+ const MockApiClient = RevisiumApiClient as jest.MockedClass<
135
+ typeof RevisiumApiClient
136
+ >;
137
+ MockApiClient.mockImplementation(
138
+ () =>
139
+ ({
140
+ client: {
141
+ branch: jest
142
+ .fn()
143
+ .mockRejectedValueOnce(projectNotFoundError)
144
+ .mockResolvedValueOnce(mockBranchScope),
145
+ org: jest.fn().mockReturnValue(mockOrgScope),
146
+ },
147
+ authenticate: jest.fn().mockResolvedValue('test-user'),
148
+ }) as unknown as RevisiumApiClient,
149
+ );
150
+
151
+ service = new ConnectionFactoryService(
152
+ urlBuilderFake as unknown as UrlBuilderService,
153
+ loggerFake as unknown as LoggerService,
154
+ );
155
+
156
+ const result = await service.createConnection(baseUrl, {
157
+ createProject: true,
158
+ });
159
+
160
+ expect(mockOrgScope.createProject).toHaveBeenCalledWith({
161
+ projectName: 'test-project',
162
+ });
163
+ expect(result.revisionScope).toBe(mockDraftScope);
164
+ expect(loggerFake.info).toHaveBeenCalledWith(
165
+ 'Project "test-project" not found — creating automatically',
166
+ );
167
+ });
168
+
169
+ it('throws with hint when createProject is false and project not found', async () => {
170
+ const projectNotFoundError = new Error(
171
+ 'A project with this name does not exist in the organization',
172
+ );
173
+
174
+ const MockApiClient = RevisiumApiClient as jest.MockedClass<
175
+ typeof RevisiumApiClient
176
+ >;
177
+ MockApiClient.mockImplementation(
178
+ () =>
179
+ ({
180
+ client: {
181
+ branch: jest.fn().mockRejectedValue(projectNotFoundError),
182
+ },
183
+ authenticate: jest.fn().mockResolvedValue('test-user'),
184
+ }) as unknown as RevisiumApiClient,
185
+ );
186
+
187
+ service = new ConnectionFactoryService(
188
+ urlBuilderFake as unknown as UrlBuilderService,
189
+ loggerFake as unknown as LoggerService,
190
+ );
191
+
192
+ await expect(service.createConnection(baseUrl)).rejects.toThrow(
193
+ 'Use --create-project to auto-create',
194
+ );
195
+ });
196
+
197
+ it('creates project on generic not-found error when createProject is true', async () => {
198
+ const notFoundError = new Error('Resource not found');
199
+
200
+ const MockApiClient = RevisiumApiClient as jest.MockedClass<
201
+ typeof RevisiumApiClient
202
+ >;
203
+ MockApiClient.mockImplementation(
204
+ () =>
205
+ ({
206
+ client: {
207
+ branch: jest
208
+ .fn()
209
+ .mockRejectedValueOnce(notFoundError)
210
+ .mockResolvedValueOnce(mockBranchScope),
211
+ org: jest.fn().mockReturnValue(mockOrgScope),
212
+ },
213
+ authenticate: jest.fn().mockResolvedValue('test-user'),
214
+ }) as unknown as RevisiumApiClient,
215
+ );
216
+
217
+ service = new ConnectionFactoryService(
218
+ urlBuilderFake as unknown as UrlBuilderService,
219
+ loggerFake as unknown as LoggerService,
220
+ );
221
+
222
+ const result = await service.createConnection(baseUrl, {
223
+ createProject: true,
224
+ });
225
+
226
+ expect(mockOrgScope.createProject).toHaveBeenCalledWith({
227
+ projectName: 'test-project',
228
+ });
229
+ expect(result.revisionScope).toBe(mockDraftScope);
230
+ });
231
+
232
+ it('rethrows non-project errors even with createProject', async () => {
233
+ const networkError = new Error('ECONNREFUSED');
234
+
235
+ const MockApiClient = RevisiumApiClient as jest.MockedClass<
236
+ typeof RevisiumApiClient
237
+ >;
238
+ MockApiClient.mockImplementation(
239
+ () =>
240
+ ({
241
+ client: {
242
+ branch: jest.fn().mockRejectedValue(networkError),
243
+ },
244
+ authenticate: jest.fn().mockResolvedValue('test-user'),
245
+ }) as unknown as RevisiumApiClient,
246
+ );
247
+
248
+ service = new ConnectionFactoryService(
249
+ urlBuilderFake as unknown as UrlBuilderService,
250
+ loggerFake as unknown as LoggerService,
251
+ );
252
+
253
+ await expect(
254
+ service.createConnection(baseUrl, { createProject: true }),
255
+ ).rejects.toThrow('ECONNREFUSED');
256
+ });
257
+ });
117
258
  });
@@ -157,6 +157,7 @@ describe('ConnectionService', () => {
157
157
 
158
158
  expect(connectionFactoryFake.createConnection).toHaveBeenCalledWith(
159
159
  mockUrl,
160
+ { createProject: undefined },
160
161
  );
161
162
  });
162
163
  });
@@ -13,6 +13,7 @@ export interface ConnectionInfo {
13
13
 
14
14
  export interface ConnectOptions {
15
15
  label?: string;
16
+ createProject?: boolean;
16
17
  }
17
18
 
18
19
  @Injectable()
@@ -33,11 +34,7 @@ export class ConnectionFactoryService {
33
34
 
34
35
  const client = await this.createAuthenticatedClient(url);
35
36
 
36
- const branchScope = await client.client.branch({
37
- org: url.organization,
38
- project: url.project,
39
- branch: url.branch || 'master',
40
- });
37
+ const branchScope = await this.resolveBranchScope(client, url, options);
41
38
 
42
39
  const revisionScope = await this.resolveRevisionScope(
43
40
  url.revision,
@@ -74,6 +71,51 @@ export class ConnectionFactoryService {
74
71
  return client;
75
72
  }
76
73
 
74
+ private async resolveBranchScope(
75
+ client: RevisiumApiClient,
76
+ url: RevisiumUrlComplete,
77
+ options: ConnectOptions,
78
+ ): Promise<BranchScope> {
79
+ const branchOptions = {
80
+ org: url.organization,
81
+ project: url.project,
82
+ branch: url.branch || 'master',
83
+ };
84
+
85
+ try {
86
+ return await client.client.branch(branchOptions);
87
+ } catch (error) {
88
+ if (options.createProject && this.isProjectNotFoundError(error)) {
89
+ this.logger.info(
90
+ `Project "${url.project}" not found — creating automatically`,
91
+ );
92
+
93
+ await client.client
94
+ .org(url.organization)
95
+ .createProject({ projectName: url.project });
96
+
97
+ return client.client.branch(branchOptions);
98
+ }
99
+
100
+ if (!options.createProject && this.isProjectNotFoundError(error)) {
101
+ throw new Error(
102
+ `Project "${url.project}" not found in organization "${url.organization}". Use --create-project to auto-create`,
103
+ );
104
+ }
105
+
106
+ throw error;
107
+ }
108
+ }
109
+
110
+ private isProjectNotFoundError(error: unknown): boolean {
111
+ const message = error instanceof Error ? error.message : String(error);
112
+ return (
113
+ message.includes('does not exist') ||
114
+ message.includes('not found') ||
115
+ message.includes('Not Found')
116
+ );
117
+ }
118
+
77
119
  private async resolveRevisionScope(
78
120
  revision: string,
79
121
  branchScope: BranchScope,
@@ -11,6 +11,7 @@ export { ConnectionInfo } from './connection-factory.service';
11
11
 
12
12
  export interface ConnectionOptions {
13
13
  url?: string;
14
+ createProject?: boolean;
14
15
  }
15
16
 
16
17
  @Injectable()
@@ -38,7 +39,9 @@ export class ConnectionService {
38
39
  const env = this.getEnvConfig();
39
40
  const url = await this.urlBuilder.parseAndComplete(options.url, 'api', env);
40
41
 
41
- this._connection = await this.connectionFactory.createConnection(url);
42
+ this._connection = await this.connectionFactory.createConnection(url, {
43
+ createProject: options.createProject,
44
+ });
42
45
  }
43
46
 
44
47
  private getEnvConfig(): UrlEnvConfig {