How to move changed created migration to dist folder? - typeorm

I started to use Typeorm Migration. I've defined in the package.json the next commands:
"migration:create": "cross-env NODE_ENV=dev npm run typeorm migration:create -- -n",
"migration:generate": "cross-env NODE_ENV=dev npm run typeorm migration:generate -- -n",
"migration:run": "cross-env NODE_ENV=dev npm run typeorm migration:run",
"migration:revert": "cross-env NODE_ENV=dev npm run typeorm migration:revert"
And my ormconfig looks like that
const connectionOptions: ConnectionOptions = {
type: envConfig.DATABASE_TYPE,
host: envConfig.DATABASE_HOST,
port: envConfig.DATABASE_PORT,
username: envConfig.DATABASE_USERNAME,
password: envConfig.DATABASE_PASSWORD,
database: envConfig.DATABASE_NAME,
entities: ["dist/**/*.entity.js"],
synchronize: envConfig.DATABASE_SYNCHRONIZE,
migrations: ["dist/migrations/*{.js, .ts}"],
cli: {
migrationsDir: 'src/migrations',
},
}
export = connectionOptions
When I change something into my entity, and run migration:generate all are working fine, and in the dist folder( migrations) I get a migration file. But for example a need to insert a new value into an existing table( without modifying any fields). For that, I run a migration:create that generates an empty file with up and down functions, where in up I've already described my query. But in the dist folder this file is empty( because it was created when I ran the create command)
My question is, how I can update the file from the dist folder to the actual information that I've written into a created file? Thanks

Related

Typeorm Migration:generate failure Not enough non-option arguments: got 0, need at least 1

I tried this command in different possible ways but the basic structure of my command was.
yarn typeorm migration:generate -n=consent-record -d=\"./src/db/CliDataSource.ts\"
this is my typeorm command in the package.json for yarn berry
"typeorm": "ts-node -P ./tsconfig.typeorm.json $(yarn bin typeorm) -d ./src/db/CliDataSource.ts",
I also tried installing typeorm locally as an npm. and also tried with npx.
but they all give the following error. "Not enough non-option arguments: got 0, need at least 1"
this error clearly doesn't mention what is missing.
my CliDataSource goes like this.
export const CliDataSource = new DataSource({
type: 'postgres',
host: 'localhost',
port: 5436,
username: '****',
password: '******',
database: 'consent',
synchronize: false,
logging: false,
entities,
migrations,
migrationsRun: true,
subscribers: [],
});
I am using typeorm "^0.3.6"
Latest updates to the typeorm has removed -n flag which we used to rename migrations. how it works now is that we need to provide the migration file path. that will store the migration in that specified file. so the updated operations were
my typeorm alias inside package.json.
"typeorm": "ts-node -P ./tsconfig.typeorm.json $(yarn bin typeorm) -d ./src/db/CliDataSource.ts",
CLI Command
yarn typeorm migration:generate ./src/db/migrations/consent-record
The official documentation seems outdated. hope it will be updated soon.
Special thanks to Jacob Copini #woov
I had a similar issue but solved it initially using a util file like this
// contents of migration.ts
import { exec } from 'child_process';
const command = `npm run typeorm migration:create ./src/migrations/${process.argv[2]}`;
(() => exec(command, (error, stdout, stderr) => {
if (error !== null) {
console.error(stderr);
}
console.log(stdout);
}))();
In the package.json:
"migration:create": "ts-node migration.ts"
And to use, type the following:
npm run migration:create unique-key-username
But here's how it should be done after the latest changes in TypeORM:
// new syntax for TypeORM ormconfig.ts
const { DataSource } = require("typeorm");
require('dotenv').config();
for (const envName of Object.keys(process.env)) {
process.env[envName] = process.env[envName].replace(/\\n/g, '\n');
}
const connectionSource = new DataSource({
type: 'mysql',
host: process.env.DB_HOST,
port: +process.env.DB_PORT,
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
entities: [__dirname + '/entities/**/*.{js,ts}'],
migrations: [__dirname + '/dist/src/migrations/*.js'],
});
module.exports = {
connectionSource,
}
// package.json
"typeorm": "ts-node node_modules/typeorm/cli.js",
"migration:create": "ts-node migration.ts -d ./ormconfig.ts",
"migration:run": "typeorm migration:run -d ./ormconfig.ts",
"migration:revert": "typeorm migration:revert -d ./ormconfig.ts",
Had the same issue, the workaround I found for now is to use the compiled js file
For example, this will work
npx typeorm migration:generate Init -d dist/db/CliDataSource.js
As the CLI command fails to import the .ts file for some reason, probably because of the file URL it tries to generate, also for some reason it will only import a ts file if the closest package.json file is set to a type => module
File URL ->
typeorm/src/db/CliDataSource.ts
: pathToFileURL(filePath).toString(),
package.json module type check ->
typeorm/src/db/CliDataSource.ts/
const isModule = (packageJson as any)?.type === "module"
This command below does not work
npx typeorm migration:generate Init -d ./src/db/CliDataSource.ts

