Tuesday, June 26, 2018

Useful commands for the MariaDB/Mysql DB

So, today I was caught up in a problem where, I was using a migration generator in Python. I accidentally made some mistakes in one of my models, for which, the migration generator lost it's track and subsequently, was generating a duplicate column issue.

Also, another problem was the use of a non UTF-8 collation type. So, I had a pretty rough time trying to solve these things.

I will list down some of the things that I did, not in any useful order, to solve these problems.

#1 I was using SQLAlchemy with Alembic.
Here's the migration that went berserk

def upgrade(): # ### commands auto generated by Alembic - please adjust! ### op.add_column('bot_article', sa.Column('blogs_id', sa.Integer(), nullable=True)) #op.drop_constraint('bot_article_ibfk_1', 'bot_article', type_='foreignkey') op.create_foreign_key('bot_article_ibfk_1', 'bot_article', 'blogs', ['blogs_id'], ['id']) # ### end Alembic commands ### def downgrade(): # ### commands auto generated by Alembic - please adjust! ### op.drop_constraint('bot_article_ibfk_1', 'bot_article', type_='foreignkey') # op.create_foreign_key('bot_article_ibfk_1', 'bot_article', 'blogs', ['blogs_id'], ['id']) op.drop_column('bot_article', 'blogs_id') # ### end Alembic commands ###

So, as you can see from the commands, there was a foreign key constraint for the column that I had deleted. So I had to first delete the creation process of that column. Which subsequently failed since I had to actually also remove the constraint that involved that column. But how to do that?


alter table bot_article drop foreign key bot_article_ibfk_3;

This command above removes that constraint from the table. Now I was free to use the proper column and customize my Alembic script, which is what you just saw.

#2 Fixing column collation.
My problem in this case was that had to fix the collation of a column that was using a latin collations. For this reason, I could not insert my unicode encoded strings. But how did I find out what collation was this using, or even if it was using a wrong collation?


SHOW FULL COLUMNS from bot_article;

This command, shows whatever settings you have for each column. So I had a latin collation. I had to convert it to 'utf8_general_ci'. Way to do that?

ALTER DATABASE flask_app CHARACTER SET utf8 COLLATE utf8_general_ci;

#3 Finding out what foreign key constraints are in effect in columns
This one's pretty useful. To find out everything you need to type to create a column (intact with all it's settings) you have to do the following

show create table bot_article;

#4 How to add collation using SQLAlchemy

db.Column(db.Text(collation='utf8_general_ci'), nullable=True)

It's possible that Alembic will not detect this using the migrate command, you have to manually add this in a migration file.

Monday, December 18, 2017

Change Matplotlib backend to avoid QT5 issues.

This is an issue that has been bugging me for about 5 hours. Until I reached the solution of changing the backend of Matplotlib for good.

import matplotlib
gui_env = ['TKAgg','GTKAgg','Qt4Agg','WXAgg']
for gui in gui_env:
    try:
        print "testing", gui
        matplotlib.use(gui,warn=False, force=True)
        from matplotlib import pyplot as plt
        break
    except:
        continue
print "Using:",matplotlib.get_backend()


Credits: https://stackoverflow.com/a/43015816/1928610

Tuesday, October 17, 2017

Playing around with NL Toolkit

So I was planning to play around with Natural Language a little. I have started from this page here http://www.nltk.org/book/ch01.html since it uses Python and is easily accessible.
I am going to this use this page as a log for this activity to log how I fixed certain errors and progressed through the processes. So here goes.
#1 Error: Resource gutenberg not found
Solution: I almost gave up when I finally hit this URL https://groups.google.com/forum/#!topic/nltk-users/LEGj1IHJJ1g
I used this command to fix the path to the library
nltk.data.path.append('path_to_nltk_data')

Wednesday, September 27, 2017

How to configure Wordpress to run on Docker

Well, it's not a big deal. You need two (2) things.
#1. This Nginx config
server {
    listen 80;
    server_name wp-hakase.co;
 
    root /var/www/html;
    index index.php;
 
    access_log /var/log/nginx/hakase-access.log;
    error_log /var/log/nginx/hakase-error.log;
 
    location / {
        try_files $uri $uri/ /index.php?$args;
    }
 
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass wordpress:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}
#2 You have to make sure your PHP instance (docker) has mysqli installed (or installing via DockerFile) everytime.

Credits: https://www.howtoforge.com/tutorial/dockerizing-wordpress-with-nginx-and-php-fpm/
https://forums.docker.com/t/status-code-500-wordpress/36581

Sunday, November 27, 2016

Display switcher for terminal buffs

I am not a Linux developer or anything, I just love writing scripts that facilitate in making me lazy. So here's one that lets me switch my displays (dual). I usually work on only one and when I need to quickly switch, I use this. It depends on xrandr though. And it has my display names hardcoded (lots of assumptions, I know), but it might just come in handy.
Switch to left or right
./reset_display.sh left
./reset_display.sh right
Set both on with duplicate view
./reset_display.sh
Set both on their individual view
./reset_display.sh both

You can simlink it /usr/local/bin too.
Github gist

Sunday, November 06, 2016

Getting file list from torrent magnet URLs using Aria2

So, I hit upon this realy awesome SO reply on how to fetch the torrent file list using magnet URLs. I use Aria2 for torrents and it's be design that you cannot get any meta data from Magnet URLs because they do not hold any information off the link. They fetch the meta data when you use them. So, what you have to do is, generate a torrent file from them and then use THAT torrent file for listing. So it's a 2 step process.

First you create the torrent file using:
aria2c --bt-metadata-only=true --bt-save-metadata=true -q "magnet url here"
It will generate a torrent file right there with a name that is a hash just use that to get the file list.

Credits: http://unix.stackexchange.com/a/134286/52595

Saturday, October 08, 2016

Replacing Apache in your VPS with Nginx

If you have not been following the web development scene recently, you need to know that Apache is being seriously challenged by Nginx in the ideal server candidate position. Nginx is a great server daemon that also uses much less resources. Recently I attempted to make a move to Nginx from Apache. But there were some issues that needed to be addressed.

Well, setting up Nginx is really straightforward, just run
# apt-get install nginx
But you might face problems with starting the nginx service. Well, the most likely reason probably is that by default Nginx is configured to run on Port 80. In computers one port cannot be used by multiple programs at the same time. But Apache too is configured to listen on port 80. Now, if you want to move to Nginx and serve all your sites via Nginx, you can unassign Apache from port and give Nginx the instruction to listen to port 80. To do that you will need to tweak an apache configuration file. This may be different for different distros and scenarios but the most common place to look for the Apache configuration files is
/etc/apache2
or
/etc/httpd
Now usually the configuration can be found in
/etc/apache2/apache2.conf
or
/etc/httpd/httpd.conf
Just open your appropriate configuration file and try to find the the line:
Listen 80
If you can't find the line it's very much possible that the port instruction is in different file. Just read through the configuration and try to find if any "Include" directive. It could be like
Include "ports.conf"
Which means the port Listen directive is there. Open the file (you should be able to find that in the same directory).
Now change "Listen 80" to "Listen 1234". This will make Apache listen to port 1234.
Now you should restart Apache with
#servicectl restart apache2
And start nginx with
#servicectl restart nginx

Connect Rapoo MT750S with Linux (Tested on Manjaro)

 I bought this obvious copy of MX Master 2S in hopes of having the device switching functionality along with a lightweight body because I ha...