Skip to content

Commit

Permalink
Merge pull request #2711 from drizzle-team/kit
Browse files Browse the repository at this point in the history
Kit
  • Loading branch information
AndriiSherman authored Aug 1, 2024
2 parents 55231b0 + e9a7a6c commit d65afd3
Show file tree
Hide file tree
Showing 117 changed files with 49,364 additions and 215 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ examples
**/playground
integration-tests/tests/prisma/*/client
integration-tests/tests/prisma/*/drizzle
drizzle-kit/*
1 change: 1 addition & 0 deletions .github/workflows/release-feature-branch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ jobs:
matrix:
package:
- drizzle-orm
- drizzle-kit
- drizzle-zod
- drizzle-typebox
- drizzle-valibot
Expand Down
33 changes: 33 additions & 0 deletions .github/workflows/release-latest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ jobs:
matrix:
package:
- drizzle-orm
- drizzle-kit
- drizzle-zod
- drizzle-typebox
- drizzle-valibot
Expand Down Expand Up @@ -232,3 +233,35 @@ jobs:
} catch (e) {
core.setFailed(e.message);
}
- name: Create GitHub release for KIT package
uses: actions/github-script@v6
if: matrix.package == 'drizzle-kit' && steps.checks.outputs.has_new_release == 'true'
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
try {
const fs = require("fs");
const path = require("path");
const version = "${{ steps.checks.outputs.version }}";
const changelog = fs.readFileSync("${{ steps.checks.outputs.changelog_path }}", "utf8");
const release = await github.rest.repos.createRelease({
owner: context.repo.owner,
repo: context.repo.repo,
tag_name: `drizzle-kit@${version}`,
name: `drizzle-kit@${version}`,
body: changelog,
});
await github.rest.repos.uploadReleaseAsset({
owner: context.repo.owner,
repo: context.repo.repo,
release_id: release.data.id,
name: `${{ matrix.package }}-${version}-dist.tgz`,
data: fs.readFileSync(path.resolve("${{ matrix.package }}", "package.tgz")),
});
} catch (e) {
core.setFailed(e.message);
}
1 change: 1 addition & 0 deletions .github/workflows/unpublish-release-feature-branch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ jobs:
matrix:
package:
- drizzle-orm
- drizzle-kit
- drizzle-zod
- drizzle-typebox
- drizzle-valibot
Expand Down
162 changes: 137 additions & 25 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,27 @@ There are several ways how you can provide a feedback

---

- [Contributing](#contributing)
- [Submitting bug report](#-submitting-bug-report)
- [Submitting feature request](#-submitting-feature-request)
- [Providing feedback](#-providing-feedback)
- [Contribution guidelines](#-contribution-guidelines)
- [General setup](#-general-setup)
- [Installing node](#-installing-node)
- [Install pnpm](#-install-pnpm)
- [Install docker](#-install-docker)
- [Local project setup](#-local-project-setup)
- [Clone project](#-clone-project)
- [Building project](#-building-project)
- [Build project](#-build-project)
- [Run tests](#-run-tests)
- [Commits and PRs](#-commits-and-prs)
- [Commit guideline](#-commit-guideline)
- [Pre-Contribution setup](#pre-contribution)
- [Installing node](#-installing-node)
- [Install pnpm](#-install-pnpm)
- [Install docker](#-install-docker)
- [Clone project](#-clone-project)
- [Repository Structure](#repo-structure)
- [Build project](#-build-project)
- [Contributing to `drizzle-orm`](#contributing-orm)
- [Project structure](#project-structure-orm)
- [Run tests](#run-tests-orm)
- [Commits and PRs](#commits-and-prs-orm)
- [Commit guideline](#commit-guideline-orm)
- [PR guideline](#pr-guideline-orm)
- [Contributing to `drizzle-kit`](#contributing-kit)
- [Project structure](#project-structure-kit)
- [Run tests](#run-tests-kit)
- [Commits and PRs](#commits-and-prs-kit)
- [Commit guideline](#commit-guideline-kit)
- [PR guideline](#-pr-guideline)

## <a name="general-setup"></a> General setup
## <a name="pre-contribution"></a> Pre-Contribution setup

### <a name="installing-node"></a> Installing node

Expand Down Expand Up @@ -99,14 +101,22 @@ git clone https://github.com/drizzle-team/drizzle-orm.git
cd drizzle-orm
```

## <a name="building-project"></a> Building project
### <a name="repo-structure"></a> Repository Structure

```
Project sctructure
📂 drizzle-orm/ - orm core package with all main logic for each dialect
📂 drizzle-orm/ - core package with all main logic for each dialect
📂 drizzle-kit/ - kit core package with all main logic and tests for each dialect
📂 changelogs/ - all changelogs for drizzle-orm module
📂 drizzle-typebox/ - all the code related to drizzle+typebox extension
📂 drizzle-valibot/ - all the code related to drizzle+valibot extension
📂 drizzle-zod/ - all the code related to drizzle+zod extension
📂 eslint-plugin-drizzle/ - all the code related to drizzle eslint plugin
📂 changelogs/ - all changelogs for drizzle-orm, drizzle-kit, drizzle-typebox, drizzle-zod, drizzle-valibot modules
📂 examples/ - package with Drizzle ORM usage examples
Expand All @@ -119,7 +129,21 @@ Project sctructure

- `"pnpm i && pnpm build"` -> if you run this script from root folder - it will build whole monorepo. Running this script from specific package folder will only build current package

### <a name="run-tests"></a> Run tests
## <a name="contributing-orm"></a> Contributing to `drizzle-orm`

### <a name="project-structure-orm"></a> Project structure

```
Project structure
📂 pg-core, mysql-core, sqlite-core - core packages for each dialect with all the main logic for relation and query builder
📂 sql/ - package containing all expressions and SQL template implementation
All other folders are for specific drivers that Drizzle ORM supports.
```

### <a name="run-tests-orm"></a> Run tests

---
All tests for Drizzle ORM are integration tests, that are simulating real database and different queries and responses from database. Each file in `integration-tests` has a list of different scenarios for different dialect+driver. Each file is creating a docker instance with needed database and running test cases there. Right after all tests were run - docker container with database will be deleted
Expand All @@ -130,9 +154,9 @@ If you have added additional logic to core package - make sure that all tests we
- `"cd integration-tests && pnpm test"` -> will run all tests in integration test folder

## <a name="commits-pr"></a> Commits and PRs
## <a name="commits-and-prs-orm"></a> Commits and PRs

### <a name="commit-guideline"></a> Commit guideline
### <a name="commit-guideline-orm"></a> Commit guideline

---

Expand Down Expand Up @@ -160,7 +184,7 @@ In specific case, groupBy was responding with unreadable error
> **Warning**:
> All commits should be signed, before submitting PR. Please check detailed info on [how to sign commits](https://docs.github.com/en/authentication/managing-commit-signature-verification/about-commit-signature-verification)
### <a name="pr-guideline"></a> PR guideline
### <a name="pr-guideline-orm"></a> PR guideline

---

Expand All @@ -183,3 +207,91 @@ Example
- Tests on bugs, that was fixed;

To understand how test should be created and run - please check [Run tests](#-run-tests) section


## <a name="contributing-kit"></a> Contributing to `drizzle-kit`

### <a name="project-structure-kit"></a> Project structure

```
📂 cli/
|
| -> 📄 schema.ts - all the commands defined using brocli
|
| -> 📂 commands - all the business logic for drizzle-kit commands
📂 extensions/ - all the extension helpers for databases
📂 serialaizer/ - all the necessary logic to read from the Drizzle ORM schema and convert it to a common JSON format, as well as the logic to introspect all tables, types, and other database elements and convert them to a common JSON format
📄 introspect-pg.ts, introspect-mysql.ts, introspect-sqlite.ts - these files are responsible for mapping JSON snapshots to TypeScript files during introspect commands
📄 snapshotsDiffer.ts - this file handles the mapping from JSON snapshot format to JSON statement objects.
📄 jsonStatements.ts - this file defines JSON statement types, interfaces, and helper functions.
📄 sqlgenerator.ts - this file converts JSON statements to SQL strings.
```

### <a name="run-tests-kit"></a> Run tests

---
All tests for Drizzle Kit are integration tests, that are simulating real database and different queries and responses from database. Each file in `drizzle-kit/tests` has a list of different scenarios for different commands. Each MySQL file is creating a docker instance with needed database and running test cases there. Right after all tests were run - docker container with database will be deleted. For PostgreSQL we are using PgLite and for SQLite we are using SQLite files.

If you are in the root of repo:

- `"cd drizzle-kit && pnpm test"` -> will run all tests

## <a name="commits-and-prs-kit"></a> Commits and PRs

### <a name="commit-guideline-kit"></a> Commit guideline

---

We have specific rules on how commit messages should be structured.

It's important to make sure your commit messages are clear, concise, and informative to make it easier for others to understand the changes you are making

Commit message pattern

```
<subject>
<BLANK LINE>
<body>
```

Example

```
Add groupBy error message
In specific case, groupBy was responding with unreadable error
...
```

> **Warning**:
> All commits should be signed, before submitting PR. Please check detailed info on [how to sign commits](https://docs.github.com/en/authentication/managing-commit-signature-verification/about-commit-signature-verification)
### <a name="pr-guideline-kit"></a> PR guideline

---

1. PR should be created with specific name pattern

```
[<Dialect name>-kit]: <subject>
```

Example

```
[Pg-kit] Add PostGIS extension support
```

2. PR should contain detailed description with everything, that was changed

3. Each PR should contain
- Tests on feature, that was created;
- Tests on bugs, that was fixed;

To understand how test should be created and run - please check [Run tests](#run-tests) section
2 changes: 2 additions & 0 deletions changelogs/drizzle-kit/0.23.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Fixed a bug in PostgreSQL with push and introspect where the `schemaFilter` object was passed. It was detecting enums even in schemas that were not defined in the schemaFilter.
- Fixed the `drizzle-kit up` command to work as expected, starting from the sequences release.
23 changes: 23 additions & 0 deletions drizzle-kit/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
**/.DS_Store