The term 'System.DefaultWorkingDirectory' is not recognized in YAML pipeline

I had a pipeline with PowerShell task that run some python script. It worked without any problems.
After I convert my pipeline into YAML format to store it as code and got something like this (a part of whole yaml pipeline):
variables:
Build.SyncSources: false
REPO_PATH_DA: '/asdfg/qwerty'
REPO_PATH_DS: '/zxcvbn/tyuio'
PIP_REPO_HOST: 'bbbb.nnnn.yyyy.com'
PIP_REPO_URL: 'https://$(PIP_REPO_HOST)/api/pypi/pypi/simple'
PIP_VENV_NAME: 'my_test_venv'
SelectedBranch: ''
WorkingDirectory: $(System.DefaultWorkingDirectory)
…………………………………………….
- task: PowerShell#1
displayName: 'Install package'
inputs:
scriptType: inlineScript
inlineScript: |
.\$(PIP_VENV_NAME)\Scripts\activate
python.exe -m pip install --index-url=$(PIP_REPO_URL) --trusted-host=$(PIP_REPO_HOST) mypackage
python.exe -m pip install --index-url=$(PIP_REPO_URL) --trusted-host=$(PIP_REPO_HOST) $(WorkingDirectory)$(REPO_PATH_DA)\qwerty
And after I run this pipeline I get an error:
##[error]System.DefaultWorkingDirectory : The term 'System.DefaultWorkingDirectory' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
I tried to change the name of variable in format: $(env:System_DefaultWorkingDirectory) , but no success. I suppose that predefined variables are not passed into yaml pipeline. Do you have any ideas how to resolve it?
Based on my test, the same script could work fine in my yaml pipeline. The $(WorkingDirectory) will be converted to paths xxx/xx/s.
To check if the predefined variable: $(System.DefaultWorkingDirectory) has been passed to Yaml Pipeline.
You could add a task to list all Environment variables:
steps:
- script: SET | more
In the task log, you could search the SYSTEM_DEFAULTWORKINGDIRECTORY and check if the variable exists.
For example:
If this environment variable exists, you could try to use the following format: $env:SYSTEM_DEFAULTWORKINGDIRECTORY
Here is the example:
variables:
Build.SyncSources: false
.....
WorkingDirectory: '$env:SYSTEM_DEFAULTWORKINGDIRECTORY'
If you couldn't find this variable, you can also check if there are equivalent variables.
For example:
BUILD_SOURCESDIRECTORY , BUILD_REPOSITORY_LOCALPATH
Is this a build or a release pipeline? If it's a release, you may need to use $(Pipeline.Workspace) instead of $(System.DefaultWorkingDirectory).
Also, have you tried using $(System.WorkingDirectory) directly in your Powershell task, instead of declaring a variable that references it?

Use MongoDB with docker-compose: create database and user

