script para automatizar a extensão do banco de dados
This commit is contained in:
14
README.md
14
README.md
@@ -251,19 +251,27 @@ Esta é a **melhor opção para desenvolvedores** que vão modificar o código.
|
|||||||
|
|
||||||
Isso sobe **apenas o banco de dados** em container. A aplicação roda localmente.
|
Isso sobe **apenas o banco de dados** em container. A aplicação roda localmente.
|
||||||
|
|
||||||
5. **Execute as migrations**
|
5. **Ative as extensões necessárias no PostgreSQL**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pnpm db:enableExtensions
|
||||||
|
```
|
||||||
|
|
||||||
|
Ou você pode importar o script diretamente no banco de dados: `scripts/postgres/init.sql`
|
||||||
|
|
||||||
|
6. **Execute as migrations**
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
pnpm db:push
|
pnpm db:push
|
||||||
```
|
```
|
||||||
|
|
||||||
6. **Inicie o servidor de desenvolvimento**
|
7. **Inicie o servidor de desenvolvimento**
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
pnpm dev
|
pnpm dev
|
||||||
```
|
```
|
||||||
|
|
||||||
7. **Acesse a aplicação**
|
8. **Acesse a aplicação**
|
||||||
```
|
```
|
||||||
http://localhost:3000
|
http://localhost:3000
|
||||||
```
|
```
|
||||||
|
|||||||
26
pnpm-lock.yaml
generated
26
pnpm-lock.yaml
generated
@@ -112,7 +112,7 @@ importers:
|
|||||||
version: 4.1.0
|
version: 4.1.0
|
||||||
drizzle-orm:
|
drizzle-orm:
|
||||||
specifier: 0.44.7
|
specifier: 0.44.7
|
||||||
version: 0.44.7(@opentelemetry/api@1.9.0)(better-sqlite3@12.4.1)(kysely@0.28.8)(pg@8.16.3)
|
version: 0.44.7(@opentelemetry/api@1.9.0)(@types/pg@8.15.6)(better-sqlite3@12.4.1)(kysely@0.28.8)(pg@8.16.3)
|
||||||
lucide-react:
|
lucide-react:
|
||||||
specifier: 0.554.0
|
specifier: 0.554.0
|
||||||
version: 0.554.0(react@19.2.0)
|
version: 0.554.0(react@19.2.0)
|
||||||
@@ -165,6 +165,9 @@ importers:
|
|||||||
'@types/node':
|
'@types/node':
|
||||||
specifier: 24.10.1
|
specifier: 24.10.1
|
||||||
version: 24.10.1
|
version: 24.10.1
|
||||||
|
'@types/pg':
|
||||||
|
specifier: ^8.15.6
|
||||||
|
version: 8.15.6
|
||||||
'@types/react':
|
'@types/react':
|
||||||
specifier: 19.2.7
|
specifier: 19.2.7
|
||||||
version: 19.2.7
|
version: 19.2.7
|
||||||
@@ -174,6 +177,9 @@ importers:
|
|||||||
depcheck:
|
depcheck:
|
||||||
specifier: ^1.4.7
|
specifier: ^1.4.7
|
||||||
version: 1.4.7
|
version: 1.4.7
|
||||||
|
dotenv:
|
||||||
|
specifier: ^17.2.3
|
||||||
|
version: 17.2.3
|
||||||
drizzle-kit:
|
drizzle-kit:
|
||||||
specifier: 0.31.7
|
specifier: 0.31.7
|
||||||
version: 0.31.7
|
version: 0.31.7
|
||||||
@@ -1682,6 +1688,9 @@ packages:
|
|||||||
'@types/parse-json@4.0.2':
|
'@types/parse-json@4.0.2':
|
||||||
resolution: {integrity: sha512-dISoDXWWQwUquiKsyZ4Ng+HX2KsPL7LyHKHQwgGFEA3IaKac4Obd+h2a/a6waisAoepJlBcx9paWqjA8/HVjCw==}
|
resolution: {integrity: sha512-dISoDXWWQwUquiKsyZ4Ng+HX2KsPL7LyHKHQwgGFEA3IaKac4Obd+h2a/a6waisAoepJlBcx9paWqjA8/HVjCw==}
|
||||||
|
|
||||||
|
'@types/pg@8.15.6':
|
||||||
|
resolution: {integrity: sha512-NoaMtzhxOrubeL/7UZuNTrejB4MPAJ0RpxZqXQf2qXuVlTPuG6Y8p4u9dKRaue4yjmC7ZhzVO2/Yyyn25znrPQ==}
|
||||||
|
|
||||||
'@types/react-dom@19.2.3':
|
'@types/react-dom@19.2.3':
|
||||||
resolution: {integrity: sha512-jp2L/eY6fn+KgVVQAOqYItbF0VY/YApe5Mz2F0aykSO8gx31bYCZyvSeYxCHKvzHG5eZjc+zyaS5BrBWya2+kQ==}
|
resolution: {integrity: sha512-jp2L/eY6fn+KgVVQAOqYItbF0VY/YApe5Mz2F0aykSO8gx31bYCZyvSeYxCHKvzHG5eZjc+zyaS5BrBWya2+kQ==}
|
||||||
peerDependencies:
|
peerDependencies:
|
||||||
@@ -2301,6 +2310,10 @@ packages:
|
|||||||
resolution: {integrity: sha512-35mSku4ZXK0vfCuHEDAwt55dg2jNajHZ1odvF+8SSr82EsZY4QmXfuWso8oEd8zRhVObSN18aM0CjSdoBX7zIw==}
|
resolution: {integrity: sha512-35mSku4ZXK0vfCuHEDAwt55dg2jNajHZ1odvF+8SSr82EsZY4QmXfuWso8oEd8zRhVObSN18aM0CjSdoBX7zIw==}
|
||||||
engines: {node: '>=0.10.0'}
|
engines: {node: '>=0.10.0'}
|
||||||
|
|
||||||
|
dotenv@17.2.3:
|
||||||
|
resolution: {integrity: sha512-JVUnt+DUIzu87TABbhPmNfVdBDt18BLOWjMUFJMSi/Qqg7NTYtabbvSNJGOJ7afbRuv9D/lngizHtP7QyLQ+9w==}
|
||||||
|
engines: {node: '>=12'}
|
||||||
|
|
||||||
drizzle-kit@0.31.7:
|
drizzle-kit@0.31.7:
|
||||||
resolution: {integrity: sha512-hOzRGSdyKIU4FcTSFYGKdXEjFsncVwHZ43gY3WU5Bz9j5Iadp6Rh6hxLSQ1IWXpKLBKt/d5y1cpSPcV+FcoQ1A==}
|
resolution: {integrity: sha512-hOzRGSdyKIU4FcTSFYGKdXEjFsncVwHZ43gY3WU5Bz9j5Iadp6Rh6hxLSQ1IWXpKLBKt/d5y1cpSPcV+FcoQ1A==}
|
||||||
hasBin: true
|
hasBin: true
|
||||||
@@ -5308,6 +5321,12 @@ snapshots:
|
|||||||
|
|
||||||
'@types/parse-json@4.0.2': {}
|
'@types/parse-json@4.0.2': {}
|
||||||
|
|
||||||
|
'@types/pg@8.15.6':
|
||||||
|
dependencies:
|
||||||
|
'@types/node': 24.10.1
|
||||||
|
pg-protocol: 1.10.3
|
||||||
|
pg-types: 2.2.0
|
||||||
|
|
||||||
'@types/react-dom@19.2.3(@types/react@19.2.7)':
|
'@types/react-dom@19.2.3(@types/react@19.2.7)':
|
||||||
dependencies:
|
dependencies:
|
||||||
'@types/react': 19.2.7
|
'@types/react': 19.2.7
|
||||||
@@ -5944,6 +5963,8 @@ snapshots:
|
|||||||
dependencies:
|
dependencies:
|
||||||
esutils: 2.0.3
|
esutils: 2.0.3
|
||||||
|
|
||||||
|
dotenv@17.2.3: {}
|
||||||
|
|
||||||
drizzle-kit@0.31.7:
|
drizzle-kit@0.31.7:
|
||||||
dependencies:
|
dependencies:
|
||||||
'@drizzle-team/brocli': 0.10.2
|
'@drizzle-team/brocli': 0.10.2
|
||||||
@@ -5953,9 +5974,10 @@ snapshots:
|
|||||||
transitivePeerDependencies:
|
transitivePeerDependencies:
|
||||||
- supports-color
|
- supports-color
|
||||||
|
|
||||||
drizzle-orm@0.44.7(@opentelemetry/api@1.9.0)(better-sqlite3@12.4.1)(kysely@0.28.8)(pg@8.16.3):
|
drizzle-orm@0.44.7(@opentelemetry/api@1.9.0)(@types/pg@8.15.6)(better-sqlite3@12.4.1)(kysely@0.28.8)(pg@8.16.3):
|
||||||
optionalDependencies:
|
optionalDependencies:
|
||||||
'@opentelemetry/api': 1.9.0
|
'@opentelemetry/api': 1.9.0
|
||||||
|
'@types/pg': 8.15.6
|
||||||
better-sqlite3: 12.4.1
|
better-sqlite3: 12.4.1
|
||||||
kysely: 0.28.8
|
kysely: 0.28.8
|
||||||
pg: 8.16.3
|
pg: 8.16.3
|
||||||
|
|||||||
47
scripts/postgres/enable-extensions.ts
Normal file
47
scripts/postgres/enable-extensions.ts
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
#!/usr/bin/env node
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Script to initialize database extensions before running migrations
|
||||||
|
* This ensures pgcrypto extension is available for gen_random_bytes()
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { config } from 'dotenv';
|
||||||
|
import { drizzle } from 'drizzle-orm/node-postgres';
|
||||||
|
import { Pool } from 'pg';
|
||||||
|
import * as fs from 'fs';
|
||||||
|
import * as path from 'path';
|
||||||
|
|
||||||
|
// Load environment variables from .env
|
||||||
|
config();
|
||||||
|
|
||||||
|
async function initDatabase() {
|
||||||
|
const databaseUrl = process.env.DATABASE_URL;
|
||||||
|
|
||||||
|
if (!databaseUrl) {
|
||||||
|
console.error('DATABASE_URL environment variable is required');
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
const pool = new Pool({ connectionString: databaseUrl });
|
||||||
|
const db = drizzle(pool);
|
||||||
|
|
||||||
|
try {
|
||||||
|
console.log('🔧 Initializing database extensions...');
|
||||||
|
|
||||||
|
// Read and execute init.sql as a single query
|
||||||
|
const initSqlPath = path.join(process.cwd(), 'scripts', 'postgres', 'init.sql');
|
||||||
|
const initSql = fs.readFileSync(initSqlPath, 'utf-8');
|
||||||
|
|
||||||
|
console.log('Executing init.sql...');
|
||||||
|
await db.execute(initSql);
|
||||||
|
|
||||||
|
console.log('✅ Database initialization completed');
|
||||||
|
} catch (error) {
|
||||||
|
console.error('❌ Database initialization failed:', error);
|
||||||
|
process.exit(1);
|
||||||
|
} finally {
|
||||||
|
await pool.end();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
initDatabase();
|
||||||
Reference in New Issue
Block a user