Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions PLAN.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ For existing LaunchQL projects:
- Command-line compatibility layer
- Unit test suite
- Test project setup
- Transaction support with --tx/--no-tx flags
- Cross-project dependency support (project:change format)

**IN PROGRESS:**
- Integration testing
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Deploy project-a:base_schema to pg

BEGIN;

CREATE SCHEMA base;

COMMIT;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- Deploy project-a:base_types to pg
-- requires: base_schema

BEGIN;

CREATE TYPE base.status AS ENUM ('active', 'inactive', 'pending');

COMMIT;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Revert project-a:base_schema from pg

BEGIN;

DROP SCHEMA base CASCADE;

COMMIT;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Revert project-a:base_types from pg

BEGIN;

DROP TYPE base.status;

COMMIT;
6 changes: 6 additions & 0 deletions __fixtures__/migrate/cross-project/project-a/sqitch.plan
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
%syntax-version=1.0.0
%project=project-a
%uri=https://github.com/test/project-a

base_schema 2024-01-01T00:00:00Z test <[email protected]> # Create base schema
base_types [base_schema] 2024-01-02T00:00:00Z test <[email protected]> # Create base types
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- Deploy project-b:app_schema to pg
-- requires: project-a:base_schema

BEGIN;

CREATE SCHEMA app;

COMMIT;
13 changes: 13 additions & 0 deletions __fixtures__/migrate/cross-project/project-b/deploy/app_tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- Deploy project-b:app_tables to pg
-- requires: app_schema
-- requires: project-a:base_types

BEGIN;

CREATE TABLE app.items (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
status base.status DEFAULT 'pending'
);

COMMIT;
6 changes: 6 additions & 0 deletions __fixtures__/migrate/cross-project/project-b/sqitch.plan
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
%syntax-version=1.0.0
%project=project-b
%uri=https://github.com/test/project-b

app_schema [project-a:base_schema] 2024-01-03T00:00:00Z test <[email protected]> # Create app schema
app_tables [app_schema project-a:base_types] 2024-01-04T00:00:00Z test <[email protected]> # Create app tables using base types
9 changes: 9 additions & 0 deletions __fixtures__/migrate/simple/deploy/index.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- Deploy test-simple:index to pg
-- requires: table

BEGIN;

CREATE INDEX idx_users_email ON test_app.users(email);
CREATE INDEX idx_users_created_at ON test_app.users(created_at);

COMMIT;
7 changes: 7 additions & 0 deletions __fixtures__/migrate/simple/deploy/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Deploy test-simple:schema to pg

BEGIN;

CREATE SCHEMA test_app;

COMMIT;
13 changes: 13 additions & 0 deletions __fixtures__/migrate/simple/deploy/table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- Deploy test-simple:table to pg
-- requires: schema

BEGIN;

CREATE TABLE test_app.users (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
email TEXT UNIQUE NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW()
);

COMMIT;
8 changes: 8 additions & 0 deletions __fixtures__/migrate/simple/revert/index.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- Revert test-simple:index from pg

BEGIN;

DROP INDEX test_app.idx_users_email;
DROP INDEX test_app.idx_users_created_at;

COMMIT;
7 changes: 7 additions & 0 deletions __fixtures__/migrate/simple/revert/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Revert test-simple:schema from pg

BEGIN;

DROP SCHEMA test_app CASCADE;

COMMIT;
7 changes: 7 additions & 0 deletions __fixtures__/migrate/simple/revert/table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Revert test-simple:table from pg

BEGIN;

DROP TABLE test_app.users;

COMMIT;
7 changes: 7 additions & 0 deletions __fixtures__/migrate/simple/sqitch.plan
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
%syntax-version=1.0.0
%project=test-simple
%uri=https://github.com/test/simple

schema 2024-01-01T00:00:00Z test <[email protected]> # Create schema
table [schema] 2024-01-02T00:00:00Z test <[email protected]> # Create table
index [table] 2024-01-03T00:00:00Z test <[email protected]> # Create index
7 changes: 7 additions & 0 deletions __fixtures__/migrate/simple/verify/index.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Verify test-simple:index on pg

SELECT 1/COUNT(*) FROM pg_indexes
WHERE schemaname = 'test_app'
AND tablename = 'users'
AND indexname IN ('idx_users_email', 'idx_users_created_at')
HAVING COUNT(*) = 2;
3 changes: 3 additions & 0 deletions __fixtures__/migrate/simple/verify/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- Verify test-simple:schema on pg

SELECT 1/COUNT(*) FROM information_schema.schemata WHERE schema_name = 'test_app';
4 changes: 4 additions & 0 deletions __fixtures__/migrate/simple/verify/table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- Verify test-simple:table on pg