!src
!tests
!vitest.config.ts
!README.md
!CONTRIBUTING.md
!schema.ts

!.eslint
!.gitignore
!package.json
!tsconfig.json
!tsconfig.cli-types.json
!pnpm-lock.yaml
!.github
!build.ts
!build.dev.ts

tests/test.ts

!patches
79 changes: 79 additions & 0 deletions drizzle-kit/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
## Drizzle Kit

DrizzleKit - is a CLI migrator tool for DrizzleORM. It is probably one and only tool that lets you completely automatically generate SQL migrations and covers ~95% of the common cases like deletions and renames by prompting user input.
<https://github.com/drizzle-team/drizzle-kit-mirror> - is a mirror repository for issues.

## Documentation

Check the full documenation on [the website](https://orm.drizzle.team/kit-docs/overview)

### How it works

`drizzle-kit` will traverse `schema folder` or `schema file`, generate schema snapshot and compare it to the previous version, if there's one.
Based on the difference it will generate all needed SQL migrations and if there are any `automatically unresolvable` cases like `renames` it will prompt user for input.

For schema file:

```typescript
// ./src/db/schema.ts

import { integer, pgTable, serial, text, varchar } from "drizzle-orm/pg-core";

const users = pgTable("users", {
id: serial("id").primaryKey(),
fullName: varchar("full_name", { length: 256 }),
}, (table) => ({
nameIdx: index("name_idx", table.fullName),
})
);

export const authOtp = pgTable("auth_otp", {
id: serial("id").primaryKey(),
phone: varchar("phone", { length: 256 }),
userId: integer("user_id").references(() => users.id),
});
```

It will generate:

```SQL
CREATE TABLE IF NOT EXISTS auth_otp (
"id" SERIAL PRIMARY KEY,
"phone" character varying(256),
"user_id" INT
);

CREATE TABLE IF NOT EXISTS users (
"id" SERIAL PRIMARY KEY,
"full_name" character varying(256)
);

DO $$ BEGIN
ALTER TABLE auth_otp ADD CONSTRAINT auth_otp_user_id_fkey FOREIGN KEY ("user_id") REFERENCES users(id);
EXCEPTION
WHEN duplicate_object THEN null;
END $$;

CREATE INDEX IF NOT EXISTS users_full_name_index ON users (full_name);
```

### Installation & configuration

```shell
npm install -D drizzle-kit
```

Running with CLI options

```jsonc
// package.json
{
"scripts": {
"generate": "drizzle-kit generate --out migrations-folder --schema src/db/schema.ts"
}
}
```

```shell
npm run generate
```
Loading

0 comments on commit d65afd3

Please sign in to comment.