Is possible to create a database using docker-compose? I'm trying to run mongodb on docker but I'm not able to create user and initial database :(
version: '3.1'
services:
mongo:
image: mongo
restart: always
environment:
- MONGO_INITDB_DATABASE=test
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=admin
volumes:
- ~/docker/volumes/mongodb:/data/db
ports:
- 27017:27017
What is missing in my script?
test code:
from pymongo import MongoClient
mongo_client = MongoClient('mongodb://%s:%s#127.0.0.1' % ('admin', 'admin'))
cursor = mongo_client.list_databases()
for db in cursor:
print(db)
The above docker-compose seems fine, it will create a user named admin and the DB named will be test if your *.js file contain insert data script. otherwise, it will not create Database because so if you do not insert data with your JavaScript files, then no database is created.
You can log in with these credential that specifies in env.
- MONGO_INITDB_DATABASE=test
- MONGO_INITDB_ROOT_USERNAME=test
- MONGO_INITDB_ROOT_PASSWORD=admin
To initialize DB, you need to mount you DB init script.
volumes:
- ~/docker/volumes/mongodb:/data/db
- youdbscript/init.js:/docker-entrypoint-initdb.d/
MONGO_INITDB_DATABASE
This variable allows you to specify the name of a database to be used
for creation scripts in /docker-entrypoint-initdb.d/*.js (see
Initializing a fresh instance below). MongoDB is fundamentally
designed for "create on first use", so if you do not insert data with
your JavaScript files, then no database is created.
Initializing a fresh instance
When a container is started for the first time it will execute files
with extensions .sh and .js that are found in
/docker-entrypoint-initdb.d. Files will be executed in alphabetical
order. .js files will be executed by mongo using the database
specified by the MONGO_INITDB_DATABASE variable, if it is present, or
test otherwise. You may also switch databases within the .js script.
mongo-docker
Update:
For your information the code you pasted code is not js file. its python script. Also, change the user name from admin might conflict with admin.
from pymongo import MongoClient
mongo_client = MongoClient('mongodb://%s:%s#127.0.0.1' % ('admin', 'admin'))
cursor = mongo_client.list_databases()
for db in cursor:
print(db)
You init script will some thing like
youdbscript/init.js
You can try this.
db = db.getSiblingDB("test");
db.article.drop();
db.article.save( {
title : "this is my title" ,
author : "bob" ,
posted : new Date(1079895594000) ,
pageViews : 5 ,
tags : [ "fun" , "good" , "fun" ] ,
comments : [
{ author :"joe" , text : "this is cool" } ,
{ author :"sam" , text : "this is bad" }
],
other : { foo : 5 }
});
Just like #Adiii answer. If you just declare a db name and not insert some data. The db will not created actually.
As your question. You can add one simple script (such as .sh or .js script) to the path /docker-entrypoint-initdb.d. Like the docs on mongo Initializing a fresh instance section.

how to deploy your dart app (using Web ui) without using Pub Deploy

What is the best strategy to deploy a Dart Web-ui app manually ?
pub deploy doesn't work for me and I have raised bug report. So am thinking what is the best way to manually deploy.
This is how I started:
1) From project root I compile the webui components (dwc.dart)
2) change directory to web/out then run dart2js
3) copy all .js files into that scripts/js public folder on server
4) copy appname.html to server changing css and script paths to option 3
5) Make sure dart.js is also in the same directory as item 3
this is as far as I got. So what else do I need to do ?
A few questions:
1) Do I manually change the file paths in the generated .js files to point to public folders on server for the files they are referencing and make sure those files are on server also ?
2) Do I need to copy all packages to server also ?
3) Any preferred file structure on server?
Any tips on this really appreciated.
Thanks.
I wrote a Grunt script for it (since I had no time to look up how to properly write code for Grunt, I did not share the code since it's a mess) but I basically do this:
compiling a list of files with dwc to a given out dir
compile it to javascript
clean up all non-deployable files
change some paths inside the HTML to match the server paths (for some reasons, this gets changed by the compilation process)
remove all packages except the ones I really need (JS interopt and browser)
Since I'm only using the JS version, I remove all dart packages. Since the paths inside the HTML files are up to you, you can already use a structure that suits you/your server.
I can provide you with a Grunt script to understand the order of tasks. Practically the order I use is this one:
Create the build directory. I usually use /build/web. I usually create these files (index.html, main.dart, /css and so on into the /web dir). I create the rest of components into /lib directory.
Compile the .dart file that contains the main() function ("main.dart" in my case for simpler projects) file to Javascript and put it into /build/web directory
Copy the other needed files and folders to the /build/web directory. Also, during this process you'll be copying the packages that your project needs. You'll see in the example provided below.
Remove all empty folders from the project
You can create a Grunt task to open the /index.html file in the browser once the building process has ended (I will not provide this example)
The structure of the dart test project:
testApp
- gruntfile.js
- package.js
/lib
/packages
/angular
/web
- index.html
- main.dart
/css
/img
So, the Grunt example script to cover steps from 1 - 4 looks like this (copy it to gruntfile.js):
module.exports = function (grunt) {
grunt.initConfig({
// 1.
// create build web directory
mkdir: {
build: {
options: {
create: ['build/web']
}
}
},
// 2.
// compile dart files
dart2js: {
options: {
// use this to fix a problem into dart2js node module. The module calls dart2js not dart2js.bat.
// this is needed for Windows. So use the path to your dart2js.bat file
"dart2js_bin": "C:/dart/dart-sdk/bin/dart2js.bat"
},
compile: {
files: {'build/web/main.dart.js': 'web/main.dart'}
}
},
// 3.
// copy all needed files, including all needed packages
// except the .dart files.
copy: {
build: {
files: [
{
expand: true,
src: [
'web/!(*.dart)',
'web/css/*.css',
'web/res/*.svg',
'web/packages/angular/**/!(*.dart)',
'web/packages/browser/**/!(*.dart)'
],
dest: 'build'
}
]
}
},
// 4.
// remove empty directories copied using the previous task
cleanempty: {
build: {
options: {
files: false
},
src: ['build/web/packages/**/*']
}
},
});
require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);
grunt.registerTask('default', [
'mkdir:build',
'dart2js',
'copy:build',
'cleanempty:build'
]);
};
So this is the Grunt script example.
Create a /gruntfile.js file into your project's root directory and copy/paste the script to it.
Create a /package.json file into your project's root directory and copy/paste the following script:
{
"name": "testApp",
"version": "0.0.1",
"description": "SomeDescriptionForTheTestApp",
"main": "",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "YourName",
"peerDependencies": {
"grunt-cli": "^0.1.13"
},
"devDependencies": {
"grunt": "^0.4.5",
"grunt-cleanempty": "^1.0.3",
"grunt-contrib-copy": "^0.7.0",
"grunt-dart2js": "0.0.5",
"grunt-mkdir": "^0.1.2",
"matchdep": "^0.3.0"
}
}
Open Command Prompt in Windows, Terminal in Linux, navigate to your project's root directory and use this command:
npm install
Wait untill all Grunt modules needed will be downloaded to your local project. Once this is finished, issue this command in Command Prompt or Terminal:
node -e "require('grunt').cli()"
You can use this to initiate Grunt default task without having Grunt installed globally on your system.
Now, to know the exact build structure for your project (including the packages that the project needs), make a build using Pub Build. Then you will be able to instruct Grunt to create the same dir structure.
You can add other tasks (like minification) if you want.
Hope this will help you all to understand the process and get you started with a test app first. Add your comments to make this even better and simplify it even more.

