Where It All Started.

Where It All Started.

Life, Stock Trading, Investments, Business and Startup. Most are programming stuff.

Month: May 2019

Output File from PSQL Command

She had an immense curiosity about life, and was constantly staring and wondering.

— Henry James in The Portrait of a Lady.

Hi guys, if one day your on a vacation on a remote island and can’t access your workstation. And the only access you can get due to slow internet speed is SSH (Secure Shell). You tried to access the remote production database using a rented computer and an SSH client, the tools inside the server is only compose of a psql command.

Don’t panic and don’t worry we got you covered, thepsql saves your day if you’ll just gonna be exporting SQL schema layouts.

First and foremost, we fire up and execute the psql command. Inside the psql environment execute the commands below:

\o dump.txt
SELECT 'postgresql' AS dbms,t.table_catalog,t.table_schema,t.table_name,c.column_name,c.ordinal_position,c.data_type,c.character_maximum_length,n.constraint_type,k2.table_schema,k2.table_name,k2.column_name
  FROM information_schema.tables t
  NATURAL LEFT JOIN information_schema.columns c 
  LEFT JOIN(information_schema.key_column_usage k 
    NATURAL JOIN information_schema.table_constraints n
    NATURAL LEFT JOIN information_schema.referential_constraints r) 
  ON c.table_catalog=k.table_catalog AND c.table_schema=k.table_schema AND c.table_name=k.table_name AND c.column_name=k.column_name
  LEFT JOIN information_schema.key_column_usage k2 
  ON k.position_in_unique_constraint=k2.ordinal_position AND r.unique_constraint_catalog=k2.constraint_catalog AND r.unique_constraint_schema=k2.constraint_schema AND r.unique_constraint_name=k2.constraint_name 
  WHERE t.TABLE_TYPE='BASE TABLE' AND t.table_schema NOT IN('information_schema','pg_catalog');

This command will create a dump file containing all the table structure containing each constraint available, this is usually needed when you need to analyze ER (Entity Relation) diagram. You could example paste the output of this command on Lucid Chart to analyze entity relationship.

A break down of this is, the SELECT command will query all table structure and schema.

Switches Legend:
\o => outputs to file
\O => turns off the output to file feature

The output file will be generated on the same directory where you run the psql command.

So guys, when on vacation what are the things you can do on a slow internet and a rented computer? Hope you enjoyed this article!

C# .NET Projects Can Be Compiled and Run in MacOS or Linux

My primary goal of hacking was the intellectual curiosity, the seduction of adventure.

— Kevin Mitnick.

Before I never thought that a .NET solution project can be compiled and run on Linux. But as I’ve checked the GitHub of dotnet-core, I found there were many ways to do it.

First is through Mono, which is a compatible open source alternative to the .NET Framework (the latter is a proprietary of Microsoft). You can create WPF (Windows Presentation Foundation) forms using it and other UI intensive .NET projects. Mono is sponsored by Microsoft, but it is unofficially supported.

The other solution is, if your working on a .NET core project you’ll be using a dotnet-core. Microsoft published last 2014 an open source .NET SDK (Software Development Kit) (bare bones) which was derived from ASP.NET, they’ve called it dotnet-core. Basically, it is a stripped down version of .NET framework without all the heavy UI and forms. The project itself is modular and can be compiled in different platforms.

So if you’re planning to use C# or a .NET dependent language, don’t be afraid as it can be run and created using different platforms.

If you’re planning to install the package on Arch here is the command:

pacman -Sy dotnet-core

Or check the mono flavor:

pacman -Sy mono

That’s it guys, a brand new knowledge for me. Probably on my next project I’ll try to use .NET Core. 🤔 Hope you guys, enjoyed this article and as always live life.

Check Total File Size of Files with Specific File Extension

Posted on by Edward Fitz

It is simply this: do not tire, never lose interest, never grow indifferent—lose your invaluable curiosity and you let yourself die. It’s as simple as that.

— Tove Jansson in Fair Play.

Recently, I wondered on how to check the file size of all files with a specific file extension on a folder / project. This is what I’ve found out after several experimentation using command line tools in Linux, this specific task could be accomplish with just one line of command. Check below for the actual command:

find . -name "*.dart" | xargs cat | wc -c

What this command does, is first find all file with the dart extension. Then pipe the output of that command in xargs converts the standard input to command arguments. After that pipe the output of xargs again to our favorite word counter wc.

Here are the command line tools use in the process.

  • xargs is a command on Unix and most Unix-like operating systems used to build and execute commands from standard input. It converts input from standard input into arguments to a command. Some commands such as grep and awk can take input either as command-line arguments or from the standard input. Wikipedia
  • find is a command-line utility that locates files based on some user-specified criteria and then applies some requested action on each matched object. Wikipedia
  • wc is a command in Unix and Unix-like operating systems. The program reads either standard input or a list of files and generates one or more of the following statistics: newline count, word count, and byte count. If a list of files is provided, both individual file and total statistics follow. Wikipedia

That’s it!

So guys, do you have any one-liner commands that you want to share? Just message me on my social media accounts. Hope you guys enjoyed this article!