SELECT 1/COUNT(*) FROM information_schema.tables
WHERE table_schema = 'test_app' AND table_name = 'users';
24 changes: 14 additions & 10 deletions packages/cli/src/commands/revert.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CLIOptions, Inquirerer, Question } from 'inquirerer';
import { listModules, revert } from '@launchql/core';
import { LaunchQLProject, revert } from '@launchql/core';
import { errors, getEnvOptions, LaunchQLOptions } from '@launchql/types';
import { getPgEnvOptions, getSpawnEnvWithPg } from 'pg-env';
import { Logger } from '@launchql/logger';
Expand Down Expand Up @@ -43,34 +43,38 @@ export default async (
return;
}

log.debug(`Using current directory: ${cwd}`);

const project = new LaunchQLProject(cwd);

if (recursive) {
const modules = await listModules(cwd);
const mods = Object.keys(modules);
const modules = await project.getModules();
const moduleNames = modules.map(mod => mod.getModuleName());

if (!mods.length) {
log.error('No modules found to revert.');
if (!moduleNames.length) {
log.error('No modules found in the specified directory.');
prompter.close();
throw errors.NOT_FOUND({}, 'No modules found to revert.');
throw errors.NOT_FOUND({}, 'No modules found in the specified directory.');
}

const { project } = await prompter.prompt(argv, [
const { project: selectedProject } = await prompter.prompt(argv, [
{
type: 'autocomplete',
name: 'project',
message: 'Choose a project to revert',
options: mods,
options: moduleNames,
required: true
}
]);

log.success(`Reverting project ${project} on database ${database}...`);
log.success(`Reverting project ${selectedProject} on database ${database}...`);
const options: LaunchQLOptions = getEnvOptions({
pg: {
database
}
});

await revert(options, project, database, cwd, { useSqitch, useTransaction: tx });
await revert(options, selectedProject, database, cwd, { useSqitch, useTransaction: tx });
log.success('Revert complete.');
} else {
const pgEnv = getPgEnvOptions();
Expand Down
43 changes: 20 additions & 23 deletions packages/cli/src/commands/verify.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { CLIOptions, Inquirerer, Question } from 'inquirerer';
import { listModules, verify } from '@launchql/core';
import { LaunchQLProject, verify } from '@launchql/core';
import { errors, getEnvOptions, LaunchQLOptions } from '@launchql/types';
import { getPgEnvOptions, getSpawnEnvWithPg } from 'pg-env';
import { Logger } from '@launchql/logger';
import { verifyCommand } from '@launchql/migrate';
import { execSync } from 'child_process';
import { getTargetDatabase } from '../utils';

const log = new Logger('verify');

Expand All @@ -13,38 +14,34 @@ export default async (
prompter: Inquirerer,
_options: CLIOptions
) => {
const questions: Question[] = [
{
name: 'database',
message: 'Database name',
type: 'text',
required: true
}
];
const database = await getTargetDatabase(argv, prompter, {
message: 'Select database'
});

let { database, recursive, cwd, 'use-sqitch': useSqitch } = await prompter.prompt(argv, questions);
const questions: Question[] = [];

if (!cwd) {
cwd = process.cwd();
log.debug(`Using current directory: ${cwd}`);
}
let { recursive, cwd, 'use-sqitch': useSqitch } = await prompter.prompt(argv, questions);

log.debug(`Using current directory: ${cwd}`);

const project = new LaunchQLProject(cwd);

if (recursive) {
const modules = await listModules(cwd);
const mods = Object.keys(modules);
const modules = await project.getModules();
const moduleNames = modules.map(mod => mod.getModuleName());

if (!mods.length) {
log.error('No modules found to verify.');
if (!moduleNames.length) {
log.error('No modules found in the specified directory.');
prompter.close();
throw errors.NOT_FOUND({}, 'No modules found to verify.');
throw errors.NOT_FOUND({}, 'No modules found in the specified directory.');
}

const { project } = await prompter.prompt(argv, [
const { project: selectedProject } = await prompter.prompt(argv, [
{
type: 'autocomplete',
name: 'project',
message: 'Choose a project to verify',
options: mods,
options: moduleNames,
required: true
}
]);
Expand All @@ -55,8 +52,8 @@ export default async (
}
});

log.info(`Verifying project ${project} on database ${database}...`);
await verify(options, project, database, cwd, { useSqitch });
log.info(`Verifying project ${selectedProject} on database ${database}...`);
await verify(options, selectedProject, database, cwd, { useSqitch });
log.success('Verify complete.');
} else {
const pgEnv = getPgEnvOptions();
Expand Down
8 changes: 0 additions & 8 deletions packages/migrate/CHANGELOG.md

This file was deleted.

Loading