Step by step to making db connection in symfony1.4

I'm new to symfony. My existing application is developed in symfony1.4. They didn't used any db connection. Now I want to create new MySQL database connection in this application.
How can I do that?
in settings.yml
all:
use_database: true
from the console run this command
php symfony configure:database "mysql:host=dbhost;dbname=yourdbname" dbuser dbpassword
Please check all the steps to make db connection symfony1.4
Step 1 :
file : config/ProjectConfiguration.class.php
find what:
---------
public function setup()
{
$this->enablePlugins('owCorePlugin');
}
Repalce with :
------------
public function setup()
{
$this->enablePlugins('owCorePlugin');
$this->enablePlugins('sfDoctrinePlugin');
}
Step 2 :
config/databases.yml
You need to create this file and add below code.replace caps letter with your details.You need to maintain same space also.
all:
doctrine:
class: sfDoctrineDatabase
param:
dsn: mysql:host=HOST_NAME;dbname=DB_NAME
username: USERNAME
password: PASSWORD
Step 3:
frontend/config/settings.yml
find what :
use_database: false
Replace with
use_database: true
Run the following command:
change the project configuration.Make sure you need to run this command in root folder of our project
php symfony configure:database --name=doctrine --class=sfDoctrineDatabase "mysql:host=HOST_NAME;dbname=DB_NAME" USERNAME PASSWORD
step 5: schema files
$ php symfony doctrine:build-schema
$ php symfony doctrine:build-model
$ php symfony doctrine:build-forms
$ php symfony doctrine:build-filters
$ ln -s lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/web web/sfDoctrinePlugin

Resources